diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 38 |
1 files changed, 12 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.320 2018/02/25 12:48:16 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.321 2018/02/27 17:48:28 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 | */ |
@@ -253,23 +253,6 @@ static int luaB_type (lua_State *L) { | |||
253 | } | 253 | } |
254 | 254 | ||
255 | 255 | ||
256 | static int pairsmeta (lua_State *L, const char *method, int iszero, | ||
257 | lua_CFunction iter) { | ||
258 | luaL_checkany(L, 1); | ||
259 | if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ | ||
260 | lua_pushcfunction(L, iter); /* will return generator, */ | ||
261 | lua_pushvalue(L, 1); /* state, */ | ||
262 | if (iszero) lua_pushinteger(L, 0); /* and initial value */ | ||
263 | else lua_pushnil(L); | ||
264 | } | ||
265 | else { | ||
266 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ | ||
267 | lua_call(L, 1, 3); /* get 3 values from metamethod */ | ||
268 | } | ||
269 | return 3; | ||
270 | } | ||
271 | |||
272 | |||
273 | static int luaB_next (lua_State *L) { | 256 | static int luaB_next (lua_State *L) { |
274 | luaL_checktype(L, 1, LUA_TTABLE); | 257 | luaL_checktype(L, 1, LUA_TTABLE); |
275 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | 258 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
@@ -283,7 +266,17 @@ static int luaB_next (lua_State *L) { | |||
283 | 266 | ||
284 | 267 | ||
285 | static int luaB_pairs (lua_State *L) { | 268 | static int luaB_pairs (lua_State *L) { |
286 | return pairsmeta(L, "__pairs", 0, luaB_next); | 269 | luaL_checkany(L, 1); |
270 | if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ | ||
271 | lua_pushcfunction(L, luaB_next); /* will return generator, */ | ||
272 | lua_pushvalue(L, 1); /* state, */ | ||
273 | lua_pushnil(L); /* and initial value */ | ||
274 | } | ||
275 | else { | ||
276 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ | ||
277 | lua_call(L, 1, 3); /* get 3 values from metamethod */ | ||
278 | } | ||
279 | return 3; | ||
287 | } | 280 | } |
288 | 281 | ||
289 | 282 | ||
@@ -302,15 +295,11 @@ static int ipairsaux (lua_State *L) { | |||
302 | ** (The given "table" may not be a table.) | 295 | ** (The given "table" may not be a table.) |
303 | */ | 296 | */ |
304 | static int luaB_ipairs (lua_State *L) { | 297 | static int luaB_ipairs (lua_State *L) { |
305 | #if defined(LUA_COMPAT_IPAIRS) | ||
306 | return pairsmeta(L, "__ipairs", 1, ipairsaux); | ||
307 | #else | ||
308 | luaL_checkany(L, 1); | 298 | luaL_checkany(L, 1); |
309 | lua_pushcfunction(L, ipairsaux); /* iteration function */ | 299 | lua_pushcfunction(L, ipairsaux); /* iteration function */ |
310 | lua_pushvalue(L, 1); /* state */ | 300 | lua_pushvalue(L, 1); /* state */ |
311 | lua_pushinteger(L, 0); /* initial value */ | 301 | lua_pushinteger(L, 0); /* initial value */ |
312 | return 3; | 302 | return 3; |
313 | #endif | ||
314 | } | 303 | } |
315 | 304 | ||
316 | 305 | ||
@@ -506,9 +495,6 @@ static const luaL_Reg base_funcs[] = { | |||
506 | {"ipairs", luaB_ipairs}, | 495 | {"ipairs", luaB_ipairs}, |
507 | {"loadfile", luaB_loadfile}, | 496 | {"loadfile", luaB_loadfile}, |
508 | {"load", luaB_load}, | 497 | {"load", luaB_load}, |
509 | #if defined(LUA_COMPAT_LOADSTRING) | ||
510 | {"loadstring", luaB_load}, | ||
511 | #endif | ||
512 | {"next", luaB_next}, | 498 | {"next", luaB_next}, |
513 | {"pairs", luaB_pairs}, | 499 | {"pairs", luaB_pairs}, |
514 | {"pcall", luaB_pcall}, | 500 | {"pcall", luaB_pcall}, |