-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmap.go
105 lines (91 loc) · 2.17 KB
/
map.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
package gates
import (
"math"
"reflect"
"sort"
)
type Map map[string]Value
type mapIter struct {
m Map
i int
keys []string
}
func (Map) Type() string { return "map" }
func (Map) IsString() bool { return false }
func (Map) IsInt() bool { return false }
func (Map) IsFloat() bool { return false }
func (Map) IsBool() bool { return false }
func (Map) IsFunction() bool { return false }
func (Map) ToString() string { return "[object Map]" }
func (Map) ToInt() int64 { return 0 }
func (Map) ToFloat() float64 { return math.NaN() }
func (m Map) ToNumber() Number { return Float(m.ToFloat()) }
func (Map) ToBool() bool { return true }
func (Map) ToFunction() Function { return _EmptyFunction }
func (m Map) ToNative(ops ...ToNativeOption) interface{} {
return toNative(nil, m, convertToNativeOption2BinaryOptions(ops))
}
func (m Map) toNative(seen map[interface{}]interface{}, options int) interface{} {
if m == nil {
return map[string]interface{}(nil)
}
v := reflect.ValueOf(m)
ptr := v.Pointer()
if v, ok := seen[ptr]; ok && !checkToNativeOption(SkipCircularReference, options) {
return v
} else if ok {
return nil
}
result := make(map[string]interface{}, len(m))
seen[ptr] = result
for k, v := range m {
result[k] = toNative(seen, v, options)
}
delete(seen, ptr)
return result
}
func (m Map) Equals(other Value) bool {
o, ok := other.(Map)
if !ok {
return false
}
return reflect.DeepEqual(m, o)
}
func (m Map) SameAs(other Value) bool { return false }
func (m Map) Get(r *Runtime, key Value) Value {
if m == nil {
return Null
}
return r.ToValue(m[key.ToString()])
}
func (m Map) Set(r *Runtime, key, value Value) {
if m == nil {
return
}
m[key.ToString()] = value
}
func (m Map) Iterator() Iterator {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return &mapIter{m: m, i: 0, keys: keys}
}
func (m *mapIter) Next() (Value, bool) {
SkipEmpty:
i := m.i
if i >= 0 && i < len(m.keys) {
m.i++
k := m.keys[i]
if _, ok := m.m[k]; !ok {
goto SkipEmpty
}
v := m.m[k]
return Map(map[string]Value{
"key": String(k),
"value": v,
}), true
}
return Null, false
}