Android 在AndroLua中使用LuaJava将字符串arg传递给Lua方法
我有一个用Java字符串定义的Lua函数,我想给它传递一个字符串argAndroid 在AndroLua中使用LuaJava将字符串arg传递给Lua方法,android,luajava,androlua,Android,Luajava,Androlua,我有一个用Java字符串定义的Lua函数,我想给它传递一个字符串arg String luaCode = "function hello(name) return 'Hello ' + name +'!' end"; 执行代码 public String runGreetingFromLua(String src, String arg) throws LuaException { L = LuaStateFactory.newLuaState(); L.openLibs();
String luaCode = "function hello(name) return 'Hello ' + name +'!' end";
执行代码
public String runGreetingFromLua(String src, String arg) throws LuaException {
L = LuaStateFactory.newLuaState();
L.openLibs();
L.setTop(0);
int ok = L.LloadString(src);
if (ok == 0) {
L.getGlobal("debug");
L.getField(-1, "traceback");
L.remove(-2);
L.insert(-2);
L.getGlobal("hello");
L.pushString(arg);
ok = L.pcall(1, 0, -2);
if (ok == 0) {
String res = output.toString();
output.setLength(0);
return res;
}
}
throw new LuaException(errorReason(ok) + ": " + L.toString(-1));
}
获取未知错误5:错误处理错误
我对lua和luajava完全陌生,我确信这个不了解LauState对象是如何工作的简单例子,java文档并不令人惊讶(或者我错过了一些非常新的东西)。如果我从lua代码中调用hello函数并使用print方法,我就能够得到一个返回值。我在Android设备上执行时使用了删除了一些错误处理代码,如luajava示例中所述,这些代码似乎会干扰并将调用
L.LLloadString
转换为L.LdoString(src)
和L.pcall
到L.call
public String runLuaHello(String src, String arg) throws LuaException {
//init
L = LuaStateFactory.newLuaState();
L.openLibs();
L.setTop(0);
//load the lua source code
int ok = L.LdoString(src);
if (ok == 0) {
//don't quite understand why it's getGlobal? but here you set the method name
L.getGlobal("hello");
//send the arg to lua
L.pushString(arg);
//this specifies 1 arg and 1 result
L.call(1, 1);
//get the result
String result = L.toString(-1);
//pop the result off the stack
L.pop(1);
return result;
}
throw new LuaException(errorReason(ok) + ": " + L.toString(-1));
}
恐怕不行,而且tbh已经有好几年没有在Android上研究lua了。