Add typedef lua_Destructor

This commit is contained in:
Petri Häkkinen 2023-04-11 08:46:50 +03:00
parent c6e4f49e32
commit 25384218ca
3 changed files with 8 additions and 6 deletions

View file

@ -313,8 +313,11 @@ LUA_API uintptr_t lua_encodepointer(lua_State* L, uintptr_t p);
LUA_API double lua_clock(); LUA_API double lua_clock();
LUA_API void lua_setuserdatatag(lua_State* L, int idx, int tag); LUA_API void lua_setuserdatatag(lua_State* L, int idx, int tag);
LUA_API void lua_setuserdatadtor(lua_State* L, int tag, void (*dtor)(lua_State*, void*));
LUA_API void (*lua_getuserdatadtor(lua_State* L, int tag))(lua_State*, void*); typedef void (*lua_Destructor)(lua_State* L, void* userdata);
LUA_API void lua_setuserdatadtor(lua_State* L, int tag, lua_Destructor dtor);
LUA_API lua_Destructor lua_getuserdatadtor(lua_State* L, int tag);
LUA_API void lua_clonefunction(lua_State* L, int idx); LUA_API void lua_clonefunction(lua_State* L, int idx);

View file

@ -1378,13 +1378,13 @@ void lua_setuserdatatag(lua_State* L, int idx, int tag)
uvalue(o)->tag = uint8_t(tag); uvalue(o)->tag = uint8_t(tag);
} }
void lua_setuserdatadtor(lua_State* L, int tag, void (*dtor)(lua_State*, void*)) void lua_setuserdatadtor(lua_State* L, int tag, lua_Destructor dtor)
{ {
api_check(L, unsigned(tag) < LUA_UTAG_LIMIT); api_check(L, unsigned(tag) < LUA_UTAG_LIMIT);
L->global->udatagc[tag] = dtor; L->global->udatagc[tag] = dtor;
} }
void (*lua_getuserdatadtor(lua_State* L, int tag))(lua_State*, void*) lua_Destructor lua_getuserdatadtor(lua_State* L, int tag)
{ {
api_check(L, unsigned(tag) < LUA_UTAG_LIMIT); api_check(L, unsigned(tag) < LUA_UTAG_LIMIT);
return L->global->udatagc[tag]; return L->global->udatagc[tag];

View file

@ -24,8 +24,7 @@ void luaU_freeudata(lua_State* L, Udata* u, lua_Page* page)
{ {
if (u->tag < LUA_UTAG_LIMIT) if (u->tag < LUA_UTAG_LIMIT)
{ {
void (*dtor)(lua_State*, void*) = nullptr; lua_Destructor dtor = L->global->udatagc[u->tag];
dtor = L->global->udatagc[u->tag];
// TODO: access to L here is highly unsafe since this is called during internal GC traversal // TODO: access to L here is highly unsafe since this is called during internal GC traversal
// certain operations such as lua_getthreaddata are okay, but by and large this risks crashes on improper use // certain operations such as lua_getthreaddata are okay, but by and large this risks crashes on improper use
if (dtor) if (dtor)