Skip to content

VM: Add functions to get/set inline destructors of userdata#2457

Open
Nerixyz wants to merge 2 commits into
luau-lang:masterfrom
Nerixyz:feat/idtor-get-set
Open

VM: Add functions to get/set inline destructors of userdata#2457
Nerixyz wants to merge 2 commits into
luau-lang:masterfrom
Nerixyz:feat/idtor-get-set

Conversation

@Nerixyz

@Nerixyz Nerixyz commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

This adds

lua_InlineDestructor lua_toinlineuserdatadtor(lua_State* L, int idx);
void lua_setinlineuserdatadtor(lua_State* L, int idx, lua_InlineDestructor dtor);

They get/set the destructor for userdata with an inline destructor (UTAG_IDTOR). Setting the destructor after the userdata has been created is more important here. The use-case is fallible construction of the userdata:

void *mem = lua_newuserdatadtor(L, sizeof(T), [](void*mem){
    std::destroy_at<T>(static_cast<T*>(mem));
});
T *udata = static_cast<T*>(mem);
// Try to construct T. If this fails (e.g. arguments can't be converted), we shouldn't
// invoke the destructor, so we should unset it.

I found this when trying to get the sol2 Lua bindings to work with Luau. There, the userdata is constructed first and the arguments are matched later. In case of overloaded functions, there might not exist an overload, so we'd error out and drop the userdata. We can only set the destructor once we constructed the value inside the userdata.

@vegorov-rbx vegorov-rbx self-assigned this Jul 9, 2026
Comment thread VM/src/lapi.cpp Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants