forked from dop251/goja
/
builtin_weakset.go
158 lines (136 loc) · 3.93 KB
/
builtin_weakset.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
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
package goja
type weakSet struct {
data map[uint64]struct{}
}
type weakSetObject struct {
baseObject
s *weakSet
}
func newWeakSet() *weakSet {
return &weakSet{
data: make(map[uint64]struct{}),
}
}
func (ws *weakSetObject) init() {
ws.baseObject.init()
ws.s = newWeakSet()
}
func (ws *weakSet) removeId(id uint64) {
delete(ws.data, id)
}
func (ws *weakSet) add(o *Object) {
ref := o.getWeakRef()
ws.data[ref.id] = struct{}{}
o.runtime.addWeakKey(ref.id, ws)
}
func (ws *weakSet) remove(o *Object) bool {
ref := o.weakRef
if ref == nil {
return false
}
_, exists := ws.data[ref.id]
if exists {
delete(ws.data, ref.id)
o.runtime.removeWeakKey(ref.id, ws)
}
return exists
}
func (ws *weakSet) has(o *Object) bool {
ref := o.weakRef
if ref == nil {
return false
}
_, exists := ws.data[ref.id]
return exists
}
func (r *Runtime) weakSetProto_add(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wso, ok := thisObj.self.(*weakSetObject)
if !ok {
panic(r.NewTypeError("Method WeakSet.prototype.add called on incompatible receiver %s", thisObj.String()))
}
wso.s.add(r.toObject(call.Argument(0)))
return call.This
}
func (r *Runtime) weakSetProto_delete(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wso, ok := thisObj.self.(*weakSetObject)
if !ok {
panic(r.NewTypeError("Method WeakSet.prototype.delete called on incompatible receiver %s", thisObj.String()))
}
obj, ok := call.Argument(0).(*Object)
if ok && wso.s.remove(obj) {
return valueTrue
}
return valueFalse
}
func (r *Runtime) weakSetProto_has(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wso, ok := thisObj.self.(*weakSetObject)
if !ok {
panic(r.NewTypeError("Method WeakSet.prototype.has called on incompatible receiver %s", thisObj.String()))
}
obj, ok := call.Argument(0).(*Object)
if ok && wso.s.has(obj) {
return valueTrue
}
return valueFalse
}
func (r *Runtime) populateWeakSetGeneric(s *Object, adderValue Value, iterable Value) {
adder := toMethod(adderValue)
if adder == nil {
panic(r.NewTypeError("WeakSet.add is not set"))
}
iter := r.getIterator(iterable, nil)
r.iterate(iter, func(val Value) {
adder(FunctionCall{This: s, Arguments: []Value{val}})
})
}
func (r *Runtime) builtin_newWeakSet(args []Value, newTarget *Object) *Object {
if newTarget == nil {
panic(r.needNew("WeakSet"))
}
proto := r.getPrototypeFromCtor(newTarget, r.global.WeakSet, r.global.WeakSetPrototype)
o := &Object{runtime: r}
wso := &weakSetObject{}
wso.class = classWeakSet
wso.val = o
wso.extensible = true
o.self = wso
wso.prototype = proto
wso.init()
if len(args) > 0 {
if arg := args[0]; arg != nil && arg != _undefined && arg != _null {
adder := wso.getStr("add", nil)
if adder == r.global.weakSetAdder {
if arr := r.checkStdArrayIter(arg); arr != nil {
for _, v := range arr.values {
wso.s.add(r.toObject(v))
}
return o
}
}
r.populateWeakSetGeneric(o, adder, arg)
}
}
return o
}
func (r *Runtime) createWeakSetProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
o._putProp("constructor", r.global.WeakSet, true, false, true)
r.global.weakSetAdder = r.newNativeFunc(r.weakSetProto_add, nil, "add", nil, 1)
o._putProp("add", r.global.weakSetAdder, true, false, true)
o._putProp("delete", r.newNativeFunc(r.weakSetProto_delete, nil, "delete", nil, 1), true, false, true)
o._putProp("has", r.newNativeFunc(r.weakSetProto_has, nil, "has", nil, 1), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classWeakSet), false, false, true))
return o
}
func (r *Runtime) createWeakSet(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newWeakSet, r.global.WeakSetPrototype, "WeakSet", 0)
return o
}
func (r *Runtime) initWeakSet() {
r.global.WeakSetPrototype = r.newLazyObject(r.createWeakSetProto)
r.global.WeakSet = r.newLazyObject(r.createWeakSet)
r.addToGlobal("WeakSet", r.global.WeakSet)
}