# HG changeset patch # User jbe # Date 1408492795 -7200 # Node ID 2cb22d01fdd0449747711e259ef5ace69c24286d # Parent 7b05ca6ef925a4eb4e1c95edf46daffa366eb02e Reverted last commit and added TODO for first argument to __call diff -r 7b05ca6ef925 -r 2cb22d01fdd0 README --- a/README Wed Aug 20 01:52:35 2014 +0200 +++ b/README Wed Aug 20 01:59:55 2014 +0200 @@ -133,15 +133,21 @@ printcsv{"a", "b", "c"} -- prints: a,b,c -NOTE: If the index passed to ``seqlua_iterinit`` points to a value that has a -``__call`` metamethod, then the value at the stack position will be replaced by -that metamethod. +NOTE: ``seqlua_iterinit`` will store one extra element on the stack during +iteration. When ``seqlua_iternext`` returns 0, this extra element is popped +from the stack automatically. Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``, which converts a table at a given stack index into a function closure (stored on the same stack index) that iterates over the elements of the table. If the value at the given stack index is already a function, it leaves the value unchanged. If the value is convertible to a function using ``__call,`` then the -value is replaced by its ``__call`` metamethod. +value is replaced by a closure calling the ``__call`` metamethod. +TODO +---- + +* pass the original value to the ``__call`` metamethod as first argument + + diff -r 7b05ca6ef925 -r 2cb22d01fdd0 seqlualib.c --- a/seqlualib.c Wed Aug 20 01:52:35 2014 +0200 +++ b/seqlualib.c Wed Aug 20 01:59:55 2014 +0200 @@ -11,9 +11,9 @@ iter->L = L; if (luaL_getmetafield(L, idx, "__call")) { if (lua_type(L, -1) != LUA_TFUNCTION) luaL_error(L, "__call is not a function"); - lua_replace(L, idx); iter->itertype = SEQLUA_ITERTYPE_FUNC; } else if (lua_type(L, idx) == LUA_TFUNCTION) { + lua_pushvalue(L, idx); iter->itertype = SEQLUA_ITERTYPE_FUNC; } else { if (luaL_getmetafield(L, idx, "__index")) { @@ -23,6 +23,7 @@ luaL_checktype(L, idx, LUA_TTABLE); iter->itertype = SEQLUA_ITERTYPE_RAW; } + lua_pushvalue(L, idx); } iter->i = 0; } @@ -43,7 +44,7 @@ lua_rawgeti(L, -1, i); } if (lua_isnil(L, -1)) { - lua_pop(L, 1); + lua_pop(L, 2); return 0; } return 1;