forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-length.js
More file actions
112 lines (93 loc) · 2.61 KB
/
array-length.js
File metadata and controls
112 lines (93 loc) · 2.61 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// RUN: %hermes -non-strict -O -target=HBC %s | %FileCheck --match-full-lines %s
// RUN: %hermes -non-strict -O -target=HBC -emit-binary -out %t.hbc %s && %hermes %t.hbc | %FileCheck --match-full-lines %s
if (typeof print === "undefined")
var print = console.log;
function mustThrow(f) {
try {
f();
} catch (e) {
print("caught", e.name, e.message);
return;
}
print("DID NOT THROW");
}
print("array-length");
//CHECK-LABEL: array-length
var a = Array(5);
print(a.length);
//CHECK-NEXT: 5
a.length = null;
print(a.length);
//CHECK-NEXT: 0
a.length = 2;
print(a.length);
//CHECK-NEXT: 2
mustThrow(function(){ a.length = "foo"; });
//CHECK-NEXT: caught RangeError Invalid array length
print(a.length);
//CHECK-NEXT: 2
mustThrow(function(){ a.length = 3.14; });
//CHECK-NEXT: caught RangeError Invalid array length
print(a.length);
//CHECK-NEXT: 2
mustThrow(function(){ Object.defineProperty(a, "length", {value:3.14}); });
//CHECK-NEXT: caught RangeError Invalid array length
print(a.length);
//CHECK-NEXT: 2
// Make ".length" read-only and try to extend the array
Object.defineProperty(a, "length", {writable:false});
a[0]=0;
a[1]=1;
a[2]=2;
print(a.length, a[0], a[1], a[2]);
//CHECK-NEXT: 2 0 1 undefined
mustThrow(function(){ "use strict"; a[3] = 3;});
//CHECK-NEXT: caught TypeError {{.*}}
print(a.length, a[0], a[1], a[3]);
//CHECK-NEXT: 2 0 1 undefined
// Make sure .length is not an accessor.
print("getter", Object.getOwnPropertyDescriptor(a, "length").get);
//CHECK-NEXT: getter undefined
var child = {}
child.__proto__ = [1,2];
child.length = 10;
print(child.length, child.__proto__.length);
//CHECK-NEXT: 10 2
var arr = [];
arr[5] = 0;
arr.length = 0;
print(arr.length, arr[5]);
//CHECK-NEXT: 0 undefined
// Check that shrinking the length on a big non-standard array works
arr = [];
for (var i = 0; i < 50; i++) {
Object.defineProperty(arr, i, {enumerable: false, configurable: true, value: i});
}
print(arr.length);
//CHECK-NEXT: 50
arr.length = 0;
print(arr.length);
//CHECK-NEXT: 0
// Ensure that we can make the length read-only while changing it in the same
// operation.
var a = [];
Object.defineProperty(a, "length", {value:2, writable: false});
print(a.length);
//CHECK-NEXT: 2
// Make sure that setting length as a computed property works.
var a = [1, 2, 3, 4, 5]
print(a);
//CHECK-NEXT: 1,2,3,4,5
var t = "length";
a[t] = 3;
print(a);
//CHECK-NEXT: 1,2,3
a[t] = 5;
print(a);
//CHECK-NEXT: 1,2,3,,