org.luaj.vm2.LuaClosure Java Examples

The following examples show how to use org.luaj.vm2.LuaClosure. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
    LuaValue func = args.checkfunction(1);
    int up = args.checkint(2);
    if (func instanceof LuaClosure) {
        LuaClosure c = (LuaClosure) func;
        LuaString name = findupvalue(c, up);
        if (name != null) {
            return varargsOf(name, c.upValues[up - 1].getValue());
        }
    }
    return NIL;
}
 
Example #2
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
    LuaValue func = args.checkfunction(1);
    int up = args.checkint(2);
    LuaValue value = args.arg(3);
    if (func instanceof LuaClosure) {
        LuaClosure c = (LuaClosure) func;
        LuaString name = findupvalue(c, up);
        if (name != null) {
            c.upValues[up - 1].setValue(value);
            return name;
        }
    }
    return NIL;
}
 
Example #3
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
    LuaValue func = args.checkfunction(1);
    int up = args.checkint(2);
    if (func instanceof LuaClosure) {
        LuaClosure c = (LuaClosure) func;
        if (c.upValues != null && up > 0 && up <= c.upValues.length) {
            return valueOf(c.upValues[up - 1].hashCode());
        }
    }
    return NIL;
}
 
Example #4
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
    LuaClosure f1 = args.checkclosure(1);
    int n1 = args.checkint(2);
    LuaClosure f2 = args.checkclosure(3);
    int n2 = args.checkint(4);
    if (n1 < 1 || n1 > f1.upValues.length)
        argerror("index out of range");
    if (n2 < 1 || n2 > f2.upValues.length)
        argerror("index out of range");
    f1.upValues[n1 - 1] = f2.upValues[n2 - 1];
    return NONE;
}
 
Example #5
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #6
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
static LuaString findupvalue(LuaClosure c, int up) {
    if (c.upValues != null && up > 0 && up <= c.upValues.length) {
        if (c.p.upvalues != null && up <= c.p.upvalues.length)
            return c.p.upvalues[up - 1].name;
        else
            return LuaString.valueOf("." + up);
    }
    return null;
}
 
Example #7
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: StringLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * string.dump (function)
 * <p>
 * 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.
 * <p>
 * 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());
    }
}
 
Example #8
Source Project: XPrivacyLua   Author: M66B   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			return varargsOf(name, c.upValues[up-1].getValue() );
		}
	}
	return NIL;
}
 
Example #9
Source Project: XPrivacyLua   Author: M66B   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	LuaValue value = args.arg(3);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			c.upValues[up-1].setValue(value);
			return name;
		}
	}
	return NIL;
}
 
Example #10
Source Project: XPrivacyLua   Author: M66B   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
			return valueOf(c.upValues[up-1].hashCode());
		}
	}
	return NIL;
}
 
Example #11
Source Project: XPrivacyLua   Author: M66B   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaClosure f1 = args.checkclosure(1);
	int n1 = args.checkint(2);
	LuaClosure f2 = args.checkclosure(3);
	int n2 = args.checkint(4);
	if (n1 < 1 || n1 > f1.upValues.length)
		argerror("index out of range");
	if (n2 < 1 || n2 > f2.upValues.length)
		argerror("index out of range");
	f1.upValues[n1-1] = f2.upValues[n2-1];
	return NONE;
}
 
Example #12
Source Project: XPrivacyLua   Author: M66B   File: DebugLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
static LuaString findupvalue(LuaClosure c, int up) {
	if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
		if ( c.p.upvalues != null && up <= c.p.upvalues.length )
			return c.p.upvalues[up-1].name;
		else
			return LuaString.valueOf( "."+up );
	}
	return null;
}
 
Example #13
Source Project: XPrivacyLua   Author: M66B   File: StringLib.java    License: GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(LuaValue arg) {
	LuaValue f = arg.checkfunction();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		DumpState.dump( ((LuaClosure)f).p, baos, true );
		return LuaString.valueUsing(baos.toByteArray());
	} catch (IOException e) {
		return error( e.getMessage() );
	}
}
 
Example #14
Source Project: HtmlNative   Author: hsllany   File: DebugLib.java    License: Apache License 2.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			return varargsOf(name, c.upValues[up-1].getValue() );
		}
	}
	return NIL;
}
 
Example #15
Source Project: HtmlNative   Author: hsllany   File: DebugLib.java    License: Apache License 2.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	LuaValue value = args.arg(3);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			c.upValues[up-1].setValue(value);
			return name;
		}
	}
	return NIL;
}
 
Example #16
Source Project: HtmlNative   Author: hsllany   File: DebugLib.java    License: Apache License 2.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
			return valueOf(c.upValues[up-1].hashCode());
		}
	}
	return NIL;
}
 
