org.luaj.vm2.lib.ThreeArgFunction Java Examples

The following examples show how to use org.luaj.vm2.lib.ThreeArgFunction. 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: MetatableTest.java    From luaj with MIT License 4 votes vote down vote up
public void testMetatableNewIndex() {
		// 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;
		
		// plain metatable
		final LuaValue fallback = LuaValue.tableOf();
		LuaValue abc = LuaValue.valueOf("abc");
		mt.set( LuaValue.NEWINDEX, fallback );
		table.set(2,abc);
		userdata.set(3,abc);
		LuaValue.NIL.set(4,abc);
		LuaValue.TRUE.set(5,abc);
		LuaValue.ONE.set(6,abc);
// 		string.set(7,abc);
		function.set(8,abc);
		thread.set(9,abc);
		assertEquals( abc, fallback.get(2) );
		assertEquals( abc, fallback.get(3) );
		assertEquals( abc, fallback.get(4) );
		assertEquals( abc, fallback.get(5) );
		assertEquals( abc, fallback.get(6) );
// 		assertEquals( abc, StringLib.instance.get(7) );
		assertEquals( abc, fallback.get(8) );
		assertEquals( abc, fallback.get(9) );
		
		// metatable with function call
		mt.set( LuaValue.NEWINDEX, new ThreeArgFunction() {
			public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
				fallback.rawset(arg2, LuaValue.valueOf( "via-func-"+arg3 ));
				return NONE;
			}
			
		});
		table.set(12,abc);
		userdata.set(13,abc);
		LuaValue.NIL.set(14,abc);
		LuaValue.TRUE.set(15,abc);
		LuaValue.ONE.set(16,abc);
// 		string.set(17,abc);
		function.set(18,abc);
		thread.set(19,abc);
		LuaValue via = LuaValue.valueOf( "via-func-abc" );
		assertEquals( via, fallback.get(12) );
		assertEquals( via, fallback.get(13) );
		assertEquals( via, fallback.get(14) );
		assertEquals( via, fallback.get(15) );
		assertEquals( via, fallback.get(16) );
//		assertEquals( via, StringLib.instance.get(17) );
		assertEquals( via, fallback.get(18) );
		assertEquals( via, fallback.get(19) );
	}