Java 安卓&;LuaJ 3.0绑定

Java 安卓&;LuaJ 3.0绑定,java,android,lua,luaj,Java,Android,Lua,Luaj,我正在使用LuaJ 3.0编写Android应用程序。如何将Java对象绑定到特定的LuaClosure(对于整个脚本) Lua代码: local = state or nil state.foo("some string") Java代码: Prototype prototype; try{ InputStream stream = mContext.getResources().getAssets().open(LUA_ASSETS_DIRECTORY + "

我正在使用LuaJ 3.0编写Android应用程序。如何将Java对象绑定到特定的LuaClosure(对于整个脚本)

Lua代码:

local  = state or nil
state.foo("some string")
Java代码:

Prototype prototype;

    try{

        InputStream stream = mContext.getResources().getAssets().open(LUA_ASSETS_DIRECTORY + "test.lua");
        prototype = LuaC.compile(stream, "script");

    } catch (IOException e) {

        return false;
    }

    LuaClosure closure = new LuaClosure(prototype, mLuaGlobals);
    // binding code
    closure.call();

我知道在LuaJ 2.0中(但不是在3.0中)有LuaValue.setenv,我还知道如何创建lib并将它们绑定到Globals。

从LuaJ 3.0中,修改闭包的功能从API中删除。 如果您只想将Java对象绑定到特定的Lua环境,那么可以创建多个。然后,您可以将Java对象绑定到特定的全局对象,并从每个全局对象创建一个闭包:

Globals mLuaGlobals1 = JsePlatform.standardGlobals();
Globals mLuaGlobals2 = JsePlatform.standardGlobals();
Globals mLuaGlobals3 = JsePlatform.standardGlobals();

// binding code
mLuaGlobals1.rawset("state", CoerceJavaToLua.coerce(yourJavaObject));
mLuaGlobals2.rawset("state", CoerceJavaToLua.coerce(anotherJavaObject));
// We don't expose anything to mLuaGlobals3 in this example

// Create closures
// We use exact same prototype (script code) for each of them
LuaClosure closure1 = new LuaClosure(prototype, mLuaGlobals1);
LuaClosure closure2 = new LuaClosure(prototype, mLuaGlobals2);
LuaClosure closure3 = new LuaClosure(prototype, mLuaGlobals3);

closure1.call(); // yourJavaObject is exposed through "state" variable
closure2.call(); // anotherJavaObject is exposed through "state" variable
closure3.call(); // "state" variable is absent

是的,这是一个愚蠢的解决方案,因此如果您真的需要控制LuaClosure,那么降级到LuaJ 2.0.3是最好的选择。

从LuaJ 3.0中,修改闭包的功能将从API中删除。 如果您只想将Java对象绑定到特定的Lua环境,那么可以创建多个。然后,您可以将Java对象绑定到特定的全局对象,并从每个全局对象创建一个闭包:

Globals mLuaGlobals1 = JsePlatform.standardGlobals();
Globals mLuaGlobals2 = JsePlatform.standardGlobals();
Globals mLuaGlobals3 = JsePlatform.standardGlobals();

// binding code
mLuaGlobals1.rawset("state", CoerceJavaToLua.coerce(yourJavaObject));
mLuaGlobals2.rawset("state", CoerceJavaToLua.coerce(anotherJavaObject));
// We don't expose anything to mLuaGlobals3 in this example

// Create closures
// We use exact same prototype (script code) for each of them
LuaClosure closure1 = new LuaClosure(prototype, mLuaGlobals1);
LuaClosure closure2 = new LuaClosure(prototype, mLuaGlobals2);
LuaClosure closure3 = new LuaClosure(prototype, mLuaGlobals3);

closure1.call(); // yourJavaObject is exposed through "state" variable
closure2.call(); // anotherJavaObject is exposed through "state" variable
closure3.call(); // "state" variable is absent

是的,这是一个愚蠢的解决方案,所以如果你真的需要控制LuaClosure,降级到LuaJ 2.0.3是最好的选择。

谢谢,我已经按项目降级到LuaJ 2了。是的,这是最好的选择。谢谢你,我已经被LuaJ 2的项目搞砸了。是的,这是最好的选择。