Java Code Examples for org.luaj.vm2.Globals#load()
The following examples show how to use
org.luaj.vm2.Globals#load() .
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: JsePlatform.java From VideoOS-Android-SDK with GNU General Public License v3.0 | 6 votes |
/** * Create a standard set of globals for JSE including all the libraries. * * @return Table of globals initialized with the standard JSE libraries * @see #debugGlobals() * @see JsePlatform * @see JmePlatform */ public static Globals standardGlobals(Globals globals) { globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseOsLib()); // globals.load(new JseIoLib());//安全考虑,删除for LuaView // globals.load(new LuajavaLib());//安全考虑,删除for LuaView LoadState.install(globals); LuaC.install(globals); return globals; }
Example 2
Source File: JsePlatform.java From XPrivacyLua with GNU General Public License v3.0 | 6 votes |
/** * Create a standard set of globals for JSE including all the libraries. * * @return Table of globals initialized with the standard JSE libraries * @see #debugGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform */ public static Globals standardGlobals() { Globals globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new LuajavaLib()); LoadState.install(globals); LuaC.install(globals); return globals; }
Example 3
Source File: LuaEngine.java From remixed-dungeon with GNU General Public License v3.0 | 6 votes |
private LuaEngine() { globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new MultiDexLuajavaLib()); LoadState.install(globals); LuaC.install(globals); globals.finder = this; globals.set("loadResource", new resLoader()); }
Example 4
Source File: JsePlatform.java From luaj with MIT License | 6 votes |
/** * Create a standard set of globals for JSE including all the libraries. * * @return Table of globals initialized with the standard JSE libraries * @see #debugGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform */ public static Globals standardGlobals() { Globals globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new JseStringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new LuajavaLib()); LoadState.install(globals); LuaC.install(globals); return globals; }
Example 5
Source File: CollectingOrphanedCoroutines.java From luaj with MIT License | 6 votes |
public static void main(String[] args) throws InterruptedException { // This timer controls how often each Java thread wakes up and checks if // it has been orhaned or not. A large number here will produce a long // delay between orphaning and colleciton, and a small number here will // consumer resources polling for orphaned status if there are many threads. LuaThread.thread_orphan_check_interval = 500; // Should work with standard or debug globals. Globals globals = JsePlatform.standardGlobals(); // Globals globals = JsePlatform.debugGlobals(); // Should work with plain compiler or lua-to-Java compiler. // org.luaj.vm2.luajc.LuaJC.install(globals);; // Load and run the script, which launches coroutines over and over forever. LuaValue chunk = globals.load(script, "main"); chunk.call(); }
Example 6
Source File: JmePlatform.java From luaj with MIT License | 6 votes |
/** * Create a standard set of globals for JME including all the libraries. * * @return Table of globals initialized with the standard JME libraries * @see #debugGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform */ public static Globals standardGlobals() { Globals globals = new Globals(); globals.load(new BaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new OsLib()); globals.load(new MathLib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new CoroutineLib()); globals.load(new JmeIoLib()); LoadState.install(globals); LuaC.install(globals); return globals; }
Example 7
Source File: XLua.java From XPrivacyLua with GNU General Public License v3.0 | 5 votes |
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); }
Example 8
Source File: SampleApplet.java From luaj with MIT License | 5 votes |
public void init() { // Find the script to load from the applet parameters. String script = getParameter(script_parameter); if (script == null) script = default_script; System.out.println("Loading " + script); // Construct globals specific to the applet platform. globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new JseStringLib()); globals.load(new CoroutineLib()); globals.load(new JseMathLib()); globals.load(new JseIoLib()); globals.load(new JseOsLib()); globals.load(new AppletLuajavaLib()); LoadState.install(globals); LuaC.install(globals); // Use custom resource finder. globals.finder = this; // Look up and save the handy pcall method. pcall = globals.get("pcall"); // Load and execute the script, supplying this Applet as the only // argument. globals.loadfile(script).call(CoerceJavaToLua.coerce(this)); }
Example 9
Source File: AppTest.java From luaj with MIT License | 5 votes |
public void testDirectEvaluation() { String script = "return math.pow(..., 3)"; Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load(script, "cube"); int result = chunk.call(LuaValue.valueOf(5)).toint(); assertEquals(125, result); }
Example 10
Source File: App.java From luaj with MIT License | 5 votes |
public static void main( String[] args ) { String script = "print('hello, world', _VERSION)"; // create an environment to run in Globals globals = JsePlatform.standardGlobals(); // Use the convenience function on the globals to load a chunk. LuaValue chunk = globals.load(script, "maven-exmaple"); // Use any of the "call()" or "invoke()" functions directly on the chunk. chunk.call(); }
Example 11
Source File: LuaTest.java From Bytecoder with Apache License 2.0 | 5 votes |
@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 12
Source File: LuaTest.java From Bytecoder with Apache License 2.0 | 5 votes |
@Test public void testLuaReturnIntegerAdd() { final Globals theGlobals = new Globals(); LuaC.install(theGlobals); final LuaValue chunk = theGlobals.load("return 123 + 231"); final LuaString theResult = chunk.call().strvalue(); Assert.assertEquals("354", theResult.tojstring()); }
Example 13
Source File: JsePlatformTest.java From luaj with MIT License | 5 votes |
public void testLuaMainPassesArguments() { Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load("return #arg, arg.n, arg[2], arg[1]"); Varargs results = JsePlatform.luaMain(chunk, new String[] { "aaa", "bbb" }); assertEquals(results.narg(), 4); assertEquals(results.arg(1), LuaValue.valueOf(2)); assertEquals(results.arg(2), LuaValue.valueOf(2)); assertEquals(results.arg(3), LuaValue.valueOf("bbb")); assertEquals(results.arg(4), LuaValue.valueOf("aaa")); }
Example 14
Source File: ScriptTest.java From HtmlNative with Apache License 2.0 | 5 votes |
@Test public void execute() throws Exception { Globals globals = JsePlatform.standardGlobals(); // globals.set("LCallback", new LCallback()); LuaValue value = globals.load(helloWorldScript); value.call(); }
Example 15
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 16
Source File: JsePlatform.java From HtmlNative with Apache License 2.0 | 5 votes |
/** * Create a standard set of globals for JSE including all the libraries. * * @return Table of globals initialized with the standard JSE libraries * @see #debugGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform */ public static Globals standardGlobals() { Globals globals = new Globals(); globals.load(new JseBaseLib()); globals.load(new PackageLib()); globals.load(new Bit32Lib()); globals.load(new TableLib()); globals.load(new StringLib()); globals.load(new JseMathLib()); globals.load(new JseOsLib()); LoadState.install(globals); LuaC.install(globals); return globals; }
Example 17
Source File: SampleSandboxed.java From luaj with MIT License | 4 votes |
public static void main(String[] args) { // Create server globals with just enough library support to compile user scripts. server_globals = new Globals(); server_globals.load(new JseBaseLib()); server_globals.load(new PackageLib()); server_globals.load(new JseStringLib()); // To load scripts, we occasionally need a math library in addition to compiler support. // To limit scripts using the debug library, they must be closures, so we only install LuaC. server_globals.load(new JseMathLib()); LoadState.install(server_globals); LuaC.install(server_globals); // Set up the LuaString metatable to be read-only since it is shared across all scripts. LuaString.s_metatable = new ReadOnlyLuaTable(LuaString.s_metatable); // Example normal scripts that behave as expected. runScriptInSandbox( "return 'foo'" ); runScriptInSandbox( "return ('abc'):len()" ); runScriptInSandbox( "return getmetatable('abc')" ); runScriptInSandbox( "return getmetatable('abc').len" ); runScriptInSandbox( "return getmetatable('abc').__index" ); // Example user scripts that attempt rogue operations, and will fail. runScriptInSandbox( "return setmetatable('abc', {})" ); runScriptInSandbox( "getmetatable('abc').len = function() end" ); runScriptInSandbox( "getmetatable('abc').__index = {}" ); runScriptInSandbox( "getmetatable('abc').__index.x = 1" ); runScriptInSandbox( "while true do print('loop') end" ); // Example use of other shared metatables, which should also be made read-only. // This toy example allows booleans to be added to numbers. runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" ); LuaBoolean.s_metatable = new ReadOnlyLuaTable(LuaValue.tableOf(new LuaValue[] { LuaValue.ADD, new TwoArgFunction() { public LuaValue call(LuaValue x, LuaValue y) { return LuaValue.valueOf( (x == TRUE ? 1.0 : x.todouble()) + (y == TRUE ? 1.0 : y.todouble()) ); } }, })); runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" ); }
Example 18
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); }
Example 19
Source File: JsePlatform.java From HtmlNative with Apache License 2.0 | 2 votes |
/** * Create standard globals including the {@link DebugLib} library. * * @return Table of globals initialized with the standard JSE and debug libraries * @see #standardGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform * @see DebugLib */ public static Globals debugGlobals() { Globals globals = standardGlobals(); globals.load(new DebugLib()); return globals; }
Example 20
Source File: JmePlatform.java From luaj with MIT License | 2 votes |
/** Create standard globals including the {@link DebugLib} library. * * @return Table of globals initialized with the standard JSE and debug libraries * @see #standardGlobals() * @see org.luaj.vm2.lib.jse.JsePlatform * @see org.luaj.vm2.lib.jme.JmePlatform * @see DebugLib */ public static Globals debugGlobals() { Globals globals = standardGlobals(); globals.load(new DebugLib()); return globals; }