# HG changeset patch # User jbe # Date 1408492355 -7200 # Node ID 7b05ca6ef925a4eb4e1c95edf46daffa366eb02e # Parent e29e2d7b73a59e5bcae418b5e8d754ae37887ccd Allow seqlua_iterinit to modify the value on stack instead of storing an extra element on stack diff -r e29e2d7b73a5 -r 7b05ca6ef925 README --- a/README Wed Aug 20 01:45:04 2014 +0200 +++ b/README Wed Aug 20 01:52:35 2014 +0200 @@ -133,9 +133,9 @@ printcsv{"a", "b", "c"} -- prints: a,b,c -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. +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. Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``, which converts a table at a given stack index into a function closure (stored diff -r e29e2d7b73a5 -r 7b05ca6ef925 seqlualib.c --- a/seqlualib.c Wed Aug 20 01:45:04 2014 +0200 +++ b/seqlualib.c Wed Aug 20 01:52:35 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,7 +23,6 @@ luaL_checktype(L, idx, LUA_TTABLE); iter->itertype = SEQLUA_ITERTYPE_RAW; } - lua_pushvalue(L, idx); } iter->i = 0; } @@ -44,7 +43,7 @@ lua_rawgeti(L, -1, i); } if (lua_isnil(L, -1)) { - lua_pop(L, 2); + lua_pop(L, 1); return 0; } return 1;