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 File: LuaTest.java    From Bytecoder with 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 #2
Source File: DebugLib.java    From XPrivacyLua with 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 #3
Source File: StringLib.java    From XPrivacyLua with 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 #4
Source File: DebugLib.java    From HtmlNative with 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 #5
Source File: DebugLib.java    From HtmlNative with 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 #6
Source File: DebugLib.java    From HtmlNative with 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 #7
Source File: DebugLib.java    From HtmlNative with 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 #8
Source File: DebugLib.java    From HtmlNative with 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 #9
Source File: StringLib.java    From HtmlNative with 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 #10
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #11
Source File: LuaTest.java    From Bytecoder with 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 #12
Source File: LuaTest.java    From Bytecoder with 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 #13
Source File: DebugLib.java    From luaj with 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 #14
Source File: DebugLib.java    From luaj with 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 #15
Source File: DebugLib.java    From luaj with 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 #16
Source File: DebugLib.java    From luaj with 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 #17
Source File: DebugLib.java    From luaj with 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 #18
Source File: StringLib.java    From luaj with 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 #19
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #20
Source File: DebugLib.java    From XPrivacyLua with 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 #21
Source File: DebugLib.java    From XPrivacyLua with 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 #22
Source File: DebugLib.java    From XPrivacyLua with 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 #23
Source File: DebugLib.java    From XPrivacyLua with 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 #24
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #25
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #26
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #27
Source File: DebugLib.java    From VideoOS-Android-SDK with 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 #28
Source File: StringLib.java    From VideoOS-Android-SDK with 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 #29
Source File: LuaC.java    From luaj with MIT License 4 votes vote down vote up
public LuaFunction load(Prototype prototype, String chunkname, LuaValue env) throws IOException {
	return new LuaClosure(prototype, env);
}
 
Example #30
Source File: DebugLib.java    From luaj with MIT License 4 votes vote down vote up
void set(LuaClosure function, Varargs varargs, LuaValue[] stack) {
	this.f = function;
	this.v = varargs;
	this.stack = stack;
}