Skip to content

Commit

Permalink
Null out the *P<> instead of removing the userdata
Browse files Browse the repository at this point in the history
  • Loading branch information
GinjaNinja32 committed Sep 7, 2024
1 parent 053d0e5 commit 526ed00
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/scriptInterfaceMagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,6 @@ struct convert<T*>
lua_pushstring(L, "__ptr");
lua_rawget(L, idx++);

if (!lua_isuserdata(L, -1))
{
ptr = NULL;
if (lua_rawequal(L, -1, -2))
return; // GCed object, just convert to NULL
const char *msg = lua_pushfstring(L, "Object expected, got table");
luaL_argerror(L, idx-1, msg);
return;
}

P<PObject>** p = static_cast< P<PObject>** >(lua_touserdata(L, -1));
lua_pop(L, 1);
if (p == NULL)
Expand All @@ -146,6 +136,11 @@ struct convert<T*>
luaL_argerror(L, idx-1, msg);
return;
}
if (*p == NULL)
{
ptr = NULL;
return;
}
ptr = dynamic_cast<T*>(***p);
//printf("ObjParam: %p\n", ptr);
}
Expand All @@ -165,13 +160,6 @@ struct convert<P<T>>
}
lua_pushstring(L, "__ptr");
lua_rawget(L, idx++);
if (!lua_isuserdata(L, -1))
{
ptr = NULL;
const char* msg = lua_pushfstring(L, "Object expected, got %s", lua_rawequal(L, -1, -2) ? "destroyed object" : "table");
luaL_argerror(L, idx-1, msg);
return;
}

P<PObject>** p = static_cast< P<PObject>** >(lua_touserdata(L, -1));
lua_pop(L, 1);
Expand All @@ -182,6 +170,13 @@ struct convert<P<T>>
luaL_argerror(L, idx-1, msg);
return;
}
if (*p == NULL)
{
ptr = NULL;
const char* msg = lua_pushliteral(L, "Object expected, got destroyed object");
luaL_argerror(L, idx-1, msg);
return;
}
ptr = **p;
//printf("ObjParam: %p\n", ptr);
}
Expand Down Expand Up @@ -546,13 +541,10 @@ template<class T> class scriptBindObject
if (lua_isuserdata(L, -1)) //When a subclass is destroyed, it's metatable might call the __gc function on it's sub-metatable. So we can get nil values here, ignore that.
{
PT* p = static_cast< PT* >(lua_touserdata(L, -1));
if (*p)
if (*p) {
delete *p;

// Clear the __ptr so we don't try to deref it if Lua can still see this table
lua_pushstring(L, "__ptr");
lua_pushvalue(L, -3); // set `v.__ptr = v` for easy checking that this has happened later
lua_rawset(L, -4);
*p = nullptr;
}
}
lua_pop(L, 1);
return 0;
Expand Down

0 comments on commit 526ed00

Please sign in to comment.