mirror of
https://github.com/luau-lang/luau.git
synced 2024-11-15 14:25:44 +08:00
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:
parent
1173e415ef
commit
5c94984935
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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")
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user