[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Unloading shared libraries loaded by require()
- From: Steven Johnson <steve@...>
- Date: Thu, 3 Feb 2011 22:50:32 -0600
On Thu, Feb 3, 2011 at 9:22 PM, dptr1988 <junk_mail@dptr1988.mooo.com> wrote:
>
> Assuming that it would be possible to change Lua to record a reference to the shared library each time a C closure was created, what would be the disadvantages of doing it? The extra load put on the GC to track C function closures should be minimal, as most C modules don't export many functions.
What about something like this, paired with unrequire()?
local closures_to_uds = setmetatable({}, { __mode = "k" })
function my_require (modname)
local mod = require(modname)
local ud = debug.getregistry()._LOADED[modname]
if type(mod) == "table" then
for _, func in pairs(mod) do
closures_to_uds[func] = ud
end
end
return mod
end
(You could even put the unrequire() logic before the return, if you
won't have any further reason to look that info up.)
Just assign any needed functions to locals after calling my_require(),
so they can be captured as upvalues if necessary, and go through those
locals from then onward.