diff options
Diffstat (limited to 'apps/plugins/lua/lbaselib.c')
-rw-r--r-- | apps/plugins/lua/lbaselib.c | 47 |
1 files changed, 25 insertions, 22 deletions
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) { | |||
225 | } | 225 | } |
226 | 226 | ||
227 | 227 | ||
228 | /** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ */ | ||
229 | static int pairsmeta (lua_State *L, const char *method, int iszero, | ||
230 | lua_CFunction iter) { | ||
231 | if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ | ||
232 | luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ | ||
233 | lua_pushcfunction(L, iter); /* will return generator, */ | ||
234 | lua_pushvalue(L, 1); /* state, */ | ||
235 | if (iszero) lua_pushinteger(L, 0); /* and initial value */ | ||
236 | else lua_pushnil(L); | ||
237 | } | ||
238 | else { | ||
239 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ | ||
240 | lua_call(L, 1, 3); /* get 3 values from metamethod */ | ||
241 | } | ||
242 | return 3; | ||
243 | } | ||
244 | |||
245 | |||
228 | static int luaB_next (lua_State *L) { | 246 | static int luaB_next (lua_State *L) { |
229 | luaL_checktype(L, 1, LUA_TTABLE); | 247 | luaL_checktype(L, 1, LUA_TTABLE); |
230 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | 248 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
@@ -238,11 +256,8 @@ static int luaB_next (lua_State *L) { | |||
238 | 256 | ||
239 | 257 | ||
240 | static int luaB_pairs (lua_State *L) { | 258 | static int luaB_pairs (lua_State *L) { |
241 | luaL_checktype(L, 1, LUA_TTABLE); | 259 | /* pairs function from lua 5.2 */ |
242 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ | 260 | return pairsmeta(L, "__pairs", 0, luaB_next); |
243 | lua_pushvalue(L, 1); /* state, */ | ||
244 | lua_pushnil(L); /* and initial value */ | ||
245 | return 3; | ||
246 | } | 261 | } |
247 | 262 | ||
248 | 263 | ||
@@ -252,16 +267,13 @@ static int ipairsaux (lua_State *L) { | |||
252 | i++; /* next value */ | 267 | i++; /* next value */ |
253 | lua_pushinteger(L, i); | 268 | lua_pushinteger(L, i); |
254 | lua_rawgeti(L, 1, i); | 269 | lua_rawgeti(L, 1, i); |
255 | return (lua_isnil(L, -1)) ? 0 : 2; | 270 | return (lua_isnil(L, -1)) ? 1 : 2; |
256 | } | 271 | } |
257 | 272 | ||
258 | 273 | ||
259 | static int luaB_ipairs (lua_State *L) { | 274 | static int luaB_ipairs (lua_State *L) { |
260 | luaL_checktype(L, 1, LUA_TTABLE); | 275 | return pairsmeta(L, "__ipairs", 1, ipairsaux); |
261 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ | 276 | /* ipairs function from lua 5.2 */ |
262 | lua_pushvalue(L, 1); /* state, */ | ||
263 | lua_pushinteger(L, 0); /* and initial value */ | ||
264 | return 3; | ||
265 | } | 277 | } |
266 | 278 | ||
267 | 279 | ||
@@ -454,10 +466,12 @@ static const luaL_Reg base_funcs[] = { | |||
454 | {"gcinfo", luaB_gcinfo}, | 466 | {"gcinfo", luaB_gcinfo}, |
455 | {"getfenv", luaB_getfenv}, | 467 | {"getfenv", luaB_getfenv}, |
456 | {"getmetatable", luaB_getmetatable}, | 468 | {"getmetatable", luaB_getmetatable}, |
469 | {"ipairs", luaB_ipairs}, | ||
457 | {"loadfile", luaB_loadfile}, | 470 | {"loadfile", luaB_loadfile}, |
458 | {"load", luaB_load}, | 471 | {"load", luaB_load}, |
459 | {"loadstring", luaB_loadstring}, | 472 | {"loadstring", luaB_loadstring}, |
460 | {"next", luaB_next}, | 473 | {"next", luaB_next}, |
474 | {"pairs", luaB_pairs}, | ||
461 | {"pcall", luaB_pcall}, | 475 | {"pcall", luaB_pcall}, |
462 | #if 0 | 476 | #if 0 |
463 | {"print", luaB_print}, | 477 | {"print", luaB_print}, |
@@ -619,14 +633,6 @@ static const luaL_Reg co_funcs[] = { | |||
619 | /* }====================================================== */ | 633 | /* }====================================================== */ |
620 | 634 | ||
621 | 635 | ||
622 | static void auxopen (lua_State *L, const char *name, | ||
623 | lua_CFunction f, lua_CFunction u) { | ||
624 | lua_pushcfunction(L, u); | ||
625 | lua_pushcclosure(L, f, 1); | ||
626 | lua_setfield(L, -2, name); | ||
627 | } | ||
628 | |||
629 | |||
630 | static void base_open (lua_State *L) { | 636 | static void base_open (lua_State *L) { |
631 | /* set global _G */ | 637 | /* set global _G */ |
632 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 638 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
@@ -635,9 +641,6 @@ static void base_open (lua_State *L) { | |||
635 | luaL_register(L, "_G", base_funcs); | 641 | luaL_register(L, "_G", base_funcs); |
636 | lua_pushliteral(L, LUA_VERSION); | 642 | lua_pushliteral(L, LUA_VERSION); |
637 | lua_setglobal(L, "_VERSION"); /* set global _VERSION */ | 643 | lua_setglobal(L, "_VERSION"); /* set global _VERSION */ |
638 | /* `ipairs' and `pairs' need auxliliary functions as upvalues */ | ||
639 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | ||
640 | auxopen(L, "pairs", luaB_pairs, luaB_next); | ||
641 | /* `newproxy' needs a weaktable as upvalue */ | 644 | /* `newproxy' needs a weaktable as upvalue */ |
642 | lua_createtable(L, 0, 1); /* new table `w' */ | 645 | lua_createtable(L, 0, 1); /* new table `w' */ |
643 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ | 646 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ |