lua-users home
lua-l archive

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


Leo Razoumov wrote:
On Tue, 20 Jan 2009, Peter Kümmel wrote:
I also think A_B_C isn't perfect, but more important is the fact that
extracting namespaces from the header files works.

With a simple binding, like slb, and a simplified generator we could
easily change such things. At the moment only Mauro knows the generator
code good enough to change it.

Peter


Why so complicated. Lua package/module framework is very flexible and has
a natural support for namespaces. For instance, standard Lua API function
"luaL_register" has a nice property that if called as

luaL_register(L, "N.M.mylib", mylib);

it will create nested tables so that N.M.foo() is the way to call "foo" function from mylib.

Below is a pseudo-example:


/*file n1/mylib.c (will become n1/mylib.so) */

But then we need for each namespace a shared library, or I am wrong?

Peter


static const struct luaL_Reg  n1_mylib [] = {
    {"foo", lwrap_n1_foo}, /*wrap n1::foo*/
    {"bar", lwrap_n1_bar}, /*wrap n1::bar*/
    {NULL, NULL}
};

int luaopen_n1_mylib(lua_State *pL)
{
    luaL_register(pL, "n1.mylib", n1_mylib);
}

/*file n2/mylib.c (will become n2/mylib.so) */

static const struct luaL_Reg  n2_mylib [] = {
    {"foo", lwrap_n2_foo}, /*wrap n2::foo*/
    {"bar", lwrap_n2_bar}, /*wrap n2::bar*/
    {NULL, NULL}
};


int luaopen_n2_mylib(lua_State *pL)
{
    luaL_register(pL, "n2.mylib", n2_mylib);

}


Now in a

--file test.lua

require "n1.mylib"
require "n2.mylib"

n1.foo() --foo from namespace n1 (via lwrap_n1_foo)
n2.foo() --foo from namespace n2 (via lwrap_n2_foo)


Hope it helps.
--Leo--