void Reference::ref(lua_State *L) { unref(); // Just to be safe. this->L = L; luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); lua_insert(L, -2); // Move reference table behind value. idx = luaL_ref(L, -2); lua_pop(L, 1); }
void Reference::unref() { if (idx != LUA_REFNIL) { luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); luaL_unref(L, -1, idx); lua_pop(L, 1); idx = LUA_REFNIL; } }
int luax_insistlove(lua_State *L, const char *k) { luax_insistglobal(L, "love"); luax_insist(L, -1, k); // The love table should be replaced with the top stack // item. Only the reqested table should remain on the stack. lua_replace(L, -2); return 1; }
void Reference::push() { if (idx != LUA_REFNIL) { luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME); lua_rawgeti(L, -1, idx); lua_remove(L, -2); } else lua_pushnil(L); }
int luax_insistregistry(lua_State *L, Registry r) { switch (r) { case REGISTRY_MODULES: return luax_insistlove(L, "_modules"); case REGISTRY_OBJECTS: return luax_insist(L, LUA_REGISTRYINDEX, "_loveobjects"); default: return luaL_error(L, "Attempted to use invalid registry."); } }