use of org.luaj.vm2.lib.LibFunction in project LuaViewSDK by alibaba.
the class LuaViewManager method bindMethods.
/**
* bind lua functions using method
*
* @param factory
* @param methods
* @return
*/
public static LuaTable bindMethods(Class<? extends LibFunction> factory, List<Method> methods) {
LuaTable env = new LuaTable();
try {
if (methods != null) {
for (int i = 0; i < methods.size(); i++) {
LibFunction f = factory.newInstance();
f.opcode = -1;
f.method = methods.get(i);
f.name = methods.get(i).getName();
env.set(f.name, f);
}
}
} catch (Exception e) {
throw new LuaError("[Bind Failed] " + e);
} finally {
return env;
}
}
use of org.luaj.vm2.lib.LibFunction in project LuaViewSDK by alibaba.
the class LuaViewManager method bind.
/**
* bind lua functions using opcode
*
* @param factory
* @param methods
* @return
*/
public static LuaTable bind(Class<? extends LibFunction> factory, String[] methods) {
LuaTable env = new LuaTable();
try {
if (methods != null) {
for (int i = 0; i < methods.length; i++) {
LibFunction f = factory.newInstance();
f.opcode = i;
f.method = null;
f.name = methods[i];
env.set(f.name, f);
}
}
} catch (Exception e) {
throw new LuaError("[Bind Failed] " + e);
} finally {
return env;
}
}
use of org.luaj.vm2.lib.LibFunction in project LuaViewSDK by alibaba.
the class LuaViewManager method createMetatable.
//-----------------------------------------metatable--------------------------------------------
/**
* create metatable for libs
*
* @return
*/
public static LuaTable createMetatable(Class<? extends LibFunction> libClass) {
//get from cache
LuaTable result = AppCache.getCache(CACHE_METATABLES).get(libClass);
if (result == null) {
LuaTable libTable = null;
if (LuaViewConfig.isUseNoReflection()) {
final List<String> methodNames = getMapperMethodNames(libClass);
libTable = LuaViewManager.bind(libClass, methodNames);
} else {
final List<Method> methods = getMapperMethods(libClass);
libTable = LuaViewManager.bindMethods(libClass, methods);
}
result = LuaValue.tableOf(new LuaValue[] { LuaValue.INDEX, libTable, LuaValue.NEWINDEX, new NewIndexFunction(libTable) });
//update cache
AppCache.getCache(CACHE_METATABLES).put(libClass, result);
}
return result;
}
use of org.luaj.vm2.lib.LibFunction in project LuaViewSDK by alibaba.
the class LibFunction method bind.
/**
* Bind a set of library functions, with an offset
* <p/>
* An array of names is provided, and the first name is bound
* with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
*
* @param env The environment to apply to each bound function
* @param factory the Class to instantiate for each bound function
* @param names array of String names, one for each function.
* @param firstopcode the first opcode to use
* @see #bind(LuaValue, Class, String[])
*/
protected void bind(LuaValue env, Class factory, String[] names, int firstopcode) {
try {
for (int i = 0, n = names.length; i < n; i++) {
LibFunction f = (LibFunction) factory.newInstance();
f.opcode = firstopcode + i;
f.name = names[i];
env.set(f.name, f);
}
} catch (Exception e) {
throw new LuaError("bind failed: " + e);
}
}
use of org.luaj.vm2.lib.LibFunction in project LuaViewSDK by alibaba.
the class LuaViewManager method bind.
/**
* bind lua functions using opcode
*
* @param factory
* @param methods
* @return
*/
public static LuaTable bind(Class<? extends LibFunction> factory, List<String> methods) {
LuaTable env = new LuaTable();
try {
if (methods != null) {
for (int i = 0; i < methods.size(); i++) {
LibFunction f = factory.newInstance();
f.opcode = i;
f.method = null;
f.name = methods.get(i);
env.set(f.name, f);
}
}
} catch (Exception e) {
throw new LuaError("[Bind Failed] " + e);
} finally {
return env;
}
}
Aggregations