Lua错误尝试对局部变量执行算术运算 [英] Lua Error Attempt to perform arithmetic on local variable

查看:147
本文介绍了Lua错误尝试对局部变量执行算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是功能 calc.lua:

Here is the function calc.lua:

function foo(n) 
return n*2 
end

这是我的LuaJavaCall

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'

更新:如下所示,我需要使用L.pushNumber(8.0)而不是L.pushJavaObject()

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

推荐答案

尝试像这样使用L.pushNumber代替L.pushJavaObject:

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

Lua可能会将JavaObject视为一种用户数据",在这种情况下,没有针对它的预定义操作; Lua不知道如何处理JavaObject * 2,因为您没有定义如何处理它.

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确实知道如何处理数字,因为那是内置的原始类型.对于您介绍的代码段,压入数字是使它起作用的最痛苦的方法,而不是编写额外的代码来告诉Lua如何使用包装在JavaObject中的数字.

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.

这篇关于Lua错误尝试对局部变量执行算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