forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
external.go
101 lines (86 loc) · 2.27 KB
/
external.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
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
package v8go
// #include <stdlib.h>
// #include "v8go.h"
import "C"
import (
"errors"
"sync"
"unsafe"
)
// ExternalStore is a store for external values.
type ExternalStore struct {
counter uintptr
store sync.Map
lock sync.Mutex
}
var externals = NewExternalStore()
// NewExternalStore creates a new ExternalStore.
func NewExternalStore() *ExternalStore {
return &ExternalStore{
counter: 0,
store: sync.Map{},
lock: sync.Mutex{},
}
}
// Get returns a value from the store.
func (s *ExternalStore) Get(key uintptr) (interface{}, bool) {
return s.store.Load(key)
}
// Add adds a value to the store. It returns a key that can be used to retrieve the value.
func (s *ExternalStore) Add(value interface{}) uintptr {
s.lock.Lock()
defer s.lock.Unlock()
s.counter++
s.store.Store(s.counter, value)
return s.counter
}
// Remove removes a value from the store.
func (s *ExternalStore) Remove(key uintptr) {
s.store.Delete(key)
}
// ExternalCount returns the number of external values in the store.
func ExternalCount() int {
count := 0
externals.store.Range(func(_, _ interface{}) bool {
count++
return true
})
return count
}
// NewExternal creates a new external value.
func NewExternal(iso *Isolate, val interface{}) (*Value, error) {
ptr := externals.Add(val)
rtnVal := &Value{
ptr: C.NewValueExternal(iso.ptr, unsafe.Pointer(ptr)),
}
return rtnVal, nil
}
// External returns the external value.
// then an error is returned. Use `value.Object()` to do the JS equivalent of `Object(value)`.
func (v *Value) External() (interface{}, error) {
if !v.IsYaoExternal() {
return nil, errors.New("v8go: value is not an External")
}
rtnValue := C.ValueToExternal(v.ptr)
if rtnValue == 0 {
return nil, errors.New("v8go: failed to get external value")
}
value, ok := externals.Get(uintptr(rtnValue))
if !ok {
return nil, errors.New("v8go: failed to get external value, not found")
}
return value, nil
}
// IsYaoExternal returns true if the value is an external value.
func (v *Value) IsYaoExternal() bool {
return C.ValueIsExternal(v.ptr) != 0
}
// ReleaseExternal releases the external value.
func (v *Value) ReleaseExternal() {
if v.IsYaoExternal() {
rtnValue := C.ValueToExternal(v.ptr)
if rtnValue != 0 {
externals.Remove(uintptr(rtnValue))
}
}
}