Search in sources :

Example 31 with LuaValue

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;
}
Also used : LuaValue(org.luaj.vm2.LuaValue)

Example 32 with LuaValue

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;
}
Also used : LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue) LuaString(org.luaj.vm2.LuaString)

Example 33 with LuaValue

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());
    }
}
Also used : LuaValue(org.luaj.vm2.LuaValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 34 with LuaValue

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);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 35 with LuaValue

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;
}
Also used : LuaTable(org.luaj.vm2.LuaTable)

Aggregations

LuaValue (org.luaj.vm2.LuaValue)55 LuaTable (org.luaj.vm2.LuaTable)37 Varargs (org.luaj.vm2.Varargs)12 LuaString (org.luaj.vm2.LuaString)9 VarArgFunction (org.luaj.vm2.lib.VarArgFunction)7 UDView (com.taobao.luaview.userdata.ui.UDView)6 LuaError (org.luaj.vm2.LuaError)6 LuaFunction (org.luaj.vm2.LuaFunction)6 View (android.view.View)4 Point (android.graphics.Point)3 ILVView (com.taobao.luaview.view.interfaces.ILVView)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 HorizontalScrollView (android.widget.HorizontalScrollView)2 AerospikeException (com.aerospike.client.AerospikeException)2 LuaViewApi (com.taobao.luaview.fun.mapper.LuaViewApi)2 UDLuaTable (com.taobao.luaview.userdata.base.UDLuaTable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2