Quick hack: loadlib on Lua 4.0

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Quick hack: loadlib on Lua 4.0

Eduardo Ochs-2
Hi lhf, rtt, and everybody,

>>So, yes we need a good way to add third party libraries to an existing Lua
>>executable without having to recompile it.
>
>I think loadlib can help here: http://www.tecgraf.puc-rio.br/~rborges/loadlib/
>--lhf

But loadlib does not work with Lua 4.0... I tried to patch it a little
to make it work with 4.0 but couldn't (it would be the first time I
programmed a Lua extension in C, BTW), and I ended up reimplementing a
much simpler "loadlib" function, whose bulk is just this

--snip--snip--
#include <lualib.h>
#include <dlfcn.h>

static int loadlib(lua_State *L) {
  void *libptr;
  lua_CFunction f;
  libptr=dlopen(lua_tostring(L, 1), RTLD_LAZY);
  if (libptr==0)
    luaL_argerror(L, 1, "dlopen cannot open shared library");
  else {
    f=dlsym(libptr, lua_tostring(L, 2));
    if (f==0)
      luaL_argerror(L, 2, "dlsym cannot find init function");
    else
      f(L);
  }
  return 0;
}

LUALIB_API void lua_loadlib_open(lua_State *L) {
  lua_register(L, "loadlib", loadlib);
}
--snip--snip--

For how to install it and test it, how to download the Debian package,
how to recompile it include this function in the library, etc, etc,
see:

  <http://angg.twu.net/e/lua.e.html>

Cheers, and please provide feedback,
  Eduardo Ochs
  http://angg.twu.net/
  http://www.mat.puc-rio.br/~edrx/
  [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Quick hack: loadlib on Lua 4.0

Jean-Claude Wippler
Eduardo Ochs <[hidden email]> wrote:

>>>So, yes we need a good way to add third party libraries to an existing Lua
>>>executable without having to recompile it.
>>
>>I think loadlib can help here: http://www.tecgraf.puc-rio.br/~rborges/
>loadlib/
>>--lhf
>
>But loadlib does not work with Lua 4.0... I tried to patch it a little
>to make it work with 4.0 but couldn't (it would be the first time I
>programmed a Lua extension in C, BTW), and I ended up reimplementing a
>much simpler "loadlib" function, whose bulk is just this
[...]

Funny, I recently went through the same process and ended up rewriting
too.  Tested on Linux and Windows, with some tentative code for Macintosh
(PPC), it's now part of a larger work-in-progress project at http://
www.equi4.com/lux/ - I could package it separately if anyone is interested.

One change, which I hope could be adopted in standard Lua, is that the
call signature of extension entry points become "int blah(lua_State*)"
instead of the current "void blah(lua_State*)".  With the same semantics
as all other C functions in Lua: returning the number of results left on
the stack.  That makes all sorts of packaging variations feasible,
including optional "delayed loading", i.e. not calling the lib init
routine itself, but a wrapper which registers the init routine as a Lua
function.  It can also simplify static vs. dynamic linking.

The big "gotcha" is that this change breaks existing extensions - so let
me add a <strong> plea to do this swiftly and get over the pain while
it's still bearable.

-jcw