-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_instance.go
More file actions
312 lines (285 loc) · 8.98 KB
/
native_instance.go
File metadata and controls
312 lines (285 loc) · 8.98 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package ramune
import (
"fmt"
"reflect"
"strconv"
"strings"
"sync"
)
// nativeTypeRegistry manages per-type callback registration and per-instance data.
// Callbacks are registered once per type, instances are tracked by ID.
// Instances are automatically released when the JS object is garbage collected
// via FinalizationRegistry (if available). Falls back to clearInstances() on Runtime.Close().
type nativeTypeRegistry struct {
mu sync.RWMutex
types map[reflect.Type]*nativeTypeInfo
instances map[int64]reflect.Value // instance ID → pointer to struct
nextID int64
setupOnce sync.Once
}
type nativeTypeInfo struct {
once sync.Once
prefix string // e.g., "__nt_pkgpath_Counter_"
fields []nativeFieldInfo
methods []nativeMethodInfo
jsTemplate string // cached JS template with __INSTANCE_ID__ placeholder
}
type nativeFieldInfo struct {
index int
jsName string
}
type nativeMethodInfo struct {
index int
jsName string
}
func newNativeTypeRegistry() *nativeTypeRegistry {
return &nativeTypeRegistry{
types: make(map[reflect.Type]*nativeTypeInfo),
instances: make(map[int64]reflect.Value),
}
}
// registerInstance stores a struct pointer and returns its instance ID.
func (reg *nativeTypeRegistry) registerInstance(ptr reflect.Value) int64 {
reg.mu.Lock()
defer reg.mu.Unlock()
reg.nextID++
id := reg.nextID
reg.instances[id] = ptr
return id
}
// getInstance retrieves a struct pointer by instance ID.
func (reg *nativeTypeRegistry) getInstance(id int64) (reflect.Value, bool) {
reg.mu.RLock()
defer reg.mu.RUnlock()
v, ok := reg.instances[id]
return v, ok
}
// releaseInstance removes an instance from the registry.
func (reg *nativeTypeRegistry) releaseInstance(id int64) {
reg.mu.Lock()
defer reg.mu.Unlock()
delete(reg.instances, id)
}
// instanceCount returns the number of live instances.
func (reg *nativeTypeRegistry) instanceCount() int {
reg.mu.RLock()
defer reg.mu.RUnlock()
return len(reg.instances)
}
// clearInstances removes all instances from the registry.
func (reg *nativeTypeRegistry) clearInstances() {
reg.mu.Lock()
defer reg.mu.Unlock()
reg.instances = make(map[int64]reflect.Value)
}
// ensureTypeRegistered registers per-type callbacks if not already done.
func (reg *nativeTypeRegistry) ensureTypeRegistered(r *Runtime, t reflect.Type) *nativeTypeInfo {
reg.mu.Lock()
info, exists := reg.types[t]
if !exists {
// Use PkgPath to avoid collision between same-named types in different packages.
sanitized := strings.NewReplacer("/", "_", ".", "_", "-", "_").Replace(t.PkgPath())
info = &nativeTypeInfo{
prefix: fmt.Sprintf("__nt_%s_%s_", sanitized, t.Name()),
}
reg.types[t] = info
}
reg.mu.Unlock()
info.once.Do(func() {
reg.registerType(r, info, t)
})
return info
}
func (reg *nativeTypeRegistry) registerType(r *Runtime, info *nativeTypeInfo, t reflect.Type) {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
jsName := fieldJSName(field)
info.fields = append(info.fields, nativeFieldInfo{index: i, jsName: jsName})
}
ptrType := reflect.PointerTo(t)
for i := 0; i < ptrType.NumMethod(); i++ {
method := ptrType.Method(i)
if !method.IsExported() {
continue
}
// Skip Go-style getters (`GetFoo() X`). Those are covered by the field
// getter path — exposing both would double-register the same property
// under "foo" and "getFoo". A zero-arg, zero-return mutator (`Reset()`,
// `Increment()` from a TS `void` method) is NOT a getter and must be
// callable from JS, so the old blanket skip on NumIn==1 && NumOut==0
// was dropped.
if strings.HasPrefix(method.Name, "Get") && method.Type.NumIn() == 1 && method.Type.NumOut() == 1 {
continue
}
info.methods = append(info.methods, nativeMethodInfo{
index: i,
jsName: toCamelCase(method.Name),
})
}
for _, f := range info.fields {
fInfo := f
r.registerFuncLocked(info.prefix+"get_"+fInfo.jsName, func(args []any) (any, error) {
if len(args) < 1 {
return nil, nil
}
id, _ := toFloat64(args[0])
inst, ok := reg.getInstance(int64(id))
if !ok {
return nil, nil
}
return reflectToAny(inst.Elem().Field(fInfo.index)), nil
})
r.registerFuncLocked(info.prefix+"set_"+fInfo.jsName, func(args []any) (any, error) {
if len(args) < 2 {
return nil, nil
}
id, _ := toFloat64(args[0])
inst, ok := reg.getInstance(int64(id))
if !ok {
return nil, nil
}
if err := setReflectValue(inst.Elem().Field(fInfo.index), args[1]); err != nil {
return nil, err
}
return nil, nil
})
}
for _, m := range info.methods {
mInfo := m
r.registerFuncLocked(info.prefix+"m_"+mInfo.jsName, func(args []any) (result any, retErr error) {
defer func() {
if rec := recover(); rec != nil {
retErr = fmt.Errorf("panic in %s: %v", mInfo.jsName, rec)
}
}()
if len(args) < 1 {
return nil, nil
}
id, _ := toFloat64(args[0])
inst, ok := reg.getInstance(int64(id))
if !ok {
return nil, fmt.Errorf("instance not found")
}
methodVal := inst.Method(mInfo.index)
mType := methodVal.Type()
methodArgs := args[1:]
in := make([]reflect.Value, mType.NumIn())
for j := 0; j < mType.NumIn(); j++ {
if j < len(methodArgs) {
converted, err := convertArg(methodArgs[j], mType.In(j))
if err != nil {
return nil, fmt.Errorf("%s: arg %d: %w", mInfo.jsName, j, err)
}
in[j] = converted
} else {
in[j] = reflect.Zero(mType.In(j))
}
}
out := methodVal.Call(in)
switch len(out) {
case 0:
return nil, nil
case 1:
v := out[0]
if v.Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
if !v.IsNil() {
return nil, v.Interface().(error)
}
return nil, nil
}
return reflectToAny(v), nil
case 2:
if !out[1].IsNil() {
return nil, out[1].Interface().(error)
}
return reflectToAny(out[0]), nil
default:
return reflectToAny(out[0]), nil
}
})
}
info.buildJSTemplate()
}
const jsInstanceIDPlaceholder = "__INSTANCE_ID__"
func (info *nativeTypeInfo) buildJSTemplate() {
var sb strings.Builder
sb.WriteString("(function(){var o={};var _id=")
sb.WriteString(jsInstanceIDPlaceholder)
sb.WriteString(";")
for _, f := range info.fields {
sb.WriteString("Object.defineProperty(o,\"")
sb.WriteString(f.jsName)
sb.WriteString("\",{get:function(){return ")
sb.WriteString(info.prefix)
sb.WriteString("get_")
sb.WriteString(f.jsName)
sb.WriteString("(_id)},set:function(v){")
sb.WriteString(info.prefix)
sb.WriteString("set_")
sb.WriteString(f.jsName)
sb.WriteString("(_id,v)},enumerable:true,configurable:true});")
}
for _, m := range info.methods {
sb.WriteString("o[\"")
sb.WriteString(m.jsName)
sb.WriteString("\"]=function(){var a=[_id];for(var i=0;i<arguments.length;i++)a.push(arguments[i]);return ")
sb.WriteString(info.prefix)
sb.WriteString("m_")
sb.WriteString(m.jsName)
sb.WriteString(".apply(null,a)};")
}
sb.WriteString("if(globalThis.__nativeInstanceRegistry)globalThis.__nativeInstanceRegistry.register(o,_id);")
sb.WriteString("return o;})()")
info.jsTemplate = sb.String()
}
// ensureNativeReg initializes the native type registry if not already created.
func (r *Runtime) ensureNativeReg() {
if r.nativeReg == nil {
r.nativeReg = newNativeTypeRegistry()
}
}
// installNativeReleaseBridge registers the Go callback and JS FinalizationRegistry
// used to automatically release native instances when their JS wrapper is GC'd.
// Must be called from module Init (not from within a GoFunc callback).
func (r *Runtime) installNativeReleaseBridge() {
r.ensureNativeReg()
reg := r.nativeReg
reg.setupOnce.Do(func() {
if err := r.registerFuncLocked("__nativeRelease", func(args []any) (any, error) {
if len(args) < 1 {
return nil, nil
}
id, _ := toFloat64(args[0])
reg.releaseInstance(int64(id))
return nil, nil
}); err != nil {
return
}
// Install the JS-side FinalizationRegistry hook (backend-specific). On
// backends whose GC doesn't fire FR callbacks synchronously during
// allocation (JSC / goja), FR is wired to __nativeRelease so JS GC
// decrements the Go registry. On qjswasm (fastschema/qjs) the GC is
// aggressive enough that FR would fire during the creation loop
// itself, making NativeInstanceCount impossible to observe; there we
// skip FR and rely on Runtime.Close() / explicit __nativeRelease
// calls. This matches the CLAUDE.md documented behavior
// ("struct instances returned to JS are not freed
// on JS GC").
r.installFinalizationRegistryHook()
})
}
// NativeInstanceCount returns the number of live native struct instances.
// Useful for testing and debugging instance lifecycle.
func (r *Runtime) NativeInstanceCount() int {
if r.nativeReg == nil {
return 0
}
return r.nativeReg.instanceCount()
}
// generateJSObject returns the JS IIFE code for creating an instance object.
func (info *nativeTypeInfo) generateJSObject(instanceID int64) string {
return strings.Replace(info.jsTemplate, jsInstanceIDPlaceholder, strconv.FormatInt(instanceID, 10), 1)
}