-
Notifications
You must be signed in to change notification settings - Fork 586
/
type_array.go
120 lines (112 loc) · 3.15 KB
/
type_array.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
package otto
import (
"strconv"
)
func (rt *runtime) newArrayObject(length uint32) *object {
obj := rt.newObject()
obj.class = classArrayName
obj.defineProperty(propertyLength, uint32Value(length), 0o100, false)
obj.objectClass = classArray
return obj
}
func isArray(obj *object) bool {
if obj == nil {
return false
}
switch obj.class {
case classArrayName, classGoArrayName, classGoSliceName:
return true
default:
return false
}
}
func objectLength(obj *object) uint32 {
if obj == nil {
return 0
}
switch obj.class {
case classArrayName:
return obj.get(propertyLength).value.(uint32)
case classStringName:
return uint32(obj.get(propertyLength).value.(int))
case classGoArrayName, classGoSliceName:
return uint32(obj.get(propertyLength).value.(int))
}
return 0
}
func arrayUint32(rt *runtime, value Value) uint32 {
nm := value.number()
if nm.kind != numberInteger || !isUint32(nm.int64) {
// FIXME
panic(rt.panicRangeError())
}
return uint32(nm.int64)
}
func arrayDefineOwnProperty(obj *object, name string, descriptor property, throw bool) bool {
lengthProperty := obj.getOwnProperty(propertyLength)
lengthValue, valid := lengthProperty.value.(Value)
if !valid {
panic("Array.length != Value{}")
}
reject := func(reason string) bool {
if throw {
panic(obj.runtime.panicTypeError("Array.DefineOwnProperty %s", reason))
}
return false
}
length := lengthValue.value.(uint32)
if name == propertyLength {
if descriptor.value == nil {
return objectDefineOwnProperty(obj, name, descriptor, throw)
}
newLengthValue, isValue := descriptor.value.(Value)
if !isValue {
panic(obj.runtime.panicTypeError("Array.DefineOwnProperty %q is not a value", descriptor.value))
}
newLength := arrayUint32(obj.runtime, newLengthValue)
descriptor.value = uint32Value(newLength)
if newLength > length {
return objectDefineOwnProperty(obj, name, descriptor, throw)
}
if !lengthProperty.writable() {
return reject("property length for not writable")
}
newWritable := true
if descriptor.mode&0o700 == 0 {
// If writable is off
newWritable = false
descriptor.mode |= 0o100
}
if !objectDefineOwnProperty(obj, name, descriptor, throw) {
return false
}
for newLength < length {
length--
if !obj.delete(strconv.FormatInt(int64(length), 10), false) {
descriptor.value = uint32Value(length + 1)
if !newWritable {
descriptor.mode &= 0o077
}
objectDefineOwnProperty(obj, name, descriptor, false)
return reject("delete failed")
}
}
if !newWritable {
descriptor.mode &= 0o077
objectDefineOwnProperty(obj, name, descriptor, false)
}
} else if index := stringToArrayIndex(name); index >= 0 {
if index >= int64(length) && !lengthProperty.writable() {
return reject("property length not writable")
}
if !objectDefineOwnProperty(obj, strconv.FormatInt(index, 10), descriptor, false) {
return reject("Object.DefineOwnProperty failed")
}
if index >= int64(length) {
lengthProperty.value = uint32Value(uint32(index + 1))
objectDefineOwnProperty(obj, propertyLength, *lengthProperty, false)
return true
}
}
return objectDefineOwnProperty(obj, name, descriptor, throw)
}