Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/builtins/builtins-async-iterator.cc

Issue 2622833002: WIP [esnext] implement async iteration proposal (Closed)
Patch Set: simplify AsyncIteratorValueUnwrap Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/builtins/builtins-async-generator.cc ('k') | src/builtins/builtins-function.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/builtins/builtins-async.h"
6 #include "src/builtins/builtins-utils.h"
7 #include "src/builtins/builtins.h"
8 #include "src/code-factory.h"
9 #include "src/code-stub-assembler.h"
10 #include "src/frames-inl.h"
11
12 namespace v8 {
13 namespace internal {
14
15 // Describe fields of Context associated with the AsyncIterator unwrap closure.
16 class ValueUnwrapContext {
17 public:
18 enum Fields { kDoneSlot = Context::MIN_CONTEXT_SLOTS, kLength };
19 };
20
21 // https://tc39.github.io/proposal-async-iteration/
22 // Section #sec-%asyncfromsynciteratorprototype%.next
23 TF_BUILTIN(AsyncFromSyncIteratorPrototypeNext, AsyncBuiltinsAssembler) {
24 const char* method_str = "[Async-from-Sync Iterator].prototype.next";
25 Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
26
27 Node* const iterator = Parameter(0);
28 Node* const value = Parameter(1);
29 Node* const context = Parameter(4);
30
31 Node* const native_context = LoadNativeContext(context);
32 Node* const promise = AllocateAndInitJSPromise(context);
33
34 Variable var_parent(this, MachineRepresentation::kTagged);
35 Variable var_exception(this, MachineRepresentation::kTagged);
36 var_exception.Bind(UndefinedConstant());
37 Label reject_promise(this, Label::kDeferred);
38 Label if_receiverisincompatible(this, Label::kDeferred);
39
40 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
41 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
42 &if_receiverisincompatible);
43
44 Node* const sync_iterator =
45 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
46
47 // Let nextResult be IteratorNext(syncIterator, value).
48 // IfAbruptRejectPromise(nextResult, promiseCapability).
49 Node* next_method =
50 CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
51 HeapConstant(factory()->next_string()));
52 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
53 next_method, sync_iterator, value);
54 GotoIfException(iter_result, &reject_promise, &var_exception);
55
56 Label if_fastpath(this), if_slowpath(this), merge(this);
57 GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
58
59 Node* const fast_iter_result_map =
60 LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
61 Node* const iter_result_map = LoadMap(iter_result);
62
63 Variable var_value(this, MachineRepresentation::kTagged);
64 Variable var_done(this, MachineRepresentation::kTagged);
65 Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
66 &if_slowpath);
67
68 Bind(&if_fastpath);
69 {
70 var_value.Bind(
71 LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
72 var_done.Bind(LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
73 Goto(&merge);
74 }
75
76 Bind(&if_slowpath);
77 {
78 Label if_exception(this, Label::kDeferred);
79
80 // Let nextValue be IteratorValue(nextResult).
81 // IfAbruptRejectPromise(nextValue, promiseCapability).
82 Node* const value =
83 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
84 HeapConstant(factory()->value_string()));
85 GotoIfException(value, &reject_promise, &var_exception);
86
87 // Let nextDone be IteratorComplete(nextResult).
88 // IfAbruptRejectPromise(nextDone, promiseCapability).
89 Node* const done =
90 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
91 HeapConstant(factory()->done_string()));
92 GotoIfException(done, &reject_promise, &var_exception);
93
94 var_value.Bind(value);
95 var_done.Bind(done);
96 Goto(&merge);
97 }
98
99 Bind(&merge);
100
101 // Convert `done` status to a Boolean value if needed.
102 Label to_boolean(this, Label::kDeferred), wrap(this);
103 GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
104 Branch(IsBooleanMap(LoadMap(var_done.value())), &wrap, &to_boolean);
105
106 Bind(&to_boolean);
107 {
108 var_done.Bind(
109 CallStub(CodeFactory::ToBoolean(isolate()), context, var_done.value()));
110 Goto(&wrap);
111 }
112
113 Bind(&wrap);
114 {
115 Node* const value = var_value.value();
116 Node* const done = var_done.value();
117
118 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
119 Node* const wrapper = AllocateAndInitJSPromise(context);
120
121 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined,
122 // « nextValue »).
123 InternalResolvePromise(context, wrapper, value);
124
125 // Let onFulfilled be a new built-in function object as defined in
126 // Async Iterator Value Unwrap Functions.
127 // Set onFulfilled.[[Done]] to nextDone.
128 Node* const map = LoadContextElement(
129 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
130 Node* const on_resolve_shared = LoadContextElement(
131 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
132 Node* const closure_context =
133 AllocateAsyncIteratorValueUnwrapContext(native_context, done);
134 Node* const on_resolve = AllocateFunctionWithMapAndContext(
135 map, on_resolve_shared, closure_context);
136
137 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
138 // onFulfilled, undefined, promiseCapability).
139 FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
140
141 Return(promise);
142 }
143
144 Bind(&if_receiverisincompatible);
145 {
146 // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
147 // internal slot, then
148
149 // Let badIteratorError be a new TypeError exception.
150 Node* const make_type_error =
151 LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
152 Node* const error =
153 CallJS(CodeFactory::Call(isolate()), context, make_type_error,
154 UndefinedConstant(),
155 SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
156 HeapConstant(method_name), iterator);
157
158 // Perform ! Call(promiseCapability.[[Reject]], undefined,
159 // « badIteratorError »).
160 var_exception.Bind(error);
161 Goto(&reject_promise);
162 }
163
164 Bind(&reject_promise);
165 {
166 Node* const promise = AllocateAndInitJSPromise(context);
167 Node* const exception = var_exception.value();
168 CallRuntime(Runtime::kPromiseReject, context, promise, exception,
169 TrueConstant());
170
171 // Return promiseCapability.[[Promise]].
172 Return(promise);
173 }
174 }
175
176 // https://tc39.github.io/proposal-async-iteration/
177 // Section #sec-%asyncfromsynciteratorprototype%.return
178 TF_BUILTIN(AsyncFromSyncIteratorPrototypeReturn, AsyncBuiltinsAssembler) {
179 const char* method_str = "[Async-from-Sync Iterator].prototype.return";
180 Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
181
182 Node* const iterator = Parameter(0);
183 Node* const value = Parameter(1);
184 Node* const context = Parameter(4);
185
186 Node* const native_context = LoadNativeContext(context);
187 Node* const promise = AllocateAndInitJSPromise(context);
188
189 Variable var_exception(this, MachineRepresentation::kTagged);
190 var_exception.Bind(UndefinedConstant());
191 Label reject_promise(this, Label::kDeferred);
192 Label if_receiverisincompatible(this, Label::kDeferred);
193
194 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
195 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
196 &if_receiverisincompatible);
197
198 Node* const sync_iterator =
199 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
200
201 // Let return be GetMethod(syncIterator, "return").
202 // IfAbruptRejectPromise(return, promiseCapability).
203 Node* const return_method =
204 CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
205 HeapConstant(factory()->return_string()));
206 GotoIfException(return_method, &reject_promise, &var_exception);
207
208 Variable var_value(this, MachineRepresentation::kTagged);
209 Variable var_done(this, MachineRepresentation::kTagged);
210
211 Label if_isundefined(this), if_isnotundefined(this), resolve_promise(this);
212
213 Branch(IsUndefined(return_method), &if_isundefined, &if_isnotundefined);
214 Bind(&if_isundefined);
215 {
216 // If return is undefined, then
217 // Let iterResult be ! CreateIterResultObject(value, true)
218 Node* const iter_result =
219 CallStub(CodeFactory::CreateIterResultObject(isolate()), context, value,
220 TrueConstant());
221
222 // Perform ! Call(promiseCapability.[[Resolve]], undefined, « iterResult »).
223 // IfAbruptRejectPromise(nextDone, promiseCapability).
224 // Return promiseCapability.[[Promise]].
225 PromiseFulfill(context, promise, iter_result, v8::Promise::kFulfilled);
226 Return(promise);
227 }
228
229 Bind(&if_isnotundefined);
230 {
231 // Let returnResult be Call(return, syncIterator, « value »).
232 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
233 return_method, sync_iterator, value);
234 // IfAbruptRejectPromise(returnResult, promiseCapability).
235 GotoIfException(iter_result, &reject_promise, &var_exception);
236
237 Label if_fastpath(this), if_slowpath(this), merge(this);
238
239 GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
240
241 Node* const fast_iter_result_map =
242 LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
243 Node* const iter_result_map = LoadMap(iter_result);
244
245 Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
246 &if_slowpath);
247
248 Bind(&if_fastpath);
249 {
250 var_value.Bind(
251 LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
252 var_done.Bind(
253 LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
254 Goto(&merge);
255 }
256
257 Bind(&if_slowpath);
258 {
259 // Let returnValue be IteratorValue(nextResult).
260 // IfAbruptRejectPromise(returnValue, promiseCapability).
261 Node* const value =
262 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
263 HeapConstant(factory()->value_string()));
264 GotoIfException(value, &reject_promise, &var_exception);
265
266 // Let returnDone be IteratorComplete(nextResult).
267 // IfAbruptRejectPromise(returnDone, promiseCapability).
268 Node* const done =
269 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
270 HeapConstant(factory()->done_string()));
271 GotoIfException(done, &reject_promise, &var_exception);
272
273 var_value.Bind(value);
274 var_done.Bind(done);
275 Goto(&merge);
276 }
277
278 Bind(&merge);
279 {
280 Label to_boolean(this), done(this);
281 GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
282 Branch(IsBooleanMap(LoadMap(var_done.value())), &done, &to_boolean);
283
284 Bind(&to_boolean);
285 {
286 Node* const result = CallStub(CodeFactory::ToBoolean(isolate()),
287 context, var_done.value());
288 var_done.Bind(result);
289 Goto(&done);
290 }
291
292 Bind(&done);
293 Goto(&resolve_promise);
294 }
295 }
296
297 Bind(&resolve_promise);
298 {
299 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
300 Node* const wrapper = AllocateAndInitJSPromise(context);
301 Node* const value = var_value.value();
302 Node* const done = var_done.value();
303
304 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined,
305 // « nextValue »).
306 InternalResolvePromise(context, promise, value);
307
308 // Let onFulfilled be a new built-in function object as defined in
309 // Async Iterator Value Unwrap Functions.
310 // Set onFulfilled.[[Done]] to nextDone.
311 Node* const map = LoadContextElement(
312 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
313 Node* const on_resolve_shared = LoadContextElement(
314 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
315 Node* const closure_context =
316 AllocateAsyncIteratorValueUnwrapContext(native_context, done);
317 Node* const on_resolve = AllocateFunctionWithMapAndContext(
318 map, on_resolve_shared, closure_context);
319
320 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
321 // onFulfilled, undefined, promiseCapability).
322 FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
323 Return(promise);
324 }
325
326 Bind(&if_receiverisincompatible);
327 {
328 // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
329 // internal slot, then
330
331 // Let badIteratorError be a new TypeError exception.
332 Node* const make_type_error =
333 LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
334 Node* const error =
335 CallJS(CodeFactory::Call(isolate()), context, make_type_error,
336 UndefinedConstant(),
337 SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
338 HeapConstant(method_name), iterator);
339
340 // Perform ! Call(promiseCapability.[[Reject]], undefined,
341 // « badIteratorError »).
342 var_exception.Bind(error);
343 Goto(&reject_promise);
344 }
345
346 Bind(&reject_promise);
347 {
348 Node* const exception = var_exception.value();
349 CallRuntime(Runtime::kPromiseReject, context, promise, exception,
350 TrueConstant());
351
352 // Return promiseCapability.[[Promise]].
353 Return(promise);
354 }
355 }
356
357 // https://tc39.github.io/proposal-async-iteration/
358 // Section #sec-%asyncfromsynciteratorprototype%.throw
359 TF_BUILTIN(AsyncFromSyncIteratorPrototypeThrow, AsyncBuiltinsAssembler) {
360 const char* method_str = "[Async-from-Sync Iterator].prototype.throw";
361 Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
362
363 Node* const iterator = Parameter(0);
364 Node* const value = Parameter(1);
365 Node* const context = Parameter(4);
366
367 Node* const native_context = LoadNativeContext(context);
368 Node* const promise = AllocateAndInitJSPromise(context);
369
370 Variable var_exception(this, MachineRepresentation::kTagged);
371 var_exception.Bind(value);
372 Variable var_value(this, MachineRepresentation::kTagged);
373 Variable var_done(this, MachineRepresentation::kTagged);
374 Label reject_promise(this, Label::kDeferred);
375 Label if_receiverisincompatible(this, Label::kDeferred);
376
377 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
378 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
379 &if_receiverisincompatible);
380
381 Node* const sync_iterator =
382 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
383
384 // Let throw be GetMethod(syncIterator, "throw").
385 // IfAbruptRejectPromise(nextResult, promiseCapability).
386 // IfAbruptRejectPromise(throw, promiseCapability).
387 Node* const throw_method =
388 CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
389 HeapConstant(factory()->throw_string()));
390 GotoIfException(throw_method, &reject_promise, &var_exception);
391
392 Label if_isnotundefined(this), resolve_promise(this);
393
394 // If throw is undefined, then
395 // Perform ! Call(promiseCapability.[[Reject]], undefined, « value »).
396 Branch(IsUndefined(throw_method), &reject_promise, &if_isnotundefined);
397
398 Bind(&if_isnotundefined);
399 {
400 // Let throwResult be Call(throw, syncIterator, « value »).
401 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
402 throw_method, sync_iterator, value);
403 GotoIfException(iter_result, &reject_promise, &var_exception);
404
405 Label if_fastpath(this), if_slowpath(this), merge(this);
406
407 GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
408
409 Node* const fast_iter_result_map =
410 LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
411 Node* const iter_result_map = LoadMap(iter_result);
412
413 Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
414 &if_slowpath);
415
416 Bind(&if_fastpath);
417 {
418 var_value.Bind(
419 LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
420 var_done.Bind(
421 LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
422 Goto(&merge);
423 }
424
425 Bind(&if_slowpath);
426 {
427 // Let throwValue be IteratorValue(nextResult).
428 // IfAbruptRejectPromise(throwValue, promiseCapability).
429 Node* const value =
430 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
431 HeapConstant(factory()->value_string()));
432 GotoIfException(value, &reject_promise, &var_exception);
433
434 // Let throwDone be IteratorComplete(nextResult).
435 // IfAbruptRejectPromise(throwDone, promiseCapability).
436 Node* const done =
437 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
438 HeapConstant(factory()->done_string()));
439 GotoIfException(done, &reject_promise, &var_exception);
440
441 var_value.Bind(value);
442 var_done.Bind(done);
443 Goto(&merge);
444 }
445
446 Bind(&merge);
447
448 // Convert `done` status to a Boolean value if needed.
449 Label to_boolean(this, Label::kDeferred), wrap(this);
450 GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
451 Branch(IsBooleanMap(LoadMap(var_done.value())), &wrap, &to_boolean);
452
453 Bind(&to_boolean);
454 {
455 var_done.Bind(CallStub(CodeFactory::ToBoolean(isolate()), context,
456 var_done.value()));
457 Goto(&wrap);
458 }
459
460 Bind(&wrap);
461 {
462 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
463 Node* const wrapper = AllocateAndInitJSPromise(context);
464 Node* const value = var_value.value();
465 Node* const done = var_done.value();
466
467 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, «
468 // throwValue »).
469 InternalResolvePromise(context, wrapper, value);
470
471 // Let onFulfilled be a new built-in function object as defined in
472 // Async Iterator Value Unwrap Functions.
473 // Set onFulfilled.[[Done]] to throwDone.
474 Node* const map = LoadContextElement(
475 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
476 Node* const on_resolve_shared = LoadContextElement(
477 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
478 Node* const closure_context =
479 AllocateAsyncIteratorValueUnwrapContext(native_context, done);
480 Node* const on_resolve = AllocateFunctionWithMapAndContext(
481 map, on_resolve_shared, closure_context);
482
483 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
484 // onFulfilled, undefined, promiseCapability).
485 FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
486 Return(promise);
487 }
488 }
489
490 Bind(&if_receiverisincompatible);
491 {
492 // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
493 // internal slot, then
494
495 // Let badIteratorError be a new TypeError exception.
496 Node* const make_type_error =
497 LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
498 Node* const error =
499 CallJS(CodeFactory::Call(isolate()), context, make_type_error,
500 UndefinedConstant(),
501 SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
502 HeapConstant(method_name), iterator);
503
504 // Perform ! Call(promiseCapability.[[Reject]], undefined,
505 // « badIteratorError »).
506 var_exception.Bind(error);
507 Goto(&reject_promise);
508 }
509
510 Bind(&reject_promise);
511 {
512 Node* const exception = var_exception.value();
513 CallRuntime(Runtime::kPromiseReject, context, promise, exception,
514 TrueConstant());
515
516 // Return promiseCapability.[[Promise]].
517 Return(promise);
518 }
519 }
520
521 TF_BUILTIN(AsyncIteratorValueUnwrap, AsyncBuiltinsAssembler) {
522 Node* const value = Parameter(1);
523 Node* const context = Parameter(4);
524
525 Node* const done = LoadContextElement(context, ValueUnwrapContext::kDoneSlot);
526 Node* const unwrapped_value = CallStub(
527 CodeFactory::CreateIterResultObject(isolate()), context, value, done);
528
529 Return(unwrapped_value);
530 }
531
532 Node* AsyncBuiltinsAssembler::AllocateAsyncIteratorValueUnwrapContext(
533 Node* native_context, Node* done) {
534 Node* const context =
535 CreatePromiseContext(native_context, ValueUnwrapContext::kLength);
536 StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kDoneSlot,
537 done);
538 return context;
539 }
540
541 } // namespace internal
542 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins-async-generator.cc ('k') | src/builtins/builtins-function.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698