diff --git a/src/api/event.c b/src/api/event.c index b893d9d0..932ac1bf 100644 --- a/src/api/event.c +++ b/src/api/event.c @@ -39,7 +39,7 @@ void luax_checkvariant(lua_State* L, int index, Variant* variant) { case LUA_TUSERDATA: variant->type = TYPE_OBJECT; - variant->value.ref = *(Ref**) lua_touserdata(L, index); + variant->value.ref = ((Proxy*) lua_touserdata(L, index))->ref; lovrRetain(variant->value.ref); break; diff --git a/src/luax.c b/src/luax.c index 14674f5e..ce9e07a1 100644 --- a/src/luax.c +++ b/src/luax.c @@ -12,7 +12,8 @@ static int luax_meta__tostring(lua_State* L) { } static int luax_meta__gc(lua_State* L) { - lovrGenericRelease(*(Ref**) lua_touserdata(L, 1)); + Proxy* p = lua_touserdata(L, 1); + lovrGenericRelease(p->ref); return 0; } @@ -139,13 +140,10 @@ void _luax_extendtype(lua_State* L, const char* name, const luaL_Reg* baseFuncti } void* _luax_totype(lua_State* L, int index, Type type) { - void** p = lua_touserdata(L, index); + Proxy* p = lua_touserdata(L, index); - if (p) { - Ref* object = *(Ref**) p; - if (object->type == type || lovrTypeInfo[object->type].super == type) { - return object; - } + if (p && (p->type == type || lovrTypeInfo[p->type].super == type)) { + return p->ref; } return NULL; @@ -201,11 +199,13 @@ void luax_pushobject(lua_State* L, void* object) { } // Allocate userdata - void** u = (void**) lua_newuserdata(L, sizeof(void**)); - luaL_getmetatable(L, lovrTypeInfo[((Ref*) object)->type].name); + Ref* ref = (Ref*) object; + Proxy* p = (Proxy*) lua_newuserdata(L, sizeof(Proxy)); + luaL_getmetatable(L, lovrTypeInfo[ref->type].name); lua_setmetatable(L, -2); lovrRetain(object); - *u = object; + p->type = ref->type; + p->ref = ref; // Write to registry and remove registry, leaving userdata on stack lua_pushlightuserdata(L, object); diff --git a/src/luax.h b/src/luax.h index 1a89b532..400cae60 100644 --- a/src/luax.h +++ b/src/luax.h @@ -7,6 +7,11 @@ struct Color; +typedef struct { + Type type; + Ref* ref; +} Proxy; + #ifndef LUA_RIDX_MAINTHERAD #define LUA_RIDX_MAINTHREAD 1 #endif