Add lua_getallocf API function (#1068)

This function matches the corresponding Lua 5.1-5.4 function:

[`lua_getallocf`](https://www.lua.org/manual/5.4/manual.html#lua_getallocf)
and [source](https://www.lua.org/source/5.4/lapi.c.html#lua_getallocf)

It would be useful to get/manipulate auxiliary "userdata" pointer that was originally passed to `lua_newstate`.
This commit is contained in:
Alex Orlenko 2023-10-13 16:47:33 +01:00 committed by GitHub
parent 1173e415ef
commit 5c94984935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -323,6 +323,8 @@ LUA_API void lua_clonefunction(lua_State* L, int idx);
LUA_API void lua_cleartable(lua_State* L, int idx);
LUA_API lua_Alloc lua_getallocf(lua_State* L, void** ud);
/*
** reference system, can be used to pin objects
*/

View File

@ -1432,3 +1432,11 @@ size_t lua_totalbytes(lua_State* L, int category)
api_check(L, category < LUA_MEMORY_CATEGORIES);
return category < 0 ? L->global->totalbytes : L->global->memcatbytes[category];
}
lua_Alloc lua_getallocf(lua_State* L, void** ud)
{
lua_Alloc f = L->global->frealloc;
if (ud)
*ud = L->global->ud;
return f;
}

View File

@ -1163,6 +1163,18 @@ TEST_CASE("ApiType")
CHECK(lua_type(L, -1) == LUA_TUSERDATA);
}
TEST_CASE("AllocApi")
{
int ud = 0;
StateRef globalState(lua_newstate(limitedRealloc, &ud), lua_close);
lua_State* L = globalState.get();
void* udCheck = nullptr;
bool allocfIsSet = lua_getallocf(L, &udCheck) == limitedRealloc;
CHECK(allocfIsSet);
CHECK(udCheck == &ud);
}
#if !LUA_USE_LONGJMP
TEST_CASE("ExceptionObject")
{