lua-users home
lua-l archive

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


It was thus said that the Great dualface once stated:
> I tried:
> 
> extern int luaopen_lanes_core( lua_State* L);
> 
> int lanes_core_loader(lua_State* L)
> {
>     lua_pushliteral(L, "lanes.core");
>     luaopen_lanes_core(L);
>     return 1;
> }
> 
> lua_getglobal(L, "package");
> lua_getfield(L, -1, "preload");
> lua_pushcfunction(L, lanes_core_loader);
> lua_setfield(L, -2, "lanes.core");
> lua_pop(L, 2);
> 
> ----------------------------------------
> 
> test.lua:
> 
> local lanes = require("lanes").configure()
> 
> 
> get error:
> 
> lanes.lua:120: main: function 'package.preload.lanes.core' not found
> in Keeper #1 destination transfer database.
> 
> ----------------------------------------
> 
> Hoping to get help, thanks.

  This isn't LuaLanes, but I have embedded LPeg, re, LuaXML and cURL (Lua
wrapper for curl) in a statically compiled program (for use at work).  The
code looks something like:

int luaopen_lpeg	(lua_State *);
int luaopen_LuaXML_lib	(lua_State *);
int luaopen_cURL	(lua_State *);

/*---------------------------------------------------------------------------
; the following is Lua code.  The tool chain I use creates a C file from a
; bunch of Lua code with the following defintions that is linked in with the
; rest of the code.
;----------------------------------------------------------------------------*/

extern const char   c_re[];
extern const size_t c_re_size;
extern const char   c_LuaXml[];
extern const size_t c_LuaXml_size;
extern const char   c_util[];	/* some misc. code */
extern const size_t c_util_size;

/*---------------------------------------------------------------------------
; the following code assumes a freshly created Lua state is being passed in.
;----------------------------------------------------------------------------*/

void preload_lua(lua_State *L)
{
  luaL_openlibs(L);

  /*----------------------------------------------------------------
  ; the following modules, due to the way they're written, need to be
  ; "preloaded" into the package.preload table.  The normal method of
  ; module initiation (see below) will cause the program to fail.
  ;-------------------------------------------------------------------*/
  
  lua_getglobal(L,"package");
  lua_getfield(L,-1,"preload");
  luaL_loadbuffer(L,c_re,c_re_size,"re");
  lua_setfield(L,-2,"re");
  luaL_loadbuffer(L,c_LuaXml,c_LuaXml_size,"LuaXml");
  lua_setfield(L,-2,"LuaXml");
  lua_pop(L,2);

  /*----------------------------------------------------------
  ; because of the way that LuaXML works, we need to also set
  ; package.loaded.LuaXML_lib  
  ;----------------------------------------------------------*/

  luaopen_LuaXML_lib(L);
  lua_getglobal(L,"package");
  lua_getfield(L,-1,"loaded");
  lua_pushvalue(L,-3);
  lua_setfield(L,-2,"LuaXML_lib");
  lua_pop(L,3);

  /*----------------------------------------------------------
  ; we resume with more sanely written modules.
  ;-----------------------------------------------------------*/

  luaopen_lpeg(L);
  luaopen_cURL(L);

  luaL_loadbuffer(L,c_util,c_util_size,"util");
  lua_call(L,0,0);
  
  lua_settop(L,0);
}

  I found that I had to play around with how the modules are "loaded", but I
did not have to change any of the code for LPeg, re, LuaXML or cURL (they're
left, "as is").  In the scripts I write, I can use require all I want with
no issues.

  -spc (In fact, there are more modules in use than the ones shown here, but
	they all either end up in package,preloaded or under the "more
	sanely written" stanza.  Hope this helps some ... )