savi
savi copied to clipboard
Add GC finalizers
In my case this would be useful so that GLFW.terminate will be called once all references go out of scope / get recyled (there will only ever be one instance...). Meaning, you cannot terminate GLFW while there are still references to it. Atm, this is not possible, or I didn't find the appropriate functionality.
Yes, finalizers are still missing. The mechanism is there in the Pony runtime (which we leverage in Savi), but the Savi language/compiler is not set up to use it yet.
What do you think of the following syntax?
:class GLFW
:finalizer
_FFI.glfwTerminate
A few things at top of mind that the compiler needs to take care of to make finalizers work:
-
Allow only one
:finalizerdeclaration per type -
Allow
:finalizeronly on heap-allocated types (i.e.:classand:actor, or a:traitthat gets folded into a:classor:actor) -
Ensure
:isand:copiesdeclaration can copy a finalizer to a destination type -
Ensure a finalizer body has a
boxreference to the class it is owned by -
Analysis to know whether a given function "can send a message"
-
Prevent finalizers (via a compilation error) that "can send a message"
-
In the future, when fat pointers are added, prevent fat-pointer "transmutation" if either the source type or destination type has a finalizer
With all that done, then it's just a matter of hooking into the finalizer desc slot in the Pony runtime in the LLVM IR generation pass to make it happen.
The syntax would work perfectly!