-
Notifications
You must be signed in to change notification settings - Fork 383
/
array_sparse_test.go
264 lines (240 loc) · 5.46 KB
/
array_sparse_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package goja
import (
"testing"
)
func TestSparseArraySetLengthWithPropItems(t *testing.T) {
const SCRIPT = `
var a = [1,2,3,4];
a[100000] = 5;
var thrown = false;
Object.defineProperty(a, "2", {value: 42, configurable: false, writable: false});
try {
Object.defineProperty(a, "length", {value: 0, writable: false});
} catch (e) {
thrown = e instanceof TypeError;
}
thrown && a.length === 3;
`
testScript(SCRIPT, valueTrue, t)
}
func TestSparseArraySwitch(t *testing.T) {
vm := New()
_, err := vm.RunString(`
var a = [];
a[20470] = 5; // switch to sparse`)
if err != nil {
t.Fatal(err)
}
a := vm.Get("a").(*Object)
if _, ok := a.self.(*sparseArrayObject); !ok {
t.Fatal("1: array is not sparse")
}
_, err = vm.RunString(`
var cutoffIdx = Math.round(20470 - 20470/8);
for (var i = a.length - 1; i >= cutoffIdx; i--) {
a[i] = i;
}
// At this point it will have switched to a normal array
if (a.length != 20471) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx; i < a.length; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*arrayObject); !ok {
t.Fatal("2: array is not normal")
}
_, err = vm.RunString(`
// Now try to expand. Should stay a normal array
a[20471] = 20471;
if (a.length != 20472) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx; i < a.length; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*arrayObject); !ok {
t.Fatal("3: array is not normal")
}
_, err = vm.RunString(`
// Delete enough elements for it to become sparse again.
var cutoffIdx1 = Math.round(20472 - 20472/10);
for (var i = cutoffIdx; i < cutoffIdx1; i++) {
delete a[i];
}
// This should switch it back to sparse.
a[25590] = 25590;
if (a.length != 25591) {
throw new Error("Invalid length: " + a.length);
}
for (var i = 0; i < cutoffIdx1; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = cutoffIdx1; i < 20472; i++) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
for (var i = 20472; i < 25590; i++) {
if (a[i] !== undefined) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}
if (a[25590] !== 25590) {
throw new Error("Invalid value at 25590: " + a[25590]);
}
`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*sparseArrayObject); !ok {
t.Fatal("4: array is not sparse")
}
}
func TestSparseArrayOwnKeys(t *testing.T) {
const SCRIPT = `
var a1 = [];
a1[500000] = 1;
var seen = false;
var count = 0;
var keys = Object.keys(a1);
keys.length === 1 && keys[0] === "500000";
`
testScript(SCRIPT, valueTrue, t)
}
func TestSparseArrayEnumerate(t *testing.T) {
const SCRIPT = `
var a1 = [];
a1[500000] = 1;
var seen = false;
var count = 0;
for (var i in a1) {
if (i === "500000") {
if (seen) {
throw new Error("seen twice");
}
seen = true;
}
count++;
}
seen && count === 1;
`
testScript(SCRIPT, valueTrue, t)
}
func TestArraySparseMaxLength(t *testing.T) {
const SCRIPT = `
var a = [];
a[4294967294]=1;
a.length === 4294967295 && a[4294967294] === 1;
`
testScript(SCRIPT, valueTrue, t)
}
func TestArraySparseExportProps(t *testing.T) {
vm := New()
proto := vm.NewArray()
for _, idx := range []string{"0", "500", "9999", "10001", "20471"} {
err := proto.Set(idx, true)
if err != nil {
t.Fatal(err)
}
}
arr := vm.NewArray()
err := arr.SetPrototype(proto)
if err != nil {
t.Fatal(err)
}
err = arr.DefineDataProperty("20470", vm.ToValue(true), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
err = arr.DefineDataProperty("10000", vm.ToValue(true), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
err = arr.Set("length", 20472)
if err != nil {
t.Fatal(err)
}
actual := arr.Export()
if actualArr, ok := actual.([]interface{}); ok {
if len(actualArr) == 20472 {
expectedIdx := map[int]struct{}{
0: {},
500: {},
9999: {},
10000: {},
10001: {},
20470: {},
20471: {},
}
for i, v := range actualArr {
if _, exists := expectedIdx[i]; exists {
if v != true {
t.Fatalf("Expected true at %d, got %v", i, v)
}
} else {
if v != nil {
t.Fatalf("Expected nil at %d, got %v", i, v)
}
}
}
} else {
t.Fatalf("Expected len 20471, actual: %d", len(actualArr))
}
} else {
t.Fatalf("Invalid export type: %T", actual)
}
}
func TestSparseArrayExportToSlice(t *testing.T) {
vm := New()
arr := vm.NewArray()
err := arr.Set("20470", 120470)
if err != nil {
t.Fatal(err)
}
err = arr.DefineDataProperty("20471", vm.ToValue(220471), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
if err != nil {
t.Fatal(err)
}
var exp []int
err = vm.ExportTo(arr, &exp)
if err != nil {
t.Fatal(err)
}
if len(exp) != 20472 {
t.Fatalf("len: %d", len(exp))
}
if e := exp[20470]; e != 120470 {
t.Fatalf("20470: %d", e)
}
if e := exp[20471]; e != 220471 {
t.Fatalf("20471: %d", e)
}
for i := 0; i < 20470; i++ {
if exp[i] != 0 {
t.Fatalf("at %d: %d", i, exp[i])
}
}
}