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

Unified Diff: src/interpreter/bytecode-generator.cc

Issue 2622833002: WIP [esnext] implement async iteration proposal (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: src/interpreter/bytecode-generator.cc
diff --git a/src/interpreter/bytecode-generator.cc b/src/interpreter/bytecode-generator.cc
index 48a55570e48a99eb201e9697eca2e95aea812c61..8cb9f80d96d9053c2259b41f51a34da8bb16d55c 100644
--- a/src/interpreter/bytecode-generator.cc
+++ b/src/interpreter/bytecode-generator.cc
@@ -590,6 +590,8 @@ BytecodeGenerator::BytecodeGenerator(CompilationInfo* info,
loop_depth_(0),
home_object_symbol_(info->isolate()->factory()->home_object_symbol()),
iterator_symbol_(info->isolate()->factory()->iterator_symbol()),
+ async_iterator_symbol_(
+ info->isolate()->factory()->async_iterator_symbol()),
empty_fixed_array_(info->isolate()->factory()->empty_fixed_array()) {
AstValueFactory* ast_value_factory = info->parse_info()->ast_value_factory();
const AstRawString* prototype_string = ast_value_factory->prototype_string();
@@ -2248,12 +2250,27 @@ void BytecodeGenerator::VisitYield(Yield* expr) {
Register generator = VisitForRegisterValue(expr->generator_object());
- // Save context, registers, and state. Then return.
- builder()
- ->LoadLiteral(Smi::FromInt(expr->yield_id()))
- .SuspendGenerator(generator)
- .LoadAccumulatorWithRegister(value)
- .Return(); // Hard return (ignore any finally blocks).
+ if (IsAsyncGeneratorFunction(scope()->function_kind()) &&
+ expr->yield_id() > 0 && expr->yield_type() == Yield::kNormal) {
+ Expression* yield_value = expr->expression();
+ int context_index = Context::ASYNC_GENERATOR_YIELD;
+
+ RegisterList args = register_allocator()->NewRegisterList(2);
+ builder()
+ ->LoadLiteral(Smi::FromInt(expr->yield_id()))
+ .SuspendGenerator(generator)
+ .MoveRegister(generator, args[0])
+ .MoveRegister(value, args[1])
+ .CallJSRuntime(context_index, args)
+ .Return(); // Hard return (ignore any finally blocks).
+ } else {
+ // Save context, registers, and state. Then return.
+ builder()
caitp 2017/01/10 04:13:42 AwaitExpressions are still desugared in the parser
+ ->LoadLiteral(Smi::FromInt(expr->yield_id()))
+ .SuspendGenerator(generator)
+ .LoadAccumulatorWithRegister(value)
+ .Return(); // Hard return (ignore any finally blocks).
+ }
builder()->Bind(&(generator_resume_points_[expr->yield_id()]));
// Upon resume, we continue here.
@@ -2892,21 +2909,61 @@ void BytecodeGenerator::VisitGetIterator(GetIterator* expr) {
VisitForAccumulatorValue(expr->iterable());
- // Let method be GetMethod(obj, @@iterator).
- builder()
- ->StoreAccumulatorInRegister(obj)
- .LoadNamedProperty(obj, iterator_symbol(), feedback_index(load_slot))
- .StoreAccumulatorInRegister(method);
-
- // Let iterator be Call(method, obj).
- builder()->Call(method, args, feedback_index(call_slot),
- Call::NAMED_PROPERTY_CALL);
-
- // If Type(iterator) is not Object, throw a TypeError exception.
- BytecodeLabel no_type_error;
- builder()->JumpIfJSReceiver(&no_type_error);
- builder()->CallRuntime(Runtime::kThrowSymbolIteratorInvalid);
- builder()->Bind(&no_type_error);
+ if (expr->hint() == GetIterator::kAsync) {
+ FeedbackVectorSlot load_slot2 = expr->AsyncIteratorPropertyFeedbackSlot();
+ FeedbackVectorSlot call_slot2 = expr->AsyncIteratorCallFeedbackSlot();
+
+ // Set method to GetMethod(obj, @@asyncIterator)
+ builder()->StoreAccumulatorInRegister(obj).LoadNamedProperty(
+ obj, async_iterator_symbol(), feedback_index(load_slot2));
+
+ BytecodeLabel create_async_from_sync, done;
+ builder()->JumpIfUndefined(&create_async_from_sync);
+
+ // Let iterator be Call(method, obj)
+ builder()->StoreAccumulatorInRegister(method).Call(
+ method, args, feedback_index(call_slot), Call::NAMED_PROPERTY_CALL);
+
+ // If Type(iterator) is not Object, throw a TypeError exception.
+ builder()->JumpIfJSReceiver(&done);
+ builder()->CallRuntime(Runtime::kThrowSymbolAsyncIteratorInvalid);
+
+ builder()->Bind(&create_async_from_sync);
+ // If method is undefined,
+ // Let syncMethod be GetMethod(obj, @@iterator)
+ builder()
+ ->LoadNamedProperty(obj, iterator_symbol(), feedback_index(load_slot))
+ .StoreAccumulatorInRegister(method);
+
+ // Let syncIterator be Call(syncMethod, obj)
+ builder()->Call(method, args, feedback_index(call_slot2),
+ Call::NAMED_PROPERTY_CALL);
+
+ // Return CreateAsyncFromSyncIterator(syncIterator)
+ RegisterList args = register_allocator()->NewRegisterList(1);
+ Register sync_iterator = args[0];
+ builder()
+ ->StoreAccumulatorInRegister(sync_iterator)
+ .CallRuntime(Runtime::kInlineCreateAsyncFromSyncIterator, args);
+
+ builder()->Bind(&done);
+ } else {
+ // Let method be GetMethod(obj, @@iterator).
+ builder()
+ ->StoreAccumulatorInRegister(obj)
+ .LoadNamedProperty(obj, iterator_symbol(), feedback_index(load_slot))
+ .StoreAccumulatorInRegister(method);
+
+ // Let iterator be Call(method, obj).
+ builder()->Call(method, args, feedback_index(call_slot),
+ Call::NAMED_PROPERTY_CALL);
+
+ // If Type(iterator) is not Object, throw a TypeError exception.
+ BytecodeLabel no_type_error;
+ builder()->JumpIfJSReceiver(&no_type_error);
+ builder()->CallRuntime(Runtime::kThrowSymbolIteratorInvalid);
+ builder()->Bind(&no_type_error);
+ }
}
void BytecodeGenerator::VisitThisFunction(ThisFunction* expr) {

Powered by Google App Engine
This is Rietveld 408576698