forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_gomap_reflect.go
203 lines (168 loc) · 4.56 KB
/
object_gomap_reflect.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
203
package goja
import "reflect"
type objectGoMapReflect struct {
objectGoReflect
keyType, valueType reflect.Type
}
func (o *objectGoMapReflect) init() {
o.objectGoReflect.init()
o.keyType = o.value.Type().Key()
o.valueType = o.value.Type().Elem()
}
func (o *objectGoMapReflect) toKey(n Value) reflect.Value {
key, err := o.val.runtime.toReflectValue(n, o.keyType)
if err != nil {
o.val.runtime.typeErrorResult(true, "map key conversion error: %v", err)
panic("unreachable")
}
return key
}
func (o *objectGoMapReflect) strToKey(name string) reflect.Value {
if o.keyType.Kind() == reflect.String {
return reflect.ValueOf(name).Convert(o.keyType)
}
return o.toKey(newStringValue(name))
}
func (o *objectGoMapReflect) _get(n Value) Value {
if v := o.value.MapIndex(o.toKey(n)); v.IsValid() {
return o.val.runtime.ToValue(v.Interface())
}
return nil
}
func (o *objectGoMapReflect) _getStr(name string) Value {
if v := o.value.MapIndex(o.strToKey(name)); v.IsValid() {
return o.val.runtime.ToValue(v.Interface())
}
return nil
}
func (o *objectGoMapReflect) get(n Value) Value {
if v := o._get(n); v != nil {
return v
}
return o.objectGoReflect.get(n)
}
func (o *objectGoMapReflect) getStr(name string) Value {
if v := o._getStr(name); v != nil {
return v
}
return o.objectGoReflect.getStr(name)
}
func (o *objectGoMapReflect) getProp(n Value) Value {
return o.get(n)
}
func (o *objectGoMapReflect) getPropStr(name string) Value {
return o.getStr(name)
}
func (o *objectGoMapReflect) getOwnProp(name string) Value {
if v := o._getStr(name); v != nil {
return &valueProperty{
value: v,
writable: true,
enumerable: true,
}
}
return o.objectGoReflect.getOwnProp(name)
}
func (o *objectGoMapReflect) toValue(val Value, throw bool) (reflect.Value, bool) {
v, err := o.val.runtime.toReflectValue(val, o.valueType)
if err != nil {
o.val.runtime.typeErrorResult(throw, "map value conversion error: %v", err)
return reflect.Value{}, false
}
return v, true
}
func (o *objectGoMapReflect) put(key, val Value, throw bool) {
k := o.toKey(key)
v, ok := o.toValue(val, throw)
if !ok {
return
}
o.value.SetMapIndex(k, v)
}
func (o *objectGoMapReflect) putStr(name string, val Value, throw bool) {
k := o.strToKey(name)
v, ok := o.toValue(val, throw)
if !ok {
return
}
o.value.SetMapIndex(k, v)
}
func (o *objectGoMapReflect) _putProp(name string, value Value, writable, enumerable, configurable bool) Value {
o.putStr(name, value, true)
return value
}
func (o *objectGoMapReflect) defineOwnProperty(n Value, descr propertyDescr, throw bool) bool {
name := n.String()
if !o.val.runtime.checkHostObjectPropertyDescr(name, descr, throw) {
return false
}
o.put(n, descr.Value, throw)
return true
}
func (o *objectGoMapReflect) hasOwnPropertyStr(name string) bool {
return o.value.MapIndex(o.strToKey(name)).IsValid()
}
func (o *objectGoMapReflect) hasOwnProperty(n Value) bool {
return o.value.MapIndex(o.toKey(n)).IsValid()
}
func (o *objectGoMapReflect) hasProperty(n Value) bool {
if o.hasOwnProperty(n) {
return true
}
return o.objectGoReflect.hasProperty(n)
}
func (o *objectGoMapReflect) hasPropertyStr(name string) bool {
if o.hasOwnPropertyStr(name) {
return true
}
return o.objectGoReflect.hasPropertyStr(name)
}
func (o *objectGoMapReflect) delete(n Value, throw bool) bool {
o.value.SetMapIndex(o.toKey(n), reflect.Value{})
return true
}
func (o *objectGoMapReflect) deleteStr(name string, throw bool) bool {
o.value.SetMapIndex(o.strToKey(name), reflect.Value{})
return true
}
type gomapReflectPropIter struct {
o *objectGoMapReflect
keys []reflect.Value
idx int
recursive bool
}
func (i *gomapReflectPropIter) next() (propIterItem, iterNextFunc) {
for i.idx < len(i.keys) {
key := i.keys[i.idx]
v := i.o.value.MapIndex(key)
i.idx++
if v.IsValid() {
return propIterItem{name: key.String(), enumerable: _ENUM_TRUE}, i.next
}
}
if i.recursive {
return i.o.objectGoReflect._enumerate(true)()
}
return propIterItem{}, nil
}
func (o *objectGoMapReflect) _enumerate(recusrive bool) iterNextFunc {
r := &gomapReflectPropIter{
o: o,
keys: o.value.MapKeys(),
recursive: recusrive,
}
return r.next
}
func (o *objectGoMapReflect) enumerate(all, recursive bool) iterNextFunc {
return (&propFilterIter{
wrapped: o._enumerate(recursive),
all: all,
seen: make(map[string]bool),
}).next
}
func (o *objectGoMapReflect) equal(other objectImpl) bool {
if other, ok := other.(*objectGoMapReflect); ok {
return o.value.Interface() == other.value.Interface()
}
return false
}