Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何访问lua';卢奥·托波因特的目标是什么?_C++_C_Lua - Fatal编程技术网

C++ 如何访问lua';卢奥·托波因特的目标是什么?

C++ 如何访问lua';卢奥·托波因特的目标是什么?,c++,c,lua,C++,C,Lua,在Lua代码中 Test = {} function Test:new() local obj = {} setmetatable(obj, self) self.__index = self return obj end local a = Test:new() a.ID = "abc123" callCfunc(a) 在C代码中 int callCfunc(lua_State* l) { SetLuaState(l); void* lua_obj = lua_top

在Lua代码中

Test = {}
function Test:new()
  local obj = {}
  setmetatable(obj, self)
  self.__index = self
  return obj
end
local a = Test:new()
a.ID = "abc123"
callCfunc(a)
在C代码中

int callCfunc(lua_State* l)
{
  SetLuaState(l);
  void* lua_obj = lua_topointer(l, 1);            //I hope get lua's a variable
  processObj(lua_obj);
  ...
  return 0;
}

int processObj(void *lua_obj)
{
  lua_State* l = GetLuaState();
  lua_pushlightuserdata(l, lua_obj);              //access lua table obj
  int top = lua_gettop(l);
  lua_getfield(l, top, "ID");                     //ERROR: attempt to index a userdata value
  std::string id = lua_tostring(l, -1);           //I hoe get the value "abc123"
  ...
  return 0;
}
我得到错误:尝试为userdata值编制索引
如何从lua_topointer()访问lua的对象

在C中存储一个lua对象,然后从C调用它。

您不想为该任务使用
lua\u topointer
。事实上,
lua\u topointer
的唯一合理使用是用于调试目的(如日志记录)


由于
a
是一个表,您需要使用
lua\u gettable
访问它的一个字段,或者更简单地使用
lua\u getfield
。当然,您不能为该任务传递指向
processObj
void*
指针,但您可以使用堆栈索引。

您不想为该任务使用
lua\u topointer
。事实上,
lua\u topointer
的唯一合理使用是用于调试目的(如日志记录)


由于
a
是一个表,您需要使用
lua\u gettable
访问它的一个字段,或者更简单地使用
lua\u getfield
。当然,您不能为该任务传递指向
processObj
void*
指针,但您可以使用堆栈索引。

您不应该使用
lua\u topointer
,因为您无法将其转换回lua对象,请将对象存储在注册表中,然后传递它的注册表索引

int callCfunc(lua_State* L)
{
    lua_pushvalue(L, 1);//push arg #1 onto the stack
    int r = luaL_ref(L, LUA_REGISTRYINDEX);//stores reference to your object(and pops it from the stask)
    processObj(r);
    luaL_unref(L, LUA_REGISTRYINDEX, r); // removes object reference from the registry
    ...


int processObj(int lua_obj_ref)
{
    lua_State* L = GetLuaState();
    lua_rawgeti(L, LUA_REGISTRYINDEX, lua_obj_ref);//retrieves your object from registry (to the stack top)
    ...

您不应该使用
lua\u topointer
,因为您无法将其转换回lua对象,将对象存储在注册表中,并传递其注册表索引:

int callCfunc(lua_State* L)
{
    lua_pushvalue(L, 1);//push arg #1 onto the stack
    int r = luaL_ref(L, LUA_REGISTRYINDEX);//stores reference to your object(and pops it from the stask)
    processObj(r);
    luaL_unref(L, LUA_REGISTRYINDEX, r); // removes object reference from the registry
    ...


int processObj(int lua_obj_ref)
{
    lua_State* L = GetLuaState();
    lua_rawgeti(L, LUA_REGISTRYINDEX, lua_obj_ref);//retrieves your object from registry (to the stack top)
    ...