forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_weakmap.go
202 lines (177 loc) · 5.06 KB
/
builtin_weakmap.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
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
package goja
type weakMap struct {
data map[uint64]Value
}
type weakMapObject struct {
baseObject
m *weakMap
}
func newWeakMap() *weakMap {
return &weakMap{
data: make(map[uint64]Value),
}
}
func (wmo *weakMapObject) init() {
wmo.baseObject.init()
wmo.m = newWeakMap()
}
func (wm *weakMap) removeId(id uint64) {
delete(wm.data, id)
}
func (wm *weakMap) set(key *Object, value Value) {
ref := key.getWeakRef()
wm.data[ref.id] = value
key.runtime.addWeakKey(ref.id, wm)
}
func (wm *weakMap) get(key *Object) Value {
ref := key.weakRef
if ref == nil {
return nil
}
ret := wm.data[ref.id]
return ret
}
func (wm *weakMap) remove(key *Object) bool {
ref := key.weakRef
if ref == nil {
return false
}
_, exists := wm.data[ref.id]
if exists {
delete(wm.data, ref.id)
key.runtime.removeWeakKey(ref.id, wm)
}
return exists
}
func (wm *weakMap) has(key *Object) bool {
ref := key.weakRef
if ref == nil {
return false
}
_, exists := wm.data[ref.id]
return exists
}
func (r *Runtime) weakMapProto_delete(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wmo, ok := thisObj.self.(*weakMapObject)
if !ok {
panic(r.NewTypeError("Method WeakMap.prototype.delete called on incompatible receiver %s", thisObj.String()))
}
key, ok := call.Argument(0).(*Object)
if ok && wmo.m.remove(key) {
return valueTrue
}
return valueFalse
}
func (r *Runtime) weakMapProto_get(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wmo, ok := thisObj.self.(*weakMapObject)
if !ok {
panic(r.NewTypeError("Method WeakMap.prototype.get called on incompatible receiver %s", thisObj.String()))
}
var res Value
if key, ok := call.Argument(0).(*Object); ok {
res = wmo.m.get(key)
}
if res == nil {
return _undefined
}
return res
}
func (r *Runtime) weakMapProto_has(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wmo, ok := thisObj.self.(*weakMapObject)
if !ok {
panic(r.NewTypeError("Method WeakMap.prototype.has called on incompatible receiver %s", thisObj.String()))
}
key, ok := call.Argument(0).(*Object)
if ok && wmo.m.has(key) {
return valueTrue
}
return valueFalse
}
func (r *Runtime) weakMapProto_set(call FunctionCall) Value {
thisObj := r.toObject(call.This)
wmo, ok := thisObj.self.(*weakMapObject)
if !ok {
panic(r.NewTypeError("Method WeakMap.prototype.set called on incompatible receiver %s", thisObj.String()))
}
key := r.toObject(call.Argument(0))
wmo.m.set(key, call.Argument(1))
return call.This
}
func (r *Runtime) needNew(name string) *Object {
return r.NewTypeError("Constructor %s requires 'new'", name)
}
func (r *Runtime) getPrototypeFromCtor(newTarget, defCtor, defProto *Object) *Object {
if newTarget == defCtor {
return defProto
}
proto := newTarget.self.getStr("prototype", nil)
if obj, ok := proto.(*Object); ok {
return obj
}
return defProto
}
func (r *Runtime) builtin_newWeakMap(args []Value, newTarget *Object) *Object {
if newTarget == nil {
panic(r.needNew("WeakMap"))
}
proto := r.getPrototypeFromCtor(newTarget, r.global.WeakMap, r.global.WeakMapPrototype)
o := &Object{runtime: r}
wmo := &weakMapObject{}
wmo.class = classWeakMap
wmo.val = o
wmo.extensible = true
o.self = wmo
wmo.prototype = proto
wmo.init()
if len(args) > 0 {
if arg := args[0]; arg != nil && arg != _undefined && arg != _null {
adder := wmo.getStr("set", nil)
iter := r.getIterator(arg, nil)
i0 := valueInt(0)
i1 := valueInt(1)
if adder == r.global.weakMapAdder {
r.iterate(iter, func(item Value) {
itemObj := r.toObject(item)
k := itemObj.self.getIdx(i0, nil)
v := nilSafe(itemObj.self.getIdx(i1, nil))
wmo.m.set(r.toObject(k), v)
})
} else {
adderFn := toMethod(adder)
if adderFn == nil {
panic(r.NewTypeError("WeakMap.set in missing"))
}
r.iterate(iter, func(item Value) {
itemObj := r.toObject(item)
k := itemObj.self.getIdx(i0, nil)
v := itemObj.self.getIdx(i1, nil)
adderFn(FunctionCall{This: o, Arguments: []Value{k, v}})
})
}
}
}
return o
}
func (r *Runtime) createWeakMapProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
o._putProp("constructor", r.global.WeakMap, true, false, true)
r.global.weakMapAdder = r.newNativeFunc(r.weakMapProto_set, nil, "set", nil, 2)
o._putProp("set", r.global.weakMapAdder, true, false, true)
o._putProp("delete", r.newNativeFunc(r.weakMapProto_delete, nil, "delete", nil, 1), true, false, true)
o._putProp("has", r.newNativeFunc(r.weakMapProto_has, nil, "has", nil, 1), true, false, true)
o._putProp("get", r.newNativeFunc(r.weakMapProto_get, nil, "get", nil, 1), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classWeakMap), false, false, true))
return o
}
func (r *Runtime) createWeakMap(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newWeakMap, r.global.WeakMapPrototype, "WeakMap", 0)
return o
}
func (r *Runtime) initWeakMap() {
r.global.WeakMapPrototype = r.newLazyObject(r.createWeakMapProto)
r.global.WeakMap = r.newLazyObject(r.createWeakMap)
r.addToGlobal("WeakMap", r.global.WeakMap)
}