Commit 5bc39360 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[promise] Construct promises through API in C++, rather than CSA.

We don't need to pay the cost of going to JS to simply stamp out a
new object.

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I8942771ff9c19dff1908243fd6d3bd701d3fb5a3
Reviewed-on: https://chromium-review.googlesource.com/897803
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51267}
parent fc04f8ce
......@@ -7374,13 +7374,11 @@ Local<Array> Set::AsArray() const {
MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver);
i::Handle<i::Object> result;
Local<Promise::Resolver> result;
has_pending_exception =
!i::Execution::Call(isolate, isolate->promise_internal_constructor(),
isolate->factory()->undefined_value(), 0, nullptr)
.ToHandle(&result);
!ToLocal<Promise::Resolver>(isolate->factory()->NewJSPromise(), &result);
RETURN_ON_FAILED_EXECUTION(Promise::Resolver);
RETURN_ESCAPED(Local<Promise::Resolver>::Cast(Utils::ToLocal(result)));
RETURN_ESCAPED(result);
}
......
......@@ -2422,15 +2422,6 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Handle<Map> prototype_map(prototype->map());
Map::SetShouldBeFastPrototypeMap(prototype_map, true, isolate);
{ // Internal: PromiseInternalConstructor
// Also exposed as extrasUtils.createPromise.
Handle<JSFunction> function =
SimpleCreateFunction(isolate, factory->empty_string(),
Builtins::kPromiseInternalConstructor, 1, true);
function->shared()->set_native(false);
native_context()->set_promise_internal_constructor(*function);
}
{ // Internal: IsPromise
Handle<JSFunction> function = SimpleCreateFunction(
isolate, factory->empty_string(), Builtins::kIsPromise, 1, false);
......@@ -4638,7 +4629,11 @@ bool Genesis::InstallNatives(GlobalContextType context_type) {
InstallInternalArray(extras_utils, "InternalPackedArray", PACKED_ELEMENTS);
InstallFunction(extras_utils, isolate()->promise_internal_constructor(),
Handle<JSFunction> promise_internal_constructor =
SimpleCreateFunction(isolate(), factory()->empty_string(),
Builtins::kPromiseInternalConstructor, 1, true);
promise_internal_constructor->shared()->set_native(false);
InstallFunction(extras_utils, promise_internal_constructor,
factory()->NewStringFromAsciiChecked("createPromise"));
InstallFunction(extras_utils, isolate()->is_promise(),
factory()->NewStringFromAsciiChecked("isPromise"));
......
......@@ -3113,6 +3113,19 @@ Handle<Map> Factory::CreateClassFunctionMap(Handle<JSFunction> empty_function) {
return map;
}
Handle<JSPromise> Factory::NewJSPromiseWithoutHook(PretenureFlag pretenure) {
CALL_HEAP_FUNCTION(isolate(),
isolate()->heap()->AllocateJSPromise(
*isolate()->promise_function(), pretenure),
JSPromise);
}
Handle<JSPromise> Factory::NewJSPromise(PretenureFlag pretenure) {
Handle<JSPromise> promise = NewJSPromiseWithoutHook(pretenure);
isolate()->RunPromiseHook(PromiseHookType::kInit, promise, undefined_value());
return promise;
}
// static
NewFunctionArgs NewFunctionArgs::ForWasm(Handle<String> name, Handle<Code> code,
Handle<Map> map) {
......
......@@ -856,6 +856,10 @@ class V8_EXPORT_PRIVATE Factory final {
// Converts the given ToPrimitive hint to it's string representation.
Handle<String> ToPrimitiveHintString(ToPrimitiveHint hint);
Handle<JSPromise> NewJSPromiseWithoutHook(
PretenureFlag pretenure = NOT_TENURED);
Handle<JSPromise> NewJSPromise(PretenureFlag pretenure = NOT_TENURED);
private:
Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
......
......@@ -3407,6 +3407,21 @@ AllocationResult Heap::Allocate(Map* map, AllocationSpace space,
return result;
}
AllocationResult Heap::AllocateJSPromise(JSFunction* constructor,
PretenureFlag pretenure) {
AllocationResult allocation = AllocateJSObject(constructor, pretenure);
JSPromise* promise = nullptr;
if (!allocation.To(&promise)) return allocation;
// Setup JSPromise fields
promise->set_reactions_or_result(Smi::kZero);
promise->set_flags(0);
for (int i = 0; i < v8::Promise::kEmbedderFieldCount; i++) {
promise->SetEmbedderField(i, Smi::kZero);
}
return promise;
}
void Heap::InitializeJSObjectFromMap(JSObject* obj, Object* properties,
Map* map) {
obj->set_raw_properties_or_hash(properties);
......
......@@ -2304,6 +2304,9 @@ class Heap {
Movability movability, uint32_t stub_key, bool is_turbofanned,
int stack_slots, int safepoint_table_offset);
MUST_USE_RESULT AllocationResult AllocateJSPromise(
JSFunction* constructor, PretenureFlag pretenure = NOT_TENURED);
void set_force_oom(bool value) { force_oom_ = value; }
// ===========================================================================
......
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