seqlua
diff seqlua.c @ 52:3362ec36cb09
Do not automatically assume that functions passed to ipairs are iterators
but require ipairs(func, mode) to have an explicit mode set to "call" or "generator"
but require ipairs(func, mode) to have an explicit mode set to "call" or "generator"
author | jbe |
---|---|
date | Tue Aug 26 21:10:03 2014 +0200 (2014-08-26) |
parents | 332216604f83 |
children | 92ce3958aca7 |
line diff
1.1 --- a/seqlua.c Mon Aug 25 12:12:46 2014 +0200 1.2 +++ b/seqlua.c Tue Aug 26 21:10:03 2014 +0200 1.3 @@ -35,6 +35,9 @@ 1.4 } 1.5 1.6 static int seqlua_ipairs(lua_State *L) { 1.7 + const char *method; 1.8 + int generated = 0; 1.9 + seqlua_ipairs_repeat: 1.10 if (luaL_getmetafield(L, 1, "__ipairs")) { 1.11 lua_pushvalue(L, 1); 1.12 lua_call(L, 1, 3); 1.13 @@ -55,13 +58,27 @@ 1.14 } else { 1.15 int t = lua_type(L, 1); 1.16 if (t == LUA_TFUNCTION) { 1.17 - lua_pushcfunction(L, seqlua_ipairsaux_call); 1.18 + if (generated) goto seqlua_ipairs_call; 1.19 + method = lua_tostring(L, 2); 1.20 + if (method) { 1.21 + if (!strcmp(method, "call")) { 1.22 + seqlua_ipairs_call: 1.23 + lua_pushcfunction(L, seqlua_ipairsaux_call); 1.24 + goto seqlua_ipairs_finish; 1.25 + } else if (!strcmp(method, "generator")) { 1.26 + lua_settop(L, 1); 1.27 + lua_call(L, 0, 1); 1.28 + generated = 1; 1.29 + goto seqlua_ipairs_repeat; 1.30 + } 1.31 + } 1.32 } else if (luaL_getmetafield(L, 1, "__index")) { 1.33 lua_pushcfunction(L, seqlua_ipairsaux_index); 1.34 - } else { 1.35 - luaL_checktype(L, 1, LUA_TTABLE); 1.36 - lua_pushcfunction(L, seqlua_ipairsaux_raw); 1.37 + goto seqlua_ipairs_finish; 1.38 } 1.39 + luaL_checktype(L, 1, LUA_TTABLE); 1.40 + lua_pushcfunction(L, seqlua_ipairsaux_raw); 1.41 + seqlua_ipairs_finish: 1.42 lua_pushvalue(L, 1); 1.43 lua_pushinteger(L, 0); 1.44 } 1.45 @@ -77,7 +94,7 @@ 1.46 luaL_checkany(L, 2); 1.47 lua_settop(L, 3); 1.48 luaL_buffinit(L, &buf); 1.49 - seqlua_iterloop(L, &iter, 2) { 1.50 + seqlua_iterloop(L, &iter, SEQLUA_MODE_CALL, 2) { 1.51 lua_replace(L, 3); 1.52 if (seqlua_itercount(&iter) > 1) luaL_addlstring(&buf, sep, seplen); 1.53 luaL_tolstring(L, 3, NULL);