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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! §10.4.2 and §23.1 as a script sees them — the one exotic object in the language.
use super::*;
#[test]
fn an_index_at_or_past_the_end_raises_the_length() {
// §10.4.2.1 step 3. This is the half people know, and it is what makes an array grow by being
// written to rather than by being told to.
assert_eq!(run("var a = []; a[0] = 1; a.length"), "1");
assert_eq!(run("var a = []; a[5] = 1; a.length"), "6");
assert_eq!(run("var a = [1, 2, 3]; a[1] = 9; a.length"), "3");
// The indices in between are absent rather than `undefined` — that is what a hole is, and
// `in` is what tells them apart.
assert_eq!(
run("var a = []; a[2] = 1; (0 in a) + '|' + (2 in a)"),
"false|true"
);
// A key that is not the canonical spelling of an index is an ordinary property and moves
// nothing: `a["01"]` and `a["1.0"]` are names, not indices.
assert_eq!(run("var a = []; a['01'] = 1; a.length"), "0");
assert_eq!(run("var a = []; a['1.0'] = 1; a.length"), "0");
assert_eq!(run("var a = []; a['-1'] = 1; a.length"), "0");
// `"-0"` is a canonical numeric index string (§7.1.21) and is not an array index (§6.1.7),
// because `ToUint32` writes it back as `"0"`. Its own row because `-0.0 < 0.0` is false, so
// a sign test written as a comparison lets it through and nothing else notices.
assert_eq!(
run("var a = []; a['-0'] = 1; a.length + '|' + a['-0']"),
"0|1"
);
// §6.1.7 stops one short of `2^32`, so the last value is a name rather than an index.
assert_eq!(run("var a = []; a['4294967295'] = 1; a.length"), "0");
assert_eq!(
run("var a = []; a['4294967294'] = 1; a.length"),
"4294967295"
);
}
#[test]
fn writing_the_length_deletes_the_indices_above_it() {
// §10.4.2.4, the half that is less well known and is why `a.length = 0` is the idiom for
// emptying an array.
assert_eq!(
run("var a = [1, 2, 3]; a.length = 1; a.length + '|' + a[0] + '|' + a[1]"),
"1|1|undefined"
);
assert_eq!(
run("var a = [1, 2, 3]; a.length = 0; a.length + '|' + (0 in a)"),
"0|false"
);
// Growing deletes nothing and adds nothing: the indices between are simply absent.
assert_eq!(
run("var a = [1]; a.length = 5; a.length + '|' + (3 in a)"),
"5|false"
);
}
#[test]
fn a_shortening_that_cannot_finish_stops_where_it_got_to() {
// §10.4.2.4 step 15 deletes from the top down and stops at the first index that refuses,
// leaving `length` one past *that* one rather than where it was asked to go. The elements
// below the immovable one survive and the ones above it are already gone — which is a state
// no other operation in the language can produce.
let frozen = "var a = [1, 2, 3, 4, 5]; \
Object.defineProperty(a, '2', {value: 3, configurable: false}); \
a.length = 0; ";
assert_eq!(run(&format!("{frozen} a.length")), "3");
assert_eq!(
run(&format!("{frozen} (2 in a) + '|' + (3 in a)")),
"true|false"
);
assert_eq!(
run(&format!("{frozen} (0 in a) + '|' + (1 in a)")),
"true|true"
);
}
#[test]
fn a_length_that_is_not_an_integer_index_throws_where_every_other_refusal_is_silent() {
// §10.4.2.4 step 2, and the one place an *assignment* in this language throws because of its
// value rather than because of what it was assigned to. Every other refused write in sloppy
// code is dropped on the floor, which is why this needs saying twice: once for the
// assignment and once for `defineProperty`.
assert_eq!(
run("var a = []; try { a.length = -1 } catch (e) { e.name }"),
"RangeError"
);
assert_eq!(
run("var a = []; try { a.length = 1.5 } catch (e) { e.name }"),
"RangeError"
);
assert_eq!(
run("try { Object.defineProperty([], 'length', {value: -1}) } catch (e) { e.name }"),
"RangeError"
);
// …while an ordinary refusal stays silent, which is the contrast that gives the rows above
// their meaning.
assert_eq!(
run(
"var a = [1]; Object.defineProperty(a, 'length', {writable: false}); a.length = 5; a.length"
),
"1"
);
}
#[test]
fn a_length_that_may_not_move_stops_the_array_growing() {
// §10.4.2.1 step 3.b — an index past the end needs `length` to move, so a `length` that is
// not writable is what makes an array fixed.
let fixed = "var a = [1]; Object.defineProperty(a, 'length', {writable: false}); ";
assert_eq!(
run(&format!("{fixed} a[5] = 1; a.length + '|' + (5 in a)")),
"1|false"
);
// …and writing *inside* the array still works, because that needs no length change at all.
assert_eq!(run(&format!("{fixed} a[0] = 9; a[0]")), "9");
}
#[test]
fn a_literal_counts_its_holes_and_does_not_fill_them() {
// §13.2.4.1 — the length is the element count including elisions, so a trailing hole counts
// and an absent element is absent rather than `undefined`.
assert_eq!(run("[1, 2, 3].length"), "3");
assert_eq!(run("[].length"), "0");
assert_eq!(run("[, 1].length + '|' + (0 in [, 1])"), "2|false");
assert_eq!(run("[1, , 2].length"), "3");
// The difference a hole makes, said the only way it can be said without iteration.
assert_eq!(run("(0 in [undefined]) + '|' + (0 in [,])"), "true|false");
assert_eq!(run("[,].length"), "1");
}
#[test]
fn the_constructors_single_number_is_a_length_and_anything_else_is_an_element() {
// §23.1.1.1 steps 2 and 3 — the reason `Array(3)` and `[3]` differ, and the reason nobody
// uses the constructor to make a literal.
assert_eq!(
run("new Array(3).length + '|' + (0 in new Array(3))"),
"3|false"
);
assert_eq!(
run("new Array('3').length + '|' + new Array('3')[0]"),
"1|3"
);
assert_eq!(
run("new Array(1, 2).length + '|' + new Array(1, 2)[1]"),
"2|2"
);
assert_eq!(run("new Array().length"), "0");
// Called without `new` it does the same thing, like `Error` and unlike most constructors.
assert_eq!(run("Array(3).length"), "3");
// A length that is not an integer index throws rather than rounding.
assert_eq!(
run("try { new Array(1.5) } catch (e) { e.name }"),
"RangeError"
);
assert_eq!(
run("try { new Array(-1) } catch (e) { e.name }"),
"RangeError"
);
}
#[test]
fn is_array_asks_what_an_object_is_where_instanceof_asks_what_it_inherits() {
// §23.1.2.2. `instanceof` walks a prototype chain, so it is false for an array from another
// realm and true for anything simply *given* `Array.prototype`; `typeof` says `"object"` for
// both. This is the only way to ask the real question.
assert_eq!(run("Array.isArray([])"), "true");
assert_eq!(run("Array.isArray(new Array(2))"), "true");
assert_eq!(run("Array.isArray({})"), "false");
assert_eq!(
run("Array.isArray(1) + '|' + Array.isArray(null)"),
"false|false"
);
// An ordinary object with `Array.prototype` behind it is *not* an array, and `instanceof`
// cannot tell.
let borrowed = "var fake = Object.create(Array.prototype); ";
assert_eq!(run(&format!("{borrowed} fake instanceof Array")), "true");
assert_eq!(run(&format!("{borrowed} Array.isArray(fake)")), "false");
// §23.1.3 — `Array.prototype` is itself an Array, which is why its `length` is 0 rather than
// absent.
assert_eq!(
run("Array.isArray(Array.prototype) + '|' + Array.prototype.length"),
"true|0"
);
}
#[test]
fn an_arrays_length_is_a_property_and_not_a_count_beside_the_elements() {
// §10.4.2.2 step 6 gives it writable, not enumerable and not configurable — so it may be
// assigned to, never shows in an enumeration, and cannot be deleted.
assert_eq!(run("delete [1].length"), "false");
assert_eq!(run("Object.keys([1, 2]).length"), "2");
let attributes = "var d = Object.getOwnPropertyDescriptor([1], 'length'); \
d.writable + '|' + d.enumerable + '|' + d.configurable";
assert_eq!(run(attributes), "true|false|false");
// …and now that arrays exist, the list `Object.keys` answers with is a real one.
assert_eq!(run("Array.isArray(Object.keys({a: 1}))"), "true");
assert_eq!(run("Object.keys({a: 1, b: 2})[1]"), "b");
}
#[test]
fn every_way_of_writing_a_length_agrees_about_what_a_length_is() {
// Three callers can throw §10.4.2.4 step 2's RangeError, and they must agree — a bad length
// that is a RangeError through an assignment and a TypeError through `defineProperties`
// would be the same mistake reported as two different ones.
for write in [
"a.length = -1",
"a.length = 1.5",
"a.length = '1.5'",
"a.length = 4294967296",
"Object.defineProperty(a, 'length', {value: -1})",
"Object.defineProperties(a, {length: {value: -1}})",
] {
let source = format!("var a = [1]; try {{ {write} }} catch (e) {{ e.name }}");
assert_eq!(run(&source), "RangeError", "writing {write}");
}
// …and a length that *is* an integer index goes through every one of them.
for write in [
"a.length = 0",
"a.length = '0'",
"Object.defineProperty(a, 'length', {value: 0})",
"Object.defineProperties(a, {length: {value: 0}})",
] {
let source = format!("var a = [1]; {write}; a.length");
assert_eq!(run(&source), "0", "writing {write}");
}
}
#[test]
fn a_length_that_may_not_move_may_still_be_written_with_the_value_it_has() {
// §10.4.2.4 step 12 — the comparison is with the *current* length, not a blanket refusal. So
// `a.length = a.length` is allowed on a fixed-length array and `a.length = 0` is not, which
// is the difference between "frozen" and "unwritable".
let fixed = "var a = [1, 2]; Object.defineProperty(a, 'length', {writable: false}); ";
assert_eq!(run(&format!("{fixed} a.length = 2; a.length")), "2");
assert_eq!(
run(&format!("{fixed} a.length = 0; a.length + '|' + (1 in a)")),
"2|true"
);
assert_eq!(run(&format!("{fixed} a.length = 5; a.length")), "2");
}
#[test]
fn growing_and_shrinking_are_told_apart_at_exactly_the_current_length() {
// Shrinking deletes and growing does not, so the boundary between them is where a
// half-implemented `<` shows up: writing the length it already has must delete nothing.
assert_eq!(
run("var a = [1, 2, 3]; a.length = 3; (2 in a) + '|' + a.length"),
"true|3"
);
assert_eq!(
run("var a = [1, 2, 3]; a.length = 2; (2 in a) + '|' + a.length"),
"false|2"
);
assert_eq!(
run("var a = [1, 2, 3]; a.length = 4; (2 in a) + '|' + a.length"),
"true|4"
);
// A shortening that finished answers `true` to the assignment; one that could not answers
// `false` — and in sloppy code the only trace of that is where `length` ended up.
let stuck =
"var a = [1, 2, 3]; Object.defineProperty(a, '1', {value: 2, configurable: false}); ";
assert_eq!(run(&format!("{stuck} a.length = 0; a.length")), "2");
assert_eq!(run(&format!("{stuck} a.length = 3; a.length")), "3");
}
#[test]
fn a_fixed_length_may_be_redefined_with_the_value_it_already_has() {
// §10.4.2.4 step 12. An *assignment* never gets here — §10.1.9.2 refuses a write to a
// non-writable data property before the array's rules are consulted at all — so this is
// reachable only through `defineProperty`, which is why it needs its own rows.
let fixed = "var a = [1, 2]; Object.defineProperty(a, 'length', {writable: false}); ";
assert_eq!(
run(&format!(
"{fixed} Object.defineProperty(a, 'length', {{value: 2}}); a.length"
)),
"2"
);
assert_eq!(
run(&format!(
"{fixed} try {{ Object.defineProperty(a, 'length', {{value: 0}}) }} catch (e) {{ e.name }}"
)),
"TypeError"
);
assert_eq!(
run(&format!(
"{fixed} try {{ Object.defineProperty(a, 'length', {{value: 5}}) }} catch (e) {{ e.name }}"
)),
"TypeError"
);
// The refusal has to come *before* anything is deleted. §10.1.6.3 would refuse the length
// write on its own, so the error is the same either way — but a shortening that ran first
// would already have thrown the elements away, and the throw would be the only trace left.
assert_eq!(
run(&format!(
"{fixed} try {{ Object.defineProperty(a, 'length', {{value: 0}}) }} catch (e) {{}} (0 in a) + '|' + (1 in a)"
)),
"true|true"
);
}
#[test]
fn a_length_define_that_the_ordinary_rules_refuse_is_refused_whole() {
// §10.4.2.4 step 16 stores the length through §10.1.6.3, and step 17 answers with *both*
// halves: the store having worked and the shortening having finished. `length` is not
// configurable, so asking to make it so is refused even though the length itself is fine —
// and a refusal that only looked at the shortening would report success.
assert_eq!(
run(
"var a = [1]; try { Object.defineProperty(a, 'length', {value: 1, configurable: true}) } catch (e) { e.name }"
),
"TypeError"
);
assert_eq!(
run(
"var a = [1]; try { Object.defineProperty(a, 'length', {value: 1, enumerable: true}) } catch (e) { e.name }"
),
"TypeError"
);
}
#[test]
fn an_index_exactly_at_the_length_is_the_one_that_needs_the_length_to_move() {
// §10.4.2.1 step 3.b compares with `>=`, and the boundary is the whole of what it means: an
// index *below* the length needs no room made, and one *at* it does.
let fixed = "var a = [1]; Object.defineProperty(a, 'length', {writable: false}); ";
assert_eq!(run(&format!("{fixed} a[0] = 9; a[0]")), "9");
assert_eq!(
run(&format!("{fixed} a[1] = 9; (1 in a) + '|' + a.length")),
"false|1"
);
// …and with a writable length the same write is ordinary and moves it by one.
assert_eq!(run("var a = [1]; a[1] = 9; a.length"), "2");
}
#[test]
fn the_constructors_elements_are_ordinary_properties_and_its_prototype_is_not() {
// §23.1.1.1 step 4 uses `CreateDataPropertyOrThrow`, which is §6.1.7.1's three defaults —
// so an element made this way is indistinguishable from one written by assignment.
let element = "var d = Object.getOwnPropertyDescriptor(new Array(7, 8), '0'); \
d.value + '|' + d.writable + '|' + d.enumerable + '|' + d.configurable";
assert_eq!(run(element), "7|true|true|true");
// §23.1.4 — `Array.prototype` on the constructor is none of the three, for the same reason
// `Object.prototype` is not: every array in the realm inherits from it.
let prototype = "var d = Object.getOwnPropertyDescriptor(Array, 'prototype'); \
d.writable + '|' + d.enumerable + '|' + d.configurable";
assert_eq!(run(prototype), "false|false|false");
assert_eq!(run("delete Array.prototype"), "false");
assert_eq!(
run("var p = Array.prototype; Array.prototype = {}; Array.prototype === p"),
"true"
);
}
#[test]
fn setting_an_arrays_length_converts_what_it_is_given() {
// §10.4.2.4 steps 3 to 5. `Heap::set_array_length` cannot do this: `ToUint32` and `ToNumber`
// both run a script's own `valueOf`, and the heap has no machine to re-enter — DR-0011's seam.
// So the value is settled where an interpreter is, and the heap is handed a Number.
assert_eq!(
run("var a = []; a.length = { valueOf: function () { return 3 } }; a.length"),
"3"
);
assert_eq!(run("var a = []; a.length = '3'; a.length"), "3");
// Whatever the conversion threw is what the assignment throws — a RangeError here would be the
// engine answering for a failure that was not about length at all.
assert_eq!(
run(
"var a = []; try { a.length = { valueOf: function () { throw new TypeError('boom') } } } catch (e) { e.message }"
),
"boom"
);
// §7.1.6 `ToUint32` is `ToNumber` and then the modulo, so a BigInt is its **TypeError** rather
// than a length that does not fit.
assert_eq!(
run("var a = []; try { a.length = 1n; 'no error' } catch (e) { e.constructor.name }"),
"TypeError"
);
// Step 5's RangeError survives, for a value that converts twice and disagrees with itself.
assert_eq!(
run("var a = []; try { a.length = 1.5; 'no error' } catch (e) { e.constructor.name }"),
"RangeError"
);
assert_eq!(
run("var a = []; try { a.length = -1; 'no error' } catch (e) { e.constructor.name }"),
"RangeError"
);
// **Twice**, and that is the clause rather than a slip: steps 3 and 4 each convert
// `Desc.[[Value]]`. Converting once and reusing the answer would be a tidier engine and a
// different language.
assert_eq!(
run(
"var n = 0; var a = []; a.length = { valueOf: function () { n = n + 1; return 2 } }; a.length + '|' + n"
),
"2|2"
);
// `Object.defineProperty` is the other way into the same clause and must agree about all of it.
assert_eq!(
run(
"var a = []; Object.defineProperty(a, 'length', { value: { valueOf: function () { return 3 } } }); a.length"
),
"3"
);
assert_eq!(
run(
"var a = []; try { Object.defineProperty(a, 'length', { value: 1n }); 'no error' } catch (e) { e.constructor.name }"
),
"TypeError"
);
// A define with no value at all changes only the attributes, and there is nothing to convert.
assert_eq!(
run(
"var a = [1, 2]; Object.defineProperty(a, 'length', { writable: false }); try { a.push(3) } catch (e) {} a.length"
),
"2"
);
}
#[test]
fn a_number_and_its_spelling_are_one_property_however_the_key_arrives() {
// DR-0026's invariant seen from the language: a key is held as a number when §6.1.7 says its
// spelling is an array index, so the four ways a program can name element zero all name one
// property. A representation that let two of these coexist would put two entries in the table
// and the read would answer `undefined` — which is why this is the first test of that record.
assert_eq!(run("var a = []; a[0] = 1; a['0']"), "1");
assert_eq!(run("var a = []; a['0'] = 1; a[0]"), "1");
assert_eq!(
run("var a = []; a[0] = 1; a['0'] = 2; a.length + ',' + a[0]"),
"1,2"
);
assert_eq!(
run("var o = {}; o[1] = 'a'; o['1'] = 'b'; Object.keys(o).length + ',' + o[1]"),
"1,b"
);
// …and a key that merely *reads* as the same number is a different property, because its
// spelling is not the canonical one. `"01"` is the trap a parse-the-digits shortcut falls into.
assert_eq!(
run("var a = [9]; a['01'] = 'x'; a.length + ',' + a[1] + ',' + a['01']"),
"1,undefined,x"
);
assert_eq!(run("var a = []; a['1.0'] = 'x'; a.length"), "0");
assert_eq!(run("var a = []; a[-0] = 'x'; a.length + ',' + a[0]"), "1,x");
assert_eq!(
run("var a = []; a['-0'] = 'x'; a.length + ',' + a[0]"),
"0,undefined"
);
// §10.1.11 orders the indices ascending ahead of every named key, and it must do so from the
// number rather than from a spelling that is no longer stored.
assert_eq!(
run(
"var o = {}; o.b = 1; o[10] = 1; o[2] = 1; o.a = 1; o['03'] = 1; Object.keys(o).join()"
),
"2,10,b,a,03"
);
// The last index and the first non-index, which is where §6.1.7's interval ends: one moves
// `length` and the other is an ordinary property on the same object.
assert_eq!(
run("var a = []; a[4294967294] = 1; a[4294967295] = 1; a.length"),
"4294967295"
);
}