(3.1) implementation of next()

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

(3.1) implementation of next()

David Jeske-2
In looking into my problem, I don't understand why this works:

static void next (void)
{
  lua_Object o = luaL_tablearg(1);
  lua_Object r = luaL_nonnullarg(2);
  Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
  if (n) {
    luaA_pushobject(&n->ref);
    luaA_pushobject(&n->val);
  }
}


Why is arg2 checked to be a nonnullarg? It's a property of next that if
you call it with a second arg == nil, then it starts at the beginning of
the list. This seems to actually WORK like that, but I don't understand,
given the code above, why I don't get an error when I do: "next(foo,nil)"
within lua.


-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (3.1) implementation of next()

Luiz Henrique de Figueiredo
>From [hidden email] Sun Jun 28 20:52:59 1998
>
>In looking into my problem, I don't understand why this works:
>
>static void next (void)
>{
>  lua_Object o = luaL_tablearg(1);
>  lua_Object r = luaL_nonnullarg(2);
>  Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
>  if (n) {
>    luaA_pushobject(&n->ref);
>    luaA_pushobject(&n->val);
>  }
>}
>
>
>Why is arg2 checked to be a nonnullarg? It's a property of next that if
>you call it with a second arg == nil, then it starts at the beginning of
>the list. This seems to actually WORK like that, but I don't understand,
>given the code above, why I don't get an error when I do: "next(foo,nil)"
>within lua.

luaL_nonnullarg checks compares with LUA_NOOBJECT, which is not the same as nil.
here is luaL_nonnullarg:

lua_Object luaL_nonnullarg (int numArg)
{
  lua_Object o = lua_getparam(numArg);
  luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
  return o;
}

lua_getparam (which is really lua_lua2C) returns LUA_NOOBJECT when you ask
for a non-existent param.
again, this is not the same as a param with value nil.

a C function that is registered in Lua should compare the value returned by
with LUA_NOOBJECT to know when to stop getting params.
--lhf