org.luaj.vm2.lib.TwoArgFunction Java Examples

The following examples show how to use org.luaj.vm2.lib.TwoArgFunction. 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: LuaPackage.java    From Cubes with 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 #2
Source File: SampleSandboxed.java    From luaj with MIT License 4 votes vote down vote up
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 #3
Source File: MetatableTest.java    From luaj with MIT License 4 votes vote down vote up
public void testMetatableIndex() {
		assertEquals( table, table.setmetatable(null) );
		assertEquals( userdata, userdata.setmetatable(null) );
		assertEquals( userdatamt, userdatamt.setmetatable(null) );
		assertEquals( LuaValue.NIL, table.get(1) );
		assertEquals( LuaValue.NIL, userdata.get(1) );
		assertEquals( LuaValue.NIL, userdatamt.get(1) );
		
		// empty metatable
		LuaValue mt = LuaValue.tableOf();
		assertEquals( table, table.setmetatable(mt) );
		assertEquals( userdata, userdata.setmetatable(mt) );
		LuaBoolean.s_metatable = mt;
		LuaFunction.s_metatable = mt;
		LuaNil.s_metatable = mt;
		LuaNumber.s_metatable = mt;
//		LuaString.s_metatable = mt;
		LuaThread.s_metatable = mt;
		assertEquals( mt, table.getmetatable() );
		assertEquals( mt, userdata.getmetatable() );
		assertEquals( mt, LuaValue.NIL.getmetatable() );
		assertEquals( mt, LuaValue.TRUE.getmetatable() );
		assertEquals( mt, LuaValue.ONE.getmetatable() );
// 		assertEquals( StringLib.instance, string.getmetatable() );
		assertEquals( mt, function.getmetatable() );
		assertEquals( mt, thread.getmetatable() );
		
		// plain metatable
		LuaValue abc = LuaValue.valueOf("abc");
		mt.set( LuaValue.INDEX, LuaValue.listOf(new LuaValue[] { abc } ) );
		assertEquals( abc, table.get(1) );
		assertEquals( abc, userdata.get(1) );
		assertEquals( abc, LuaValue.NIL.get(1) );
		assertEquals( abc, LuaValue.TRUE.get(1) );
		assertEquals( abc, LuaValue.ONE.get(1) );
// 		assertEquals( abc, string.get(1) );
		assertEquals( abc, function.get(1) );
		assertEquals( abc, thread.get(1) );
		
		// plain metatable
		mt.set( LuaValue.INDEX, new TwoArgFunction() {
			public LuaValue call(LuaValue arg1, LuaValue arg2) {
				return LuaValue.valueOf( arg1.typename()+"["+arg2.tojstring()+"]=xyz" );
			}
			
		});
		assertEquals( "table[1]=xyz",    table.get(1).tojstring() );
		assertEquals( "userdata[1]=xyz", userdata.get(1).tojstring() );
		assertEquals( "nil[1]=xyz",      LuaValue.NIL.get(1).tojstring() );
		assertEquals( "boolean[1]=xyz",  LuaValue.TRUE.get(1).tojstring() );
		assertEquals( "number[1]=xyz",   LuaValue.ONE.get(1).tojstring() );
	//	assertEquals( "string[1]=xyz",   string.get(1).tojstring() );
		assertEquals( "function[1]=xyz", function.get(1).tojstring() );
		assertEquals( "thread[1]=xyz",   thread.get(1).tojstring() );
	}
 
Example #4
Source File: TableHashTest.java    From luaj with MIT License 4 votes vote down vote up
public void testIndexFunction() {
	final LuaTable t = new_Table();
	final LuaTable mt = new_Table();		
	
	final TwoArgFunction fb = new TwoArgFunction() {
		public LuaValue call(LuaValue tbl, LuaValue key) {
			assertEquals(tbl, t);
			return valueOf("from mt: "+key);
		}
	};
	
	// set basic values
	t.set( "ppp", "abc" );
	t.set( 123, "def" );
	mt.set(LuaValue.INDEX, fb);
	
	// check before setting metatable
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "nil", t.get("qqq").tojstring() );
	assertEquals( "nil", t.get(456).tojstring() );
	
	
	// check before setting metatable
	t.setmetatable(mt);
	assertEquals( mt, t.getmetatable() );
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "from mt: qqq", t.get("qqq").tojstring() );
	assertEquals( "from mt: 456", t.get(456).tojstring() );
	
	// use raw set
	t.rawset("qqq", "alt-qqq");
	t.rawset(456, "alt-456");
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "alt-qqq", t.get("qqq").tojstring() );
	assertEquals( "alt-456", t.get(456).tojstring() );

	// remove using raw set
	t.rawset("qqq", LuaValue.NIL);
	t.rawset(456, LuaValue.NIL);
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "from mt: qqq", t.get("qqq").tojstring() );
	assertEquals( "from mt: 456", t.get(456).tojstring() );

	// set metatable to null
	t.setmetatable(null);
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "nil", t.get("qqq").tojstring() );
	assertEquals( "nil", t.get(456).tojstring() );
}