Temporary tweak: patch in __pairs and __ipairs from 5.2 into lua 5.1.

develop
Alexander Gavrilov 2012-03-29 11:32:22 +04:00
parent 17ff235c81
commit f6c6218909
1 changed files with 22 additions and 8 deletions

@ -236,10 +236,17 @@ static int luaB_next (lua_State *L) {
static int luaB_pairs (lua_State *L) { static int luaB_pairs (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 1);
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ if (luaL_getmetafield(L, 1, "__pairs")) {
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1);
lua_pushnil(L); /* and initial value */ 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; return 3;
} }
@ -255,10 +262,17 @@ static int ipairsaux (lua_State *L) {
static int luaB_ipairs (lua_State *L) { static int luaB_ipairs (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 1);
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ if (luaL_getmetafield(L, 1, "__ipairs")) {
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1);
lua_pushinteger(L, 0); /* and initial value */ 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; return 3;
} }