Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/VM/src/ludata.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
3
#include "ludata.h"
4
5
#include "lgc.h"
6
#include "lmem.h"
7
8
#include <string.h>
9
10
Udata* luaU_newudata(lua_State* L, size_t s, int tag)
11
{
12
if (s > INT_MAX - sizeof(Udata))
13
luaM_toobig(L);
14
Udata* u = luaM_newgco(L, Udata, sizeudata(s), L->activememcat);
15
luaC_init(L, u, LUA_TUSERDATA);
16
u->len = int(s);
17
u->metatable = NULL;
18
LUAU_ASSERT(tag >= 0 && tag <= 255);
19
u->tag = uint8_t(tag);
20
return u;
21
}
22
23
void luaU_freeudata(lua_State* L, Udata* u, lua_Page* page)
24
{
25
if (u->tag < LUA_UTAG_LIMIT)
26
{
27
lua_Destructor dtor = L->global->udatagc[u->tag];
28
// TODO: access to L here is highly unsafe since this is called during internal GC traversal
29
// certain operations such as lua_getthreaddata are okay, but by and large this risks crashes on improper use
30
if (dtor)
31
dtor(L, u->data);
32
}
33
else if (u->tag == UTAG_IDTOR)
34
{
35
void (*dtor)(void*) = nullptr;
36
memcpy(&dtor, &u->data + u->len - sizeof(dtor), sizeof(dtor));
37
if (dtor)
38
dtor(u->data);
39
}
40
41
42
luaM_freegco(L, u, sizeudata(u->len), u->memcat, page);
43
}
44
45