lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Yes, that approach came up in previous discussions of this. The only catch
is that you need to watch out for the limit on the number of upvalues. If
you hit that limit, you can reasonably fall back to an array stored as an
upvalue.

What would be nice is a standardized name now so that if this were to appear
in 5.2 existing Lua code wouldn't need updating.

Mark

on 4/5/06 11:33 AM, Javier Guerra at javier@guerrag.com wrote:

> On Wednesday 05 April 2006 3:01 pm, Mark Hamburg wrote:
>> Or at the very least, the Lua library needs a standard function to generate
>> an iterator from a series of values so that one could write:
>> 
>>     for i, arg in varargs( ... ) do
>>         -- code goes here
>>     end
> 
> it's easy in C (or TCC), no need to wrap on a table:
> 
> require "lua_tcc"
> 
> params= tcc.compile ([[
> 
> #include "lua.h"
> #include "lauxlib.h"
> 
> static int iter (lua_State *L) {
> int n = luaL_checkint (L, 1);
> int i = luaL_checkint (L, 2)+1;
> if (i > n)
> return 0;
> lua_pushnumber (L, i);
> lua_pushvalue (L, lua_upvalueindex (i));
> return 2;
> }
> 
> int params (lua_State *L) {
> int n = lua_gettop (L);
> lua_pushcclosure (L, iter, n);
> lua_pushnumber (L, n);
> lua_pushnumber (L, 0);
> 
> return 3;
> }
> 
> ]], "params")
> 
> for i,v in params (4, 19, "anything", nil, 10) do
> print (i,v)
> end