我正在尝试使用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:尝试索引?(一个函数值)
感谢您的帮助!