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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-async-iterator.cc
diff --git a/src/builtins/builtins-async-iterator.cc b/src/builtins/builtins-async-iterator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3b6dff0223d57832b18a60478d4c7b8abea93b5d
--- /dev/null
+++ b/src/builtins/builtins-async-iterator.cc
@@ -0,0 +1,542 @@
+// Copyright 2016 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.
+
+#include "src/builtins/builtins-async.h"
+#include "src/builtins/builtins-utils.h"
+#include "src/builtins/builtins.h"
+#include "src/code-factory.h"
+#include "src/code-stub-assembler.h"
+#include "src/frames-inl.h"
+
+namespace v8 {
+namespace internal {
+
+// Describe fields of Context associated with the AsyncIterator unwrap closure.
+class ValueUnwrapContext {
+ public:
+ enum Fields { kDoneSlot = Context::MIN_CONTEXT_SLOTS, kLength };
+};
+
+// https://tc39.github.io/proposal-async-iteration/
+// Section #sec-%asyncfromsynciteratorprototype%.next
+TF_BUILTIN(AsyncFromSyncIteratorPrototypeNext, AsyncBuiltinsAssembler) {
+ const char* method_str = "[Async-from-Sync Iterator].prototype.next";
+ Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
+
+ Node* const iterator = Parameter(0);
+ Node* const value = Parameter(1);
+ Node* const context = Parameter(4);
+
+ Node* const native_context = LoadNativeContext(context);
+ Node* const promise = AllocateAndInitJSPromise(context);
+
+ Variable var_parent(this, MachineRepresentation::kTagged);
+ Variable var_exception(this, MachineRepresentation::kTagged);
+ var_exception.Bind(UndefinedConstant());
+ Label reject_promise(this, Label::kDeferred);
+ Label if_receiverisincompatible(this, Label::kDeferred);
+
+ GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
+ GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
+ &if_receiverisincompatible);
+
+ Node* const sync_iterator =
+ LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
+
+ // Let nextResult be IteratorNext(syncIterator, value).
+ // IfAbruptRejectPromise(nextResult, promiseCapability).
+ Node* next_method =
+ CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
+ HeapConstant(factory()->next_string()));
+ Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
+ next_method, sync_iterator, value);
+ GotoIfException(iter_result, &reject_promise, &var_exception);
+
+ Label if_fastpath(this), if_slowpath(this), merge(this);
+ GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
+
+ Node* const fast_iter_result_map =
+ LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
+ Node* const iter_result_map = LoadMap(iter_result);
+
+ Variable var_value(this, MachineRepresentation::kTagged);
+ Variable var_done(this, MachineRepresentation::kTagged);
+ Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
+ &if_slowpath);
+
+ Bind(&if_fastpath);
+ {
+ var_value.Bind(
+ LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
+ var_done.Bind(LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
+ Goto(&merge);
+ }
+
+ Bind(&if_slowpath);
+ {
+ Label if_exception(this, Label::kDeferred);
+
+ // Let nextValue be IteratorValue(nextResult).
+ // IfAbruptRejectPromise(nextValue, promiseCapability).
+ Node* const value =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->value_string()));
+ GotoIfException(value, &reject_promise, &var_exception);
+
+ // Let nextDone be IteratorComplete(nextResult).
+ // IfAbruptRejectPromise(nextDone, promiseCapability).
+ Node* const done =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->done_string()));
+ GotoIfException(done, &reject_promise, &var_exception);
+
+ var_value.Bind(value);
+ var_done.Bind(done);
+ Goto(&merge);
+ }
+
+ Bind(&merge);
+
+ // Convert `done` status to a Boolean value if needed.
+ Label to_boolean(this, Label::kDeferred), wrap(this);
+ GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
+ Branch(IsBooleanMap(LoadMap(var_done.value())), &wrap, &to_boolean);
+
+ Bind(&to_boolean);
+ {
+ var_done.Bind(
+ CallStub(CodeFactory::ToBoolean(isolate()), context, var_done.value()));
+ Goto(&wrap);
+ }
+
+ Bind(&wrap);
+ {
+ Node* const value = var_value.value();
+ Node* const done = var_done.value();
+
+ // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
+ Node* const wrapper = AllocateAndInitJSPromise(context);
+
+ // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined,
+ // « nextValue »).
+ InternalResolvePromise(context, wrapper, value);
+
+ // Let onFulfilled be a new built-in function object as defined in
+ // Async Iterator Value Unwrap Functions.
+ // Set onFulfilled.[[Done]] to nextDone.
+ Node* const map = LoadContextElement(
+ native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
+ Node* const on_resolve_shared = LoadContextElement(
+ native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
+ Node* const closure_context =
+ AllocateAsyncIteratorValueUnwrapContext(native_context, done);
+ Node* const on_resolve = AllocateFunctionWithMapAndContext(
+ map, on_resolve_shared, closure_context);
+
+ // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
+ // onFulfilled, undefined, promiseCapability).
+ FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
+
+ Return(promise);
+ }
+
+ Bind(&if_receiverisincompatible);
+ {
+ // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
+ // internal slot, then
+
+ // Let badIteratorError be a new TypeError exception.
+ Node* const make_type_error =
+ LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
+ Node* const error =
+ CallJS(CodeFactory::Call(isolate()), context, make_type_error,
+ UndefinedConstant(),
+ SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
+ HeapConstant(method_name), iterator);
+
+ // Perform ! Call(promiseCapability.[[Reject]], undefined,
+ // « badIteratorError »).
+ var_exception.Bind(error);
+ Goto(&reject_promise);
+ }
+
+ Bind(&reject_promise);
+ {
+ Node* const promise = AllocateAndInitJSPromise(context);
+ Node* const exception = var_exception.value();
+ CallRuntime(Runtime::kPromiseReject, context, promise, exception,
+ TrueConstant());
+
+ // Return promiseCapability.[[Promise]].
+ Return(promise);
+ }
+}
+
+// https://tc39.github.io/proposal-async-iteration/
+// Section #sec-%asyncfromsynciteratorprototype%.return
+TF_BUILTIN(AsyncFromSyncIteratorPrototypeReturn, AsyncBuiltinsAssembler) {
+ const char* method_str = "[Async-from-Sync Iterator].prototype.return";
+ Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
+
+ Node* const iterator = Parameter(0);
+ Node* const value = Parameter(1);
+ Node* const context = Parameter(4);
+
+ Node* const native_context = LoadNativeContext(context);
+ Node* const promise = AllocateAndInitJSPromise(context);
+
+ Variable var_exception(this, MachineRepresentation::kTagged);
+ var_exception.Bind(UndefinedConstant());
+ Label reject_promise(this, Label::kDeferred);
+ Label if_receiverisincompatible(this, Label::kDeferred);
+
+ GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
+ GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
+ &if_receiverisincompatible);
+
+ Node* const sync_iterator =
+ LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
+
+ // Let return be GetMethod(syncIterator, "return").
+ // IfAbruptRejectPromise(return, promiseCapability).
+ Node* const return_method =
+ CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
+ HeapConstant(factory()->return_string()));
+ GotoIfException(return_method, &reject_promise, &var_exception);
+
+ Variable var_value(this, MachineRepresentation::kTagged);
+ Variable var_done(this, MachineRepresentation::kTagged);
+
+ Label if_isundefined(this), if_isnotundefined(this), resolve_promise(this);
+
+ Branch(IsUndefined(return_method), &if_isundefined, &if_isnotundefined);
+ Bind(&if_isundefined);
+ {
+ // If return is undefined, then
+ // Let iterResult be ! CreateIterResultObject(value, true)
+ Node* const iter_result =
+ CallStub(CodeFactory::CreateIterResultObject(isolate()), context, value,
+ TrueConstant());
+
+ // Perform ! Call(promiseCapability.[[Resolve]], undefined, « iterResult »).
+ // IfAbruptRejectPromise(nextDone, promiseCapability).
+ // Return promiseCapability.[[Promise]].
+ PromiseFulfill(context, promise, iter_result, v8::Promise::kFulfilled);
+ Return(promise);
+ }
+
+ Bind(&if_isnotundefined);
+ {
+ // Let returnResult be Call(return, syncIterator, « value »).
+ Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
+ return_method, sync_iterator, value);
+ // IfAbruptRejectPromise(returnResult, promiseCapability).
+ GotoIfException(iter_result, &reject_promise, &var_exception);
+
+ Label if_fastpath(this), if_slowpath(this), merge(this);
+
+ GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
+
+ Node* const fast_iter_result_map =
+ LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
+ Node* const iter_result_map = LoadMap(iter_result);
+
+ Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
+ &if_slowpath);
+
+ Bind(&if_fastpath);
+ {
+ var_value.Bind(
+ LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
+ var_done.Bind(
+ LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
+ Goto(&merge);
+ }
+
+ Bind(&if_slowpath);
+ {
+ // Let returnValue be IteratorValue(nextResult).
+ // IfAbruptRejectPromise(returnValue, promiseCapability).
+ Node* const value =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->value_string()));
+ GotoIfException(value, &reject_promise, &var_exception);
+
+ // Let returnDone be IteratorComplete(nextResult).
+ // IfAbruptRejectPromise(returnDone, promiseCapability).
+ Node* const done =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->done_string()));
+ GotoIfException(done, &reject_promise, &var_exception);
+
+ var_value.Bind(value);
+ var_done.Bind(done);
+ Goto(&merge);
+ }
+
+ Bind(&merge);
+ {
+ Label to_boolean(this), done(this);
+ GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
+ Branch(IsBooleanMap(LoadMap(var_done.value())), &done, &to_boolean);
+
+ Bind(&to_boolean);
+ {
+ Node* const result = CallStub(CodeFactory::ToBoolean(isolate()),
+ context, var_done.value());
+ var_done.Bind(result);
+ Goto(&done);
+ }
+
+ Bind(&done);
+ Goto(&resolve_promise);
+ }
+ }
+
+ Bind(&resolve_promise);
+ {
+ // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
+ Node* const wrapper = AllocateAndInitJSPromise(context);
+ Node* const value = var_value.value();
+ Node* const done = var_done.value();
+
+ // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined,
+ // « nextValue »).
+ InternalResolvePromise(context, promise, value);
+
+ // Let onFulfilled be a new built-in function object as defined in
+ // Async Iterator Value Unwrap Functions.
+ // Set onFulfilled.[[Done]] to nextDone.
+ Node* const map = LoadContextElement(
+ native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
+ Node* const on_resolve_shared = LoadContextElement(
+ native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
+ Node* const closure_context =
+ AllocateAsyncIteratorValueUnwrapContext(native_context, done);
+ Node* const on_resolve = AllocateFunctionWithMapAndContext(
+ map, on_resolve_shared, closure_context);
+
+ // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
+ // onFulfilled, undefined, promiseCapability).
+ FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
+ Return(promise);
+ }
+
+ Bind(&if_receiverisincompatible);
+ {
+ // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
+ // internal slot, then
+
+ // Let badIteratorError be a new TypeError exception.
+ Node* const make_type_error =
+ LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
+ Node* const error =
+ CallJS(CodeFactory::Call(isolate()), context, make_type_error,
+ UndefinedConstant(),
+ SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
+ HeapConstant(method_name), iterator);
+
+ // Perform ! Call(promiseCapability.[[Reject]], undefined,
+ // « badIteratorError »).
+ var_exception.Bind(error);
+ Goto(&reject_promise);
+ }
+
+ Bind(&reject_promise);
+ {
+ Node* const exception = var_exception.value();
+ CallRuntime(Runtime::kPromiseReject, context, promise, exception,
+ TrueConstant());
+
+ // Return promiseCapability.[[Promise]].
+ Return(promise);
+ }
+}
+
+// https://tc39.github.io/proposal-async-iteration/
+// Section #sec-%asyncfromsynciteratorprototype%.throw
+TF_BUILTIN(AsyncFromSyncIteratorPrototypeThrow, AsyncBuiltinsAssembler) {
+ const char* method_str = "[Async-from-Sync Iterator].prototype.throw";
+ Handle<String> method_name = factory()->NewStringFromAsciiChecked(method_str);
+
+ Node* const iterator = Parameter(0);
+ Node* const value = Parameter(1);
+ Node* const context = Parameter(4);
+
+ Node* const native_context = LoadNativeContext(context);
+ Node* const promise = AllocateAndInitJSPromise(context);
+
+ Variable var_exception(this, MachineRepresentation::kTagged);
+ var_exception.Bind(value);
+ Variable var_value(this, MachineRepresentation::kTagged);
+ Variable var_done(this, MachineRepresentation::kTagged);
+ Label reject_promise(this, Label::kDeferred);
+ Label if_receiverisincompatible(this, Label::kDeferred);
+
+ GotoIf(TaggedIsSmi(iterator), &if_receiverisincompatible);
+ GotoUnless(HasInstanceType(iterator, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE),
+ &if_receiverisincompatible);
+
+ Node* const sync_iterator =
+ LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset);
+
+ // Let throw be GetMethod(syncIterator, "throw").
+ // IfAbruptRejectPromise(nextResult, promiseCapability).
+ // IfAbruptRejectPromise(throw, promiseCapability).
+ Node* const throw_method =
+ CallStub(CodeFactory::GetProperty(isolate()), context, sync_iterator,
+ HeapConstant(factory()->throw_string()));
+ GotoIfException(throw_method, &reject_promise, &var_exception);
+
+ Label if_isnotundefined(this), resolve_promise(this);
+
+ // If throw is undefined, then
+ // Perform ! Call(promiseCapability.[[Reject]], undefined, « value »).
+ Branch(IsUndefined(throw_method), &reject_promise, &if_isnotundefined);
+
+ Bind(&if_isnotundefined);
+ {
+ // Let throwResult be Call(throw, syncIterator, « value »).
+ Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context,
+ throw_method, sync_iterator, value);
+ GotoIfException(iter_result, &reject_promise, &var_exception);
+
+ Label if_fastpath(this), if_slowpath(this), merge(this);
+
+ GotoIf(TaggedIsSmi(iter_result), &if_slowpath);
+
+ Node* const fast_iter_result_map =
+ LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
+ Node* const iter_result_map = LoadMap(iter_result);
+
+ Branch(WordEqual(iter_result_map, fast_iter_result_map), &if_fastpath,
+ &if_slowpath);
+
+ Bind(&if_fastpath);
+ {
+ var_value.Bind(
+ LoadObjectField(iter_result, JSIteratorResult::kValueOffset));
+ var_done.Bind(
+ LoadObjectField(iter_result, JSIteratorResult::kDoneOffset));
+ Goto(&merge);
+ }
+
+ Bind(&if_slowpath);
+ {
+ // Let throwValue be IteratorValue(nextResult).
+ // IfAbruptRejectPromise(throwValue, promiseCapability).
+ Node* const value =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->value_string()));
+ GotoIfException(value, &reject_promise, &var_exception);
+
+ // Let throwDone be IteratorComplete(nextResult).
+ // IfAbruptRejectPromise(throwDone, promiseCapability).
+ Node* const done =
+ CallStub(CodeFactory::GetProperty(isolate()), context, iter_result,
+ HeapConstant(factory()->done_string()));
+ GotoIfException(done, &reject_promise, &var_exception);
+
+ var_value.Bind(value);
+ var_done.Bind(done);
+ Goto(&merge);
+ }
+
+ Bind(&merge);
+
+ // Convert `done` status to a Boolean value if needed.
+ Label to_boolean(this, Label::kDeferred), wrap(this);
+ GotoIf(TaggedIsSmi(var_done.value()), &to_boolean);
+ Branch(IsBooleanMap(LoadMap(var_done.value())), &wrap, &to_boolean);
+
+ Bind(&to_boolean);
+ {
+ var_done.Bind(CallStub(CodeFactory::ToBoolean(isolate()), context,
+ var_done.value()));
+ Goto(&wrap);
+ }
+
+ Bind(&wrap);
+ {
+ // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%).
+ Node* const wrapper = AllocateAndInitJSPromise(context);
+ Node* const value = var_value.value();
+ Node* const done = var_done.value();
+
+ // Perform ! Call(valueWrapperCapability.[[Resolve]], undefined, «
+ // throwValue »).
+ InternalResolvePromise(context, wrapper, value);
+
+ // Let onFulfilled be a new built-in function object as defined in
+ // Async Iterator Value Unwrap Functions.
+ // Set onFulfilled.[[Done]] to throwDone.
+ Node* const map = LoadContextElement(
+ native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
+ Node* const on_resolve_shared = LoadContextElement(
+ native_context, Context::ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN);
+ Node* const closure_context =
+ AllocateAsyncIteratorValueUnwrapContext(native_context, done);
+ Node* const on_resolve = AllocateFunctionWithMapAndContext(
+ map, on_resolve_shared, closure_context);
+
+ // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]],
+ // onFulfilled, undefined, promiseCapability).
+ FastPerformPromiseThen(context, wrapper, on_resolve, nullptr, promise);
+ Return(promise);
+ }
+ }
+
+ Bind(&if_receiverisincompatible);
+ {
+ // If Type(O) is not Object, or if O does not have a [[SyncIterator]]
+ // internal slot, then
+
+ // Let badIteratorError be a new TypeError exception.
+ Node* const make_type_error =
+ LoadContextElement(native_context, Context::MAKE_TYPE_ERROR_INDEX);
+ Node* const error =
+ CallJS(CodeFactory::Call(isolate()), context, make_type_error,
+ UndefinedConstant(),
+ SmiConstant(MessageTemplate::kIncompatibleMethodReceiver),
+ HeapConstant(method_name), iterator);
+
+ // Perform ! Call(promiseCapability.[[Reject]], undefined,
+ // « badIteratorError »).
+ var_exception.Bind(error);
+ Goto(&reject_promise);
+ }
+
+ Bind(&reject_promise);
+ {
+ Node* const exception = var_exception.value();
+ CallRuntime(Runtime::kPromiseReject, context, promise, exception,
+ TrueConstant());
+
+ // Return promiseCapability.[[Promise]].
+ Return(promise);
+ }
+}
+
+TF_BUILTIN(AsyncIteratorValueUnwrap, AsyncBuiltinsAssembler) {
+ Node* const value = Parameter(1);
+ Node* const context = Parameter(4);
+
+ Node* const done = LoadContextElement(context, ValueUnwrapContext::kDoneSlot);
+ Node* const unwrapped_value = CallStub(
+ CodeFactory::CreateIterResultObject(isolate()), context, value, done);
+
+ Return(unwrapped_value);
+}
+
+Node* AsyncBuiltinsAssembler::AllocateAsyncIteratorValueUnwrapContext(
+ Node* native_context, Node* done) {
+ Node* const context =
+ CreatePromiseContext(native_context, ValueUnwrapContext::kLength);
+ StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kDoneSlot,
+ done);
+ return context;
+}
+
+} // namespace internal
+} // namespace v8
« 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