org.luaj.vm2.lib.ZeroArgFunction Java Examples

The following examples show how to use org.luaj.vm2.lib.ZeroArgFunction. 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 Project: HtmlNative   Author: hsllany   File: LHttp.java    License: Apache License 2.0 5 votes vote down vote up
LResponse() {
    super();
    set("header", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (mHeader == null) {
                return LuaValue.NIL;
            }
            return LuaString.valueOf(mHeader);
        }
    });

    set("body", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (mBody == null) {
                return LuaValue.NIL;
            }
            return LuaString.valueOf(mBody);
        }
    });

    set("status", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            return LuaInteger.valueOf(mStatusCode);
        }
    });
}
 
Example #2
Source Project: HtmlNative   Author: hsllany   File: LObject.java    License: Apache License 2.0 5 votes vote down vote up
LObject() {
    super();
    set("type", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            String className = LObject.this.onObjectClassName();
            if (className != null) {
                return LuaString.valueOf(className);
            } else {
                return LuaValue.NIL;
            }
        }
    });
}
 
Example #3
Source Project: luaj   Author: luaj   File: LuaOperationsTest.java    License: MIT License 5 votes vote down vote up
public void testFunctionClosureThreadEnv() {

		// set up suitable environments for execution
		LuaValue aaa = LuaValue.valueOf("aaa");
		LuaValue eee = LuaValue.valueOf("eee");
		final Globals globals = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
		LuaTable newenv = LuaValue.tableOf( new LuaValue[] { 
				LuaValue.valueOf("a"), LuaValue.valueOf("aaa"), 
				LuaValue.valueOf("b"), LuaValue.valueOf("bbb"), } );
		LuaTable mt = LuaValue.tableOf( new LuaValue[] { LuaValue.INDEX, globals } );
		newenv.setmetatable(mt);
		globals.set("a", aaa);
		newenv.set("a", eee);

		// function tests
		{
			LuaFunction f = new ZeroArgFunction() { public LuaValue call() { return globals.get("a");}};
			assertEquals( aaa, f.call() );
		}
		
		// closure tests
		{
			Prototype p = createPrototype( "return a\n", "closuretester" );
			LuaClosure c = new LuaClosure(p, globals);
			
			// Test that a clusure with a custom enviroment uses that environment.
			assertEquals( aaa, c.call() );
			c = new LuaClosure(p, newenv);
			assertEquals( newenv, c.upValues[0].getValue() );
			assertEquals( eee, c.call() );
		}
	}
 
Example #4
Source Project: Cubes   Author: ictrobot   File: LuaPackage.java    License: MIT License 5 votes vote down vote up
public LuaPackage(final String path) {
  super(new Object());
  this.path = path;
  
  LuaTable metatable = new LuaTable();
  metatable.rawset("__index", new TwoArgFunction() {
    @Override
    public LuaValue call(LuaValue arg1, LuaValue arg2) {
      if (arg2.checkjstring().equals("class")) {
        try {
          Class<?> cla = Class.forName(path);
          return new LuaClass(cla);
        } catch (ClassNotFoundException e) {
          throw new LuaError("No such class: " + path);
        }
      }
      String name = path.isEmpty() ? arg2.checkjstring() : path + "." + arg2.checkjstring();
      return new LuaPackage(name);
    }
  });
  metatable.rawset("__tostring", new ZeroArgFunction() {
    @Override
    public LuaValue call() {
      if (path.isEmpty()) return LuaValue.valueOf("[JPackage Root]");
      return LuaValue.valueOf("[JPackage: " + path + "]");
    }
  });
  metatable.rawset("__metatable", LuaValue.FALSE);
  setmetatable(metatable);
}
 
Example #5
Source Project: luaj   Author: luaj   File: SampleSandboxed.java    License: 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);
}
 
Example #6
Source Project: Lukkit   Author: Lukkit   File: CommandEvent.java    License: MIT License 4 votes vote down vote up
public CommandEvent(CommandSender sender, String command, String... args) {


        this.set("isPlayerSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof Player ? TRUE : FALSE;
            }
        });

        this.set("isConsoleSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof ConsoleCommandSender ? TRUE : FALSE;
            }
        });

        this.set("isBlockSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof BlockCommandSender ? TRUE : FALSE;
            }
        });

        this.set("isEntitySender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return sender instanceof Entity ? TRUE : FALSE;
            }
        });

        this.set("getSender", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return CoerceJavaToLua.coerce(sender);
            }
        });

        this.set("getArgs", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                LuaTable tbl = new LuaTable();
                for (int i = 0; i < args.length; i++)
                    tbl.set(i + 1, args[i]);
                return tbl;
            }
        });

        this.set("getCommand", new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return CoerceJavaToLua.coerce(command);
            }
        });

    }
 
Example #7
Source Project: Lukkit   Author: Lukkit   File: SkullWrapper.java    License: MIT License 4 votes vote down vote up
public SkullWrapper(ItemStack skullItem) {
    this.skull = (skullItem == null) ? new ItemStack(Material.SKULL_ITEM, 1) : skull;
    this.self = this; // Allows access from setOwner() since this is populate by the function.

    if (skullItem == null) {
        this.skull.setDurability((short) 3);
        this.meta = (SkullMeta) Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
    } else {
        this.meta = (SkullMeta) this.skull.getItemMeta();
    }

    this.set("setOwner", new OneArgFunction() {
        @Override
        public LuaValue call(LuaValue username) {
            meta.setOwner(username.checkjstring());
            return CoerceJavaToLua.coerce(self);
        }
    });

    this.set("hasOwner", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            return LuaValue.valueOf(meta.hasOwner());
        }
    });

    this.set("getOwner", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (meta.getOwner() == null) {
                return LuaValue.NIL;
            }
            return LuaValue.valueOf(meta.getOwner());
        }
    });

    this.set("getItemStack", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            skull.setItemMeta(meta);
            return CoerceJavaToLua.coerce(skull);
        }
    });
}