This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
finalizer.go
70 lines (54 loc) · 1.74 KB
/
finalizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
//go:build gc.custom
package nottinygc
/*
void GC_register_finalizer(void* obj, void* fn, void* cd, void** ofn, void** ocn);
void onFinalizer(void* obj, void* fn);
*/
import "C"
import "unsafe"
var finalizers = map[uint64]interface{}{}
var finalizersIdx = uint64(0)
//go:linkname SetFinalizer runtime.SetFinalizer
func SetFinalizer(obj interface{}, finalizer interface{}) {
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))
rf := (*registeredFinalizer)(cmalloc(unsafe.Sizeof(registeredFinalizer{})))
rf.typecode = in.typecode
rf.finKey = finKey
C.GC_register_finalizer(in.value, C.onFinalizer, unsafe.Pointer(rf), nil, nil)
}
//export onFinalizer
func onFinalizer(obj unsafe.Pointer, data unsafe.Pointer) {
defer cfree(data)
rf := (*registeredFinalizer)(data)
finalizer := finalizers[rf.finKey]
delete(finalizers, rf.finKey)
var in interface{}
inFields := (*_interface)(unsafe.Pointer(&in))
inFields.typecode = rf.typecode
inFields.value = obj
switch f := finalizer.(type) {
case func(interface{}):
f(in)
default:
panic("BUG: SetFinalizer should have filtered out non-supported finalizer interface")
}
}
type _interface struct {
typecode uintptr
value unsafe.Pointer
}
type registeredFinalizer struct {
typecode uintptr
finKey uint64
}