lua-users home
lua-l archive

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


Or like this:

#include "LuaJHcomlib.h"
static const luaL_Reg gpreloads[] = {
//  {"name", luaopen_name},
    {"com", luaopen_com},
    {NULL, NULL}
};

// When setting up the environment ...
	const luaL_Reg * lib = gpreloads;
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "preload");
	for (; lib->func; lib++) {
		lua_pushcfunction(L, lib->func);
		lua_setfield(L, -2, lib->name);
	};
	lua_pop(L, 2);


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Duncan Cross
Sent: 02 September 2009 23:51
To: Lua list
Subject: Re: Wishlists and Modules (was [ANN] libmc)

On Wed, Sep 2, 2009 at 11:06 PM, Andre de
Leiradella<aleirade@sct.microlink.com.br> wrote:
> (...) Or I'm completely missing an
> obvious way to require modules that are compiled into the application
> without hacks... In which case everyone is free to punch, ignore or
> enlighten me.

You should be able to make modules compiled into the application
available to "require" by adding them into the "package.preload" table
(the key is the module name as passed to require, and the value is the
luaopen_... function).

So, in your setup code for the Lua state, once the standard libraries
have been added in, something like this should be enough:

lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(luaopen_mymodule1);
lua_setfield(L, -2, "mymodule1");
lua_pushcfunction(luaopen_mymodule2);
lua_setfield(L, -2, "mymodule2");
// ...
lua_pop(L,2); // remove package & package.preload

-Duncan