savi icon indicating copy to clipboard operation
savi copied to clipboard

Add GC finalizers

Open mneumann opened this issue 2 years ago • 4 comments

mneumann avatar Feb 13 '23 19:02 mneumann

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.

mneumann avatar Feb 14 '23 06:02 mneumann

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

jemc avatar Feb 14 '23 20:02 jemc

A few things at top of mind that the compiler needs to take care of to make finalizers work:

  • Allow only one :finalizer declaration per type

  • Allow :finalizer only on heap-allocated types (i.e. :class and :actor, or a :trait that gets folded into a :class or :actor)

  • Ensure :is and :copies declaration can copy a finalizer to a destination type

  • Ensure a finalizer body has a box reference 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.

jemc avatar Feb 14 '23 20:02 jemc

The syntax would work perfectly!

mneumann avatar Feb 15 '23 08:02 mneumann