lua-users home
lua-l archive

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


Ralph Hempel wrote:

Beautiful! Now the next step is to figure out how to delete that
metatable programmatically so that when I do

string = nil

to get rid of the string library, it is also deleted from _LOADED
and garbage collected.

Solved!

a=""
getmetatable(a).__index = nil
string = nil

That was the missing piece. Just create a dummy string and set the one
and only metatable entry (__index) to nil allows the string library to
be garbagecollected and removed from my special weak-keyed _LOADED
registry table that I create in my main() before any of the standard
libraries are loaded.

More importantly, it allows me to reload the string library when I
need to. Previous versions would not reload a library if it is already
in _LOADED because the old __index metamethod was preventing the
metatable from getting reclaimed.

And I did not have to touch the Lua core!

Ralph