The bad thing is that the registry maintains the finalizer only as a single ordered list and not according to a dependency tree. This means that libraries cannot be unloaded if another unrelated library was registered after it and actually does not depend on the library we'd liek to unload.
How can we maintain a dependency tree (and no longer depend on the finalizer ordered list) ?
Using finalizers seems the wrong way to do that. a single finalizer should be used for the whole dependency tree (when terminating the whole Lua instance itself), but there's a need to develop better registry maintaining a dependency graph (possibly with reference counters, but this is not necessarily the best approach as it can be broken easily, causing a library to be unloaded prematurely when it is still in use): this should better be part of the GC, possibly using "weak references" for libraries that can be easily unloaded when needed and reloaded later possibly with their own persistant external storage (some DB interface or external network REST API?) for some data needed to restore its previous state.