Second argument in luaL_newlib should be an array

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

Second argument in luaL_newlib should be an array

Alexander Nasonov
void luaL_newlib (lua_State *L, const luaL_Reg *l);

is documented to be equivalent to

(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))

But since luaL_newlibtable() documents that its second argument
must be an array, the luaL_newlib should inherit this requirement:

void luaL_newlib (lua_State *L, const luaL_Reg l[]);

Alex

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Dirk Laurie-2
2014-09-13 19:16 GMT+02:00 Alexander Nasonov <[hidden email]>:

> void luaL_newlib (lua_State *L, const luaL_Reg *l);
>
> is documented to be equivalent to
>
> (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
>
> But since luaL_newlibtable() documents that its second argument
> must be an array, the luaL_newlib should inherit this requirement:
>
> void luaL_newlib (lua_State *L, const luaL_Reg l[]);

Is there any difference between *l and l[] in C?

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Alexander Nasonov
Dirk Laurie wrote:
> Is there any difference between *l and l[] in C?

There is no difference for function arguments but luaL_newlib()
and luaL_newlibtable() are macros. Because the documentation
emphasises that l must be an array, I suspect the implementation
calculates sizeof(l) / sizeof(l[0]).

Alex

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Karel Tuma-2
Excerpts from Alexander Nasonov's message of 2014-09-14 20:19:08 +0200:
> Dirk Laurie wrote:
> > Is there any difference between *l and l[] in C?
>
> There is no difference for function arguments but luaL_newlib()
> and luaL_newlibtable() are macros. Because the documentation
> emphasises that l must be an array, I suspect the implementation
> calculates sizeof(l) / sizeof(l[0]).
>
> Alex

Luckily not, it looks for NULL terminator in the name field.

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Alexander Nasonov
Karel Tuma wrote:

> Excerpts from Alexander Nasonov's message of 2014-09-14 20:19:08 +0200:
> > Dirk Laurie wrote:
> > > Is there any difference between *l and l[] in C?
> >
> > There is no difference for function arguments but luaL_newlib()
> > and luaL_newlibtable() are macros. Because the documentation
> > emphasises that l must be an array, I suspect the implementation
> > calculates sizeof(l) / sizeof(l[0]).
> >
> > Alex
>
> Luckily not, it looks for NULL terminator in the name field.

#define luaL_newlibtable(L,l)   \
  lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)

#define luaL_newlib(L,l)        (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))

Alex

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Karel Tuma-2
Excerpts from Alexander Nasonov's message of 2014-09-14 21:42:07 +0200:

> Karel Tuma wrote:
> > Excerpts from Alexander Nasonov's message of 2014-09-14 20:19:08 +0200:
> > > Dirk Laurie wrote:
> > > > Is there any difference between *l and l[] in C?
> > >
> > > There is no difference for function arguments but luaL_newlib()
> > > and luaL_newlibtable() are macros. Because the documentation
> > > emphasises that l must be an array, I suspect the implementation
> > > calculates sizeof(l) / sizeof(l[0]).
> > >
> > > Alex
> >
> > Luckily not, it looks for NULL terminator in the name field.
>
> #define luaL_newlibtable(L,l)   \
>   lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
>
> #define luaL_newlib(L,l)        (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
>
> Alex

I suspect the macro "virtual decl" was pasted from actual declaration of
luaL_setfuncs, which uses a pointer, whereas luaL_newlibtable "virtual decl"
uses [] as an emphasis that the array will be used to compute hint, indeed the docs
can be perceived a bit misleading in that regard, though no harm done in the actual API.

Interestingly, with pointer the result always yields -1 (becaue l[0] is 2 pointers wide)
which makes lua_createtable ignore it. While not the case of Lua, this might be existing
int overflow bug to look for in other software..

Reply | Threaded
Open this post in threaded view
|

Re: Second argument in luaL_newlib should be an array

Steven Degutis
In reply to this post by Alexander Nasonov
Quoth Alexander Nasonov:

> void luaL_newlib (lua_State *L, const luaL_Reg *l);
>
> is documented to be equivalent to
>
> (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
>
> But since luaL_newlibtable() documents that its second argument
> must be an array, the luaL_newlib should inherit this requirement:
>
> void luaL_newlib (lua_State *L, const luaL_Reg l[]);

Hmm. I've always been very confused as to why luaL_newlibtable
explicitly requires an array. Especially when, in practice, luaL_Reg
arrays always end with a NULL-filled sentinel. It feels like overkill.
I'd love it if 5.3 would just choose one over the other and stick with
it consistently.

-Steven