lua-users home
lua-l archive

Quick hack: loadlib on Lua 4.0

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


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/
 edrx@inx.com.br

AltStyle によって変換されたページ (->オリジナル) /