janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.43k stars 221 forks source link

Finalizers? #1261

Closed bitcompost closed 1 year ago

bitcompost commented 1 year ago

Hi, does Janet have anything similar to finalizers for foreign objects in Guile, or __gc metamethod in Lua? I only found janet_sfinalizer for scratch memory, if I understand correctly, scratch memory is always freed on next gc so that's not what I'm looking for.

Thanks!

CosmicToast commented 1 year ago

Foreign objects (especially ones that need special handling like custom gc behavior) should generally be implemented via abstract types. One of the fields in abstract types is the gc field (there's also a gcmark field) that binds to a function pointer that does exactly that.

bitcompost commented 1 year ago

Thanks, this is what I was looking for. I found a nice guide on abstract types: https://janet.guide/xenofunctions/ . Would be nice to have something in the official Janet docs.