Skip to content

Commit

Permalink
Add lua_setuserdatatag to update userdata tags (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jul 14, 2022
1 parent a934f74 commit e87009f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions VM/include/lua.h
Expand Up @@ -64,14 +64,14 @@ enum lua_Type
LUA_TNIL = 0, /* must be 0 due to lua_isnoneornil */
LUA_TBOOLEAN = 1, /* must be 1 due to l_isfalse */


LUA_TLIGHTUSERDATA,
LUA_TNUMBER,
LUA_TVECTOR,

LUA_TSTRING, /* all types above this must be value types, all types below this must be GC types - see iscollectable */


LUA_TTABLE,
LUA_TFUNCTION,
LUA_TUSERDATA,
Expand Down Expand Up @@ -300,6 +300,7 @@ LUA_API uintptr_t lua_encodepointer(lua_State* L, uintptr_t p);

LUA_API double lua_clock();

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_clonefunction(lua_State* L, int idx);
Expand Down
8 changes: 8 additions & 0 deletions VM/src/lapi.cpp
Expand Up @@ -1305,6 +1305,14 @@ void lua_unref(lua_State* L, int ref)
return;
}

void lua_setuserdatatag(lua_State* L, int idx, int tag)
{
api_check(L, unsigned(tag) < LUA_UTAG_LIMIT);
StkId o = index2addr(L, idx);
api_check(L, ttisuserdata(o));
uvalue(o)->tag = uint8_t(tag);
}

void lua_setuserdatadtor(lua_State* L, int tag, void (*dtor)(lua_State*, void*))
{
api_check(L, unsigned(tag) < LUA_UTAG_LIMIT);
Expand Down
4 changes: 4 additions & 0 deletions tests/Conformance.test.cpp
Expand Up @@ -1152,6 +1152,10 @@ TEST_CASE("UserdataApi")
CHECK(lua_touserdatatagged(L, -1, 41) == nullptr);
CHECK(lua_userdatatag(L, -1) == 42);

lua_setuserdatatag(L, -1, 43);
CHECK(lua_userdatatag(L, -1) == 43);
lua_setuserdatatag(L, -1, 42);

// user data with inline dtor
void* ud3 = lua_newuserdatadtor(L, 4, [](void* data) {
dtorhits += *(int*)data;
Expand Down

0 comments on commit e87009f

Please sign in to comment.