forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_go_map.go
95 lines (83 loc) · 2.3 KB
/
type_go_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
package otto
import (
"reflect"
)
func (runtime *_runtime) newGoMapObject(value reflect.Value) *_object {
self := runtime.newObject()
self.class = "Object" // TODO Should this be something else?
self.objectClass = _classGoMap
self.value = _newGoMapObject(value)
return self
}
type _goMapObject struct {
value reflect.Value
keyKind reflect.Kind
valueKind reflect.Kind
}
func _newGoMapObject(value reflect.Value) *_goMapObject {
if value.Kind() != reflect.Map {
dbgf("%/panic//%@: %v != reflect.Map", value.Kind())
}
self := &_goMapObject{
value: value,
keyKind: value.Type().Key().Kind(),
valueKind: value.Type().Elem().Kind(),
}
return self
}
func (self _goMapObject) toKey(name string) reflect.Value {
reflectValue, err := stringToReflectValue(name, self.keyKind)
if err != nil {
panic(err)
}
return reflectValue
}
func (self _goMapObject) toValue(value Value) reflect.Value {
reflectValue, err := value.toReflectValue(self.valueKind)
if err != nil {
panic(err)
}
return reflectValue
}
func goMapGetOwnProperty(self *_object, name string) *_property {
object := self.value.(*_goMapObject)
value := object.value.MapIndex(object.toKey(name))
if value.IsValid() {
return &_property{self.runtime.toValue(value.Interface()), 0111}
}
// Other methods
if method := self.value.(*_goMapObject).value.MethodByName(name); (method != reflect.Value{}) {
return &_property{
value: self.runtime.toValue(method.Interface()),
mode: 0110,
}
}
return nil
}
func goMapEnumerate(self *_object, all bool, each func(string) bool) {
object := self.value.(*_goMapObject)
keys := object.value.MapKeys()
for _, key := range keys {
if !each(toValue(key).String()) {
return
}
}
}
func goMapDefineOwnProperty(self *_object, name string, descriptor _property, throw bool) bool {
object := self.value.(*_goMapObject)
// TODO ...or 0222
if descriptor.mode != 0111 {
return self.runtime.typeErrorResult(throw)
}
if !descriptor.isDataDescriptor() {
return self.runtime.typeErrorResult(throw)
}
object.value.SetMapIndex(object.toKey(name), object.toValue(descriptor.value.(Value)))
return true
}
func goMapDelete(self *_object, name string, throw bool) bool {
object := self.value.(*_goMapObject)
object.value.SetMapIndex(object.toKey(name), reflect.Value{})
// FIXME
return true
}