forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_go_slice.go
126 lines (109 loc) · 2.89 KB
/
type_go_slice.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
package otto
import (
"reflect"
"strconv"
)
func (runtime *_runtime) newGoSliceObject(value reflect.Value) *_object {
self := runtime.newObject()
self.class = "GoArray" // TODO GoSlice?
self.objectClass = _classGoSlice
self.value = _newGoSliceObject(value)
return self
}
type _goSliceObject struct {
value reflect.Value
}
func _newGoSliceObject(value reflect.Value) *_goSliceObject {
self := &_goSliceObject{
value: value,
}
return self
}
func (self _goSliceObject) getValue(index int64) (reflect.Value, bool) {
if index < int64(self.value.Len()) {
return self.value.Index(int(index)), true
}
return reflect.Value{}, false
}
func (self _goSliceObject) setValue(index int64, value Value) bool {
indexValue, exists := self.getValue(index)
if !exists {
return false
}
reflectValue, err := value.toReflectValue(self.value.Type().Elem().Kind())
if err != nil {
panic(err)
}
indexValue.Set(reflectValue)
return true
}
func goSliceGetOwnProperty(self *_object, name string) *_property {
// length
if name == "length" {
return &_property{
value: toValue(self.value.(*_goSliceObject).value.Len()),
mode: 0,
}
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 {
value := Value{}
reflectValue, exists := self.value.(*_goSliceObject).getValue(index)
if exists {
value = self.runtime.toValue(reflectValue.Interface())
}
return &_property{
value: value,
mode: 0110,
}
}
// Other methods
if method := self.value.(*_goSliceObject).value.MethodByName(name); (method != reflect.Value{}) {
return &_property{
value: self.runtime.toValue(method.Interface()),
mode: 0110,
}
}
return objectGetOwnProperty(self, name)
}
func goSliceEnumerate(self *_object, all bool, each func(string) bool) {
object := self.value.(*_goSliceObject)
// .0, .1, .2, ...
for index, length := 0, object.value.Len(); index < length; index++ {
name := strconv.FormatInt(int64(index), 10)
if !each(name) {
return
}
}
objectEnumerate(self, all, each)
}
func goSliceDefineOwnProperty(self *_object, name string, descriptor _property, throw bool) bool {
if name == "length" {
return self.runtime.typeErrorResult(throw)
} else if index := stringToArrayIndex(name); index >= 0 {
if self.value.(*_goSliceObject).setValue(index, descriptor.value.(Value)) {
return true
}
return self.runtime.typeErrorResult(throw)
}
return objectDefineOwnProperty(self, name, descriptor, throw)
}
func goSliceDelete(self *_object, name string, throw bool) bool {
// length
if name == "length" {
return self.runtime.typeErrorResult(throw)
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 {
object := self.value.(*_goSliceObject)
indexValue, exists := object.getValue(index)
if exists {
indexValue.Set(reflect.Zero(object.value.Type().Elem()))
return true
}
return self.runtime.typeErrorResult(throw)
}
return self.delete(name, throw)
}