From f6c6218909fd196a5bd293b7f41ceaf9c7d4267c Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 29 Mar 2012 11:32:22 +0400 Subject: [PATCH] Temporary tweak: patch in __pairs and __ipairs from 5.2 into lua 5.1. --- depends/lua/src/lbaselib.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/depends/lua/src/lbaselib.c b/depends/lua/src/lbaselib.c index 2a4c079d3..f927e5632 100644 --- a/depends/lua/src/lbaselib.c +++ b/depends/lua/src/lbaselib.c @@ -236,10 +236,17 @@ 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 */ + luaL_checkany(L, 1); + if (luaL_getmetafield(L, 1, "__pairs")) { + lua_pushvalue(L, 1); + lua_call(L, 1, 3); + } + else { + 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; } @@ -255,10 +262,17 @@ static int ipairsaux (lua_State *L) { 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 */ + luaL_checkany(L, 1); + if (luaL_getmetafield(L, 1, "__ipairs")) { + lua_pushvalue(L, 1); + lua_call(L, 1, 3); + } + else { + 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; }