lua-users home
lua-l archive

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


Hi,

> I need to register some C functions from a lib into a table. But this
table
> is not in the global table, but a sub-table.

Umm...  you just push them into the table?  Like so (Lua 4 API)

/* assume target table is at top of stack (or
   change the index -3 below in something
   appropriate) */

/* table.func1 = func1 */
lua_pushstring(L, "func1");
lua_pushcfunction(L, func1);
lua_settable(L, -3);

/* table.func2 = func2 */
lua_pushstring(L, "func2");
lua_pushcfunction(L, func2);
lua_settable(L, -3);

/* etc... */

Of course you can write simple helper code that does all the pushing for
you, given a luaL_reg-like table.  If this is meant as part of the "import"
scheme, I would suggest that import's internals prepare the stack so that
the table to use is already there before the library entrypoint is called.
(A package gets its table passed as a parameter.)

Bye,
Wim