lua-users home
lua-l archive

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

On Thursday 03 February 2011 8:50:32 pm Steven Johnson wrote:
> On Thu, Feb 3, 2011 at 9:22 PM, dptr1988 <> 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.

This is a great idea and could easily solve another problem I have. Thanks for the suggestion. 

Of course, this method doesn't track all closures, especially closures generated after the module initialization. But if used with care, I think it could be a useful way of handling this situation.