2021-12-10 22:05:05 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
|
|
|
|
#include "ludata.h"
|
|
|
|
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
Udata* luaU_newudata(lua_State* L, size_t s, int tag)
|
|
|
|
{
|
|
|
|
if (s > INT_MAX - sizeof(Udata))
|
|
|
|
luaM_toobig(L);
|
2022-01-21 17:00:19 +00:00
|
|
|
Udata* u = luaM_newgco(L, Udata, sizeudata(s), L->activememcat);
|
2022-02-24 23:53:37 +00:00
|
|
|
luaC_init(L, u, LUA_TUSERDATA);
|
2021-12-10 22:05:05 +00:00
|
|
|
u->len = int(s);
|
|
|
|
u->metatable = NULL;
|
|
|
|
LUAU_ASSERT(tag >= 0 && tag <= 255);
|
|
|
|
u->tag = uint8_t(tag);
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:00:19 +00:00
|
|
|
void luaU_freeudata(lua_State* L, Udata* u, lua_Page* page)
|
2021-12-10 22:05:05 +00:00
|
|
|
{
|
2022-03-18 00:46:04 +00:00
|
|
|
if (u->tag < LUA_UTAG_LIMIT)
|
2022-04-29 02:24:24 +01:00
|
|
|
{
|
|
|
|
void (*dtor)(lua_State*, void*) = nullptr;
|
2021-12-10 22:05:05 +00:00
|
|
|
dtor = L->global->udatagc[u->tag];
|
2022-06-24 02:56:00 +01:00
|
|
|
// 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
|
2022-04-29 02:24:24 +01:00
|
|
|
if (dtor)
|
|
|
|
dtor(L, u->data);
|
|
|
|
}
|
2022-03-18 00:46:04 +00:00
|
|
|
else if (u->tag == UTAG_IDTOR)
|
2022-04-29 02:24:24 +01:00
|
|
|
{
|
|
|
|
void (*dtor)(void*) = nullptr;
|
2022-03-18 00:46:04 +00:00
|
|
|
memcpy(&dtor, &u->data + u->len - sizeof(dtor), sizeof(dtor));
|
2022-04-29 02:24:24 +01:00
|
|
|
if (dtor)
|
|
|
|
dtor(u->data);
|
|
|
|
}
|
2021-12-10 22:05:05 +00:00
|
|
|
|
|
|
|
|
2022-01-21 17:00:19 +00:00
|
|
|
luaM_freegco(L, u, sizeudata(u->len), u->memcat, page);
|
2021-12-10 22:05:05 +00:00
|
|
|
}
|