Skip to content

Commit

Permalink
Fix call messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Mar 16, 2016
1 parent 00799fb commit 339cb74
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
66 changes: 34 additions & 32 deletions src/main/java/org/squiddev/cobalt/LuaTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.squiddev.cobalt.Constants.*;
import static org.squiddev.cobalt.ValueFactory.valueOf;
import static org.squiddev.cobalt.ValueFactory.varargsOf;

/**
Expand Down Expand Up @@ -77,7 +79,7 @@
*/
public class LuaTable extends LuaValue {
private static final int MIN_HASH_CAPACITY = 2;
private static final LuaString N = ValueFactory.valueOf("n");
private static final LuaString N = valueOf("n");

/**
* the array values
Expand Down Expand Up @@ -108,10 +110,10 @@ public class LuaTable extends LuaValue {
* Construct empty table
*/
public LuaTable() {
super(Constants.TTABLE);
array = Constants.NOVALS;
hashKeys = Constants.NOVALS;
hashValues = Constants.NOVALS;
super(TTABLE);
array = NOVALS;
hashKeys = NOVALS;
hashValues = NOVALS;
}

/**
Expand All @@ -121,7 +123,7 @@ public LuaTable() {
* @param nhash capacity of hash part
*/
public LuaTable(int narray, int nhash) {
super(Constants.TTABLE);
super(TTABLE);
presize(narray, nhash);
}

Expand All @@ -133,7 +135,7 @@ public LuaTable(int narray, int nhash) {
* @param lastarg Additional unnamed values beyond {@code unnamed.length}
*/
public LuaTable(LuaValue[] named, LuaValue[] unnamed, Varargs lastarg) {
super(Constants.TTABLE);
super(TTABLE);
int nn = (named != null ? named.length : 0);
int nu = (unnamed != null ? unnamed.length : 0);
int nl = (lastarg != null ? lastarg.count() : 0);
Expand Down Expand Up @@ -169,11 +171,11 @@ public LuaTable(Varargs varargs) {
* @param firstarg the index in varargs of the first argument to include in the table
*/
public LuaTable(Varargs varargs, int firstarg) {
super(Constants.TTABLE);
super(TTABLE);
int nskip = firstarg - 1;
int n = Math.max(varargs.count() - nskip, 0);
presize(n, 1);
rawset(N, ValueFactory.valueOf(n));
rawset(N, valueOf(n));
for (int i = 1; i <= n; i++) {
rawset(i, varargs.arg(i + nskip));
}
Expand Down Expand Up @@ -207,9 +209,9 @@ public void presize(int narray, int nhash) {
if (nhash > 0 && nhash < MIN_HASH_CAPACITY) {
nhash = MIN_HASH_CAPACITY;
}
array = (narray > 0 ? new LuaValue[narray] : Constants.NOVALS);
hashKeys = (nhash > 0 ? new LuaValue[nhash] : Constants.NOVALS);
hashValues = (nhash > 0 ? new LuaValue[nhash] : Constants.NOVALS);
array = (narray > 0 ? new LuaValue[narray] : NOVALS);
hashKeys = (nhash > 0 ? new LuaValue[nhash] : NOVALS);
hashValues = (nhash > 0 ? new LuaValue[nhash] : NOVALS);
hashEntries = 0;
}

Expand Down Expand Up @@ -249,7 +251,7 @@ public LuaValue getMetatable(LuaState state) {
public LuaValue setMetatable(LuaState state, LuaValue metatable) {
this.metatable = metatable;
LuaValue mode;
if (this.metatable != null && (mode = this.metatable.rawget(Constants.MODE)).isString()) {
if (this.metatable != null && (mode = this.metatable.rawget(MODE)).isString()) {
String m = mode.toString();
boolean k = m.indexOf('k') >= 0;
boolean v = m.indexOf('v') >= 0;
Expand Down Expand Up @@ -277,7 +279,7 @@ public LuaValue rawget(LuaValue key) {
if (key.isIntExact()) {
int ikey = key.toInteger();
if (ikey > 0 && ikey <= array.length) {
return array[ikey - 1] != null ? array[ikey - 1] : Constants.NIL;
return array[ikey - 1] != null ? array[ikey - 1] : NIL;
}
}
return hashget(key);
Expand All @@ -286,9 +288,9 @@ public LuaValue rawget(LuaValue key) {
protected LuaValue hashget(LuaValue key) {
if (hashEntries > 0) {
LuaValue v = hashValues[hashFindSlot(key)];
return v != null ? v : Constants.NIL;
return v != null ? v : NIL;
}
return Constants.NIL;
return NIL;
}

@Override
Expand Down Expand Up @@ -331,7 +333,7 @@ private void expandarray() {
LuaValue k = LuaInteger.valueOf(i + 1);
LuaValue v = hashget(k);
if (!v.isNil()) {
hashset(k, Constants.NIL);
hashset(k, NIL);
array[i] = v;
}
}
Expand All @@ -348,14 +350,14 @@ public LuaValue remove(int pos) {
if (pos == 0) {
pos = n;
} else if (pos > n) {
return Constants.NONE;
return NONE;
}
LuaValue v = rawget(pos);
for (LuaValue r = v; !r.isNil(); ) {
r = rawget(pos + 1);
rawset(pos++, r);
}
return v.isNil() ? Constants.NONE : v;
return v.isNil() ? NONE : v;

}

Expand Down Expand Up @@ -403,7 +405,7 @@ public LuaValue getn() {
return LuaInteger.valueOf(n);
}
}
return Constants.ZERO;
return ZERO;
}


Expand Down Expand Up @@ -440,7 +442,7 @@ public double maxn() {
}
}
for (LuaValue v : hashKeys) {
if (v != null && v.type() == Constants.TNUMBER) {
if (v != null && v.type() == TNUMBER) {
double key = v.toDouble();
if (key > n) {
n = key;
Expand Down Expand Up @@ -512,12 +514,12 @@ public Varargs next(LuaValue key) {
// check hash part
for (i -= array.length; i < hashKeys.length; ++i) {
if (hashKeys[i] != null) {
return ValueFactory.varargsOf(hashKeys[i], hashValues[i]);
return varargsOf(hashKeys[i], hashValues[i]);
}
}

// nothing found, push nil, return nil.
return Constants.NIL;
return NIL;
}

/**
Expand Down Expand Up @@ -551,7 +553,7 @@ public Varargs next(LuaValue key) {
public Varargs inext(LuaValue key) {
int k = key.checkInteger() + 1;
LuaValue v = rawget(k);
return v.isNil() ? Constants.NONE : varargsOf(LuaInteger.valueOf(k), v);
return v.isNil() ? NONE : varargsOf(LuaInteger.valueOf(k), v);
}

/**
Expand All @@ -563,14 +565,14 @@ public Varargs inext(LuaValue key) {
*/
public LuaValue foreach(LuaState state, LuaValue func) {
Varargs n;
LuaValue k = Constants.NIL;
LuaValue k = NIL;
LuaValue v;
while (!(k = ((n = next(k)).first())).isNil()) {
if (!(v = OperationHelper.call(state, func, k, n.arg(2))).isNil()) {
return v;
}
}
return Constants.NIL;
return NIL;
}

/**
Expand All @@ -584,11 +586,11 @@ public LuaValue foreach(LuaState state, LuaValue func) {
public LuaValue foreachi(LuaState state, LuaValue func) {
LuaValue v, r;
for (int k = 0; !(v = rawget(++k)).isNil(); ) {
if (!(r = OperationHelper.call(state, func, ValueFactory.valueOf(k), v)).isNil()) {
if (!(r = OperationHelper.call(state, func, valueOf(k), v)).isNil()) {
return r;
}
}
return Constants.NIL;
return NIL;
}


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

if (hashEntries == 0) {
hashKeys = Constants.NOVALS;
hashValues = Constants.NOVALS;
hashKeys = NOVALS;
hashValues = NOVALS;
}
}
}
Expand Down Expand Up @@ -790,7 +792,7 @@ private void swap(int i, int j) {
* @return count of keys in the table
*/
public int keyCount() {
LuaValue k = Constants.NIL;
LuaValue k = NIL;
for (int i = 0; true; i++) {
Varargs n = next(k);
if ((k = n.first()).isNil()) {
Expand All @@ -807,7 +809,7 @@ public int keyCount() {
*/
public LuaValue[] keys() {
List<LuaValue> l = new ArrayList<LuaValue>();
LuaValue k = Constants.NIL;
LuaValue k = NIL;
while (true) {
Varargs n = next(k);
if ((k = n.first()).isNil()) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/squiddev/cobalt/OperationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static LuaValue call(LuaState state, LuaValue function, int stack) {
return ((LuaFunction) function).call(state);
} else {
LuaValue meta = function.metatag(state, Constants.CALL);
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call: " + meta.typeName(), stack);
if (!meta.isFunction()) throw ErrorFactory.operandError(state, function, "call", stack);

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

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

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

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

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

return ((LuaFunction) meta).onInvoke(state, ValueFactory.varargsOf(function, args));
}
Expand Down

0 comments on commit 339cb74

Please sign in to comment.