Skip to content

Commit

Permalink
[VM] Add an API to fetch current number of entries in a table
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas BOUQUET committed Sep 12, 2024
1 parent 1ab1e30 commit f9ff722
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 301 deletions.
1 change: 1 addition & 0 deletions Ast/src/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <string>
#include <string.h>
#include <cstdint>

namespace Luau
{
Expand Down
684 changes: 383 additions & 301 deletions Makefile

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions VM/include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ LUA_API int lua_isclosing(lua_State *L);
LUA_API int lua_findreferences(lua_State* L);
LUA_API void lua_clonetable(lua_State* L, int idx);
LUA_API void lua_remaptable(lua_State* L, int idx, int mapIdx);
LUA_API int lua_gettablesize(lua_State* L, int idx);


/******************************************************************************
Expand Down
8 changes: 8 additions & 0 deletions VM/src/lapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,14 @@ void lua_cleartable(lua_State* L, int idx)
luaH_clear(tt);
}

int lua_gettablesize(lua_State* L, int idx)
{
StkId t = index2addr(L, idx);
api_check(L, ttistable(t));
Table* tt = hvalue(t);
return luaH_getsize(tt);
}

lua_Callbacks* lua_callbacks(lua_State* L)
{
return &L->global->cb;
Expand Down
23 changes: 23 additions & 0 deletions VM/src/ltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,3 +867,26 @@ void luaH_clear(Table* tt)
// back to empty -> no tag methods present
tt->tmcache = cast_byte(~0);
}

int luaH_getsize(Table* t)
{
int count=0;
if (t->sizearray)
{
for (int k=0;k<t->sizearray;k++) {
if (!ttisnil(&t->array[k]))
count++;
}
}

if (t->node != dummynode)
{
for (int i=0; i < sizenode(t); i++)
{ // then hash part
if (!ttisnil(gval(gnode(t, i))))
count++;
}
}

return count;
}
1 change: 1 addition & 0 deletions VM/src/ltable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LUAI_FUNC int luaH_getn(Table* t);
LUAI_FUNC Table* luaH_clone(lua_State* L, Table* tt);
LUAI_FUNC void luaH_remaptable(Table* t, Table *lt);
LUAI_FUNC void luaH_clear(Table* tt);
LUAI_FUNC int luaH_getsize(Table* tt);

#define luaH_setslot(L, t, slot, key) (invalidateTMcache(t), (slot == luaO_nilobject ? luaH_newkey(L, t, key) : cast_to(TValue*, slot)))

Expand Down

0 comments on commit f9ff722

Please sign in to comment.