seqlua
changeset 7:2cb22d01fdd0
Reverted last commit and added TODO for first argument to __call
author | jbe |
---|---|
date | Wed Aug 20 01:59:55 2014 +0200 (2014-08-20) |
parents | 7b05ca6ef925 |
children | 144f0bddee2b |
files | README seqlualib.c |
line diff
1.1 --- a/README Wed Aug 20 01:52:35 2014 +0200 1.2 +++ b/README Wed Aug 20 01:59:55 2014 +0200 1.3 @@ -133,15 +133,21 @@ 1.4 1.5 printcsv{"a", "b", "c"} -- prints: a,b,c 1.6 1.7 -NOTE: If the index passed to ``seqlua_iterinit`` points to a value that has a 1.8 -``__call`` metamethod, then the value at the stack position will be replaced by 1.9 -that metamethod. 1.10 +NOTE: ``seqlua_iterinit`` will store one extra element on the stack during 1.11 +iteration. When ``seqlua_iternext`` returns 0, this extra element is popped 1.12 +from the stack automatically. 1.13 1.14 Additionally, ``seqlualib`` includes a function ``seqlua_iterclosure(L, idx)``, 1.15 which converts a table at a given stack index into a function closure (stored 1.16 on the same stack index) that iterates over the elements of the table. If the 1.17 value at the given stack index is already a function, it leaves the value 1.18 unchanged. If the value is convertible to a function using ``__call,`` then the 1.19 -value is replaced by its ``__call`` metamethod. 1.20 +value is replaced by a closure calling the ``__call`` metamethod. 1.21 1.22 1.23 +TODO 1.24 +---- 1.25 + 1.26 +* pass the original value to the ``__call`` metamethod as first argument 1.27 + 1.28 +
2.1 --- a/seqlualib.c Wed Aug 20 01:52:35 2014 +0200 2.2 +++ b/seqlualib.c Wed Aug 20 01:59:55 2014 +0200 2.3 @@ -11,9 +11,9 @@ 2.4 iter->L = L; 2.5 if (luaL_getmetafield(L, idx, "__call")) { 2.6 if (lua_type(L, -1) != LUA_TFUNCTION) luaL_error(L, "__call is not a function"); 2.7 - lua_replace(L, idx); 2.8 iter->itertype = SEQLUA_ITERTYPE_FUNC; 2.9 } else if (lua_type(L, idx) == LUA_TFUNCTION) { 2.10 + lua_pushvalue(L, idx); 2.11 iter->itertype = SEQLUA_ITERTYPE_FUNC; 2.12 } else { 2.13 if (luaL_getmetafield(L, idx, "__index")) { 2.14 @@ -23,6 +23,7 @@ 2.15 luaL_checktype(L, idx, LUA_TTABLE); 2.16 iter->itertype = SEQLUA_ITERTYPE_RAW; 2.17 } 2.18 + lua_pushvalue(L, idx); 2.19 } 2.20 iter->i = 0; 2.21 } 2.22 @@ -43,7 +44,7 @@ 2.23 lua_rawgeti(L, -1, i); 2.24 } 2.25 if (lua_isnil(L, -1)) { 2.26 - lua_pop(L, 1); 2.27 + lua_pop(L, 2); 2.28 return 0; 2.29 } 2.30 return 1;