lua-users home
lua-l archive

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


Hi list,

The luaL_register function has two distinct features. One is creating
a module like the Lua "module" function (ie. dealing with the global
tables and package.loaded). One is registering a set of functions into
a table. It is possible to use the second feature without the first by
passing NULL as the libname parameter. However it is not possible to
have the first feature without the second, ie. creating an empty
module.

I have a need for that because I'd like to have the module itself be
the environment of all the C functions I put in it. To do that I
create an empty module, set is as the current environment, and then
register all its functions. But to create the empty module I have to
pass a pointer to an array with just a terminator in it:

    static struct luaL_Reg empty[] = {
        {0, 0},
    };
    static struct luaL_Reg functions[] = {
        /* actual module functions */
        {0, 0},
    };

    luaL_register(L, "modname", empty);
    lua_pushvalue(L, -1);
    lua_replace(L, LUA_ENVIRONINDEX);
    luaL_register(L, 0, functions);

I don't mind having to call luaL_register twice in that situation. But
it would be nice to have luaL_register accept NULL as its last
parameter (named "l"). Can I expect something like that in 5.2 ? Or
will the module system C API change more significatively ?

Jérôme.