forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_test.go
133 lines (104 loc) · 2.15 KB
/
array_test.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
package goja
import (
"reflect"
"testing"
)
func TestArray1(t *testing.T) {
r := &Runtime{}
a := r.newArray(nil)
a.setOwnIdx(valueInt(0), asciiString("test"), true)
if l := a.getStr("length", nil).ToInteger(); l != 1 {
t.Fatalf("Unexpected length: %d", l)
}
}
func TestArrayExportProps(t *testing.T) {
vm := New()
arr := vm.NewArray()
err := arr.DefineDataProperty("0", vm.ToValue(true), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
actual := arr.Export()
expected := []interface{}{true}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Expected: %#v, actual: %#v", expected, actual)
}
}
func TestArrayCanonicalIndex(t *testing.T) {
const SCRIPT = `
var a = [];
a["00"] = 1;
a["01"] = 2;
if (a[0] !== undefined) {
throw new Error("a[0]");
}
`
testScript(SCRIPT, _undefined, t)
}
func BenchmarkArrayGetStr(b *testing.B) {
b.StopTimer()
r := New()
v := &Object{runtime: r}
a := &arrayObject{
baseObject: baseObject{
val: v,
extensible: true,
},
}
v.self = a
a.init()
v.setOwn(valueInt(0), asciiString("test"), false)
b.StartTimer()
for i := 0; i < b.N; i++ {
a.getStr("0", nil)
}
}
func BenchmarkArrayGet(b *testing.B) {
b.StopTimer()
r := New()
v := &Object{runtime: r}
a := &arrayObject{
baseObject: baseObject{
val: v,
extensible: true,
},
}
v.self = a
a.init()
var idx Value = valueInt(0)
v.setOwn(idx, asciiString("test"), false)
b.StartTimer()
for i := 0; i < b.N; i++ {
v.get(idx, nil)
}
}
func BenchmarkArrayPut(b *testing.B) {
b.StopTimer()
r := New()
v := &Object{runtime: r}
a := &arrayObject{
baseObject: baseObject{
val: v,
extensible: true,
},
}
v.self = a
a.init()
var idx Value = valueInt(0)
var val Value = asciiString("test")
b.StartTimer()
for i := 0; i < b.N; i++ {
v.setOwn(idx, val, false)
}
}
func BenchmarkArraySetEmpty(b *testing.B) {
r := New()
r.Get("Array").(*Object).Get("prototype").String() // materialise Array.prototype
a := r.NewArray(0, 0)
values := a.self.(*arrayObject).values
b.ResetTimer()
for i := 0; i < b.N; i++ {
values[0] = nil
a.self.setOwnIdx(0, valueTrue, true)
}
}