Search in sources :

Example 11 with Prototype

use of org.luaj.vm2.Prototype in project aerospike-client-java by aerospike.

the class LuaInstance method loadPackage.

public void loadPackage(Statement statement) {
    if (loadedTable.get(statement.getPackageName()).toboolean()) {
        return;
    }
    Prototype prototype;
    if (statement.getResourceLoader() == null || statement.getResourcePath() == null) {
        prototype = LuaCache.loadPackageFromFile(statement.getPackageName());
    } else {
        prototype = LuaCache.loadPackageFromResource(statement.getResourceLoader(), statement.getResourcePath(), statement.getPackageName());
    }
    LuaClosure function = new LuaClosure(prototype, globals);
    function.invoke();
    loadedTable.set(statement.getPackageName(), LuaValue.TRUE);
}
Also used : Prototype(org.luaj.vm2.Prototype) LuaClosure(org.luaj.vm2.LuaClosure)

Example 12 with Prototype

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

the class DebugLib method getobjname.

// return NameWhat if found, null if not
public static NameWhat getobjname(Prototype p, int lastpc, int reg) {
    // currentpc(L, ci);
    int pc = lastpc;
    LuaString name = p.getlocalname(reg + 1, pc);
    if (name != null)
        /* is a local? */
        return new NameWhat(name.tojstring(), "local");
    /* else try symbolic execution */
    pc = findsetreg(p, lastpc, reg);
    if (pc != -1) {
        /* could find instruction? */
        int i = p.code[pc];
        switch(Lua.GET_OPCODE(i)) {
            case Lua.OP_MOVE:
                {
                    int a = Lua.GETARG_A(i);
                    int b = Lua.GETARG_B(i);
                    /* move from `b' to `a' */
                    if (b < a)
                        return getobjname(p, pc, b);
                    /* get name for `b' */
                    break;
                }
            case Lua.OP_GETTABUP:
            case Lua.OP_GETTABLE:
                {
                    int k = Lua.GETARG_C(i);
                    /* key index */
                    int t = Lua.GETARG_B(i);
                    /* table index */
                    LuaString vn = (Lua.GET_OPCODE(i) == Lua.OP_GETTABLE) ? /* name of indexed variable */
                    p.getlocalname(t + 1, pc) : (t < p.upvalues.length ? p.upvalues[t].name : QMARK);
                    name = kname(p, k);
                    return new NameWhat(name.tojstring(), vn != null && vn.eq_b(ENV) ? "global" : "field");
                }
            case Lua.OP_GETUPVAL:
                {
                    int u = Lua.GETARG_B(i);
                    /* upvalue index */
                    name = u < p.upvalues.length ? p.upvalues[u].name : QMARK;
                    return new NameWhat(name.tojstring(), "upvalue");
                }
            case Lua.OP_LOADK:
            case Lua.OP_LOADKX:
                {
                    int b = (Lua.GET_OPCODE(i) == Lua.OP_LOADK) ? Lua.GETARG_Bx(i) : Lua.GETARG_Ax(p.code[pc + 1]);
                    if (p.k[b].isstring()) {
                        name = p.k[b].strvalue();
                        return new NameWhat(name.tojstring(), "constant");
                    }
                    break;
                }
            case Lua.OP_SELF:
                {
                    int k = Lua.GETARG_C(i);
                    /* key index */
                    name = kname(p, k);
                    return new NameWhat(name.tojstring(), "method");
                }
            default:
                break;
        }
    }
    return null;
/* no useful name found */
}
Also used : LuaString(org.luaj.vm2.LuaString) LuaPrint(org.luaj.vm2.log.LuaPrint)

Example 13 with Prototype

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

the class DebugLib method findsetreg.

/*
    ** try to find last instruction before 'lastpc' that modified register 'reg'
    */
static int findsetreg(Prototype p, int lastpc, int reg) {
    int pc;
    int setreg = -1;
    /* keep last instruction that changed 'reg' */
    for (pc = 0; pc < lastpc; pc++) {
        int i = p.code[pc];
        int op = Lua.GET_OPCODE(i);
        int a = Lua.GETARG_A(i);
        switch(op) {
            case Lua.OP_LOADNIL:
                {
                    int b = Lua.GETARG_B(i);
                    if (a <= reg && reg <= a + b)
                        /* set registers from 'a' to 'a+b' */
                        setreg = pc;
                    break;
                }
            case Lua.OP_TFORCALL:
                {
                    if (reg >= a + 2)
                        setreg = pc;
                    /* affect all regs above its base */
                    break;
                }
            case Lua.OP_CALL:
            case Lua.OP_TAILCALL:
                {
                    if (reg >= a)
                        setreg = pc;
                    /* affect all registers above base */
                    break;
                }
            case Lua.OP_JMP:
                {
                    int b = Lua.GETARG_sBx(i);
                    int dest = pc + 1 + b;
                    /* jump is forward and do not skip `lastpc'? */
                    if (pc < dest && dest <= lastpc)
                        pc += b;
                    /* do the jump */
                    break;
                }
            case Lua.OP_TEST:
                {
                    if (reg == a)
                        setreg = pc;
                    /* jumped code can change 'a' */
                    break;
                }
            default:
                if (Lua.testAMode(op) && reg == a)
                    /* any instruction that set A */
                    setreg = pc;
                break;
        }
    }
    return setreg;
}
Also used : LuaPrint(org.luaj.vm2.log.LuaPrint)

Example 14 with Prototype

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

the class LexState method close_func.

void close_func() {
    FuncState fs = this.fs;
    Prototype f = fs.f;
    fs.ret(0, 0);
    /* final return */
    fs.leaveblock();
    f.code = LuaC.realloc(f.code, fs.pc);
    f.lineinfo = LuaC.realloc(f.lineinfo, fs.pc);
    f.k = LuaC.realloc(f.k, fs.nk);
    f.p = LuaC.realloc(f.p, fs.np);
    f.locvars = LuaC.realloc(f.locvars, fs.nlocvars);
    f.upvalues = LuaC.realloc(f.upvalues, fs.nups);
    LuaC._assert(fs.bl == null);
    this.fs = fs.prev;
// last token read was anchored in defunct function; must reanchor it
// ls.anchor_token();
}
Also used : Prototype(org.luaj.vm2.Prototype)

Example 15 with Prototype

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

the class LexState method addprototype.

Prototype addprototype() {
    Prototype clp;
    Prototype f = fs.f;
    /* prototype of current function */
    if (f.p == null || fs.np >= f.p.length) {
        f.p = LuaC.realloc(f.p, Math.max(1, fs.np * 2));
    }
    f.p[fs.np++] = clp = new Prototype();
    return clp;
}
Also used : Prototype(org.luaj.vm2.Prototype)

Aggregations

Prototype (org.luaj.vm2.Prototype)12 LuaString (org.luaj.vm2.LuaString)4 LuaPrint (org.luaj.vm2.log.LuaPrint)3 AerospikeException (com.aerospike.client.AerospikeException)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 LocVars (org.luaj.vm2.LocVars)2 LuaClosure (org.luaj.vm2.LuaClosure)2 LuaValue (org.luaj.vm2.LuaValue)2 File (java.io.File)1 Hashtable (java.util.Hashtable)1 LuaInteger (org.luaj.vm2.LuaInteger)1