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
|
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
namespace array {
macro FastPackedArrayToReversed<Accessor: type, T: type>(
implicit context: Context)(
kind: constexpr ElementsKind, elements: FixedArrayBase,
length: Smi): JSArray {
// 3. Let A be ? ArrayCreate(𝔽(len)).
const copy: FixedArrayBase =
AllocateFixedArray(kind, SmiUntag(length), AllocationFlag::kNone);
// 4. Let k be 0.
let k: Smi = 0;
// 5. Repeat, while k < len,
while (k < length) {
// a. Let from be ! ToString(𝔽(len - k - 1)).
// b. Let Pk be ! ToString(𝔽(k)).
const from = length - k - 1;
// c. Let fromValue be ? Get(O, from).
const fromValue: T = LoadElement<Accessor, T>(elements, from);
// d. Perform ! CreateDataPropertyOrThrow(A, Pk, fromValue).
StoreElement<Accessor>(copy, k, fromValue);
// e. Set k to k + 1.
++k;
}
// 6. Return A.
const map: Map = LoadJSArrayElementsMap(kind, LoadNativeContext(context));
return NewJSArray(map, copy);
}
macro TryFastPackedArrayToReversed(implicit context: Context)(receiver: JSAny):
JSArray labels Slow {
const array: FastJSArray = Cast<FastJSArray>(receiver) otherwise Slow;
const kind: ElementsKind = array.map.elements_kind;
if (kind == ElementsKind::PACKED_SMI_ELEMENTS) {
return FastPackedArrayToReversed<array::FastPackedSmiElements, Smi>(
ElementsKind::PACKED_SMI_ELEMENTS, array.elements, array.length);
}
if (kind == ElementsKind::PACKED_ELEMENTS) {
return FastPackedArrayToReversed<array::FastPackedObjectElements, JSAny>(
ElementsKind::PACKED_ELEMENTS, array.elements, array.length);
}
if (kind == ElementsKind::PACKED_DOUBLE_ELEMENTS) {
return FastPackedArrayToReversed<array::FastPackedDoubleElements, float64>(
ElementsKind::PACKED_DOUBLE_ELEMENTS, array.elements, array.length);
}
goto Slow;
}
transitioning builtin GenericArrayToReversed(implicit context: Context)(
receiver: JSAny): JSAny {
// 1. Let O be ? ToObject(this value).
const object: JSReceiver = ToObject_Inline(context, receiver);
// 2. Let len be ? LengthOfArrayLike(O).
const len: Number = GetLengthProperty(object);
// 3. Let A be ? ArrayCreate(𝔽(len)).
const copy = ArrayCreate(len);
// 4. Let k be 0.
let k: Number = 0;
// 5. Repeat, while k < len,
while (k < len) {
// a. Let from be ! ToString(𝔽(len - k - 1)).
// b. Let Pk be ! ToString(𝔽(k)).
const from: Number = len - k - 1;
// c. Let fromValue be ? Get(object, from).
const fromValue = GetProperty(object, from);
// d. Perform ! CreateDataPropertyOrThrow(A, Pk, fromValue).
FastCreateDataProperty(copy, k, fromValue);
// e. Set k to k + 1.
++k;
}
// 6. Return A.
return copy;
}
// https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.toReversed
transitioning javascript builtin ArrayPrototypeToReversed(
js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
try {
return TryFastPackedArrayToReversed(receiver) otherwise Slow;
} label Slow {
return GenericArrayToReversed(receiver);
}
}
}
|