void tolua_pushusertype_internal (lua_State* L, void* value, const char* type, int addToRoot)
{
if (value == NULL)
lua_pushnil(L);
else
{
luaL_getmetatable(L, type); /* stack: mt /
if (lua_isnil(L, -1)) { / NOT FOUND metatable /
lua_pop(L, 1);
return;
}
lua_pushstring(L,“tolua_ubox”);
lua_rawget(L,-2); / stack: mt ubox */
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_pushstring(L, “tolua_ubox”);
lua_rawget(L, LUA_REGISTRYINDEX);
};
lua_pushlightuserdata(L,value); /* stack: mt ubox key<value> */
lua_rawget(L,-2); /* stack: mt ubox ubox */
if (lua_isnil(L,-1))
{
lua_pop(L,1); /* stack: mt ubox */
lua_pushlightuserdata(L,value);
*(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox value newud */
lua_pushvalue(L,-1); /* stack: mt ubox value newud newud */
lua_insert(L,-4); /* stack: mt newud ubox value newud */
lua_rawset(L,-3); /* ubox = newud, stack: mt newud ubox */
lua_pop(L,1); /* stack: mt newud */
/*luaL_getmetatable(L,type);*/
lua_pushvalue(L, -2); /* stack: mt newud mt */
lua_setmetatable(L,-2); /* update mt, stack: mt newud */
#ifdef LUA_VERSION_NUM
lua_pushvalue(L, TOLUA_NOPEER); /* stack: mt newud peer /
lua_setfenv(L, -2); / stack: mt newud /
#endif
}
else
{
/ check the need of updating the metatable to a more specialized class /
lua_insert(L,-2); / stack: mt ubox ubox /
lua_pop(L,1); / stack: mt ubox /
lua_pushstring(L,“tolua_super”);
lua_rawget(L,LUA_REGISTRYINDEX); / stack: mt ubox super /
lua_getmetatable(L,-2); / stack: mt ubox super mt /
lua_rawget(L,-2); / stack: mt ubox super super /
if (lua_istable(L,-1))
{
lua_pushstring(L,type); / stack: mt ubox super super type /
lua_rawget(L,-2); / stack: mt ubox super super flag /
if (lua_toboolean(L,-1) == 1) / if true /
{
lua_pop(L,3); / mt ubox*/
lua_remove(L, -2);
return;
}
}
/* type represents a more specilized type */
/luaL_getmetatable(L,type); // stack: mt ubox super super flag mt /
lua_pushvalue(L, -5); / stack: mt ubox super super flag mt /
lua_setmetatable(L,-5); / stack: mt ubox super super flag /
lua_pop(L,3); / stack: mt ubox /
}
lua_remove(L, -2); / stack: ubox/
if (0 != addToRoot)
{
lua_pushvalue(L, -1);
tolua_add_value_to_root(L, value);
}
}
}
假设super由于某种原因不是一个table 那栈上面不是就只有4个元素了么?(stack: mt ubox super super)
这时候在pushvalue(L, -5),是不是就会有问题?
求大神帮忙看下。