Example #17
Source Project: HtmlNative   Author: hsllany   File: DebugLib.java    License: Apache License 2.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaClosure f1 = args.checkclosure(1);
	int n1 = args.checkint(2);
	LuaClosure f2 = args.checkclosure(3);
	int n2 = args.checkint(4);
	if (n1 < 1 || n1 > f1.upValues.length)
		argerror("index out of range");
	if (n2 < 1 || n2 > f2.upValues.length)
		argerror("index out of range");
	f1.upValues[n1-1] = f2.upValues[n2-1];
	return NONE;
}
 
Example #18
Source Project: HtmlNative   Author: hsllany   File: DebugLib.java    License: Apache License 2.0 5 votes vote down vote up
static LuaString findupvalue(LuaClosure c, int up) {
	if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
		if ( c.p.upvalues != null && up <= c.p.upvalues.length )
			return c.p.upvalues[up-1].name;
		else
			return LuaString.valueOf( "."+up );
	}
	return null;
}
 
Example #19
Source Project: HtmlNative   Author: hsllany   File: StringLib.java    License: Apache License 2.0 5 votes vote down vote up
public LuaValue call(LuaValue arg) {
	LuaValue f = arg.checkfunction();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		DumpState.dump( ((LuaClosure)f).p, baos, true );
		return LuaString.valueUsing(baos.toByteArray());
	} catch (IOException e) {
		return error( e.getMessage() );
	}
}
 
Example #20
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaFunction() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaValue chunk = theGlobals.load("function add(a,b)\nreturn a+b\nend\nreturn add(1,2)");
    Assert.assertFalse(chunk.isnil());
    Assert.assertTrue(chunk instanceof LuaClosure);
    final LuaClosure luaClosure = (LuaClosure) chunk;
    final LuaString theResult = luaClosure.call().strvalue();
    Assert.assertEquals("3", theResult.tojstring());
}
 
Example #21
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCall() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return a + b end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final LuaValue theFunction = theGlobals.get("add");
    Assert.assertFalse(theFunction.isnil());
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaInteger theResult = (LuaInteger) theFunction.invoke(theArguments);
    Assert.assertEquals("300", theResult.tojstring());
}
 
Example #22
Source Project: Bytecoder   Author: mirkosertic   File: LuaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCallStringResult() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return 'hello' end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaValue theFunction = theGlobals.get("add");
    final LuaValue theValue = (LuaValue) theFunction.invoke(theArguments);
    Assert.assertTrue(theValue.isstring());
    Assert.assertEquals("hello", theValue.tojstring());
}
 
Example #23
Source Project: luaj   Author: luaj   File: DebugLib.java    License: MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			return varargsOf(name, c.upValues[up-1].getValue() );
		}
	}
	return NIL;
}
 
Example #24
Source Project: luaj   Author: luaj   File: DebugLib.java    License: MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	LuaValue value = args.arg(3);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		LuaString name = findupvalue(c, up);
		if ( name != null ) {
			c.upValues[up-1].setValue(value);
			return name;
		}
	}
	return NIL;
}
 
Example #25
Source Project: luaj   Author: luaj   File: DebugLib.java    License: MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue func = args.checkfunction(1);
	int up = args.checkint(2);
	if ( func instanceof LuaClosure ) {
		LuaClosure c = (LuaClosure) func;
		if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
			return valueOf(c.upValues[up-1].hashCode());
		}
	}
	return NIL;
}
 
Example #26
Source Project: luaj   Author: luaj   File: DebugLib.java    License: MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaClosure f1 = args.checkclosure(1);
	int n1 = args.checkint(2);
	LuaClosure f2 = args.checkclosure(3);
	int n2 = args.checkint(4);
	if (n1 < 1 || n1 > f1.upValues.length)
		argerror("index out of range");
	if (n2 < 1 || n2 > f2.upValues.length)
		argerror("index out of range");
	f1.upValues[n1-1] = f2.upValues[n2-1];
	return NONE;
}
 
Example #27
Source Project: luaj   Author: luaj   File: DebugLib.java    License: MIT License 5 votes vote down vote up
static LuaString findupvalue(LuaClosure c, int up) {
	if ( c.upValues != null && up > 0 && up <= c.upValues.length ) {
		if ( c.p.upvalues != null && up <= c.p.upvalues.length )
			return c.p.upvalues[up-1].name;
		else
			return LuaString.valueOf( "."+up );
	}
	return null;
}
 
Example #28
Source Project: luaj   Author: luaj   File: StringLib.java    License: MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue f = args.checkfunction(1);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		DumpState.dump( ((LuaClosure)f).p, baos, args.optboolean(2, true) );
		return LuaString.valueUsing(baos.toByteArray());
	} catch (IOException e) {
		return error( e.getMessage() );
	}
}
 
Example #29
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: LuaPrint.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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);

    VenvyLog.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;
}
 
Example #30
Source Project: VideoOS-Android-SDK   Author: VideoOS   File: DebugLib.java    License: GNU General Public License v3.0 4 votes vote down vote up
void onCall(LuaClosure function, Varargs varargs, LuaValue[] stack) {
    pushcall().set(function, varargs, stack);
}