[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C API - how to combine lua_createtable and lua_settable
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 3 Mar 2011 08:14:48 -0300
> Does this play well with lua_createtable? Could I just do this?
>
> const struct luaL_Reg myLib[] = { . . . }l
> lua_createtable(L,0,(sizeof(myLib)/sizeof(const struct luaL_Reg))-1);
> luaL_register(L,0,myLib);
>
> lua_pushstring(L,"version");
> lua_pushstring(L,"0.1");
> lua_settable(L,-3);
> [...]
If you are going to use lua_createtable, it would be slightly better to
include space for the extra string:
- lua_createtable(L,0,(sizeof(myLib)/sizeof(const struct luaL_Reg))-1);
+ lua_createtable(L,0,(sizeof(myLib)/sizeof(const struct luaL_Reg)));
(As an unrelated note, it seems simpler to write 'sizeof(myLib[0])'
instead of 'sizeof(const struct luaL_Reg)'.)
-- Roberto