diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-10-17 15:26:39 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-10-17 15:26:39 -0200 |
| commit | ab09732986d551efea3f44367ec1221afea72c7d (patch) | |
| tree | 1c47b0a0e964cad4c13fc124c11004336601c63b | |
| parent | b7fa64565af859776ca805ea5d1ee4a94a44c6b8 (diff) | |
| download | lua-ab09732986d551efea3f44367ec1221afea72c7d.tar.gz lua-ab09732986d551efea3f44367ec1221afea72c7d.tar.bz2 lua-ab09732986d551efea3f44367ec1221afea72c7d.zip | |
new metamethods for '__pairs' and '__ipairs'
| -rw-r--r-- | lbaselib.c | 30 |
1 files changed, 19 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.197 2007/02/09 12:40:21 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.198 2007/06/21 13:48:04 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -223,6 +223,22 @@ static int luaB_type (lua_State *L) { | |||
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | 225 | ||
| 226 | static int pairsmeta (lua_State *L, const char *method, int iszero) { | ||
| 227 | if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ | ||
| 228 | luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ | ||
| 229 | lua_pushvalue(L, lua_upvalueindex(1)); /* will return generator, */ | ||
| 230 | lua_pushvalue(L, 1); /* state, */ | ||
| 231 | if (iszero) lua_pushinteger(L, 0); /* and initial value */ | ||
| 232 | else lua_pushnil(L); | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ | ||
| 236 | lua_call(L, 1, 3); /* get 3 values from metamethod */ | ||
| 237 | } | ||
| 238 | return 3; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 226 | static int luaB_next (lua_State *L) { | 242 | static int luaB_next (lua_State *L) { |
| 227 | luaL_checktype(L, 1, LUA_TTABLE); | 243 | luaL_checktype(L, 1, LUA_TTABLE); |
| 228 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | 244 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
| @@ -236,11 +252,7 @@ static int luaB_next (lua_State *L) { | |||
| 236 | 252 | ||
| 237 | 253 | ||
| 238 | static int luaB_pairs (lua_State *L) { | 254 | static int luaB_pairs (lua_State *L) { |
| 239 | luaL_checktype(L, 1, LUA_TTABLE); | 255 | return pairsmeta(L, "__pairs", 0); |
| 240 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ | ||
| 241 | lua_pushvalue(L, 1); /* state, */ | ||
| 242 | lua_pushnil(L); /* and initial value */ | ||
| 243 | return 3; | ||
| 244 | } | 256 | } |
| 245 | 257 | ||
| 246 | 258 | ||
| @@ -255,11 +267,7 @@ static int ipairsaux (lua_State *L) { | |||
| 255 | 267 | ||
| 256 | 268 | ||
| 257 | static int luaB_ipairs (lua_State *L) { | 269 | static int luaB_ipairs (lua_State *L) { |
| 258 | luaL_checktype(L, 1, LUA_TTABLE); | 270 | return pairsmeta(L, "__ipairs", 1); |
| 259 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ | ||
| 260 | lua_pushvalue(L, 1); /* state, */ | ||
| 261 | lua_pushinteger(L, 0); /* and initial value */ | ||
| 262 | return 3; | ||
| 263 | } | 271 | } |
| 264 | 272 | ||
| 265 | 273 | ||
