【问题标题】:LUAJ Coerced Java object won't accept LuaValue argumentLUAJ 强制 Java 对象不接受 LuaValue 参数
【发布时间】:2015-11-16 22:18:13
【问题描述】:

当 Java 代码特别要求 LuaValue 时,我遇到了 LuaJ 不接受 LuaValue 作为参数的问题。

public void registerEvent(LuaValue id, String event, String priority,
                          LuaValue callback)
{
    if(!(id instanceof LuaTable))
    {
        throw new RuntimeException("id must be an LuaTable");
    }
    EventDispatcher.addHandler(id, event, priority, callback);
}

理想情况下,这将允许 Lua 中的代码像这样简单地读取......

function main(this)
    this.modName="Some Mod"
    this.lastX = 0
    hg.both.registerEvent(this, "inputcapturedevent", "last", eventRun)
end

function eventRun(this, event)
    this.lastX += event.getX()
end

可悲的是,这个简单的错误提示它需要用户数据,但得到了一个表。

org.luaj.vm2.LuaError: script:4 bad argument: userdata expected, got table

“this”的值在两种情况下都是相同的 LuaTable,但是因为方法 registerEvent 是通过 CoerceJavaToLua.coerce(...) 添加的,所以它认为它想要一个 java 对象,而不是意识到它真的想要一个 LuaVale。

所以我的问题是这样的。有没有更好的方法可以让我在 Java 和 Lua 中使用相同的功能?如果您一直在这里阅读,感谢您抽出宝贵时间:)

【问题讨论】:

  • 为了完整起见,这是在 Mod 加载时调用 CompiledScript script = ((Compilable) engine).compile(fr); script.eval(sb); // Put the API functions in under hg.X LuaTable library = new LuaTable(); event.getMod().setLibrary(library); LuaFunction mainFunc = (LuaFunction) sb.get("main"); mainFunc.call(CoerceJavaToLua.coerce(library));

标签: java lua luaj


【解决方案1】:

您遇到的错误可能是一个红鲱鱼,可能是由于您将“registerEvent”函数绑定到“hg.both”的值的方式。可能你只需要改用方法语法,比如

hg.both:registerEvent(this, "inputcapturedevent", "last", eventRun)

如果您想使用点语法hg.both.registerEvent,那么使用VarArgFunction 并实现invoke() 可能是一种更直接的实现方式。在此示例中,Both.registerEvent 是一个普通变量,即 VarArgFunction。

public static class Both {
    public static VarArgFunction registerEvent = new VarArgFunction() {
        public Varargs invoke(Varargs args) {
            LuaTable id = args.checktable(1);
            String event = args.tojstring(2);
            String priority = args.tojstring(3);
            LuaValue callback = args.arg(4);
            EventDispatcher.addHandler(id, event, priority, callback);
            return NIL;
        }
    };
}

public static void main(String[] args) throws ScriptException {

    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine engine = sem.getEngineByName("luaj");
    Bindings sb = engine.createBindings();

    String fr = 
        "function main(this);" +
        "   this.modName='Some Mod';" +
        "   this.lastX = 0;" +
        "   hg.both.registerEvent(this, 'inputcapturedevent', 'last', eventRun);" +
        "end;";
    System.out.println(fr);

    CompiledScript script = ((Compilable) engine).compile(fr);
    script.eval(sb);
    LuaFunction mainFunc = (LuaFunction) sb.get("main");

    LuaTable hg = new LuaTable();
    hg.set("both", CoerceJavaToLua.coerce(Both.class));
    sb.put("hg", hg);

    LuaTable library = new LuaTable();
    mainFunc.call(CoerceJavaToLua.coerce(library));
}

【讨论】:

    猜你喜欢
    • 2020-05-24
    • 2016-09-03
    • 2017-11-24
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2016-01-14
    • 2012-06-29
    • 2011-11-20
    相关资源
    最近更新 更多