Lua Error Attempting to perform arithmetic on a local variable

Here is the calc.lua function:

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 below, I needed to use L.pushNumber (8.0) instead of L.pushJavaObject ()

+1
source share
1 answer

Try using L.pushNumber instead of L.pushJavaObject as follows:

 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 does not know what to do with JavaObject * 2 , since you have not decided how to handle it.

OTOH, Lua knows how to handle a number, since it is a built-in primitive type. For the code snippet you presented, tapping 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 JavaObject.

+1
source

Source: https://habr.com/ru/post/1387505/


All Articles