Repository navigation
Expand file tree
/
Copy pathAsyncFromSyncIteratorPrototype.cpp
More file actions
284 lines (233 loc) · 12.6 KB
/
Copy pathAsyncFromSyncIteratorPrototype.cpp
File metadata and controls
284 lines (233 loc) · 12.6 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
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
/*
* Copyright (C) 2017 Oleksandr Skachkov <gskachkov@gmail.com>.
* Copyright (C) 2021-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AsyncFromSyncIteratorPrototype.h"
#include "IteratorOperations.h"
#include "JSArrayInlines.h"
#include "JSArrayIterator.h"
#include "JSArrayIteratorInlines.h"
#include "JSAsyncFromSyncIterator.h"
#include "JSCInlines.h"
#include "JSInternalFieldObjectImplInlines.h"
#include "JSMapIterator.h"
#include "JSPromise.h"
#include "JSSetIterator.h"
#include "Microtask.h"
#include "TopExceptionScope.h"
namespace JSC {
static JSC_DECLARE_HOST_FUNCTION(asyncFromSyncIteratorPrototypeFuncReturn);
static JSC_DECLARE_HOST_FUNCTION(asyncFromSyncIteratorPrototypeFuncThrow);
const ClassInfo AsyncFromSyncIteratorPrototype::s_info = { "AsyncFromSyncIterator"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(AsyncFromSyncIteratorPrototype) };
AsyncFromSyncIteratorPrototype::AsyncFromSyncIteratorPrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
void AsyncFromSyncIteratorPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
putDirectWithoutTransition(vm, vm.propertyNames->next, globalObject->asyncFromSyncIteratorPrototypeNextFunction(), static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->returnKeyword, asyncFromSyncIteratorPrototypeFuncReturn, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->throwKeyword, asyncFromSyncIteratorPrototypeFuncThrow, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
}
AsyncFromSyncIteratorPrototype* AsyncFromSyncIteratorPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
{
AsyncFromSyncIteratorPrototype* prototype = new (NotNull, allocateCell<AsyncFromSyncIteratorPrototype>(vm)) AsyncFromSyncIteratorPrototype(vm, structure);
prototype->finishCreation(vm, globalObject);
return prototype;
}
static void awaitAndContinue(JSGlobalObject* globalObject, VM& vm, JSAsyncFromSyncIterator* iterator, JSObject* target, bool closeSyncIteratorOnRejection, JSValue value, bool done)
{
InternalMicrotask task = done ? InternalMicrotask::AsyncFromSyncIteratorDone : InternalMicrotask::AsyncFromSyncIteratorContinue;
iterator->setTarget(vm, target, closeSyncIteratorOnRejection);
JSPromise::resolveWithInternalMicrotaskForAsyncAwait(globalObject, vm, value, task, iterator);
}
static bool callSyncIteratorMethodAndExtract(JSGlobalObject* globalObject, JSValue method, JSObject* syncIterator, JSValue argument, JSValue& value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto callData = JSC::getCallData(method);
if (callData.type == CallData::Type::None) [[unlikely]] {
throwTypeError(globalObject, scope, "Iterator method is not callable."_s);
return { };
}
auto args = WTF::toArray<EncodedJSValue>({
JSValue::encode(argument),
});
JSValue result = call(globalObject, method, callData, syncIterator, ArgList { args.data(), argument.isEmpty() ? 0u : 1u });
RETURN_IF_EXCEPTION(scope, { });
if (!result.isObject()) [[unlikely]] {
throwTypeError(globalObject, scope, "Iterator result interface is not an object."_s);
return { };
}
bool done = result.get(globalObject, vm.propertyNames->done).toBoolean(globalObject);
RETURN_IF_EXCEPTION(scope, { });
scope.release();
value = result.get(globalObject, vm.propertyNames->value);
return done;
}
static bool driveFastSyncIterator(JSGlobalObject* globalObject, VM& vm, JSObject* syncIterator, IterationMode mode, JSValue& value)
{
auto scope = DECLARE_THROW_SCOPE(vm);
switch (mode) {
case IterationMode::FastArrayValues:
case IterationMode::FastArrayKeys:
case IterationMode::FastArrayEntries: {
bool hasNext = uncheckedDowncast<JSArrayIterator>(syncIterator)->next(globalObject, value);
RETURN_IF_EXCEPTION(scope, false);
return !hasNext;
}
case IterationMode::FastMapKeys:
case IterationMode::FastMapValues:
case IterationMode::FastMapEntries: {
bool hasNext = uncheckedDowncast<JSMapIterator>(syncIterator)->next(globalObject, value);
RETURN_IF_EXCEPTION(scope, false);
return !hasNext;
}
case IterationMode::FastSetValues:
case IterationMode::FastSetEntries: {
bool hasNext = uncheckedDowncast<JSSetIterator>(syncIterator)->next(globalObject, value);
RETURN_IF_EXCEPTION(scope, false);
return !hasNext;
}
default:
RELEASE_ASSERT_NOT_REACHED();
return true;
}
}
static bool driveSyncIterator(JSGlobalObject* globalObject, VM& vm, JSAsyncFromSyncIterator* iterator, JSValue argument, JSValue& value)
{
JSObject* syncIterator = iterator->syncIterator();
if (iterator->iterationMode() != IterationMode::Generic)
return driveFastSyncIterator(globalObject, vm, syncIterator, iterator->iterationMode(), value);
return callSyncIteratorMethodAndExtract(globalObject, iterator->nextMethod(), syncIterator, argument, value);
}
// https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.next
JSValue asyncFromSyncIteratorNext(JSGlobalObject* globalObject, JSAsyncFromSyncIterator* iterator, JSValue argument)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSPromise* promise = JSPromise::create(vm, globalObject->promiseStructure());
JSValue value = jsUndefined();
bool done = driveSyncIterator(globalObject, vm, iterator, argument, value);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, promise->rejectWithCaughtException(vm, scope));
scope.release();
awaitAndContinue(globalObject, vm, iterator, promise, /* closeSyncIteratorOnRejection */ true, value, done);
return promise;
}
JSC_DEFINE_HOST_FUNCTION(asyncFromSyncIteratorPrototypeFuncNext, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
auto* iterator = downcast<JSAsyncFromSyncIterator>(callFrame->thisValue());
JSValue argument = callFrame->argumentCount() > 0 ? callFrame->uncheckedArgument(0) : JSValue();
return JSValue::encode(asyncFromSyncIteratorNext(globalObject, iterator, argument));
}
// https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.return
JSC_DEFINE_HOST_FUNCTION(asyncFromSyncIteratorPrototypeFuncReturn, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSPromise* promise = JSPromise::create(vm, globalObject->promiseStructure());
auto* iterator = downcast<JSAsyncFromSyncIterator>(callFrame->thisValue());
JSObject* syncIterator = iterator->syncIterator();
JSValue returnMethod = syncIterator->get(globalObject, vm.propertyNames->returnKeyword);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
if (returnMethod.isUndefinedOrNull()) {
JSValue value = callFrame->argument(0);
scope.release();
promise->resolve(globalObject, vm, createIteratorResultObject(globalObject, value, true));
return JSValue::encode(promise);
}
JSValue value;
JSValue argument = callFrame->argumentCount() > 0 ? callFrame->uncheckedArgument(0) : JSValue();
bool done = callSyncIteratorMethodAndExtract(globalObject, returnMethod, syncIterator, argument, value);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
scope.release();
awaitAndContinue(globalObject, vm, iterator, promise, /* closeSyncIteratorOnRejection */ false, value, done);
return JSValue::encode(promise);
}
// https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.throw
JSC_DEFINE_HOST_FUNCTION(asyncFromSyncIteratorPrototypeFuncThrow, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSPromise* promise = JSPromise::create(vm, globalObject->promiseStructure());
auto* iterator = downcast<JSAsyncFromSyncIterator>(callFrame->thisValue());
JSObject* syncIterator = iterator->syncIterator();
JSValue throwMethod = syncIterator->get(globalObject, vm.propertyNames->throwKeyword);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
if (throwMethod.isUndefinedOrNull()) {
JSValue returnMethod = syncIterator->get(globalObject, vm.propertyNames->returnKeyword);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
if (!returnMethod.isUndefinedOrNull()) {
auto returnCallData = JSC::getCallData(returnMethod);
if (returnCallData.type == CallData::Type::None) [[unlikely]] {
throwTypeError(globalObject, scope, "Iterator return method is not callable."_s);
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
}
JSValue returnResult = call(globalObject, returnMethod, returnCallData, syncIterator, { });
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
if (!returnResult.isObject()) [[unlikely]] {
throwTypeError(globalObject, scope, "Iterator result interface is not an object."_s);
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
}
}
throwTypeError(globalObject, scope, "Iterator does not provide a throw method."_s);
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
}
JSValue value;
JSValue argument = callFrame->argumentCount() > 0 ? callFrame->uncheckedArgument(0) : JSValue();
bool done = callSyncIteratorMethodAndExtract(globalObject, throwMethod, syncIterator, argument, value);
if (scope.exception()) [[unlikely]]
RELEASE_AND_RETURN(scope, JSValue::encode(promise->rejectWithCaughtException(vm, scope)));
scope.release();
awaitAndContinue(globalObject, vm, iterator, promise, /* closeSyncIteratorOnRejection */ true, value, done);
return JSValue::encode(promise);
}
void driveAsyncFromSyncIteratorWithDriver(JSGlobalObject* globalObject, JSAsyncFromSyncIterator* iterator, JSObject* driver, JSValue resumeValue)
{
VM& vm = globalObject->vm();
JSValue value = jsUndefined();
bool done = false;
{
auto catchScope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
done = driveSyncIterator(globalObject, vm, iterator, resumeValue, value);
if (catchScope.exception()) [[unlikely]] {
JSValue error = catchScope.exception()->value();
if (!catchScope.clearExceptionExceptTermination()) [[unlikely]]
return;
JSPromise::rejectWithInternalMicrotask(vm, globalObject, error, InternalMicrotask::AsyncGeneratorDriverResume, driver);
return;
}
}
awaitAndContinue(globalObject, vm, iterator, driver, /* closeSyncIteratorOnRejection */ true, value, done);
}
} // namespace JSC