From b69faf0bcc5ddca1d88b1a7ab47bcbbc6dbb9af1 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Thu, 8 Nov 2018 11:32:45 -0500 Subject: lua update to 5.1.5 Modify Rocklua towards upstream 5.1.5 Clean up some of the Rocklua implementation Change-Id: Iac722e827899cf84f5ca004ef7ae7ddce5f7fbbe --- apps/plugins/lua/lbaselib.c | 47 ++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'apps/plugins/lua/lbaselib.c') diff --git a/apps/plugins/lua/lbaselib.c b/apps/plugins/lua/lbaselib.c index 008e3629fe..ad94984191 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 */ -- cgit v1.2.3