// L may not be the same pointer as // was used when creating this reference // since it may be a thread that shares // the same globals table. void weak_ref::get(lua_State *L) const { assert(m_impl); assert(L); get_weak_table(L); lua_rawgeti(L, -1, m_impl->ref); lua_remove(L, -2); }
impl(lua_State* s, int index) : count(0) , state(s) , ref(0) { lua_pushvalue(s, index); get_weak_table(s); lua_rawgeti(s, -1, freelist_ref); ref = (int)lua_tonumber(s, -1); lua_pop(s, 1); if (ref == 0) { lua_rawgeti(s, -1, count_ref); ref = (int)lua_tonumber(s, -1) + 1; lua_pop(s, 1); lua_pushnumber(s, (lua_Number)ref); lua_rawseti(s, -2, count_ref); } else { lua_rawgeti(s, -1, ref); lua_rawseti(s, -2, freelist_ref); } lua_pushvalue(s, -2); // duplicate value lua_rawseti(s, -2, ref); lua_pop(s, 2); // pop weakref table and value }
impl(lua_State *main, lua_State *s, int index) : count(0) , state(main) , ref(0) { get_weak_table(s); lua_pushvalue(s, index); ref = luaL_ref(s, -2); lua_pop(s, 1); }
~impl() { get_weak_table(state); lua_rawgeti(state, -1, freelist_ref); lua_rawseti(state, -2, ref); lua_pushnumber(state, (lua_Number)ref); lua_rawseti(state, -2, freelist_ref); lua_pop(state, 1); }
impl(lua_State* main, lua_State* s, int index) : count(0) , state(main) , ref(0) { get_impl_table(s); lua_pushlightuserdata(s, this); ref = luaL_ref(s, -2); lua_pop(s, 1); get_weak_table(s); lua_pushvalue(s, index); lua_rawseti(s, -2, ref); lua_pop(s, 1); }
~impl() { get_weak_table(state); luaL_unref(state, -1, ref); lua_pop(state, 1); }