Java Code Examples for org.luaj.vm2.Globals#set()

The following examples show how to use org.luaj.vm2.Globals#set() . 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: LuaEngine.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
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 2
Source File: XLua.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
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 3
Source File: JsePlatform.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
/** 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 4
Source File: JsePlatform.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/**
 * 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 5
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaConversion() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    theGlobals.set("key", 10);
    Assert.assertEquals(10, theGlobals.get("key").toint());
    Assert.assertEquals(10, theGlobals.get(LuaString.valueOf("key")).toint());

    final LuaValue chunk = theGlobals.load("return key").call();
    Assert.assertEquals(10, chunk.toint());
}
 
Example 6
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testGlobalSize() {
    final Globals theGlobals = new Globals();
    for (int i =0;i<=100;i++) {
        theGlobals.set("ABC", Integer.toString(i));
    }
    theGlobals.presize(1000);
    Assert.assertEquals(100, theGlobals.get("ABC").toint(),0);
}
 
Example 7
Source File: JsePlatform.java    From luaj with MIT License 5 votes vote down vote up
/** 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 8
Source File: SampleSandboxed.java    From luaj with MIT License 4 votes vote down vote up
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);
}