Skip to content

Commit

Permalink
Add table.values
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan authored and well-in-that-case committed Jan 26, 2025
1 parent abb24bf commit d068125
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/ltablib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,19 +803,40 @@ static int tkeys (lua_State *L) {
lua_newtable(L);
lua_Integer i = 0;
lua_pushnil(L);
/* stack now: tKeys, key */
/* stack now: res, key */
while (lua_next(L, 1)) {
/* stack now: tKeys, key, value */
/* stack now: res, key, value */
lua_pop(L, 1);
/* stack now: tKeys, key */
/* stack now: res, key */
lua_pushinteger(L, ++i);
/* stack now: tKeys, key, index */
/* stack now: res, key, index */
lua_pushvalue(L, -2);
/* stack now: tKeys, key, index, key */
/* stack now: res, key, index, key */
lua_settable(L, -4);
/* stack now: tKeys, key */
/* stack now: res, key */
}
/* stack now: res */
return 1;
}


static int tvalues (lua_State *L) {
lua_newtable(L);
lua_Integer i = 0;
lua_pushnil(L);
/* stack now: res, key */
while (lua_next(L, 1)) {
/* stack now: res, key, value */
lua_pushinteger(L, ++i);
/* stack now: res, key, value, index */
lua_pushvalue(L, -2);
/* stack now: res, key, value, index, value */
lua_settable(L, -5);
/* stack now: res, key, value */
lua_pop(L, 1);
/* stack now: res, key */
}
/* stack now: tKeys */
/* stack now: res */
return 1;
}

Expand Down Expand Up @@ -1005,6 +1026,7 @@ static const luaL_Reg tab_funcs[] = {
{"deduped", tdeduplicate},
{"deduplicated", tdeduplicated},
{"keys", tkeys},
{"values", tvalues},
{"modget", modget},
{"modset", modset},
{"back", tback},
Expand Down
5 changes: 5 additions & 0 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,11 @@ do
assert(keys:contains("b"))
assert(keys:contains("c"))
end
do
local t = { a = 1, b = 2, c = 3 }
assert(#t == 0)
assert(#t:values() == 3)
end
do
local test_table_1 = {1, 2, 2, 3, 1}
local counts_1 = test_table_1:countvalues()
Expand Down

0 comments on commit d068125

Please sign in to comment.