Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lua_cleartable #678

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions VM/include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ LUA_API void lua_setuserdatadtor(lua_State* L, int tag, void (*dtor)(lua_State*,

LUA_API void lua_clonefunction(lua_State* L, int idx);

LUA_API void lua_cleartable(lua_State* L, int idx);

/*
** reference system, can be used to pin objects
*/
Expand Down
10 changes: 10 additions & 0 deletions VM/src/lapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,16 @@ void lua_clonefunction(lua_State* L, int idx)
api_incr_top(L);
}

void lua_cleartable(lua_State* L, int idx)
{
StkId t = index2addr(L, idx);
api_check(L, ttistable(t));
Table* tt = hvalue(t);
if (tt->readonly)
luaG_runerror(L, "Attempt to modify a readonly table");
zeux marked this conversation as resolved.
Show resolved Hide resolved
luaH_clear(tt);
}

lua_Callbacks* lua_callbacks(lua_State* L)
{
return &L->global->cb;
Expand Down
5 changes: 5 additions & 0 deletions tests/Conformance.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,11 @@ TEST_CASE("ApiTables")
CHECK(strcmp(lua_tostring(L, -1), "test") == 0);
lua_pop(L, 1);

// lua_cleartable
lua_cleartable(L, -1);
lua_pushnil(L);
CHECK(lua_next(L, -2) == 0);

lua_pop(L, 1);
}

Expand Down