summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lbaselib.c
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-11-08 11:32:45 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2018-11-11 19:42:30 -0500
commitb69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1 (patch)
tree191277cab3ef773238c1e91dafaaea04ddca1949 /apps/plugins/lua/lbaselib.c
parentde6618a2713ef26f888762cbe6539cc65a393c7c (diff)
downloadrockbox-b69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1.zip
rockbox-b69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1.tar.gz
rockbox-b69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1.tar.bz2
rockbox-b69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1.tar.xz
lua update to 5.1.5
Modify Rocklua towards upstream 5.1.5 Clean up some of the Rocklua implementation Change-Id: Iac722e827899cf84f5ca004ef7ae7ddce5f7fbbe
Diffstat (limited to 'apps/plugins/lua/lbaselib.c')
-rw-r--r--apps/plugins/lua/lbaselib.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/apps/plugins/lua/lbaselib.c b/apps/plugins/lua/lbaselib.c
index 008e362..ad94984 100644
--- a/apps/plugins/lua/lbaselib.c
+++ b/apps/plugins/lua/lbaselib.c
@@ -225,6 +225,24 @@ static int luaB_type (lua_State *L) {
}
+/** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ */
+static int pairsmeta (lua_State *L, const char *method, int iszero,
+ lua_CFunction iter) {
+ if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
+ luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
+ lua_pushcfunction(L, iter); /* will return generator, */
+ lua_pushvalue(L, 1); /* state, */
+ if (iszero) lua_pushinteger(L, 0); /* and initial value */
+ else lua_pushnil(L);
+ }
+ else {
+ lua_pushvalue(L, 1); /* argument 'self' to metamethod */
+ lua_call(L, 1, 3); /* get 3 values from metamethod */
+ }
+ return 3;
+}
+
+
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
@@ -238,11 +256,8 @@ static int luaB_next (lua_State *L) {
static int luaB_pairs (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
- lua_pushvalue(L, 1); /* state, */
- lua_pushnil(L); /* and initial value */
- return 3;
+ /* pairs function from lua 5.2 */
+ return pairsmeta(L, "__pairs", 0, luaB_next);
}
@@ -252,16 +267,13 @@ static int ipairsaux (lua_State *L) {
i++; /* next value */
lua_pushinteger(L, i);
lua_rawgeti(L, 1, i);
- return (lua_isnil(L, -1)) ? 0 : 2;
+ return (lua_isnil(L, -1)) ? 1 : 2;
}
static int luaB_ipairs (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
- lua_pushvalue(L, 1); /* state, */
- lua_pushinteger(L, 0); /* and initial value */
- return 3;
+ return pairsmeta(L, "__ipairs", 1, ipairsaux);
+ /* ipairs function from lua 5.2 */
}
@@ -454,10 +466,12 @@ static const luaL_Reg base_funcs[] = {
{"gcinfo", luaB_gcinfo},
{"getfenv", luaB_getfenv},
{"getmetatable", luaB_getmetatable},
+ {"ipairs", luaB_ipairs},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
{"loadstring", luaB_loadstring},
{"next", luaB_next},
+ {"pairs", luaB_pairs},
{"pcall", luaB_pcall},
#if 0
{"print", luaB_print},
@@ -619,14 +633,6 @@ static const luaL_Reg co_funcs[] = {
/* }====================================================== */
-static void auxopen (lua_State *L, const char *name,
- lua_CFunction f, lua_CFunction u) {
- lua_pushcfunction(L, u);
- lua_pushcclosure(L, f, 1);
- lua_setfield(L, -2, name);
-}
-
-
static void base_open (lua_State *L) {
/* set global _G */
lua_pushvalue(L, LUA_GLOBALSINDEX);
@@ -635,9 +641,6 @@ static void base_open (lua_State *L) {
luaL_register(L, "_G", base_funcs);
lua_pushliteral(L, LUA_VERSION);
lua_setglobal(L, "_VERSION"); /* set global _VERSION */
- /* `ipairs' and `pairs' need auxliliary functions as upvalues */
- auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
- auxopen(L, "pairs", luaB_pairs, luaB_next);
/* `newproxy' needs a weaktable as upvalue */
lua_createtable(L, 0, 1); /* new table `w' */
lua_pushvalue(L, -1); /* `w' will be its own metatable */