I am trying to implement a lua runtime and while figuring out the design of garbage collection, i saw that Lua actually specifies a defined order in which destructors of UserData or Table should run.
In Lua 5.4 Reference Manual: 2.5.3 – Garbage-Collection Metamethod
For an object (table or userdata) to be finalized when collected, you must mark it for finalization. You mark an object for finalization when you set its metatable and the metatable has a __gc metamethod. Note that if you set a metatable without a __gc field and later create that field in the metatable, the object will not be marked for finalization.
In the simple case of setting metatables for UserData A first and then B second, i understand that i should call B.__gc first and then A.__gc.
But, if i have two objects A and B, and i set a metatable (with __gc) for A, then B and then A again, what will be the order of finalizers being run?
will __gc method be called for A first or B first?
I can't rely on the order used by lua5.4 because it is the reference (and only) implementation.