public static LuaValue runLuaFile(Globals globals, String luafile) { LuaValue chunk = globals.load("require 'com.example.import-me';"); chunk.call(); chunk = globals.loadfile(luafile); return chunk.call(); }
public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception { Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, ""); Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load("require 'com.example.import-me';"); chunk.call(); LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, globals); return luaClosure.call(); }
/** Convenience function to load a string value as a script. Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script, String chunkname) { return load(new StrReader(script), chunkname); }
/** Convenience function to load a string value as a script with a custom environment. * Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @param environment LuaTable to be used as the environment for the loaded function. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script, String chunkname, LuaTable environment) { return load(new StrReader(script), chunkname, environment); }
/** Convenience function to load a string value as a script. Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script) { return load(new StrReader(script), script); }
/** Convenience function to load a string value as a script. Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script, String chunkname) { return load(new StrReader(script), chunkname); }
/** Convenience function to load a string value as a script with a custom environment. * Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @param environment LuaTable to be used as the environment for the loaded function. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script, String chunkname, LuaTable environment) { return load(new StrReader(script), chunkname, environment); }
/** Load the content form a reader as a text file. Must be lua source. * The source is converted to UTF-8, so any characters appearing in quoted literals * above the range 128 will be converted into multiple bytes. * @param reader Reader containing text of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(Reader reader, String chunkname) { return load(new UTF8Stream(reader), chunkname, "t", this); }
/** Convenience function to load a string value as a script. Must be lua source. * @param script Contents of a lua script, such as "print 'hello, world.'" * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(String script) { return load(new StrReader(script), script); }
/** Load the content form a reader as a text file. Must be lua source. * The source is converted to UTF-8, so any characters appearing in quoted literals * above the range 128 will be converted into multiple bytes. * @param reader Reader containing text of a lua script, such as "print 'hello, world.'" * @param chunkname Name that will be used within the chunk as the source. * @return LuaValue that may be executed via .call(), .invoke(), or .method() calls. * @throws LuaError if the script could not be compiled. */ public LuaValue load(Reader reader, String chunkname) { return load(new UTF8Stream(reader), chunkname, "t", this); }
Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load( "t = {"+ " subTitle = 'Haircut',"+ "};"+ "return t"); LuaValue result = chunk.call(); // Executes the chunk and returns 't' String text = result.get("subTitle").tojstring(); System.out.println(text);
/** Convenience function for loading a file that is either binary lua or lua source. * @param filename Name of the file to load. * @return LuaValue that can be call()'ed or invoke()'ed. * @throws LuaError if the file could not be loaded. */ public LuaValue loadfile(String filename) { try { return load(finder.findResource(filename), "@"+filename, "bt", this); } catch (Exception e) { return error("load "+filename+": "+e); } }
/** Convenience function for loading a file that is either binary lua or lua source. * @param filename Name of the file to load. * @return LuaValue that can be call()'ed or invoke()'ed. * @throws LuaError if the file could not be loaded. */ public LuaValue loadfile(String filename) { try { return load(finder.findResource(filename), "@"+filename, "bt", this); } catch (Exception e) { return error("load "+filename+": "+e); } }
@Override public void run(String script) { HNLog.d(HNLog.SANDBOX, "Execute script \"" + script + "\"."); LuaValue l = mGlobals.load(script); l.call(); }
public Varargs loadStream(InputStream is, String chunkname, String mode, LuaValue env) { try { if ( is == null ) return varargsOf(NIL, valueOf("not found: "+chunkname)); return globals.load(is, chunkname, mode, env); } catch (Exception e) { return varargsOf(NIL, valueOf(e.getMessage())); } }
public Varargs loadStream(InputStream is, String chunkname, String mode, LuaValue env) { try { if (is == null) { return varargsOf(NIL, valueOf("not found: " + chunkname)); } return globals.load(is, chunkname, mode, env); } catch (Exception e) { return varargsOf(NIL, valueOf(e.getMessage())); } }
private static Globals getGlobals(Context context, XHook hook, Map<String, String> settings) { Globals globals = JsePlatform.standardGlobals(); // base, bit32, coroutine, io, math, os, package, string, table, luajava if (BuildConfig.DEBUG) globals.load(new DebugLib()); globals.set("log", new LuaLog(context.getPackageName(), context.getApplicationInfo().uid, hook.getId())); globals.set("hook", new LuaHook(context, settings)); return new LuaLocals(globals); }
@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(); }
@Test public void execute() throws Exception { Globals globals = JsePlatform.standardGlobals(); // globals.set("LCallback", new LCallback()); LuaValue value = globals.load(helloWorldScript); value.call(); }