use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class FuncState method constfolding.
boolean constfolding(int op, expdesc e1, expdesc e2) {
LuaValue v1, v2, r;
if (!e1.isnumeral() || !e2.isnumeral())
return false;
if ((op == OP_DIV || op == OP_MOD) && e2.u.nval().eq_b(LuaValue.ZERO))
return false;
/* do not attempt to divide by 0 */
v1 = e1.u.nval();
v2 = e2.u.nval();
switch(op) {
case OP_ADD:
r = v1.add(v2);
break;
case OP_SUB:
r = v1.sub(v2);
break;
case OP_MUL:
r = v1.mul(v2);
break;
case OP_DIV:
r = v1.div(v2);
break;
case OP_MOD:
r = v1.mod(v2);
break;
case OP_POW:
r = v1.pow(v2);
break;
case OP_UNM:
r = v1.neg();
break;
case OP_LEN:
// break;
return false;
/* no constant folding for 'len' */
default:
_assert(false);
r = null;
break;
}
if (Double.isNaN(r.todouble()))
return false;
/* do not attempt to produce NaN */
e1.u.setNval(r);
return true;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class DumpState method dumpConstants.
void dumpConstants(final Prototype f) throws IOException {
final LuaValue[] k = f.k;
int i, n = k.length;
dumpInt(n);
for (i = 0; i < n; i++) {
final LuaValue o = k[i];
switch(o.type()) {
case LuaValue.TNIL:
writer.write(LuaValue.TNIL);
break;
case LuaValue.TBOOLEAN:
writer.write(LuaValue.TBOOLEAN);
dumpChar(o.toboolean() ? 1 : 0);
break;
case LuaValue.TNUMBER:
switch(NUMBER_FORMAT) {
case NUMBER_FORMAT_FLOATS_OR_DOUBLES:
writer.write(LuaValue.TNUMBER);
dumpDouble(o.todouble());
break;
case NUMBER_FORMAT_INTS_ONLY:
if (!ALLOW_INTEGER_CASTING && !o.isint())
throw new java.lang.IllegalArgumentException("not an integer: " + o);
writer.write(LuaValue.TNUMBER);
dumpInt(o.toint());
break;
case NUMBER_FORMAT_NUM_PATCH_INT32:
if (o.isint()) {
writer.write(LuaValue.TINT);
dumpInt(o.toint());
} else {
writer.write(LuaValue.TNUMBER);
dumpDouble(o.todouble());
}
break;
default:
throw new IllegalArgumentException("number format not supported: " + NUMBER_FORMAT);
}
break;
case LuaValue.TSTRING:
writer.write(LuaValue.TSTRING);
dumpString((LuaString) o);
break;
default:
throw new IllegalArgumentException("bad type for " + o);
}
}
n = f.p.length;
dumpInt(n);
for (i = 0; i < n; i++) dumpFunction(f.p[i]);
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class OsLib method call.
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable os = new LuaTable();
for (int i = 0; i < NAMES.length; ++i) os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
env.set("os", os);
env.get("package").get("loaded").set("os", os);
return os;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class TableLib method call.
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable table = new LuaTable();
table.set("concat", new concat());
table.set("insert", new insert());
table.set("pack", new pack());
table.set("remove", new remove());
table.set("sort", new sort());
table.set("unpack", new unpack());
// TODO yesong添加的函数
table.set("getn", new getn());
env.set("table", table);
env.get("package").get("loaded").set("table", table);
return NIL;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class JsePlatform method luaMain.
/**
* Simple wrapper for invoking a lua function with command line arguments.
* The supplied function is first given a new Globals object,
* then the program is run with arguments.
*/
public static void luaMain(LuaValue mainChunk, String[] args) {
Globals g = standardGlobals();
int n = args.length;
LuaValue[] vargs = new LuaValue[args.length];
for (int i = 0; i < n; ++i) vargs[i] = LuaValue.valueOf(args[i]);
LuaValue arg = LuaValue.listOf(vargs);
arg.set("n", n);
g.set("arg", arg);
mainChunk.initupvalue1(g);
mainChunk.invoke(LuaValue.varargsOf(vargs));
}
Aggregations