Skip to content

Commit 339cb74

Browse files
committed
Fix call messages
1 parent 00799fb commit 339cb74

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

src/main/java/org/squiddev/cobalt/LuaTable.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.ArrayList;
2828
import java.util.List;
2929

30+
import static org.squiddev.cobalt.Constants.*;
31+
import static org.squiddev.cobalt.ValueFactory.valueOf;
3032
import static org.squiddev.cobalt.ValueFactory.varargsOf;
3133

3234
/**
@@ -77,7 +79,7 @@
7779
*/
7880
public class LuaTable extends LuaValue {
7981
private static final int MIN_HASH_CAPACITY = 2;
80-
private static final LuaString N = ValueFactory.valueOf("n");
82+
private static final LuaString N = valueOf("n");
8183

8284
/**
8385
* the array values
@@ -108,10 +110,10 @@ public class LuaTable extends LuaValue {
108110
* Construct empty table
109111
*/
110112
public LuaTable() {
111-
super(Constants.TTABLE);
112-
array = Constants.NOVALS;
113-
hashKeys = Constants.NOVALS;
114-
hashValues = Constants.NOVALS;
113+
super(TTABLE);
114+
array = NOVALS;
115+
hashKeys = NOVALS;
116+
hashValues = NOVALS;
115117
}
116118

117119
/**
@@ -121,7 +123,7 @@ public LuaTable() {
121123
* @param nhash capacity of hash part
122124
*/
123125
public LuaTable(int narray, int nhash) {
124-
super(Constants.TTABLE);
126+
super(TTABLE);
125127
presize(narray, nhash);
126128
}
127129

@@ -133,7 +135,7 @@ public LuaTable(int narray, int nhash) {
133135
* @param lastarg Additional unnamed values beyond {@code unnamed.length}
134136
*/
135137
public LuaTable(LuaValue[] named, LuaValue[] unnamed, Varargs lastarg) {
136-
super(Constants.TTABLE);
138+
super(TTABLE);
137139
int nn = (named != null ? named.length : 0);
138140
int nu = (unnamed != null ? unnamed.length : 0);
139141
int nl = (lastarg != null ? lastarg.count() : 0);
@@ -169,11 +171,11 @@ public LuaTable(Varargs varargs) {
169171
* @param firstarg the index in varargs of the first argument to include in the table
170172
*/
171173
public LuaTable(Varargs varargs, int firstarg) {
172-
super(Constants.TTABLE);
174+
super(TTABLE);
173175
int nskip = firstarg - 1;
174176
int n = Math.max(varargs.count() - nskip, 0);
175177
presize(n, 1);
176-
rawset(N, ValueFactory.valueOf(n));
178+
rawset(N, valueOf(n));
177179
for (int i = 1; i <= n; i++) {
178180
rawset(i, varargs.arg(i + nskip));
179181
}
@@ -207,9 +209,9 @@ public void presize(int narray, int nhash) {
207209
if (nhash > 0 && nhash < MIN_HASH_CAPACITY) {
208210
nhash = MIN_HASH_CAPACITY;
209211
}
210-
array = (narray > 0 ? new LuaValue[narray] : Constants.NOVALS);
211-
hashKeys = (nhash > 0 ? new LuaValue[nhash] : Constants.NOVALS);
212-
hashValues = (nhash > 0 ? new LuaValue[nhash] : Constants.NOVALS);
212+
array = (narray > 0 ? new LuaValue[narray] : NOVALS);
213+
hashKeys = (nhash > 0 ? new LuaValue[nhash] : NOVALS);
214+
hashValues = (nhash > 0 ? new LuaValue[nhash] : NOVALS);
213215
hashEntries = 0;
214216
}
215217

@@ -249,7 +251,7 @@ public LuaValue getMetatable(LuaState state) {
249251
public LuaValue setMetatable(LuaState state, LuaValue metatable) {
250252
this.metatable = metatable;
251253
LuaValue mode;
252-
if (this.metatable != null && (mode = this.metatable.rawget(Constants.MODE)).isString()) {
254+
if (this.metatable != null && (mode = this.metatable.rawget(MODE)).isString()) {
253255
String m = mode.toString();
254256
boolean k = m.indexOf('k') >= 0;
255257
boolean v = m.indexOf('v') >= 0;
@@ -277,7 +279,7 @@ public LuaValue rawget(LuaValue key) {
277279
if (key.isIntExact()) {
278280
int ikey = key.toInteger();
279281
if (ikey > 0 && ikey <= array.length) {
280-
return array[ikey - 1] != null ? array[ikey - 1] : Constants.NIL;
282+
return array[ikey - 1] != null ? array[ikey - 1] : NIL;
281283
}
282284
}
283285
return hashget(key);
@@ -286,9 +288,9 @@ public LuaValue rawget(LuaValue key) {
286288
protected LuaValue hashget(LuaValue key) {
287289
if (hashEntries > 0) {
288290
LuaValue v = hashValues[hashFindSlot(key)];
289-
return v != null ? v : Constants.NIL;
291+
return v != null ? v : NIL;
290292
}
291-
return Constants.NIL;
293+
return NIL;
292294
}
293295

294296
@Override
@@ -331,7 +333,7 @@ private void expandarray() {
331333
LuaValue k = LuaInteger.valueOf(i + 1);
332334
LuaValue v = hashget(k);
333335
if (!v.isNil()) {
334-
hashset(k, Constants.NIL);
336+
hashset(k, NIL);
335337
array[i] = v;
336338
}
337339
}
@@ -348,14 +350,14 @@ public LuaValue remove(int pos) {
348350
if (pos == 0) {
349351
pos = n;
350352
} else if (pos > n) {
351-
return Constants.NONE;
353+
return NONE;
352354
}
353355
LuaValue v = rawget(pos);
354356
for (LuaValue r = v; !r.isNil(); ) {
355357
r = rawget(pos + 1);
356358
rawset(pos++, r);
357359
}
358-
return v.isNil() ? Constants.NONE : v;
360+
return v.isNil() ? NONE : v;
359361

360362
}
361363

@@ -403,7 +405,7 @@ public LuaValue getn() {
403405
return LuaInteger.valueOf(n);
404406
}
405407
}
406-
return Constants.ZERO;
408+
return ZERO;
407409
}
408410

409411

@@ -440,7 +442,7 @@ public double maxn() {
440442
}
441443
}
442444
for (LuaValue v : hashKeys) {
443-
if (v != null && v.type() == Constants.TNUMBER) {
445+
if (v != null && v.type() == TNUMBER) {
444446
double key = v.toDouble();
445447
if (key > n) {
446448
n = key;
@@ -512,12 +514,12 @@ public Varargs next(LuaValue key) {
512514
// check hash part
513515
for (i -= array.length; i < hashKeys.length; ++i) {
514516
if (hashKeys[i] != null) {
515-
return ValueFactory.varargsOf(hashKeys[i], hashValues[i]);
517+
return varargsOf(hashKeys[i], hashValues[i]);
516518
}
517519
}
518520

519521
// nothing found, push nil, return nil.
520-
return Constants.NIL;
522+
return NIL;
521523
}
522524

523525
/**
@@ -551,7 +553,7 @@ public Varargs next(LuaValue key) {
551553
public Varargs inext(LuaValue key) {
552554
int k = key.checkInteger() + 1;
553555
LuaValue v = rawget(k);
554-
return v.isNil() ? Constants.NONE : varargsOf(LuaInteger.valueOf(k), v);
556+
return v.isNil() ? NONE : varargsOf(LuaInteger.valueOf(k), v);
555557
}
556558

557559
/**
@@ -563,14 +565,14 @@ public Varargs inext(LuaValue key) {
563565
*/
564566
public LuaValue foreach(LuaState state, LuaValue func) {
565567
Varargs n;
566-
LuaValue k = Constants.NIL;
568+
LuaValue k = NIL;
567569
LuaValue v;
568570
while (!(k = ((n = next(k)).first())).isNil()) {
569571
if (!(v = OperationHelper.call(state, func, k, n.arg(2))).isNil()) {
570572
return v;
571573
}
572574
}
573-
return Constants.NIL;
575+
return NIL;
574576
}
575577

576578
/**
@@ -584,11 +586,11 @@ public LuaValue foreach(LuaState state, LuaValue func) {
584586
public LuaValue foreachi(LuaState state, LuaValue func) {
585587
LuaValue v, r;
586588
for (int k = 0; !(v = rawget(++k)).isNil(); ) {
587-
if (!(r = OperationHelper.call(state, func, ValueFactory.valueOf(k), v)).isNil()) {
589+
if (!(r = OperationHelper.call(state, func, valueOf(k), v)).isNil()) {
588590
return r;
589591
}
590592
}
591-
return Constants.NIL;
593+
return NIL;
592594
}
593595

594596

@@ -678,8 +680,8 @@ protected void hashClearSlot(int i) {
678680
hashValues[i] = null;
679681

680682
if (hashEntries == 0) {
681-
hashKeys = Constants.NOVALS;
682-
hashValues = Constants.NOVALS;
683+
hashKeys = NOVALS;
684+
hashValues = NOVALS;
683685
}
684686
}
685687
}
@@ -790,7 +792,7 @@ private void swap(int i, int j) {
790792
* @return count of keys in the table
791793
*/
792794
public int keyCount() {
793-
LuaValue k = Constants.NIL;
795+
LuaValue k = NIL;
794796
for (int i = 0; true; i++) {
795797
Varargs n = next(k);
796798
if ((k = n.first()).isNil()) {
@@ -807,7 +809,7 @@ public int keyCount() {
807809
*/
808810
public LuaValue[] keys() {
809811
List<LuaValue> l = new ArrayList<LuaValue>();
810-
LuaValue k = Constants.NIL;
812+
LuaValue k = NIL;
811813
while (true) {
812814
Varargs n = next(k);
813815
if ((k = n.first()).isNil()) {

src/main/java/org/squiddev/cobalt/OperationHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public static LuaValue call(LuaState state, LuaValue function, int stack) {
357357
return ((LuaFunction) function).call(state);
358358
} else {
359359
LuaValue meta = function.metatag(state, Constants.CALL);
360-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
360+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
361361

362362
return ((LuaFunction) meta).call(state, function);
363363
}
@@ -372,7 +372,7 @@ public static LuaValue call(LuaState state, LuaValue function, LuaValue arg, int
372372
return ((LuaFunction) function).call(state, arg);
373373
} else {
374374
LuaValue meta = function.metatag(state, Constants.CALL);
375-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
375+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
376376

377377
return ((LuaFunction) meta).call(state, function, arg);
378378
}
@@ -387,7 +387,7 @@ public static LuaValue call(LuaState state, LuaValue function, LuaValue arg1, Lu
387387
return ((LuaFunction) function).call(state, arg1, arg2);
388388
} else {
389389
LuaValue meta = function.metatag(state, Constants.CALL);
390-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
390+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
391391

392392
return ((LuaFunction) meta).call(state, function, arg1, arg2);
393393
}
@@ -402,7 +402,7 @@ public static LuaValue call(LuaState state, LuaValue function, LuaValue arg1, Lu
402402
return ((LuaFunction) function).call(state, arg1, arg2, arg3);
403403
} else {
404404
LuaValue meta = function.metatag(state, Constants.CALL);
405-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
405+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
406406

407407
return ((LuaFunction) meta).invoke(state, ValueFactory.varargsOf(function, arg1, arg2, arg3)).first();
408408
}
@@ -417,7 +417,7 @@ public static Varargs invoke(LuaState state, LuaValue function, Varargs args, in
417417
return ((LuaFunction) function).invoke(state, args);
418418
} else {
419419
LuaValue meta = function.metatag(state, Constants.CALL);
420-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
420+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
421421

422422
return ((LuaFunction) meta).invoke(state, ValueFactory.varargsOf(function, args));
423423
}
@@ -432,7 +432,7 @@ public static Varargs onInvoke(LuaState state, LuaValue function, Varargs args,
432432
return ((LuaFunction) function).onInvoke(state, args);
433433
} else {
434434
LuaValue meta = function.metatag(state, Constants.CALL);
435-
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
435+
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);
436436

437437
return ((LuaFunction) meta).onInvoke(state, ValueFactory.varargsOf(function, args));
438438
}

0 commit comments

Comments
 (0)