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

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

Issue 2622833002: WIP [esnext] implement async iteration proposal (Closed)
Patch Set: fix async tests 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
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 { kPromiseSlot = Context::MIN_CONTEXT_SLOTS, kDoneSlot, kLength };
19 };
Dan Ehrenberg 2017/01/13 22:09:02 Interesting new way to use contexts. I'm a little
caitp 2017/01/13 22:33:30 I believe bound functions are slower to call from
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(WordEqual(LoadMap(var_done.value()), BooleanMapConstant()), &wrap,
105 &to_boolean);
106
107 Bind(&to_boolean);
108 {
109 var_done.Bind(
110 CallStub(CodeFactory::ToBoolean(isolate()), context, var_done.value()));
111 Goto(&wrap);
112 }
113
114 Bind(&wrap);
115 {
116 Node* const value = var_value.value();
117 Node* const done = var_done.value();
118
119 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
120 Node* const wrapper = AllocateAndInitJSPromise(context);
121
122 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined,
123 // « nextValue »).
124 InternalResolvePromise(context, wrapper, value);
125
126 // Let onFulfilled be a new built-in function object as defined in
127 // Async Iterator Value Unwrap Functions.
128 // Set onFulfilled.[[Done]] to nextDone.
129 Node* const map = LoadContextElement(
130 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
131 Node* const on_resolve_shared = LoadContextElement(
132 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
133 Node* const closure_context =
134 AllocateAsyncIteratorValueUnwrapContext(native_context, promise, done);
135 Node* const on_resolve = AllocateFunctionWithMapAndContext(
136 map, on_resolve_shared, closure_context);
137
138 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
139 // onFulfilled, undefined, promiseCapability).
140 FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
141
142 Return(promise);
143 }
144
145 Bind(&if_receiverisincompatible);
146 {
147 // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
148 // internal slot, then
149
150 // Let badIteratorError be a new TypeError exception.
151 Node* const make_type_error =
152 LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
153 Node* const error =
154 CallJS(CodeFactory::Call(isolate()), context, make_type_error,
155 UndefinedConstant(),
156 SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
157 HeapConstant(method_name), iterator);
158
159 // Perform ! Call(promiseCapability.[[Reject]], undefined,
160 // « badIteratorError »).
161 var_exception.Bind(error);
162 Goto(&reject_promise);
163 }
164
165 Bind(&reject_promise);
166 {
167 Node* const promise = AllocateAndInitJSPromise(context);
168 Node* const exception = var_exception.value();
169 CallRuntime(Runtime::kPromiseReject, context, promise, exception,
170 TrueConstant());
171
172 // Return promiseCapability.[[Promise]].
173 Return(promise);
174 }
175 }
176
177 // https://tc39.github.io/proposal-async-iteration/
178 // Section #sec-%asyncfromsynciteratorprototype%.return
179 TF_BUILTIN(AsyncFromSyncIteratorPrototypeReturn, AsyncBuiltinsAssembler) {
180 const char* method_str = "[Async-from-Sync Iterator].prototype.return";
181 Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
182
183 Node* const iterator = Parameter(0);
184 Node* const value = Parameter(1);
185 Node* const context = Parameter(4);
186
187 Node* const native_context = LoadNativeContext(context);
188 Node* const promise = AllocateAndInitJSPromise(context);
189
190 Variable var_exception(this, MachineRepresentation::kTagged);
191 var_exception.Bind(UndefinedConstant());
192 Label reject_promise(this, Label::kDeferred);
193 Label if_receiverisincompatible(this, Label::kDeferred);
194
195 GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
196 GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
197 &if_receiverisincompatible);
198
199 Node* const sync_iterator =
200 LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
201
202 // Let return be GetMethod(syncIterator, "return").
203 // IfAbruptRejectPromise(return, promiseCapability).
204 Node* const return_method =
205 CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
206 HeapConstant(factory()->return_string()));
207 GotoIfException(return_method, &reject_promise, &var_exception);
208
209 Variable var_value(this, MachineRepresentation::kTagged);
210 Variable var_done(this, MachineRepresentation::kTagged);
211
212 Label if_isundefined(this), if_isnotundefined(this), resolve_promise(this);
213
214 Branch(IsUndefined(return_method), &if_isundefined, &if_isnotundefined);
215 Bind(&if_isundefined);
216 {
217 // If return is undefined, then
218 // Let iterResult be ! CreateIterResultObject(value, true)
219 Node* const iter_result = AllocateJSIteratorResult(context, value, true);
220
221 // Perform ! Call(promiseCapability.[[Resolve]], undefined, « iterResult »).
222 // IfAbruptRejectPromise(nextDone, promiseCapability).
223 // Return promiseCapability.[[Promise]].
224 PromiseFulfill(context, promise, iter_result, v8::Promise::kFulfilled);
225 Return(promise);
226 }
227
228 Bind(&if_isnotundefined);
229 {
230 // Let returnResult be Call(return, syncIterator, « value »).
231 Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
232 return_method, sync_iterator, value);
233 // IfAbruptRejectPromise(returnResult, promiseCapability).
234 GotoIfException(iter_result, &reject_promise, &var_exception);
235
236 Label if_fastpath(this), if_slowpath(this), merge(this);
237
238 GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
239
240 Node* const fast_iter_result_map =
241 LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
242 Node* const iter_result_map = LoadMap(iter_result);
243
244 Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
245 &if_slowpath);
246
247 Bind(&if_fastpath);
248 {
249 var_value.Bind(
250 LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
251 var_done.Bind(
252 LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
253 Goto(&merge);
254 }
255
256 Bind(&if_slowpath);
257 {
258 // Let returnValue be IteratorValue(nextResult).
259 // IfAbruptRejectPromise(returnValue, promiseCapability).
260 Node* const value =
261 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
262 HeapConstant(factory()->value_string()));
263 GotoIfException(value, &reject_promise, &var_exception);
264
265 // Let returnDone be IteratorComplete(nextResult).
266 // IfAbruptRejectPromise(returnDone, promiseCapability).
267 Node* const done =
268 CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
269 HeapConstant(factory()->done_string()));
270 GotoIfException(done, &reject_promise, &var_exception);
271
272 var_value.Bind(value);
273 var_done.Bind(done);
274 Goto(&merge);
275 }
276
277 Bind(&merge);
278 {
279 Label to_boolean(this), done(this);
280 GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
281 Branch(WordEqual(LoadMap(var_done.value()), BooleanMapConstant()), &done,
282 &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, promise, 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(WordEqual(LoadMap(var_done.value()), BooleanMapConstant()), &wrap,
452 &to_boolean);
453
454 Bind(&to_boolean);
455 {
456 var_done.Bind(CallStub(CodeFactory::ToBoolean(isolate()), context,
457 var_done.value()));
458 Goto(&wrap);
459 }
460
461 Bind(&wrap);
462 {
463 // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
464 Node* const wrapper = AllocateAndInitJSPromise(context);
465 Node* const value = var_value.value();
466 Node* const done = var_done.value();
467
468 // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, «
469 // throwValue »).
470 InternalResolvePromise(context, wrapper, value);
471
472 // Let onFulfilled be a new built-in function object as defined in
473 // Async Iterator Value Unwrap Functions.
474 // Set onFulfilled.[[Done]] to throwDone.
475 Node* const map = LoadContextElement(
476 native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
477 Node* const on_resolve_shared = LoadContextElement(
478 native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
479 Node* const closure_context = AllocateAsyncIteratorValueUnwrapContext(
480 native_context, promise, done);
481 Node* const on_resolve = AllocateFunctionWithMapAndContext(
482 map, on_resolve_shared, closure_context);
483
484 // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
485 // onFulfilled, undefined, promiseCapability).
486 FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
487 Return(promise);
488 }
489 }
490
491 Bind(&if_receiverisincompatible);
492 {
493 // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
494 // internal slot, then
495
496 // Let badIteratorError be a new TypeError exception.
497 Node* const make_type_error =
498 LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
499 Node* const error =
500 CallJS(CodeFactory::Call(isolate()), context, make_type_error,
501 UndefinedConstant(),
502 SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
503 HeapConstant(method_name), iterator);
504
505 // Perform ! Call(promiseCapability.[[Reject]], undefined,
506 // « badIteratorError »).
507 var_exception.Bind(error);
508 Goto(&reject_promise);
509 }
510
511 Bind(&reject_promise);
512 {
513 Node* const exception = var_exception.value();
514 CallRuntime(Runtime::kPromiseReject, context, promise, exception,
515 TrueConstant());
516
517 // Return promiseCapability.[[Promise]].
518 Return(promise);
519 }
520 }
521
522 TF_BUILTIN(AsyncIteratorValueUnwrap, AsyncBuiltinsAssembler) {
523 Node* const value = Parameter(1);
524 Node* const context = Parameter(4);
525
526 Node* const promise =
527 LoadContextElement(context, ValueUnwrapContext::kPromiseSlot);
528 Node* const done = LoadContextElement(context, ValueUnwrapContext::kDoneSlot);
529
530 Node* const unwrapped_value = AllocateJSIteratorResult(context, value, done);
531
532 InternalResolvePromise(context, promise, unwrapped_value);
533
534 Return(unwrapped_value);
535 }
536
537 Node* AsyncBuiltinsAssembler::AllocateAsyncIteratorValueUnwrapContext(
538 Node* native_context, Node* promise, Node* done) {
539 Node* const context =
540 CreatePromiseContext(native_context, ValueUnwrapContext::kLength);
541 StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kPromiseSlot,
542 promise);
543 StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kDoneSlot,
544 done);
545 return context;
546 }
547
548 } // namespace internal
549 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698