Lua Error Attempt to perform arithmetic on local variable

邮差的信 提交于 2019-12-24 10:04:56

问题


Here is the function calc.lua:

function foo(n) 
return n*2 
end

Here is my LuaJavaCall

L.getGlobal("foo");
L.pushJavaObject(8);
int retCode=L.pcall(1,1,0); // nResults)//L.pcall(1, 1,-2);
String errstr =  L.toString(-1);   // Attempt to perform arithmetic on local variable 'n'

Update: as indicated below I needed to use L.pushNumber(8.0) instead of L.pushJavaObject()


回答1:


Try using L.pushNumber instead of L.pushJavaObject like this:

L.getGlobal("foo");
L.pushNumber(8.0);
int retCode = L.pcall(1,1,0);
String errstr = L.toString(-1);

Lua probably sees JavaObject as a type of 'userdata' in which case there are no predefined operations for it; Lua won't know what to do with a JavaObject * 2 since you didn't define how to handle it.

OTOH, Lua does know how to handle a number since that's a builtin primitive type. For the code snippet you presented, pushing a number would be the least painful way to get it working instead of writing extra code that tells Lua how to work with numbers wrapped inside a JavaObject.



来源:https://stackoverflow.com/questions/8622409/lua-error-attempt-to-perform-arithmetic-on-local-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!