Lua hides a Type in its userdata;

This commit is contained in:
bjorn 2019-05-13 03:21:11 -07:00
parent 429efcb38d
commit 963f496ac1
3 changed files with 16 additions and 11 deletions

View File

@ -39,7 +39,7 @@ void luax_checkvariant(lua_State* L, int index, Variant* variant) {
case LUA_TUSERDATA: case LUA_TUSERDATA:
variant->type = TYPE_OBJECT; 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); lovrRetain(variant->value.ref);
break; break;

View File

@ -12,7 +12,8 @@ static int luax_meta__tostring(lua_State* L) {
} }
static int luax_meta__gc(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; 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* _luax_totype(lua_State* L, int index, Type type) {
void** p = lua_touserdata(L, index); Proxy* p = lua_touserdata(L, index);
if (p) { if (p && (p->type == type || lovrTypeInfo[p->type].super == type)) {
Ref* object = *(Ref**) p; return p->ref;
if (object->type == type || lovrTypeInfo[object->type].super == type) {
return object;
}
} }
return NULL; return NULL;
@ -201,11 +199,13 @@ void luax_pushobject(lua_State* L, void* object) {
} }
// Allocate userdata // Allocate userdata
void** u = (void**) lua_newuserdata(L, sizeof(void**)); Ref* ref = (Ref*) object;
luaL_getmetatable(L, lovrTypeInfo[((Ref*) object)->type].name); Proxy* p = (Proxy*) lua_newuserdata(L, sizeof(Proxy));
luaL_getmetatable(L, lovrTypeInfo[ref->type].name);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lovrRetain(object); lovrRetain(object);
*u = object; p->type = ref->type;
p->ref = ref;
// Write to registry and remove registry, leaving userdata on stack // Write to registry and remove registry, leaving userdata on stack
lua_pushlightuserdata(L, object); lua_pushlightuserdata(L, object);

View File

@ -7,6 +7,11 @@
struct Color; struct Color;
typedef struct {
Type type;
Ref* ref;
} Proxy;
#ifndef LUA_RIDX_MAINTHERAD #ifndef LUA_RIDX_MAINTHERAD
#define LUA_RIDX_MAINTHREAD 1 #define LUA_RIDX_MAINTHREAD 1
#endif #endif