Java Code Examples for org.luaj.vm2.LuaTable#setmetatable()

The following examples show how to use org.luaj.vm2.LuaTable#setmetatable() . 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: IoLib.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	
	// io lib functions
	LuaTable t = new LuaTable();
	bind(t, IoLibV.class, IO_NAMES );
	
	// create file methods table
	filemethods = new LuaTable();
	bind(filemethods, IoLibV.class, FILE_NAMES, FILE_CLOSE );

	// set up file metatable
	LuaTable mt = new LuaTable();
	bind(mt, IoLibV.class, new String[] { "__index" }, IO_INDEX );
	t.setmetatable( mt );
	
	// all functions link to library instance
	setLibInstance( t );
	setLibInstance( filemethods );
	setLibInstance( mt );
	
	// return the table
	env.set("io", t);
	env.get("package").get("loaded").set("io", t);
	return t;
}
 
Example 2
Source File: IoLib.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	
	// io lib functions
	LuaTable t = new LuaTable();
	bind(t, IoLibV.class, IO_NAMES );
	
	// create file methods table
	filemethods = new LuaTable();
	bind(filemethods, IoLibV.class, FILE_NAMES, FILE_CLOSE );

	// set up file metatable
	LuaTable mt = new LuaTable();
	bind(mt, IoLibV.class, new String[] { "__index" }, IO_INDEX );
	t.setmetatable( mt );
	
	// all functions link to library instance
	setLibInstance( t );
	setLibInstance( filemethods );
	setLibInstance( mt );
	
	// return the table
	env.set("io", t);
	env.get("package").get("loaded").set("io", t);
	return t;
}
 
Example 3
Source File: IoLib.java    From luaj with MIT License 6 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	
	// io lib functions
	LuaTable t = new LuaTable();
	bind(t, IoLibV.class, IO_NAMES );
	
	// create file methods table
	filemethods = new LuaTable();
	bind(filemethods, IoLibV.class, FILE_NAMES, FILE_CLOSE );

	// set up file metatable
	LuaTable mt = new LuaTable();
	bind(mt, IoLibV.class, new String[] { "__index" }, IO_INDEX );
	t.setmetatable( mt );
	
	// all functions link to library instance
	setLibInstance( t );
	setLibInstance( filemethods );
	setLibInstance( mt );
	
	// return the table
	env.set("io", t);
	if (!env.get("package").isnil()) env.get("package").get("loaded").set("io", t);
	return t;
}
 
Example 4
Source File: TableHashTest.java    From luaj with MIT License 4 votes vote down vote up
public void testIndexMetatag() {
	LuaTable t = new_Table();
	LuaTable mt = new_Table();
	LuaTable fb = new_Table();
	
	// set basic values
	t.set( "ppp", "abc" );
	t.set( 123, "def" );
	mt.set(LuaValue.INDEX, fb);
	fb.set( "qqq", "ghi" );
	fb.set( 456, "jkl" );
	
	// 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() );
	assertEquals( "nil", fb.get("ppp").tojstring() );
	assertEquals( "nil", fb.get(123).tojstring() );
	assertEquals( "ghi", fb.get("qqq").tojstring() );
	assertEquals( "jkl", fb.get(456).tojstring() );
	assertEquals( "nil", mt.get("ppp").tojstring() );
	assertEquals( "nil", mt.get(123).tojstring() );
	assertEquals( "nil", mt.get("qqq").tojstring() );
	assertEquals( "nil", mt.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( "ghi", t.get("qqq").tojstring() );
	assertEquals( "jkl", t.get(456).tojstring() );
	assertEquals( "nil", fb.get("ppp").tojstring() );
	assertEquals( "nil", fb.get(123).tojstring() );
	assertEquals( "ghi", fb.get("qqq").tojstring() );
	assertEquals( "jkl", fb.get(456).tojstring() );
	assertEquals( "nil", mt.get("ppp").tojstring() );
	assertEquals( "nil", mt.get(123).tojstring() );
	assertEquals( "nil", mt.get("qqq").tojstring() );
	assertEquals( "nil", mt.get(456).tojstring() );

	// set metatable to metatable without values
	t.setmetatable(fb);
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "nil", t.get("qqq").tojstring() );
	assertEquals( "nil", 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() );
}
 
Example 5
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() );
}