forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-from.tq
More file actions
199 lines (182 loc) · 6.67 KB
/
array-from.tq
File metadata and controls
199 lines (182 loc) · 6.67 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
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
// Copyright 2019 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 {
// Array.from( items [, mapfn [, thisArg ] ] )
// ES #sec-array.from
transitioning javascript builtin ArrayFrom(
js-implicit context: NativeContext, receiver: JSAny)(
...arguments): JSReceiver {
const c = HasBuiltinSubclassingFlag() ? receiver : GetArrayFunction();
// Use fast path if:
// * |items| is the only argument, and
// * the receiver is the Array function.
if (arguments.length == 1 && c == GetArrayFunction()) {
try {
return iterator::FastIterableToList(arguments[0]) otherwise Slow;
} label Slow {
// fall through
}
}
const items = arguments[0];
const mapfn = arguments[1];
const thisArg = arguments[2];
// 1. Let C be the this value.
// (Done above.)
let mapping: bool;
// 2. If mapfn is undefined, let mapping be false.
if (mapfn == Undefined) {
mapping = false;
} else {
// a. If IsCallable(mapfn) is false, throw a TypeError exception.
if (!Is<Callable>(mapfn)) deferred {
ThrowCalledNonCallable(mapfn);
}
// b. Let mapping be true.
mapping = true;
}
// 4. Let usingIterator be ? GetMethod(items, @@iterator).
// 5. If usingIterator is not undefined, then
try {
const usingIterator = GetMethod(items, IteratorSymbolConstant())
otherwise IteratorIsUndefined, IteratorNotCallable;
let a: JSReceiver;
// a. If IsConstructor(C) is true, then
typeswitch (c) {
case (c: Constructor): {
// i. Let A be ? Construct(C).
a = Construct(c);
}
case (JSAny): {
// i. Let A be ? ArrayCreate(0).
a = ArrayCreate(0);
}
}
// c. Let iteratorRecord be ? GetIterator(items, sync, usingIterator).
const iteratorRecord = iterator::GetIterator(items, usingIterator);
const fastIteratorResultMap = GetIteratorResultMap();
// d. Let k be 0.
let k: Smi = 0;
// e. Repeat,
while (true) {
// i. If k ≥ 2^53-1, then
// 1. Let error be ThrowCompletion(a newly created TypeError object).
// 2. Return ? IteratorClose(iteratorRecord, error).
// The spec requires that we throw an exception if index reaches 2^53-1,
// but an empty loop would take >100 days to do this many iterations. To
// actually run for that long would require an iterator that never set
// done to true and a target array which somehow never ran out of
// memory, e.g. a proxy that discarded the values. Ignoring this case
// just means we would repeatedly call CreateDataProperty with index =
// 2^53.
dcheck(k < kMaxSafeInteger);
// ii. Let Pk be ! ToString(k).
// iii. Let next be ? IteratorStep(iteratorRecord).
let next: JSReceiver;
try {
next = iterator::IteratorStep(iteratorRecord, fastIteratorResultMap)
otherwise NextIsFalse;
}
// iv. If next is false, then
label NextIsFalse {
// 1. Perform ? Set(A, "length", k, true).
array::SetPropertyLength(a, k);
// 2. Return A.
return a;
}
// v. Let nextValue be ? IteratorValue(next).
const nextValue = iterator::IteratorValue(next, fastIteratorResultMap);
let mappedValue: JSAny;
// vi. If mapping is true, then
if (mapping) {
// 1. Let mappedValue be Call(mapfn, thisArg, « nextValue, k »).
// 2. If mappedValue is an abrupt completion,
// return ? IteratorClose(iteratorRecord, mappedValue).
// 3. Set mappedValue to mappedValue.[[Value]].
try {
mappedValue =
Call(context, UnsafeCast<Callable>(mapfn), thisArg, nextValue, k);
} catch (e, message) {
iterator::IteratorCloseOnException(iteratorRecord.object);
ReThrowWithMessage(context, e, message);
}
} else {
mappedValue = nextValue;
}
// viii. Let defineStatus be
// CreateDataPropertyOrThrow(A, Pk, mappedValue).
// ix. If defineStatus is an abrupt completion,
// return ? IteratorClose(iteratorRecord, defineStatus).
try {
FastCreateDataProperty(a, k, mappedValue);
} catch (e, message) deferred {
iterator::IteratorCloseOnException(iteratorRecord.object);
ReThrowWithMessage(context, e, message);
}
// x. Set k to k + 1.
k += 1;
}
unreachable;
} label IteratorIsUndefined {
// 6. NOTE: items is not an Iterable so assume it is an array-like object.
// 7. Let arrayLike be ! ToObject(items).
const arrayLike = ToObject_Inline(context, items);
// 8. Let len be ? LengthOfArrayLike(arrayLike).
const len = GetLengthProperty(arrayLike);
let a: JSReceiver;
// 9. If IsConstructor(C) is true, then
try {
// Allocate an array with PACKED elements kind for fast-path rather than
// calling the constructor which creates an array with HOLEY kind.
if (c != GetArrayFunction()) goto CreateWithConstructor;
if (len > kMaxFastArrayLength) goto CreateWithConstructor;
const smiLen: Smi = 0;
const capacity: intptr = Convert<intptr>(len);
const map: Map = GetFastPackedSmiElementsJSArrayMap();
a = AllocateJSArray(
ElementsKind::PACKED_SMI_ELEMENTS, map, capacity, smiLen);
} label CreateWithConstructor {
typeswitch (c) {
case (c: Constructor): {
// a. Let A be ? Construct(C, « len »).
a = Construct(c, len);
}
case (JSAny): {
// a. Let A be ? ArrayCreate(len).
a = ArrayCreate(len);
}
}
}
// 11. Let k be 0.
let k: Smi = 0;
// 12. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(arrayLike, Pk).
const kValue = GetProperty(arrayLike, k);
let mappedValue: JSAny;
// c. If mapping is true, then
if (mapping) {
// i. Let mappedValue be ? Call(mapfn, thisArg, « kValue, k »).
mappedValue =
Call(context, UnsafeCast<Callable>(mapfn), thisArg, kValue, k);
} else {
// d. Else, let mappedValue be kValue.
mappedValue = kValue;
}
// e. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
FastCreateDataProperty(a, k, mappedValue);
// f. Set k to k + 1.
k += 1;
}
// 13. Perform ? Set(A, "length", len, true).
array::SetPropertyLength(a, len);
// 14. Return A.
return a;
} label IteratorNotCallable(_value: JSAny) deferred {
ThrowTypeError(
MessageTemplate::kFirstArgumentIteratorSymbolNonCallable,
'%Array%.from');
}
}
}