[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [LuaCheia-list] lib_open in nested table
- From: "Martin Spernau" <martin@...>
- Date: Wed, 5 Mar 2003 23:25:07 +0100
From: "Wim Couwenberg" <w.couwenberg@chello.nl>
> Umm... you just push them into the table? Like so (Lua 4 API)
<snip/>
Ok here is what I have so far (Lua5, altho I think it should be almsot same
in 4):
(inspired by the lauxlib 'luaL_openlib')
###
/* register a number of C functions in a subtable of 'lib' */
const char * basetablename = "lib";
const char * libname = "my";
/* get/create the global lib table */
lua_pushstring(L, basetablename);
lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
if (lua_isnil(L, -1)) { /* no? */
lua_pop(L, 1);
lua_newtable(L); /* create it */
}
lua_insert(L, -1);
/* we now have a table in the global scope named 'lib' */
/* we need to put our functions into a subtable of that */
/* we also need to allow for that sub-table to already exist! */
lua_pushstring(L, libname);
lua_gettable(L, -1); /* check whether subtable already exists */
if (lua_isnil(L, -1)) { /* no? */
lua_pop(L, 1);
lua_newtable(L); /* create it */
}
lua_insert(L, -1);
/* now we can simply put our functions into that subtable */
{
lua_pushstring(L, "func1name");
lua_pushcfunction(L, func1);
lua_settable(L, -3);
lua_pushstring(L, "func2name");
lua_pushcfunction(L, func2);
lua_settable(L, -3);
/* repeat for all functions to go in the libtable */
}
/* here is where I lost it ;) */
###
I'm not 100% sure how to return the tables, as I need to allow that both
'root' table and 'subtable' already have other entries....