我正在尝试调用 A:update(x)
并在 C++ 中获取返回值 x + 3
。
这是我的代码:
#include <lua.hpp>
void main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_settop(L, 0);
luaL_dostring(L, "package.preload['A'] = function () local A = {}\n"
"function A:update(x) return x + 3 end \n"
"return A end");
//call function
lua_getglobal(L, "require");
lua_pushstring(L, "A");
if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
int top = lua_gettop(L);
lua_getfield(L, -1, "update");
if (!lua_isfunction(L, -1))
{
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
lua_pushnumber(L, 5); //pass the argument 5
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
if (lua_gettop(L) - top)
{
if (lua_isnumber(L, -1))
{
std::cout << "RETURNED : " << lua_tonumber(L, -1) << std::endl;
}
}
lua_pop(L, 1); // pop 'update'
lua_pop(L, 1); // pop 'A'
lua_close(L);
}
我希望它打印 RETURNED : 8
但我收到以下错误:
Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)
我应该如何更正我的代码才能正常工作?
已编辑:当我将 A:update(x)
更改为 A.update(x)
时,它立即起作用。我认为它们的工作方式相同,只是我可以在使用 :
的函数中使用 self
。有人可以向我解释为什么会这样吗?
最佳答案
符号 A:update(x)
是 A.update(A,x)
的语法糖。这意味着您必须使用两个参数调用函数 update
。您缺少两个参数中的第一个。
第一个参数 A
已经在堆栈上,但位于 update
函数的“下方”。使用 lua_pushvalue
我们可以将表的拷贝压入堆栈。
因此你必须像这样调用函数(省略错误处理位)
lua_getfield(L, -1, "update");
lua_pushvalue(L, -2); // push a copy of "A" onto the stack
lua_pushnumber(L, 5); //pass the argument 5
lua_pcall(L, 2, LUA_MULTRET, 0);
关于c++ - 如何在 C++ 中调用 package.preload 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51434787/