Chromium Code Reviews| 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..18cba397707f892af0ae6ff1ea8fe0b98c20d067 |
| --- /dev/null |
| +++ b/src/builtins/builtins-async-iterator.cc |
| @@ -0,0 +1,421 @@ |
| +// Copyright 2017 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 { |
| + |
| +namespace { |
| + |
| +// Describe fields of Context associated with the AsyncIterator unwrap closure. |
| +class ValueUnwrapContext { |
| + public: |
| + enum Fields { kDoneSlot = Context::MIN_CONTEXT_SLOTS, kLength }; |
| +}; |
| + |
| +class AsyncFromSyncBuiltinsAssembler : public AsyncBuiltinsAssembler { |
| + public: |
| + explicit AsyncFromSyncBuiltinsAssembler(CodeAssemblerState* state) |
| + : AsyncBuiltinsAssembler(state) {} |
| + |
| + void ThrowIfNotAsyncFromSyncIterator(Node* const context, Node* const object, |
| + Label* if_exception, |
| + Variable* var_exception, |
| + const char* method_name); |
| + |
| + // Load "value" and "done" from an iterator result object. If an exception |
| + // is thrown at any point, jumps to te `if_exception` label with exception |
| + // stored in `var_exception`. |
| + // |
| + // Returns a Pair of Nodes, whose first element is the value of the "value" |
| + // property, and whose second element is the value of the "done" property, |
| + // converted to a Boolean if needed. |
| + std::pair<Node*, Node*> LoadIteratorResult(Node* const context, |
| + Node* const native_context, |
| + Node* const iter_result, |
| + Label* if_exception, |
| + Variable* var_exception); |
| + |
| + Node* CreateUnwrapClosure(Node* const native_context, Node* const done); |
| +}; |
| + |
| +void AsyncFromSyncBuiltinsAssembler::ThrowIfNotAsyncFromSyncIterator( |
| + Node* const context, Node* const object, Label* if_exception, |
| + Variable* var_exception, const char* method_name) { |
| + Label if_receiverisincompatible(this, Label::kDeferred), done(this); |
| + |
| + GotoIf(TaggedIsSmi(object), &if_receiverisincompatible); |
| + Branch(HasInstanceType(object, JS_ASYNC_FROM_SYNC_ITERATOR_TYPE), &done, |
| + &if_receiverisincompatible); |
| + |
| + 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 error = |
| + MakeTypeError(MessageTemplate::kIncompatibleMethodReceiver, context, |
| + CStringConstant(method_name), object); |
| + |
| + // Perform ! Call(promiseCapability.[[Reject]], undefined, |
| + // « badIteratorError »). |
| + var_exception->Bind(error); |
| + Goto(if_exception); |
| + } |
| + |
| + Bind(&done); |
| +} |
| + |
| +std::pair<Node*, Node*> AsyncFromSyncBuiltinsAssembler::LoadIteratorResult( |
| + Node* const context, Node* const native_context, Node* const iter_result, |
| + Label* if_exception, Variable* var_exception) { |
| + Label if_fastpath(this), if_slowpath(this), merge(this), to_boolean(this), |
| + done(this); |
| + GotoIf(TaggedIsSmi(iter_result), &if_slowpath); |
|
neis
2017/02/20 11:27:59
I think LoadIteratorResult should either assume th
|
| + |
| + 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); |
| + { |
| + // Let nextValue be IteratorValue(nextResult). |
| + // IfAbruptRejectPromise(nextValue, promiseCapability). |
| + Node* const value = |
| + GetProperty(context, iter_result, factory()->value_string()); |
| + GotoIfException(value, if_exception, var_exception); |
| + |
| + // Let nextDone be IteratorComplete(nextResult). |
| + // IfAbruptRejectPromise(nextDone, promiseCapability). |
| + Node* const done = |
| + GetProperty(context, iter_result, factory()->done_string()); |
| + GotoIfException(done, if_exception, var_exception); |
| + |
| + var_value.Bind(value); |
| + var_done.Bind(done); |
| + Goto(&merge); |
| + } |
| + |
| + Bind(&merge); |
| + // Ensure `iterResult.done` is a Boolean. |
| + GotoIf(TaggedIsSmi(var_done.value()), &to_boolean); |
| + Branch(IsBoolean(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); |
| + return std::make_pair(var_value.value(), var_done.value()); |
| +} |
| + |
| +Node* AsyncFromSyncBuiltinsAssembler::CreateUnwrapClosure(Node* native_context, |
|
neis
2017/02/20 11:27:59
It's funny that they call this "unwrap" function.
|
| + Node* done) { |
| + 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); |
| + CSA_ASSERT(this, |
| + HasInstanceType(on_resolve_shared, SHARED_FUNCTION_INFO_TYPE)); |
| + Node* const closure_context = |
| + AllocateAsyncIteratorValueUnwrapContext(native_context, done); |
| + return AllocateFunctionWithMapAndContext(map, on_resolve_shared, |
| + closure_context); |
| +} |
| +} // namespace |
| + |
| +// https://tc39.github.io/proposal-async-iteration/ |
| +// Section #sec-%asyncfromsynciteratorprototype%.next |
| +TF_BUILTIN(AsyncFromSyncIteratorPrototypeNext, AsyncFromSyncBuiltinsAssembler) { |
| + Node* const iterator = Parameter(0); |
| + Node* const parameter = Parameter(1); |
|
neis
2017/02/20 11:27:59
nit: call this "value", then it will also match th
caitp
2017/02/20 17:09:35
I opted to call it `parameter` so that I could use
neis
2017/02/21 12:48:08
Fine too.
|
| + Node* const context = Parameter(4); |
| + |
| + Node* const native_context = LoadNativeContext(context); |
| + Node* const promise = AllocateAndInitJSPromise(context); |
| + |
| + Variable var_exception(this, MachineRepresentation::kTagged, |
| + UndefinedConstant()); |
| + Label reject_promise(this, Label::kDeferred); |
| + |
| + ThrowIfNotAsyncFromSyncIterator(context, iterator, &reject_promise, |
| + &var_exception, |
| + "[Async-from-Sync Iterator].prototype.next"); |
| + |
| + Node* const sync_iterator = |
| + LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); |
| + |
| + // Let nextResult be IteratorNext(syncIterator, value). |
| + // IfAbruptRejectPromise(nextResult, promiseCapability). |
| + Node* next_method = |
| + GetProperty(context, sync_iterator, factory()->next_string()); |
| + |
| + Node* const iter_result = CallJS(CodeFactory::Call(isolate()), context, |
| + next_method, sync_iterator, parameter); |
| + GotoIfException(iter_result, &reject_promise, &var_exception); |
| + |
|
neis
2017/02/20 11:27:58
This seems to be missing the Object check of Itera
caitp
2017/02/20 17:09:35
Added the object check in LoadIteratorResult().
|
| + Node* value; |
| + Node* done; |
| + std::tie(value, done) = LoadIteratorResult( |
| + context, native_context, iter_result, &reject_promise, &var_exception); |
| + |
| + // 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 on_resolve = CreateUnwrapClosure(native_context, done); |
|
neis
2017/02/20 11:27:59
nit: s/on_resolve/on_fulfilled/
caitp
2017/02/20 17:09:35
done
|
| + |
| + // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], |
| + // onFulfilled, undefined, promiseCapability). |
| + Node* const undefined = UndefinedConstant(); |
| + InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, |
| + undefined, undefined); |
| + Return(promise); |
| + |
| + Bind(&reject_promise); |
| + { |
| + Node* const exception = var_exception.value(); |
| + InternalPromiseReject(context, promise, exception, TrueConstant()); |
| + |
| + // Return promiseCapability.[[Promise]]. |
| + Return(promise); |
| + } |
| +} |
| + |
| +// https://tc39.github.io/proposal-async-iteration/ |
| +// Section #sec-%asyncfromsynciteratorprototype%.return |
| +TF_BUILTIN(AsyncFromSyncIteratorPrototypeReturn, |
| + AsyncFromSyncBuiltinsAssembler) { |
| + Node* const iterator = Parameter(0); |
| + Node* const parameter = Parameter(1); |
| + Node* const context = Parameter(4); |
| + |
| + Node* const native_context = LoadNativeContext(context); |
| + Node* const promise = AllocateAndInitJSPromise(context); |
| + |
| + Variable var_exception(this, MachineRepresentation::kTagged, |
| + UndefinedConstant()); |
| + Label reject_promise(this, Label::kDeferred); |
| + |
| + ThrowIfNotAsyncFromSyncIterator( |
| + context, iterator, &reject_promise, &var_exception, |
| + "[Async-from-Sync Iterator].prototype.return"); |
| + |
| + Node* const sync_iterator = |
| + LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); |
| + |
| + // Let return be GetMethod(syncIterator, "return"). |
| + // IfAbruptRejectPromise(return, promiseCapability). |
| + Node* const return_method = |
| + GetProperty(context, sync_iterator, factory()->return_string()); |
| + GotoIfException(return_method, &reject_promise, &var_exception); |
| + |
| + Variable var_value(this, MachineRepresentation::kTagged); |
| + Variable var_done(this, MachineRepresentation::kTagged); |
|
neis
2017/02/20 11:27:58
These seem unnecessary, see below.
|
| + |
| + 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, |
| + parameter, 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, parameter); |
| + // IfAbruptRejectPromise(returnResult, promiseCapability). |
| + GotoIfException(iter_result, &reject_promise, &var_exception); |
| + |
| + Node* value; |
| + Node* done; |
| + std::tie(value, done) = LoadIteratorResult( |
| + context, native_context, iter_result, &reject_promise, &var_exception); |
| + |
| + var_value.Bind(value); |
| + var_done.Bind(done); |
| + Goto(&resolve_promise); |
| + } |
| + |
|
neis
2017/02/20 11:27:59
You should get rid of var_value, var_done, resolve
caitp
2017/02/20 17:09:35
Done.
|
| + 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, 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 on_resolve = CreateUnwrapClosure(native_context, done); |
| + |
| + // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], |
| + // onFulfilled, undefined, promiseCapability). |
| + Node* const undefined = UndefinedConstant(); |
| + InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, |
| + undefined, undefined); |
| + Return(promise); |
| + } |
|
neis
2017/02/20 11:27:59
Could you try to factor out this code? Seems to b
caitp
2017/02/20 17:09:35
Done.
|
| + |
| + Bind(&reject_promise); |
| + { |
| + Node* const exception = var_exception.value(); |
| + InternalPromiseReject(context, promise, exception, TrueConstant()); |
| + |
| + // Return promiseCapability.[[Promise]]. |
| + Return(promise); |
| + } |
| +} |
| + |
| +// https://tc39.github.io/proposal-async-iteration/ |
| +// Section #sec-%asyncfromsynciteratorprototype%.throw |
| +TF_BUILTIN(AsyncFromSyncIteratorPrototypeThrow, |
| + AsyncFromSyncBuiltinsAssembler) { |
| + Node* const iterator = Parameter(0); |
| + Node* const parameter = Parameter(1); |
| + Node* const context = Parameter(4); |
| + |
| + Node* const native_context = LoadNativeContext(context); |
| + Node* const promise = AllocateAndInitJSPromise(context); |
| + |
| + Variable var_exception(this, MachineRepresentation::kTagged, parameter); |
| + Variable var_value(this, MachineRepresentation::kTagged); |
| + Variable var_done(this, MachineRepresentation::kTagged); |
| + Label reject_promise(this, Label::kDeferred); |
| + |
| + ThrowIfNotAsyncFromSyncIterator(context, iterator, &reject_promise, |
| + &var_exception, |
| + "[Async-from-Sync Iterator].prototype.throw"); |
| + |
| + Node* const sync_iterator = |
| + LoadObjectField(iterator, JSAsyncFromSyncIterator::kSyncIteratorOffset); |
| + |
| + // Let throw be GetMethod(syncIterator, "throw"). |
| + // IfAbruptRejectPromise(nextResult, promiseCapability). |
|
neis
2017/02/20 11:27:59
This shouldn't be here.
|
| + // IfAbruptRejectPromise(throw, promiseCapability). |
| + Node* const throw_method = |
| + GetProperty(context, sync_iterator, 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, parameter); |
| + GotoIfException(iter_result, &reject_promise, &var_exception); |
| + |
| + Node* value; |
| + Node* done; |
| + std::tie(value, done) = LoadIteratorResult( |
| + context, native_context, iter_result, &reject_promise, &var_exception); |
| + |
| + // Let valueWrapperCapability be ! NewPromiseCapability(%Promise%). |
| + Node* const wrapper = AllocateAndInitJSPromise(context); |
| + |
| + // 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 on_resolve = CreateUnwrapClosure(native_context, done); |
| + |
| + // Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], |
| + // onFulfilled, undefined, promiseCapability). |
| + Node* const undefined = UndefinedConstant(); |
| + InternalPerformPromiseThen(context, wrapper, on_resolve, undefined, promise, |
| + undefined, undefined); |
| + Return(promise); |
| + } |
| + |
| + Bind(&reject_promise); |
| + { |
| + Node* const exception = var_exception.value(); |
| + InternalPromiseReject(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); |
| + CSA_ASSERT(this, IsBoolean(done)); |
| + |
| + Node* const unwrapped_value = CallStub( |
| + CodeFactory::CreateIterResultObject(isolate()), context, value, done); |
| + |
| + Return(unwrapped_value); |
| +} |
| + |
| +Node* AsyncBuiltinsAssembler::AllocateAsyncIteratorValueUnwrapContext( |
| + Node* native_context, Node* done) { |
| + CSA_ASSERT(this, IsNativeContext(native_context)); |
| + CSA_ASSERT(this, IsBoolean(done)); |
| + |
| + Node* const context = |
| + CreatePromiseContext(native_context, ValueUnwrapContext::kLength); |
| + StoreContextElementNoWriteBarrier(context, ValueUnwrapContext::kDoneSlot, |
| + done); |
| + return context; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace v8 |