lua-users home
lua-l archive

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


Hi,

a while ago I made a statically-linked luasocket for a project running uclinux
on Blackfin with no shared libraries. This was for lua 5.1.3 though so it may be
that the exact details of the module handling have changed a bit...?

In linit.c I added entries to the end of the module table lualibs[] (with the
appropriate protos of course):

>>>
int luaopen_mime_core (lua_State *L);
int luaopen_socket_core (lua_State *L);
<<<
...
static const luaL_Reg lualibs[] = {
  ...
>>>
  {"mime", luaopen_mime_core},
  {"socket", luaopen_socket_core},
<<<
  {NULL, NULL}
};

This loads the core C modules socket.core and mime.core. To load the lua bits I
put the following lines in the initialisation file (specified in $LUA_INIT as
"@full_path_to_file"):

>>>
loadfile("/usr/share/lua/5.1/socket.lua")()
loadfile("/usr/share/lua/5.1/mime.lua")()
<<<

This has to be done explicitly because if you just call (require"socket") the
loader will think the module is already loaded and do nothing.

Hope that helps anyone trying to do the same
Andrew



Diego Nehab wrote:
> Hi,
> 
> First, let's refresh how to statically link a module in Lua. Say the
> module would be invoked as
> 
>     require"foo"
> 
> If it is a C module, then there is a C entrypoint luaopen_foo().
> Link the object files with your application and, during your
> initialization code, produce a Lua function from this entrypoint and
> assign it to preload["foo"] (preload is a table that lives in the
> registry). This will make sure require() doesn't look for the module
> on disk. It checks that table first.
> 
> If your module is a Lua module, you first need to compile the Lua
> module using luac, then convert it to something that can be included
> in a C file with bin2c. You can then create a C entrypoint that is
> equivalent to running the Lua module. Compile this C file and link to
> your application, then put a Lua function corresponding to that
> entrypoint in preload["foo"].
> 
> LuaSocket has both C and Lua modules. Some modules, such as mime and
> socket, are hybrid. When you invoke
> 
>     require"socket"
> 
> you are actually loading socket.lua. This Lua file invokes
> require"socket.core", which loads luasocket.c. (same thing with
> mime.lua, which loads mime.c).
> 
> To link statically to LuaSocket and have require"socket" work, you
> have to do the following.  Put a Lua function corresponding to the
> entrypoint luaopen_socket_core, found in luasocket.c, in
> preload["socket.core"]. You also have to put a Lua function
> corresponding to socket.lua (made with luac and bin2c) in
> preload["socket"].
> 
> You also have to put the entrypoint corresponding to http.lua in
> preload["http"], the one corresponding to smtp.lua in preload["smtp"]
> and etc.
> 
> I think this sums up the process. I have never personally done this
> myself, but I have instructed several people off-the-list on how to do
> it and they all managed. If anybody has doubts, please post on this
> thread so it stays on record forever. :)
> 
> Regards,
> Diego
>