Search in sources :

Example 1 with LibFunction

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;
    }
}
Also used : LuaTable(org.luaj.vm2.LuaTable) LuaError(org.luaj.vm2.LuaError) LibFunction(org.luaj.vm2.lib.LibFunction)

Example 2 with LibFunction

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;
    }
}
Also used : LuaTable(org.luaj.vm2.LuaTable) LuaError(org.luaj.vm2.LuaError) LibFunction(org.luaj.vm2.lib.LibFunction)

Example 3 with LibFunction

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;
}
Also used : NewIndexFunction(com.taobao.luaview.fun.mapper.ui.NewIndexFunction) LuaTable(org.luaj.vm2.LuaTable) Method(java.lang.reflect.Method) LuaValue(org.luaj.vm2.LuaValue)

Example 4 with LibFunction

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);
    }
}
Also used : LuaError(org.luaj.vm2.LuaError)

Example 5 with LibFunction

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;
    }
}
Also used : LuaTable(org.luaj.vm2.LuaTable) LuaError(org.luaj.vm2.LuaError) LibFunction(org.luaj.vm2.lib.LibFunction)

Aggregations

LuaError (org.luaj.vm2.LuaError)4 LuaTable (org.luaj.vm2.LuaTable)4 LibFunction (org.luaj.vm2.lib.LibFunction)3 NewIndexFunction (com.taobao.luaview.fun.mapper.ui.NewIndexFunction)1 Method (java.lang.reflect.Method)1 LuaValue (org.luaj.vm2.LuaValue)1