[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to reload C functions added with "require"?
- From: Rob Shaw <rob@...>
- Date: Wed, 26 Apr 2006 11:55:10 +0200
Thanks! I think you are right, it must be done from the C side.
I looked at loadlib.c, made a small change, and was able to
successfully reload a recompiled dynamic library, but I
still don't completely understand what's going on.
It looks like nearly everything is already there in loadlib.c.
There is a call ll_unloadlib() which for mac osx is a wrapper
over NSUnLinkModule() which should do the job, at least for
PPC architecture. So I added another entry to pk_funcs[],
{"unload", ll_unload},
and defined ll_unload as follows:
static int ll_unload (lua_State *L) {
const char *path = luaL_checkstring(L, 1);
void **reg = ll_register(L, path);
// if (*reg) ll_unloadlib(*reg);
*reg = NULL; /* mark library as closed */
return 0;
}
It worked! In lua:
> package.loadlib('hello.so','_luaopen_hello' )()
> =hello()
Hello world!
(now edit and recompile hello.so)
> package.unload('hello.so')
(if you call hello() now, you will get a satisfying crash)
> package.loadlib('hello.so','_luaopen_hello')()
> =hello()
Hello walda!
The interpreter can now live forever, and continuously change its C
code.
By the way, a friend tells me you can't do this in python, at least not
right now.
But there are mysteries, for example if you comment out the ll_unloadlib
line as above, it still works! The dynamic loader must be pretty smart,
you just have to let it do its job. I wouldn't be surprised though if
different loaders behave differently, maybe that's why this
Very Important Feature hasn't been implemented. Even in macland, the
docs
discourage the use of the NSModule package, and want you to use
dlopen() / dlclose(). Also, if I understand the docs, the unlink /
relink
feature only works in PPC, though maybe it isn't even needed.
Also, I don't understand lua internals well enough to get "require"
to work this way. But I just wrote a little script:
function req(name)
package.unload(name)
package.loadlib(name..'.so','_luaopen_'..name)()
end
so now you can say > req 'hello' and reload the new C module.
Thanks again,
rob
Fabian Pe?a wrote:
I think that you must add "C" code for unload dynamic library module (
very necesary under windows to unlock the file ) and invoque a require
or directly call to loadlib function again.
Rob Shaw wrote:
Hi,
We've successfully been using "require" in lua 5.1 under mac osx
10.4.5
to dynamically load external C functions with bindings constructed
with
tolua++.
But what I'd like to do is edit and recompile the C functions, and
then
reload them, without exiting and re-entering the lua interpreter.
If you just say "require" again, nothing happens, as there is an
entry in
package.loaded[name]. I've tried setting package.loaded[name] = nil,
and this will load any new functions added, but previously registered
functions
stay the same, even if I set func = nil before the second "require".
The old function is living somewhere, and gets reattached to the name
"func".
How can I load in new versions of the old functions? It might be
nice
to have
a function "reload" to go along with "require".
Thanks, sorry if I'm missing something obvious.
rob