use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class CoroutineLib method call.
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable coroutine = new LuaTable();
coroutine.set("create", new create());
coroutine.set("resume", new resume());
coroutine.set("running", new running());
coroutine.set("status", new status());
coroutine.set("yield", new yield());
coroutine.set("wrap", new wrap());
env.set("coroutine", coroutine);
env.get("package").get("loaded").set("coroutine", coroutine);
return coroutine;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class DebugLib method callHook.
void callHook(LuaValue type, LuaValue arg) {
LuaThread t = globals.running;
t.inhook = true;
try {
t.hookfunc.call(type, arg);
} catch (LuaError e) {
throw e;
} catch (RuntimeException e) {
throw new LuaError(e);
} finally {
t.inhook = false;
}
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class IoLib method ioread.
private Varargs ioread(File f, Varargs args) throws IOException {
int i, n = args.narg();
LuaValue[] v = new LuaValue[n];
LuaValue ai, vi;
LuaString fmt;
for (i = 0; i < n; ) {
item: switch((ai = args.arg(i + 1)).type()) {
case LuaValue.TNUMBER:
vi = freadbytes(f, ai.toint());
break item;
case LuaValue.TSTRING:
fmt = ai.checkstring();
if (fmt.m_length == 2 && fmt.m_bytes[fmt.m_offset] == '*') {
switch(fmt.m_bytes[fmt.m_offset + 1]) {
case 'n':
vi = freadnumber(f);
break item;
case 'l':
vi = freadline(f);
break item;
case 'a':
vi = freadall(f);
break item;
}
}
default:
return argerror(i + 1, "(invalid format)");
}
if ((v[i++] = vi).isnil())
break;
}
return i == 0 ? NIL : varargsOf(v, 0, i);
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class IoLib method call.
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
// io lib functions
LuaTable t = new LuaTable();
bind(t, IoLibV.class, IO_NAMES);
// create file methods table
filemethods = new LuaTable();
bind(filemethods, IoLibV.class, FILE_NAMES, FILE_CLOSE);
// set up file metatable
LuaTable mt = new LuaTable();
bind(mt, IoLibV.class, new String[] { "__index" }, IO_INDEX);
t.setmetatable(mt);
// all functions link to library instance
setLibInstance(t);
setLibInstance(filemethods);
setLibInstance(mt);
// return the table
env.set("io", t);
env.get("package").get("loaded").set("io", t);
return t;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class LibFunction method bind.
/**
* Bind a set of library functions, with an offset
* <p/>
* An array of names is provided, and the first name is bound
* with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
*
* @param env The environment to apply to each bound function
* @param factory the Class to instantiate for each bound function
* @param names array of String names, one for each function.
* @param firstopcode the first opcode to use
* @see #bind(LuaValue, Class, String[])
*/
protected void bind(LuaValue env, Class factory, String[] names, int firstopcode) {
try {
for (int i = 0, n = names.length; i < n; i++) {
LibFunction f = (LibFunction) factory.newInstance();
f.opcode = firstopcode + i;
f.name = names[i];
env.set(f.name, f);
}
} catch (Exception e) {
throw new LuaError("bind failed: " + e);
}
}
Aggregations