在C中,我想要调用一个使用全局变量x的lua函数myFunction()。我希望能够直接将c(整数)变量注册为这个全局变量x。
大致上是这样:
demo.lua:
function myFunction()
return x + 3
end
demo.c:
int x = 0;
lua_bindglob(L, "x", &x);
for (x = 0; x < 100; ++x)
{
lua_getglobal(L, "myFunction");
// here I call the function and it should evaluate
// to the current value of x (in c)
lua_pcall(L, 0, 1, 0);
int result = lua_tonumber(L, -1);
lua_pop(L, 1);
}
我知道可以通过以下方式修改lua全局变量:
lua_pushnumber(L, 3);
lua_setglobal(L, "x");
但我需要一种更直接/更快的方法来完成这个任务。我得到了一个提示,说LUA轻量用户数据可能会对此有帮助,但我无法找到任何关于普通变量的示例。
编辑: 由于表达式是从基准测试中获取的,因此我不想改变它们: 在此处查看示例:https://github.com/ArashPartow/math-parser-benchmark-project/blob/master/bench_expr_complete.txt
__index
和__newindex
元方法定义为访问器函数。 - Joel Cornett