Add arrays to Lua (#222)

You can now have O(1) length and append.
This commit is contained in:
Paul Kulchenko 2021-08-06 04:55:01 -07:00 committed by GitHub
parent b142ea7176
commit 55a15c204e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 557 additions and 20 deletions

View file

@ -401,6 +401,18 @@ static int sort (lua_State *L) {
return 0;
}
static int resize (lua_State *L) {
/* reserve capacity of the array part -- useful when filling large tables/arrays */
int size;
luaL_checktype(L, 1, LUA_TTABLE);
size = luaL_checkinteger(L, 2);
luaL_argcheck(L, size >= 0, 2, "invalid size");
lua_resize(L, 1, (unsigned int)size);
return 0;
}
/* }====================================================== */
@ -412,6 +424,7 @@ static const luaL_Reg tab_funcs[] = {
{"remove", tremove},
{"move", tmove},
{"sort", sort},
{"resize", resize},
{NULL, NULL}
};