Commit 4173fda4 authored by caitp's avatar caitp Committed by Commit bot

[builtins] introduce AsyncBuiltinsAssembler for ES2016 features and beyond

First step in splitting apart https://codereview.chromium.org/2622833002/ to
land piece by piece.

Porting src/js/async-await.js to TF builtins using this boilerplate is
now very straightforward.

BUG=v8:4483, v8:5855
R=jgruber@chromium.org, gsathya@chromium.org

Review-Url: https://codereview.chromium.org/2635353002
Cr-Commit-Position: refs/heads/master@{#42466}
parent d62914f6
......@@ -969,6 +969,8 @@ v8_source_set("v8_base") {
"src/builtins/builtins-api.cc",
"src/builtins/builtins-array.cc",
"src/builtins/builtins-arraybuffer.cc",
"src/builtins/builtins-async.cc",
"src/builtins/builtins-async.h",
"src/builtins/builtins-boolean.cc",
"src/builtins/builtins-call.cc",
"src/builtins/builtins-callsite.cc",
......
// 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 {
Node* AsyncBuiltinsAssembler::Await(
Node* context, Node* generator, Node* value, Node* outer_promise,
const NodeGenerator1& create_closure_context, int on_resolve_context_index,
int on_reject_context_index, bool is_catchable) {
// Let promiseCapability be ! NewPromiseCapability(%Promise%).
Node* const wrapped_value = AllocateAndInitJSPromise(context);
// Perform ! Call(promiseCapability.[[Resolve]], undefined, « promise »).
InternalResolvePromise(context, wrapped_value, value);
Node* const native_context = LoadNativeContext(context);
Node* const closure_context = create_closure_context(native_context);
Node* const map = LoadContextElement(
native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
// Load and allocate on_resolve closure
Node* const on_resolve_shared_fun =
LoadContextElement(native_context, on_resolve_context_index);
CSA_SLOW_ASSERT(
this, HasInstanceType(on_resolve_shared_fun, SHARED_FUNCTION_INFO_TYPE));
Node* const on_resolve = AllocateFunctionWithMapAndContext(
map, on_resolve_shared_fun, closure_context);
// Load and allocate on_reject closure
Node* const on_reject_shared_fun =
LoadContextElement(native_context, on_reject_context_index);
CSA_SLOW_ASSERT(
this, HasInstanceType(on_reject_shared_fun, SHARED_FUNCTION_INFO_TYPE));
Node* const on_reject = AllocateFunctionWithMapAndContext(
map, on_reject_shared_fun, closure_context);
Node* const throwaway_promise =
AllocateAndInitJSPromise(context, wrapped_value);
// The Promise will be thrown away and not handled, but it shouldn't trigger
// unhandled reject events as its work is done
PromiseSetHasHandler(throwaway_promise);
Label do_perform_promise_then(this);
GotoUnless(IsDebugActive(), &do_perform_promise_then);
{
Label common(this);
GotoUnless(HasInstanceType(value, JS_PROMISE_TYPE), &common);
{
// Mark the reject handler callback to be a forwarding edge, rather
// than a meaningful catch handler
Node* const key =
HeapConstant(factory()->promise_forwarding_handler_symbol());
CallRuntime(Runtime::kSetProperty, on_reject, key, TrueConstant(),
SmiConstant(STRICT));
if (!is_catchable) {
PromiseSetHasHandler(value);
}
}
Goto(&common);
Bind(&common);
// Mark the dependency to outer Promise in case the throwaway Promise is
// found on the Promise stack
CSA_SLOW_ASSERT(this, HasInstanceType(outer_promise, JS_PROMISE_TYPE));
Node* const key = HeapConstant(factory()->promise_handled_by_symbol());
CallRuntime(Runtime::kSetProperty, throwaway_promise, key, outer_promise,
SmiConstant(STRICT));
}
Goto(&do_perform_promise_then);
Bind(&do_perform_promise_then);
InternalPerformPromiseThen(context, wrapped_value, on_resolve, on_reject,
throwaway_promise, UndefinedConstant(),
UndefinedConstant());
return wrapped_value;
}
} // namespace internal
} // namespace v8
// 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.
#ifndef V8_BUILTINS_BUILTINS_ASYNC_H_
#define V8_BUILTINS_BUILTINS_ASYNC_H_
#include "src/builtins/builtins-promise.h"
namespace v8 {
namespace internal {
class AsyncBuiltinsAssembler : public PromiseBuiltinsAssembler {
public:
explicit AsyncBuiltinsAssembler(CodeAssemblerState* state)
: PromiseBuiltinsAssembler(state) {}
protected:
typedef std::function<Node*(Node*)> NodeGenerator1;
// Perform steps to resume generator after `value` is resolved.
// `on_reject_context_index` is an index into the Native Context, which should
// point to a SharedFunctioninfo instance used to create the closure. The
// value following the reject index should be a similar value for the resolve
// closure. Returns the Promise-wrapped `value`.
Node* Await(Node* context, Node* generator, Node* value, Node* outer_promise,
const NodeGenerator1& create_closure_context,
int on_resolve_context_index, int on_reject_context_index,
bool is_catchable);
};
} // namespace internal
} // namespace v8
#endif // V8_BUILTINS_BUILTINS_ASYNC_H_
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BUILTINS_BUILTINS_PROMISE_H_
#define V8_BUILTINS_BUILTINS_PROMISE_H_
#include "src/code-stub-assembler.h"
#include "src/contexts.h"
......@@ -118,3 +121,5 @@ class PromiseBuiltinsAssembler : public CodeStubAssembler {
} // namespace internal
} // namespace v8
#endif // V8_BUILTINS_BUILTINS_PROMISE_H_
......@@ -480,6 +480,8 @@
'builtins/builtins-api.cc',
'builtins/builtins-arraybuffer.cc',
'builtins/builtins-array.cc',
'builtins/builtins-async.cc',
'builtins/builtins-async.h',
'builtins/builtins-boolean.cc',
'builtins/builtins-call.cc',
'builtins/builtins-callsite.cc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment