1

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()

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Androider
  • 21,125
  • 36
  • 99
  • 158
  • I'm not sure but could it have something to do with `L.pushJavaObject(8);`? What if you tried a `L.pushNumber(8);` instead? – greatwolf Dec 24 '11 at 03:12
  • do you want me to post that as an actual answer for you to accept in that case? ;) – greatwolf Dec 24 '11 at 03:38
  • yes, and please take a look at http://stackoverflow.com/questions/8621939/luajava-setting-error-handler-for-luastate-pcalla-b-error-function-index – Androider Dec 24 '11 at 04:25

1 Answers1

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.

greatwolf
  • 20,287
  • 13
  • 71
  • 105