forked from quickjs-go/quickjs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.go
62 lines (46 loc) · 785 Bytes
/
objects.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
package quickjs
import "sync"
type ObjectId int64
var refs struct {
sync.RWMutex
objs map[ObjectId]interface{}
next ObjectId
}
func init() {
refs.Lock()
defer refs.Unlock()
refs.objs = make(map[ObjectId]interface{})
refs.next = 1000
}
func NewObjectId(obj interface{}) ObjectId {
refs.Lock()
defer refs.Unlock()
id := refs.next
refs.objs[id] = obj
refs.next++
return id
}
func (id ObjectId) Get() (interface{}, bool) {
refs.RLock()
defer refs.RUnlock()
if id.IsNil() {
return nil, false
}
obj, ok := refs.objs[id]
return obj, ok
}
func (id ObjectId) IsNil() bool {
return id == 0
}
func (id *ObjectId) Free() {
refs.Lock()
defer refs.Unlock()
if id.IsNil() {
return
}
_, ok := refs.objs[*id]
if ok {
delete(refs.objs, *id)
}
*id = 0
}