lua-users home
lua-l archive

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




2010/5/4 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
>     function table.proto (t)
>         local tp = {}
>         for k in pairs(t) do
>             tp[k] = nil
>         end

In principle, this loop has not effect because the table is empty and will
remain empty after the loop. In the current implementation it does have the
side effect that the keys are temporarily created in tp. Are you counting
on this behavior??
 
You are right.
The function lua_prototable() could be written without the loop

LUA_API void lua_prototable(lua_State *L, int idx) {
  StkId o;
  Table *t;
  lua_lock(L);
  o = index2adr(L, idx);
  api_check(L, ttistable(o));
  t = hvalue(o);
  luaC_checkGC(L);
  sethvalue(L, L->top, luaH_new(L, t->sizearray, sizenode(t)));
  api_incr_top(L);
}
 
François