我正在尝试使用 LuaJ 在 Java 程序中调用 lua 函数。当我没有将任何参数传递给闭包时,它工作正常:
String script = "print 'Hello World!'";
InputStream input = new ByteArrayInputStream(script.getBytes());
Prototype prototype = LuaC.compile(input, "script");
LuaValue globals = JsePlatform.standardGlobals();
LuaClosure closure = new LuaClosure(prototype, globals);
closure.call();
但是现在我正在尝试使用带有参数的顶级函数的 lua 脚本,但我只是不知道如何从 Java 中传递参数。这是我到目前为止得到的:
String script = "function something(argument)\n"+
"test_string = 'Hello World!'\n"+
"print(test_string)\n"+
"print(argument)\n"+
"end";
InputStream input = new ByteArrayInputStream(script.getBytes());
Prototype prototype = LuaC.compile(input, "script");
LuaValue globals = JsePlatform.standardGlobals();
LuaClosure closure = new LuaClosure(prototype, globals);
closure.invokemethod("something", CoerceJavaToLua.coerce("Foo"));
这会导致 invokemethod 行出现异常:
org.luaj.vm2.LuaError: 尝试索引?(一个函数值)
谢谢你的帮助!