lua

A copy of the Lua development repository
Log | Files | Refs | README

commit ab09732986d551efea3f44367ec1221afea72c7d
parent b7fa64565af859776ca805ea5d1ee4a94a44c6b8
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Wed, 17 Oct 2007 15:26:16 -0200

new metamethods for '__pairs' and '__ipairs'

Diffstat:
Mlbaselib.c | 30+++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/lbaselib.c b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.197 2007/02/09 12:40:21 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.198 2007/06/21 13:48:04 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -223,6 +223,22 @@ static int luaB_type (lua_State *L) { } +static int pairsmeta (lua_State *L, const char *method, int iszero) { + if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ + luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ + lua_pushvalue(L, lua_upvalueindex(1)); /* 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 */ @@ -236,11 +252,7 @@ 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; + return pairsmeta(L, "__pairs", 0); } @@ -255,11 +267,7 @@ 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 */ - return 3; + return pairsmeta(L, "__ipairs", 1); }