Comparison

util-src/table.c @ 7519:d278a770eddc

util.table: Add pack() function (this is already available in Lua 5.2+)
author Matthew Wild <mwild1@gmail.com>
date Wed, 27 Jul 2016 13:39:19 +0100
parent 6615:8e4572a642cb
child 7538:a3a4ed0d34f4
comparison
equal deleted inserted replaced
7515:9e48299799d8 7519:d278a770eddc
4 static int Lcreate_table(lua_State* L) { 4 static int Lcreate_table(lua_State* L) {
5 lua_createtable(L, luaL_checkinteger(L, 1), luaL_checkinteger(L, 2)); 5 lua_createtable(L, luaL_checkinteger(L, 1), luaL_checkinteger(L, 2));
6 return 1; 6 return 1;
7 } 7 }
8 8
9 static int Lpack(lua_State* L) {
10 unsigned int n_args = lua_gettop(L);
11 lua_createtable(L, n_args, 1);
12 lua_insert(L, 1);
13 for(int arg = n_args; arg >= 1; arg--) {
14 lua_rawseti(L, 1, arg);
15 }
16 lua_pushinteger(L, n_args);
17 lua_setfield(L, -2, "n");
18 return 1;
19 }
20
21
9 int luaopen_util_table(lua_State* L) { 22 int luaopen_util_table(lua_State* L) {
10 lua_newtable(L); 23 lua_newtable(L);
11 lua_pushcfunction(L, Lcreate_table); 24 lua_pushcfunction(L, Lcreate_table);
12 lua_setfield(L, -2, "create"); 25 lua_setfield(L, -2, "create");
26 lua_pushcfunction(L, Lpack);
27 lua_setfield(L, -2, "pack");
13 return 1; 28 return 1;
14 } 29 }