use of org.luaj.vm2.Prototype in project LuaViewSDK by alibaba.
the class LuaC method luaY_parser.
/**
* Parse the input
*/
private Prototype luaY_parser(InputStream z, String name, boolean standardSyntax) throws IOException {
LexState lexstate = new LexState(this, z, standardSyntax);
FuncState funcstate = new FuncState();
// lexstate.buff = buff;
lexstate.fs = funcstate;
lexstate.setinput(this, z.read(), z, (LuaString) LuaValue.valueOf(name));
/* main func. is always vararg */
funcstate.f = new Prototype();
funcstate.f.source = (LuaString) LuaValue.valueOf(name);
lexstate.mainfunc(funcstate);
LuaC._assert(funcstate.prev == null);
/* all scopes should be correctly finished */
LuaC._assert(lexstate.dyd == null || (lexstate.dyd.n_actvar == 0 && lexstate.dyd.n_gt == 0 && lexstate.dyd.n_label == 0));
return funcstate.f;
}
use of org.luaj.vm2.Prototype 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.Prototype in project aerospike-client-java by aerospike.
the class LuaCache method loadPackageFromResource.
/**
* Load lua package from a resource.
*/
public static final Prototype loadPackageFromResource(ClassLoader resourceLoader, String resourcePath, String packageName) throws AerospikeException {
Prototype prototype = Packages.get(packageName);
if (prototype == null) {
try {
InputStream is = resourceLoader.getResourceAsStream(resourcePath);
if (is == null) {
throw new Exception();
}
prototype = compile(packageName, is);
Packages.put(packageName, prototype);
} catch (Exception e) {
throw new AerospikeException("Failed to read resource: " + resourcePath);
}
}
return prototype;
}
use of org.luaj.vm2.Prototype in project aerospike-client-java by aerospike.
the class LuaCache method loadPackageFromFile.
/**
* Load lua package from a file.
*/
public static final Prototype loadPackageFromFile(String packageName) throws AerospikeException {
Prototype prototype = Packages.get(packageName);
if (prototype == null) {
File source = new File(LuaConfig.SourceDirectory, packageName + ".lua");
try {
InputStream is = new FileInputStream(source);
prototype = compile(packageName, is);
Packages.put(packageName, prototype);
} catch (Exception e) {
throw new AerospikeException("Failed to read file: " + source.getAbsolutePath());
}
}
return prototype;
}
use of org.luaj.vm2.Prototype in project aerospike-client-java by aerospike.
the class LuaInstance method loadSystemPackage.
private void loadSystemPackage(ClassLoader resourceLoader, String packageName) {
String resourcePath = "udf/" + packageName + ".lua";
Prototype prototype = LuaCache.loadPackageFromResource(resourceLoader, resourcePath, packageName);
LuaClosure function = new LuaClosure(prototype, globals);
function.invoke();
loadedTable.set(packageName, LuaValue.TRUE);
}
Aggregations