lua-users home
lua-l archive

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



On 1-Sep-05, at 11:47 AM, Rici Lake wrote:

Not if ... had a lot of elements. I think eachi would have to use two different strategies: if called with more than maxupval arguments, it would have to create a table and use a different next function.

Untested implementation: lapi.c (5.1work6)


// insert at line 248
static int ieachaux (lua_State *L) {
  int i = luaL_checkint(L, 2);
  i++;
  lua_pushinteger(L, i);
  lua_pushvalue(lua_upvalueindex(i));
  return lua_isnil(L, -1) ? 0 : 2:
}

static int luaB_ieach (lua_State *L) {
  int n = lua_gettop(L);
  if (n <= LUAI_MAXUPVALUES) {
    lua_pushcclosure(L, ieachaux, n);
    lua_pushnil(L);
  }
  else {
    lua_pushvalue(L, lua_upvalueindex(1));
    lua_createtable(L, n, 0);
    do {
      lua_rawseti(L, -1, n);
    } while (--n);
  }
  lua_pushinteger(L, 0);
  return 3;
}


// insert in base_open, line 623:
  auxopen(L, "ieach", luaB_ieach, ipairsaux);