Repository navigation
Expand file tree
/
Copy pathcppgc_persistent.go
More file actions
440 lines (406 loc) · 14.5 KB
/
Copy pathcppgc_persistent.go
File metadata and controls
440 lines (406 loc) · 14.5 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//go:build windows && amd64
package gov8
import (
"fmt"
"sync"
"unsafe"
)
// CppGCObjectSnapshot is copied metadata for an object reached through a
// cppgc persistent handle. It contains no native pointer and remains safe to
// inspect after the managed object is collected.
type CppGCObjectSnapshot struct {
ObjectID int32
Tag CppGCTag
}
type cppgcPersistentHandle struct {
mu sync.Mutex
iso *Isolate
handle uintptr
weak bool
closed bool
}
var cppgcPersistentLifecycle = struct {
sync.Mutex
isolates map[*Isolate]map[*cppgcPersistentHandle]struct{}
}{isolates: make(map[*Isolate]map[*cppgcPersistentHandle]struct{})}
func registerCppGCPersistentLifecycle(handle *cppgcPersistentHandle) {
cppgcPersistentLifecycle.Lock()
handles := cppgcPersistentLifecycle.isolates[handle.iso]
if handles == nil {
handles = make(map[*cppgcPersistentHandle]struct{})
cppgcPersistentLifecycle.isolates[handle.iso] = handles
}
handles[handle] = struct{}{}
cppgcPersistentLifecycle.Unlock()
}
func unregisterCppGCPersistentLifecycle(handle *cppgcPersistentHandle) {
cppgcPersistentLifecycle.Lock()
handles := cppgcPersistentLifecycle.isolates[handle.iso]
delete(handles, handle)
if len(handles) == 0 {
delete(cppgcPersistentLifecycle.isolates, handle.iso)
}
cppgcPersistentLifecycle.Unlock()
}
// afterCppGCPersistentIsolateDispose destroys native handle wrappers after V8
// has disposed the isolate's cppgc heap. The pinned cppgc persistent nodes are
// cleared by heap teardown, so their later same-thread destructors do not
// revisit or redestroy managed targets. disposedHandle is used only as the
// opaque owner identity recorded in each wrapper.
func afterCppGCPersistentIsolateDispose(iso *Isolate, disposedHandle uintptr) error {
cppgcPersistentLifecycle.Lock()
registered := cppgcPersistentLifecycle.isolates[iso]
delete(cppgcPersistentLifecycle.isolates, iso)
handles := make([]*cppgcPersistentHandle, 0, len(registered))
for handle := range registered {
handles = append(handles, handle)
}
cppgcPersistentLifecycle.Unlock()
var firstErr error
for _, handle := range handles {
handle.mu.Lock()
if !handle.closed && handle.handle != 0 {
r1, _, _ := proc("gov8_cppgc_persistent_close_after_isolate_dispose").Call(
handle.handle, disposedHandle)
if int64(r1) < 0 && firstErr == nil {
firstErr = shimError("CppGCPersistent.IsolateDispose", r1)
}
handle.handle = 0
handle.closed = true
}
handle.mu.Unlock()
}
return firstErr
}
func newEmptyCppGCPersistent(iso *Isolate, weak bool) (*cppgcPersistentHandle, error) {
if iso == nil {
return nil, fmt.Errorf("gov8: nil isolate")
}
ih, err := iso.handleChecked()
if err != nil {
return nil, err
}
var handle uintptr
r1, _, _ := proc("gov8_cppgc_persistent_empty").Call(
ih, boolWord(weak), uintptr(unsafe.Pointer(&handle)))
if int64(r1) < 0 {
return nil, shimError("CppGCPersistent.Empty", r1)
}
if handle == 0 {
return nil, fmt.Errorf("gov8: cppgc persistent constructor returned null")
}
persistent := &cppgcPersistentHandle{iso: iso, handle: handle, weak: weak}
registerCppGCPersistentLifecycle(persistent)
return persistent, nil
}
func cppgcObjectPersistentInputs(object *CppGCObject) (iso *Isolate, scopeHandle, wrapperHandle uintptr, tag CppGCTag, registryID uint64, err error) {
if err = object.check(); err != nil {
return nil, 0, 0, 0, 0, err
}
scope := object.wrapper.sc
if scope == nil {
return nil, 0, 0, 0, 0, fmt.Errorf("gov8: cppgc object has no live wrapper scope")
}
if err = scope.requireCurrent(); err != nil {
return nil, 0, 0, 0, 0, err
}
scopeHandle, err = scope.checkedHandle()
if err != nil {
return nil, 0, 0, 0, 0, err
}
return object.iso, scopeHandle, object.wrapper.h, object.tag, object.registryID, nil
}
func newCppGCPersistentFromObject(object *CppGCObject, weak bool) (*cppgcPersistentHandle, error) {
iso, scopeHandle, wrapperHandle, tag, registryID, err := cppgcObjectPersistentInputs(object)
if err != nil {
return nil, err
}
ih, err := iso.handleChecked()
if err != nil {
return nil, err
}
var handle uintptr
r1, _, _ := proc("gov8_cppgc_persistent_from_wrapper").Call(
ih, scopeHandle, wrapperHandle, uintptr(tag), uintptr(registryID),
boolWord(weak), uintptr(unsafe.Pointer(&handle)))
if int64(r1) < 0 {
return nil, shimError("CppGCPersistent.New", r1)
}
if handle == 0 {
return nil, fmt.Errorf("gov8: cppgc persistent constructor returned null")
}
persistent := &cppgcPersistentHandle{iso: iso, handle: handle, weak: weak}
registerCppGCPersistentLifecycle(persistent)
return persistent, nil
}
func (persistent *cppgcPersistentHandle) set(object *CppGCObject) error {
if persistent == nil {
return fmt.Errorf("gov8: nil cppgc persistent handle")
}
if err := persistent.iso.check(); err != nil {
return err
}
iso, scopeHandle, wrapperHandle, tag, registryID, err := cppgcObjectPersistentInputs(object)
if err != nil {
return err
}
if iso != persistent.iso {
return foreignIsolate("cppgc object")
}
persistent.mu.Lock()
defer persistent.mu.Unlock()
if persistent.closed || persistent.handle == 0 {
return fmt.Errorf("gov8: cppgc persistent handle used after Close")
}
r1, _, _ := proc("gov8_cppgc_persistent_set").Call(
persistent.handle, persistent.iso.handleAssumingCheck(), scopeHandle,
wrapperHandle, uintptr(tag), uintptr(registryID))
if int64(r1) < 0 {
return shimError("CppGCPersistent.Set", r1)
}
return nil
}
func (persistent *cppgcPersistentHandle) setFromPersistent(source *cppgcPersistentHandle) error {
if persistent == nil || source == nil {
return fmt.Errorf("gov8: nil cppgc persistent handle")
}
if persistent.iso != source.iso {
return foreignIsolate("cppgc persistent source")
}
if err := persistent.iso.check(); err != nil {
return err
}
if persistent == source {
persistent.mu.Lock()
defer persistent.mu.Unlock()
if persistent.closed || persistent.handle == 0 {
return fmt.Errorf("gov8: cppgc persistent handle used after Close")
}
return nil
}
persistent.mu.Lock()
defer persistent.mu.Unlock()
source.mu.Lock()
defer source.mu.Unlock()
if persistent.closed || persistent.handle == 0 {
return fmt.Errorf("gov8: cppgc persistent handle used after Close")
}
if source.closed || source.handle == 0 {
return fmt.Errorf("gov8: cppgc persistent source used after Close")
}
r1, _, _ := proc("gov8_cppgc_persistent_set_from_persistent").Call(
persistent.handle, source.handle, persistent.iso.handleAssumingCheck())
if int64(r1) < 0 {
return shimError("CppGCPersistent.SetFromPersistent", r1)
}
return nil
}
func (persistent *cppgcPersistentHandle) get() (snapshot CppGCObjectSnapshot, registryID uint64, ok bool, err error) {
if persistent == nil {
return snapshot, 0, false, fmt.Errorf("gov8: nil cppgc persistent handle")
}
if err := persistent.iso.check(); err != nil {
return snapshot, 0, false, err
}
persistent.mu.Lock()
defer persistent.mu.Unlock()
if persistent.closed || persistent.handle == 0 {
return snapshot, 0, false, fmt.Errorf("gov8: cppgc persistent handle used after Close")
}
var objectID int32
var tag uint16
var present int32
r1, _, _ := proc("gov8_cppgc_persistent_get").Call(
persistent.handle, persistent.iso.handleAssumingCheck(),
uintptr(unsafe.Pointer(®istryID)), uintptr(unsafe.Pointer(&objectID)),
uintptr(unsafe.Pointer(&tag)), uintptr(unsafe.Pointer(&present)))
if int64(r1) < 0 {
return snapshot, 0, false, shimError("CppGCPersistent.Get", r1)
}
if present == 0 {
return snapshot, 0, false, nil
}
if registryID == 0 || !liveCppGCRegistration(registryID, persistent.iso) {
return snapshot, 0, false, fmt.Errorf("gov8: cppgc persistent returned stale metadata")
}
return CppGCObjectSnapshot{ObjectID: objectID, Tag: CppGCTag(tag)}, registryID, true, nil
}
func (persistent *cppgcPersistentHandle) matches(object *CppGCObject) (bool, error) {
if err := object.check(); err != nil {
return false, err
}
if persistent == nil || persistent.iso != object.iso {
if persistent == nil {
return false, fmt.Errorf("gov8: nil cppgc persistent handle")
}
return false, foreignIsolate("cppgc object")
}
_, registryID, ok, err := persistent.get()
return ok && registryID == object.registryID, err
}
func (persistent *cppgcPersistentHandle) close() error {
if persistent == nil {
return nil
}
if persistent.iso == nil {
return nil
}
if err := persistent.iso.check(); err != nil {
// Isolate teardown marks every registered wrapper closed before clearing
// the isolate handle. A later Close is therefore a safe idempotent no-op.
persistent.mu.Lock()
closed := persistent.closed
persistent.mu.Unlock()
if closed {
return nil
}
return err
}
persistent.mu.Lock()
defer persistent.mu.Unlock()
if persistent.closed {
return nil
}
r1, _, _ := proc("gov8_cppgc_persistent_close").Call(
persistent.handle, persistent.iso.handleAssumingCheck())
if int64(r1) < 0 {
return shimError("CppGCPersistent.Close", r1)
}
persistent.handle = 0
persistent.closed = true
unregisterCppGCPersistentLifecycle(persistent)
return nil
}
// CppGCPersistent is a strong, native-owned cppgc root. It keeps its managed
// object alive independently of the JavaScript API wrapper. Operations are
// owner-thread-only. Isolate teardown releases an outstanding native handle;
// later Close remains idempotent, while Get and Set report the closed isolate.
type CppGCPersistent struct{ persistent *cppgcPersistentHandle }
// NewEmptyCppGCPersistent creates an empty strong cppgc root.
func NewEmptyCppGCPersistent(iso *Isolate) (*CppGCPersistent, error) {
persistent, err := newEmptyCppGCPersistent(iso, false)
if err != nil {
return nil, err
}
return &CppGCPersistent{persistent: persistent}, nil
}
// NewCppGCPersistent creates a strong cppgc root initialized from object.
func NewCppGCPersistent(object *CppGCObject) (*CppGCPersistent, error) {
persistent, err := newCppGCPersistentFromObject(object, false)
if err != nil {
return nil, err
}
return &CppGCPersistent{persistent: persistent}, nil
}
// Set changes the managed object rooted by persistent.
func (persistent *CppGCPersistent) Set(object *CppGCObject) error {
if persistent == nil {
return fmt.Errorf("gov8: nil cppgc persistent")
}
return persistent.persistent.set(object)
}
// SetFromPersistent changes the rooted object to the object currently held by
// source. An empty source clears persistent.
func (persistent *CppGCPersistent) SetFromPersistent(source *CppGCPersistent) error {
if persistent == nil || source == nil {
return fmt.Errorf("gov8: nil cppgc persistent")
}
return persistent.persistent.setFromPersistent(source.persistent)
}
// SetFromWeakPersistent changes the rooted object to the object currently held
// by source. An empty source clears persistent.
func (persistent *CppGCPersistent) SetFromWeakPersistent(source *CppGCWeakPersistent) error {
if persistent == nil || source == nil {
return fmt.Errorf("gov8: nil cppgc persistent")
}
return persistent.persistent.setFromPersistent(source.persistent)
}
// Get returns copied managed-object metadata, or ok=false when empty.
func (persistent *CppGCPersistent) Get() (CppGCObjectSnapshot, bool, error) {
if persistent == nil {
return CppGCObjectSnapshot{}, false, fmt.Errorf("gov8: nil cppgc persistent")
}
snapshot, _, ok, err := persistent.persistent.get()
return snapshot, ok, err
}
// Matches reports whether persistent currently points to object.
func (persistent *CppGCPersistent) Matches(object *CppGCObject) (bool, error) {
if persistent == nil {
return false, fmt.Errorf("gov8: nil cppgc persistent")
}
return persistent.persistent.matches(object)
}
// Close releases the strong root. Close is idempotent.
func (persistent *CppGCPersistent) Close() error {
if persistent == nil {
return nil
}
return persistent.persistent.close()
}
// CppGCWeakPersistent is a weak, native-owned cppgc handle. It does not keep
// its managed object alive and becomes empty when that object is collected.
// Operations are owner-thread-only. Isolate teardown releases an outstanding
// native handle; later Close remains idempotent.
type CppGCWeakPersistent struct{ persistent *cppgcPersistentHandle }
// NewEmptyCppGCWeakPersistent creates an empty weak cppgc handle.
func NewEmptyCppGCWeakPersistent(iso *Isolate) (*CppGCWeakPersistent, error) {
persistent, err := newEmptyCppGCPersistent(iso, true)
if err != nil {
return nil, err
}
return &CppGCWeakPersistent{persistent: persistent}, nil
}
// NewCppGCWeakPersistent creates a weak cppgc handle initialized from object.
func NewCppGCWeakPersistent(object *CppGCObject) (*CppGCWeakPersistent, error) {
persistent, err := newCppGCPersistentFromObject(object, true)
if err != nil {
return nil, err
}
return &CppGCWeakPersistent{persistent: persistent}, nil
}
// Set changes the managed object observed by persistent.
func (persistent *CppGCWeakPersistent) Set(object *CppGCObject) error {
if persistent == nil {
return fmt.Errorf("gov8: nil cppgc weak persistent")
}
return persistent.persistent.set(object)
}
// SetFromPersistent changes the observed object to the object currently held
// by source. An empty source clears persistent.
func (persistent *CppGCWeakPersistent) SetFromPersistent(source *CppGCPersistent) error {
if persistent == nil || source == nil {
return fmt.Errorf("gov8: nil cppgc weak persistent")
}
return persistent.persistent.setFromPersistent(source.persistent)
}
// SetFromWeakPersistent changes the observed object to the object currently
// held by source. An empty source clears persistent.
func (persistent *CppGCWeakPersistent) SetFromWeakPersistent(source *CppGCWeakPersistent) error {
if persistent == nil || source == nil {
return fmt.Errorf("gov8: nil cppgc weak persistent")
}
return persistent.persistent.setFromPersistent(source.persistent)
}
// Get returns copied managed-object metadata, or ok=false when empty or after
// the weakly referenced object has been collected.
func (persistent *CppGCWeakPersistent) Get() (CppGCObjectSnapshot, bool, error) {
if persistent == nil {
return CppGCObjectSnapshot{}, false, fmt.Errorf("gov8: nil cppgc weak persistent")
}
snapshot, _, ok, err := persistent.persistent.get()
return snapshot, ok, err
}
// Matches reports whether persistent currently points to object.
func (persistent *CppGCWeakPersistent) Matches(object *CppGCObject) (bool, error) {
if persistent == nil {
return false, fmt.Errorf("gov8: nil cppgc weak persistent")
}
return persistent.persistent.matches(object)
}
// Close releases the weak handle. Close is idempotent.
func (persistent *CppGCWeakPersistent) Close() error {
if persistent == nil {
return nil
}
return persistent.persistent.close()
}