Search in sources :

Example 1 with LuaClosure

use of org.luaj.vm2.LuaClosure 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 2 with LuaClosure

use of org.luaj.vm2.LuaClosure 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 3 with LuaClosure

use of org.luaj.vm2.LuaClosure in project LuaViewSDK by alibaba.

the class DebugLib method onCall.

public void onCall(LuaClosure c, Varargs varargs, LuaValue[] stack) {
    LuaThread t = globals.running;
    callstack().onCall(c, varargs, stack);
    if (t.inhook)
        return;
    if (t.hookcall && t.hookfunc != null)
        callHook(CALL, NIL);
}
Also used : LuaThread(org.luaj.vm2.LuaThread)

Example 4 with LuaClosure

use of org.luaj.vm2.LuaClosure in project LuaViewSDK by alibaba.

the class DestroyUtil method onDestroyClosure.

public static void onDestroyClosure(LuaClosure closure) {
    if (closure != null) {
        if (closure.upValues != null) {
            UpValue upValue = null;
            for (int i = 0; i < closure.upValues.length; i++) {
                upValue = closure.upValues[i];
                // if (upValue != null && upValue.array != null) {
                // for (LuaValue value : upValue.array) {//destroy upvalues
                // if (value instanceof BaseUserdata) {//userdata destory
                // ((BaseUserdata) value).onDestroy();
                // } else if (value instanceof LuaTable) {//destroy table
                // onDestroyTable((LuaTable) value);
                // }
                // }
                // }
                closure.upValues[i] = null;
            }
            closure.upValues = null;
        }
    }
}
Also used : UpValue(org.luaj.vm2.UpValue)

Example 5 with LuaClosure

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

Aggregations

LuaClosure (org.luaj.vm2.LuaClosure)2 LuaString (org.luaj.vm2.LuaString)2 LuaValue (org.luaj.vm2.LuaValue)2 Prototype (org.luaj.vm2.Prototype)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 LuaThread (org.luaj.vm2.LuaThread)1 UpValue (org.luaj.vm2.UpValue)1