lua-users home
lua-l archive

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

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

   return mod

(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.