Java Code Examples for org.luaj.vm2.LuaValue#invoke()
The following examples show how to use
org.luaj.vm2.LuaValue#invoke() .
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: LuaUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 6 votes |
public static Varargs callFunction(LuaValue target, Object... objs) { if (target != null && target.isfunction()) { LuaValue[] args = null; if (objs != null && objs.length > 0) { args = new LuaValue[objs.length]; for (int i = 0; i < objs.length; i++) { args[i] = CoerceJavaToLua.coerce(objs[i]); } } if (args != null) { return target.invoke(LuaValue.varargsOf(args)); } else { return target.call(); } } return LuaValue.NIL; }
Example 2
Source File: JsePlatform.java From HtmlNative with Apache License 2.0 | 5 votes |
/** * Simple wrapper for invoking a lua function with command line arguments. * The supplied function is first given a new Globals object, * then the program is run with arguments. */ public static void luaMain(LuaValue mainChunk, String[] args) { Globals g = standardGlobals(); int n = args.length; LuaValue[] vargs = new LuaValue[args.length]; for (int i = 0; i < n; ++i) vargs[i] = LuaValue.valueOf(args[i]); LuaValue arg = LuaValue.listOf(vargs); arg.set("n", n); g.set("arg", arg); mainChunk.initupvalue1(g); mainChunk.invoke(LuaValue.varargsOf(vargs)); }
Example 3
Source File: LuaJavaCoercionTest.java From luaj with MIT License | 5 votes |
public void testLuaErrorCause() { String script = "luajava.bindClass( \""+SomeClass.class.getName()+"\"):someMethod()"; LuaValue chunk = globals.get("load").call(LuaValue.valueOf(script)); try { chunk.invoke(LuaValue.NONE); fail( "call should not have succeeded" ); } catch ( LuaError lee ) { Throwable c = lee.getCause(); assertEquals( SomeException.class, c.getClass() ); } }
Example 4
Source File: TestLuaJC.java From luaj with MIT License | 5 votes |
public static void main(String[] args) throws Exception { if (args.length > 0) filename = args[0]; System.out.println("filename: "+filename); try { // create an environment to run in globals = JsePlatform.standardGlobals(); // print the chunk as a closure, and pretty-print the closure. LuaValue f = globals.loadfile(filename).arg1(); Prototype p = f.checkclosure().p; Print.print(p); // load into a luajc java-bytecode based chunk by installing the LuaJC compiler first if ( ! (args.length>0 && args[0].equals("nocompile")) ) { LuaJC.install(globals); f = globals.loadfile(filename).arg1(); } // call with arguments Varargs v = f.invoke(LuaValue.NONE); // print the result System.out.println("result: "+v); // Write out the files. // saveClasses(); } catch ( Throwable e ) { e.printStackTrace(); } }
Example 5
Source File: PackageLib.java From luaj with MIT License | 5 votes |
public LuaValue call( LuaValue arg ) { LuaString name = arg.checkstring(); LuaValue loaded = package_.get(_LOADED); LuaValue result = loaded.get(name); if ( result.toboolean() ) { if ( result == _SENTINEL ) error("loop or previous error loading module '"+name+"'"); return result; } /* else must load it; iterate over available loaders */ LuaTable tbl = package_.get(_SEARCHERS).checktable(); StringBuffer sb = new StringBuffer(); Varargs loader = null; for ( int i=1; true; i++ ) { LuaValue searcher = tbl.get(i); if ( searcher.isnil() ) { error( "module '"+name+"' not found: "+name+sb ); } /* call loader with module name as argument */ loader = searcher.invoke(name); if ( loader.isfunction(1) ) break; if ( loader.isstring(1) ) sb.append( loader.tojstring(1) ); } // load the module using the loader loaded.set(name, _SENTINEL); result = loader.arg1().call(name, loader.arg(2)); if ( ! result.isnil() ) loaded.set( name, result ); else if ( (result = loaded.get(name)) == _SENTINEL ) loaded.set( name, result = LuaValue.TRUE ); return result; }
Example 6
Source File: JsePlatform.java From luaj with MIT License | 5 votes |
/** Simple wrapper for invoking a lua function with command line arguments. * The supplied function is first given a new Globals object as its environment * then the program is run with arguments. * @return {@link Varargs} containing any values returned by mainChunk. */ public static Varargs luaMain(LuaValue mainChunk, String[] args) { Globals g = standardGlobals(); int n = args.length; LuaValue[] vargs = new LuaValue[args.length]; for (int i = 0; i < n; ++i) vargs[i] = LuaValue.valueOf(args[i]); LuaValue arg = LuaValue.listOf(vargs); arg.set("n", n); g.set("arg", arg); mainChunk.initupvalue1(g); return mainChunk.invoke(LuaValue.varargsOf(vargs)); }
Example 7
Source File: DefaultLauncher.java From luaj with MIT License | 5 votes |
private Object[] launchChunk(LuaValue chunk, Object[] arg) { LuaValue args[] = new LuaValue[arg.length]; for (int i = 0; i < args.length; ++i) args[i] = CoerceJavaToLua.coerce(arg[i]); Varargs results = chunk.invoke(LuaValue.varargsOf(args)); final int n = results.narg(); Object return_values[] = new Object[n]; for (int i = 0; i < n; ++i) { LuaValue r = results.arg(i+1); switch (r.type()) { case LuaValue.TBOOLEAN: return_values[i] = r.toboolean(); break; case LuaValue.TNUMBER: return_values[i] = r.todouble(); break; case LuaValue.TINT: return_values[i] = r.toint(); break; case LuaValue.TNIL: return_values[i] = null; break; case LuaValue.TSTRING: return_values[i] = r.tojstring(); break; case LuaValue.TUSERDATA: return_values[i] = r.touserdata(); break; default: return_values[i] = r; } } return return_values; }
Example 8
Source File: LuaTest.java From Bytecoder with Apache License 2.0 | 5 votes |
@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 9
Source File: LuaTest.java From Bytecoder with Apache License 2.0 | 5 votes |
@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 10
Source File: LuaRunnerTest.java From HtmlNative with Apache License 2.0 | 5 votes |
@Test public void testLuaRunner() { String luaScript = Utils.toString(new File("lua_test/test.lua")); Globals globals = JsePlatform.standardGlobals(); LuaValue value = globals.load(luaScript); value.invoke(); }
Example 11
Source File: PackageLib.java From HtmlNative with Apache License 2.0 | 5 votes |
public LuaValue call( LuaValue arg ) { LuaString name = arg.checkstring(); LuaValue loaded = package_.get(_LOADED); LuaValue result = loaded.get(name); if ( result.toboolean() ) { if ( result == _SENTINEL ) error("loop or previous error loading module '"+name+"'"); return result; } /* else must load it; iterate over available loaders */ LuaTable tbl = package_.get(_SEARCHERS).checktable(); StringBuffer sb = new StringBuffer(); Varargs loader = null; for ( int i=1; true; i++ ) { LuaValue searcher = tbl.get(i); if ( searcher.isnil() ) { error( "module '"+name+"' not found: "+name+sb ); } /* call loader with module name as argument */ loader = searcher.invoke(name); if ( loader.isfunction(1) ) break; if ( loader.isstring(1) ) sb.append( loader.tojstring(1) ); } // load the module using the loader loaded.set(name, _SENTINEL); result = loader.arg1().call(name, loader.arg(2)); if ( ! result.isnil() ) loaded.set( name, result ); else if ( (result = loaded.get(name)) == _SENTINEL ) loaded.set( name, result = LuaValue.TRUE ); return result; }
Example 12
Source File: JsePlatform.java From XPrivacyLua with GNU General Public License v3.0 | 5 votes |
/** Simple wrapper for invoking a lua function with command line arguments. * The supplied function is first given a new Globals object as its environment * then the program is run with arguments. * @return {@link Varargs} containing any values returned by mainChunk. */ public static Varargs luaMain(LuaValue mainChunk, String[] args) { Globals g = standardGlobals(); int n = args.length; LuaValue[] vargs = new LuaValue[args.length]; for (int i = 0; i < n; ++i) vargs[i] = LuaValue.valueOf(args[i]); LuaValue arg = LuaValue.listOf(vargs); arg.set("n", n); g.set("arg", arg); mainChunk.initupvalue1(g); return mainChunk.invoke(LuaValue.varargsOf(vargs)); }
Example 13
Source File: PackageLib.java From XPrivacyLua with GNU General Public License v3.0 | 5 votes |
public LuaValue call( LuaValue arg ) { LuaString name = arg.checkstring(); LuaValue loaded = package_.get(_LOADED); LuaValue result = loaded.get(name); if ( result.toboolean() ) { if ( result == _SENTINEL ) error("loop or previous error loading module '"+name+"'"); return result; } /* else must load it; iterate over available loaders */ LuaTable tbl = package_.get(_SEARCHERS).checktable(); StringBuffer sb = new StringBuffer(); Varargs loader = null; for ( int i=1; true; i++ ) { LuaValue searcher = tbl.get(i); if ( searcher.isnil() ) { error( "module '"+name+"' not found: "+name+sb ); } /* call loader with module name as argument */ loader = searcher.invoke(name); if ( loader.isfunction(1) ) break; if ( loader.isstring(1) ) sb.append( loader.tojstring(1) ); } // load the module using the loader loaded.set(name, _SENTINEL); result = loader.arg1().call(name, loader.arg(2)); if ( ! result.isnil() ) loaded.set( name, result ); else if ( (result = loaded.get(name)) == _SENTINEL ) loaded.set( name, result = LuaValue.TRUE ); return result; }
Example 14
Source File: LuaUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 5 votes |
public static Varargs invokeFunction(LuaValue target, LuaValue arg1, LuaValue arg2, LuaValue arg3) { try { return (target != null && target.isfunction()) ? target.invoke(new LuaValue[]{arg1, arg2, arg3}) : LuaValue.NIL; } catch (Exception e) { return LuaValue.NIL; } }
Example 15
Source File: LuaUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 5 votes |
public static Varargs invokeFunction(LuaValue target, LuaValue arg1, LuaValue arg2) { try { return (target != null && target.isfunction()) ? target.invoke(arg1, arg2) : LuaValue.NIL; } catch (Exception e) { return LuaValue.NIL; } }
Example 16
Source File: LuaUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 5 votes |
public static Varargs invokeFunction(LuaValue target, LuaValue arg1) { try { return (target != null && target.isfunction()) ? target.invoke(arg1) : LuaValue.NIL; } catch (Exception e) { return LuaValue.NIL; } }
Example 17
Source File: LuaUtil.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 5 votes |
/** * call lua function ( return multiple values) * * @param target * @return */ public static Varargs invokeFunction(LuaValue target) { try { return (target != null && target.isfunction()) ? target.invoke() : LuaValue.NIL; } catch (Exception e) { return LuaValue.NIL; } }
Example 18
Source File: NewIndexFunction.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 5 votes |
@Override public Varargs invoke(Varargs args) { LuaValue func = metatable.get(args.arg(2)); if (func.isfunction()) {//函数调用 func.invoke(varargsOf(args.arg(1), args.arg(3))); } return NONE; }
Example 19
Source File: PackageLib.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 4 votes |
public LuaValue call(LuaValue arg) { LuaString name = arg.checkstring(); LuaValue loaded = package_.get(_LOADED); LuaValue result = loaded.get(name); if (result.toboolean()) { if (result == _SENTINEL) error("loop or previous error loading module '" + name + "'"); return result; }/* else if (globals != null && (result = globals.lazyLoad(name.checkjstring())) != null){//TODO add by song, 如果是加载自定义的内容则使用globals加载 loaded.set(name, _SENTINEL); return result; }*/ /* else must load it; iterate over available loaders */ LuaTable tbl = package_.get(_SEARCHERS).checktable(); StringBuffer sb = new StringBuffer(); Varargs loader = null; for (int i = 1; true; i++) { LuaValue searcher = tbl.get(i); if (searcher.isnil()) { error("module '" + name + "' not found: " + name + sb); } /* call loader with module name as argument */ loader = searcher.invoke(name); if (loader.isfunction(1)) break; if (loader.isstring(1)) sb.append(loader.tojstring(1)); } // load the module using the loader loaded.set(name, _SENTINEL); try { result = loader.arg1().call(name, loader.arg(2)); if (!result.isnil()) loaded.set(name, result); else if ((result = loaded.get(name)) == _SENTINEL) loaded.set(name, result = LuaValue.TRUE); return result; } catch (Exception e) { return NIL; } }
Example 20
Source File: SampleSandboxed.java From luaj with MIT License | 4 votes |
static void runScriptInSandbox(String script) { // Each script will have it's own set of globals, which should // prevent leakage between scripts running on the same server. Globals user_globals = new Globals(); user_globals.load(new JseBaseLib()); user_globals.load(new PackageLib()); user_globals.load(new Bit32Lib()); user_globals.load(new TableLib()); user_globals.load(new JseStringLib()); user_globals.load(new JseMathLib()); // This library is dangerous as it gives unfettered access to the // entire Java VM, so it's not suitable within this lightweight sandbox. // user_globals.load(new LuajavaLib()); // Starting coroutines in scripts will result in threads that are // not under the server control, so this libary should probably remain out. // user_globals.load(new CoroutineLib()); // These are probably unwise and unnecessary for scripts on servers, // although some date and time functions may be useful. // user_globals.load(new JseIoLib()); // user_globals.load(new JseOsLib()); // Loading and compiling scripts from within scripts may also be // prohibited, though in theory it should be fairly safe. // LoadState.install(user_globals); // LuaC.install(user_globals); // The debug library must be loaded for hook functions to work, which // allow us to limit scripts to run a certain number of instructions at a time. // However we don't wish to expose the library in the user globals, // so it is immediately removed from the user globals once created. user_globals.load(new DebugLib()); LuaValue sethook = user_globals.get("debug").get("sethook"); user_globals.set("debug", LuaValue.NIL); // Set up the script to run in its own lua thread, which allows us // to set a hook function that limits the script to a specific number of cycles. // Note that the environment is set to the user globals, even though the // compiling is done with the server globals. LuaValue chunk = server_globals.load(script, "main", user_globals); LuaThread thread = new LuaThread(user_globals, chunk); // Set the hook function to immediately throw an Error, which will not be // handled by any Lua code other than the coroutine. LuaValue hookfunc = new ZeroArgFunction() { public LuaValue call() { // A simple lua error may be caught by the script, but a // Java Error will pass through to top and stop the script. throw new Error("Script overran resource limits."); } }; final int instruction_count = 20; sethook.invoke(LuaValue.varargsOf(new LuaValue[] { thread, hookfunc, LuaValue.EMPTYSTRING, LuaValue.valueOf(instruction_count) })); // When we resume the thread, it will run up to 'instruction_count' instructions // then call the hook function which will error out and stop the script. Varargs result = thread.resume(LuaValue.NIL); System.out.println("[["+script+"]] -> "+result); }