static int l_anim_depersist(lua_State *L) { T* pAnimation = luaT_testuserdata<T>(L); lua_settop(L, 2); lua_insert(L, 1); LuaPersistReader* pReader = (LuaPersistReader*)lua_touserdata(L, 1); lua_rawgeti(L, luaT_environindex, 2); lua_pushlightuserdata(L, pAnimation); lua_pushvalue(L, 2); lua_settable(L, -3); lua_pop(L, 1); pAnimation->depersist(pReader); lua_rawgeti(L, luaT_environindex, 1); lua_pushlightuserdata(L, pAnimation); if(!pReader->readStackObject()) return 0; lua_settable(L, -3); lua_pop(L, 1); return 0; }
// __depersist metamethod handler static int l_str_depersist(lua_State *L) { lua_settop(L, 2); lua_insert(L, 1); LuaPersistReader *pReader = (LuaPersistReader*)lua_touserdata(L, 1); // Read the instructions for re-creating the value if(!pReader->readStackObject()) return 0; if(lua_type(L, 3) == LUA_TBOOLEAN && lua_toboolean(L, 3) == 1) { // The current code uses a boolean marker to indicate that the // instructions were stored in the environment. Replace the marker // with them. lua_getfenv(L, 2); lua_replace(L, 3); } else { // Older versions of the code wrote the instructions here, or nil for // no instructions. Convert nil to the empty table, and store the // instructions as the userdata's environment. if(lua_type(L, 3) == LUA_TNIL) { lua_newtable(L); lua_replace(L, 3); } lua_pushvalue(L, 3); lua_setfenv(L, 2); } // Prepare t, k for saving the value aux_push_weak_table(L, 0); lua_pushvalue(L, 2); if(lua_objlen(L, 3) == 0) { // No instructions provided, so read the value itself if(!pReader->readStackObject()) return 0; } else { // The instructions are a table of values; unpack them and replace // proxies with their values. bool bIsIndexOperation = false; int iCount = (int)lua_objlen(L, 3); lua_checkstack(L, iCount + 1); for(int i = 1; i <= iCount; ++i) { lua_rawgeti(L, 3, i); if(lua_type(L, -1) == LUA_TUSERDATA) { if(i == 1) bIsIndexOperation = true; lua_rawget(L, 4); } } if(iCount == 2 && bIsIndexOperation) { // If there were two values, and the first was a proxy, then the // instruction is to perform a table lookup. lua_gettable(L, -2); lua_replace(L, -2); } else { // Otherwise, the first value was a method or method name. if(lua_type(L, 6) != LUA_TFUNCTION) { lua_pushvalue(L, 6); lua_gettable(L, 7); lua_replace(L, 6); } lua_call(L, iCount - 1, 1); } } // Save the value lua_rawset(L, 4); return 0; }