use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class GlobalsExtender method doLoad.
/**
* 真正加载一个库
*
* @param globals
* @param name
* @return
*/
public LuaValue doLoad(final Globals globals, final String name) {
if (mLazyLoadLibs != null && mLazyLoadLibs.containsKey(name)) {
final LuaValue lib = mLazyLoadLibs.get(name);
LuaValue result = globals.load(lib);
mLazyLoadLibs.remove(name);
return result;
}
return null;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class LuaPrint method buildState.
/**
* Print the state of a {@link LuaClosure} that is being executed
*
* @param cl the {@link LuaClosure}
* @param pc the program counter
* @param stack the stack of {@link LuaValue}
* @param top the top of the stack
* @param varargs any {@link Varargs} value that may apply
*/
public LuaPrint buildState(LuaClosure cl, int pc, LuaValue[] stack, int top, Varargs varargs) {
// print opcode into buffer
StringBuffer previous = ps;
ps = new StringBuffer();
buildOpCode(cl.p, pc);
LogUtil.i(ps);
ps = previous;
format(ps.toString(), 50);
// print stack
ps.append('[');
for (int i = 0; i < stack.length; i++) {
LuaValue v = stack[i];
if (v == null)
ps.append(STRING_FOR_NULL);
else
switch(v.type()) {
case LuaValue.TSTRING:
LuaString s = v.checkstring();
ps.append(s.length() < 48 ? s.tojstring() : s.substring(0, 32).tojstring() + "...+" + (s.length() - 32) + "b");
break;
case LuaValue.TFUNCTION:
ps.append(v.tojstring());
break;
case LuaValue.TUSERDATA:
Object o = v.touserdata();
if (o != null) {
String n = o.getClass().getName();
n = n.substring(n.lastIndexOf('.') + 1);
ps.append(n + ": " + Integer.toHexString(o.hashCode()));
} else {
ps.append(v.toString());
}
break;
default:
ps.append(v.tojstring());
}
if (i + 1 == top)
ps.append(']');
ps.append(" | ");
}
ps.append(varargs);
ps.append("\n");
return this;
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class StringLib method dump.
/**
* string.dump (function)
*
* Returns a string containing a binary representation of the given function,
* so that a later loadstring on this string returns a copy of the function.
* function must be a Lua function without upvalues.
*
* TODO: port dumping code as optional add-on
*/
static LuaValue dump(LuaValue arg) {
LuaValue f = arg.checkfunction();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DumpState.dump(((LuaClosure) f).p, baos, true);
return LuaString.valueOf(baos.toByteArray());
} catch (IOException e) {
return error(e.getMessage());
}
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class StringLib method reverse.
/**
* string.reverse (s)
*
* Returns a string that is the string s reversed.
*/
static LuaValue reverse(LuaValue arg) {
LuaString s = arg.checkstring();
int n = s.length();
byte[] b = new byte[n];
for (int i = 0, j = n - 1; i < n; i++, j--) b[j] = (byte) s.luaByte(i);
return LuaString.valueOf(b);
}
use of org.luaj.vm2.LuaValue in project LuaViewSDK by alibaba.
the class DebugLib method call.
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.debuglib = this;
LuaTable debug = new LuaTable();
debug.set("debug", new debug());
debug.set("gethook", new gethook());
debug.set("getinfo", new getinfo());
debug.set("getlocal", new getlocal());
debug.set("getmetatable", new getmetatable());
debug.set("getregistry", new getregistry());
debug.set("getupvalue", new getupvalue());
debug.set("getuservalue", new getuservalue());
debug.set("sethook", new sethook());
debug.set("setlocal", new setlocal());
debug.set("setmetatable", new setmetatable());
debug.set("setupvalue", new setupvalue());
debug.set("setuservalue", new setuservalue());
debug.set("traceback", new traceback());
debug.set("upvalueid", new upvalueid());
debug.set("upvaluejoin", new upvaluejoin());
debug.set("traceback_count", new tracebackCount());
// extend for luaview
new com.taobao.luaview.vm.extend.DebugLib(this, globals).extend(debug);
env.set("debug", debug);
env.get("package").get("loaded").set("debug", debug);
return debug;
}
Aggregations