Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Use less hacky finalizer key and ignore rather than panic on unsuppor…
Browse files Browse the repository at this point in the history
…ted finalizer (#17)
  • Loading branch information
anuraaga authored Jun 20, 2023
1 parent 0b11eec commit b5efc65
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ void free(void* ptr);
import "C"
import "unsafe"

var finalizers = map[uintptr]interface{}{}
var finalizers = map[uint64]interface{}{}

var finalizersIdx = uint64(0)

//go:linkname SetFinalizer runtime.SetFinalizer
func SetFinalizer(obj interface{}, finalizer interface{}) {
finKey := uintptr((*_interface)(unsafe.Pointer(&finalizer)).value)
if _, ok := finalizer.(func(interface{})); !ok {
// Until function invocation with reflection is supported by TinyGo, we cannot support arbitrary finalizers.
// We make a best-effort attempt to support the generic func(interface{}). For other finalizers, we silently
// ignore, which would be the behavior for all finalizers with the default allocator.
return
}

finalizersIdx++
finKey := finalizersIdx
finalizers[finKey] = finalizer

in := (*_interface)(unsafe.Pointer(&obj))
Expand Down Expand Up @@ -47,7 +57,7 @@ func onFinalizer(obj unsafe.Pointer, data unsafe.Pointer) {
case func(interface{}):
f(in)
default:
panic("currently only finalizers of the form func(interface{}) are supported")
panic("BUG: SetFinalizer should have filtered out non-supported finalizer interface")
}
}

Expand All @@ -58,5 +68,5 @@ type _interface struct {

type registeredFinalizer struct {
typecode uintptr
finKey uintptr
finKey uint64
}

0 comments on commit b5efc65

Please sign in to comment.