本文整理汇总了Java中org.luaj.vm2.lib.TwoArgFunction类的典型用法代码示例。如果您正苦于以下问题:Java TwoArgFunction类的具体用法?Java TwoArgFunction怎么用?Java TwoArgFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TwoArgFunction类属于org.luaj.vm2.lib包,在下文中一共展示了TwoArgFunction类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: luaTable1
import org.luaj.vm2.lib.TwoArgFunction; //导入依赖的package包/类
private void luaTable1() {
LuaTable metatable = new LuaTable();
metatable.rawset("__index", new TwoArgFunction() {
@Override
public LuaValue call(LuaValue arg1, LuaValue arg2) {
String s = arg2.tojstring();
try {
Field field = c.getField(s);
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && field.getAnnotation(LuaExclude.class) == null) {
return LuaConversion.convertToLua(field.get(null));
}
} catch (Exception ignored) {
}
luaTableTry( s);
error("No such field or method: " + s);
return null;
}
});
}
示例2: LuaPackage
import org.luaj.vm2.lib.TwoArgFunction; //导入依赖的package包/类
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);
}
示例3: testMetatableIndex
import org.luaj.vm2.lib.TwoArgFunction; //导入依赖的package包/类
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() );
}
示例4: testIndexFunction
import org.luaj.vm2.lib.TwoArgFunction; //导入依赖的package包/类
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() );
}