首页 > 代码库 > lua和C++交互的lua栈操作——以LuaTinker中注册C++类为例

lua和C++交互的lua栈操作——以LuaTinker中注册C++类为例

 

 

 

 

 

--    lua栈内容(执行到pop语句)            栈地址        <--执行语句space_name[name] = t1                      -- (2b8)     -- lua_rawset(L, -4);-- t1[__gc] = destroyer<T>                  -- (2d8)     -- lua_rawset(L, -3);-- destroyer<T>                              -- (2f8)     -- lua_pushcclosure(L, destroyer<T>, 0);-- __gc                                      -- (2e8)     -- lua_pushstring(L, "__gc");-- t1[__newindex] = meta_set              -- (2d8)     -- lua_rawset(L, -3);-- meta_set                                  -- (2f8)     -- lua_pushcclosure(L, meta_set, 0);-- __newindex                              -- (2e8)     -- lua_pushstring(L, "__newindex");-- t1[__index] = meta_get                  -- (2d8)     -- lua_rawset(L, -3);-- meta_get                                  -- (2f8)     -- lua_pushcclosure(L, meta_get, 0);-- __index                                  -- (2e8)     -- lua_pushstring(L, "__index");-- t1[__name] = name                      -- (2d8)     -- lua_rawset(L, -3);-- name                                   -- (2f8)     -- lua_pushstring(L, name);-- __name                                  -- (2e8)     -- lua_pushstring(L, "__name");-- setmetatable(t1, t2)                  -- (2d8)     -- lua_setmetatable(L, -2);-- t2[__index] = static_meta_get         -- (2e8)     -- lua_rawset(L, -3);-- static_meta_get                        -- (308)     -- lua_pushcclosure(L, static_meta_get, 0);-- __index                                -- (2f8)     -- lua_pushstring(L, "__index");-- t2                                      -- (2e8)     -- lua_newtable(L);-- t1                                      -- (2d8)     -- lua_newtable(L);-- name                                 -- (2c8)     -- lua_pushstring(L, name);-- space_name[name]                     -- (2b8)     -- lua_rawget(L, -2);-- name                                 -- (2b8)     -- lua_pushstring(L, name);space_name                                 -- (2a8)     -- push_meta(L, space_name::name);L                                           -- (298)     -- 初始状态

-- C++类注册函数(LuaTinker) -- 支持注册到命名空间namespace

template<typename T>void class_addEx(lua_State* L, const char* name) {    push_meta(L, space_name::name());    if(lua_istable(L, -1))    {        class_name<T>::name(name);        lua_pushstring(L, name);        lua_rawget(L, -2);        if (!lua_istable(L, -1))        {            lua_pushstring(L, name);            lua_newtable(L);            lua_newtable(L);            lua_pushstring(L, "__index");            lua_pushcclosure(L, static_meta_get, 0);            lua_rawset(L, -3);            lua_setmetatable(L, -2);            lua_pushstring(L, "__name");            lua_pushstring(L, name);            lua_rawset(L, -3);            lua_pushstring(L, "__index");            lua_pushcclosure(L, meta_get, 0);            lua_rawset(L, -3);            lua_pushstring(L, "__newindex");            lua_pushcclosure(L, meta_set, 0);            lua_rawset(L, -3);            lua_pushstring(L, "__gc");            lua_pushcclosure(L, destroyer<T>, 0);            lua_rawset(L, -3);            lua_rawset(L, -4);        }    }    lua_pop(L, 2);}

 

lua和C++交互的lua栈操作——以LuaTinker中注册C++类为例