[wasm] Deduplicate continuation-resuming callback

For stack-switching, we create a callable object from the
WasmResume builtin and pass that as the onFulfilled argument
of Promise#then. We don't need to create this callable object each time
we suspend. Instead, create it when we initialize the Suspender object
and store it there.

R=jkummerow@chromium.org
CC=fgm@chromium.org

Bug: v8:12191
Change-Id: If8495493a71794cddc81b21a17a821fed8f4ede7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3579162
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Thibaud Michaud <thibaudm@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79930}
This commit is contained in:
Thibaud Michaud 2022-04-08 15:35:37 +02:00 committed by V8 LUCI CQ
parent 09bcc433f1
commit a108b813fe
3 changed files with 13 additions and 14 deletions

View File

@ -791,20 +791,7 @@ RUNTIME_FUNCTION(Runtime_WasmCreateResumePromise) {
Handle<Object> promise = args.at(0);
Handle<WasmSuspenderObject> suspender = args.at<WasmSuspenderObject>(1);
// Instantiate onFulfilled callback.
Handle<WasmOnFulfilledData> function_data =
isolate->factory()->NewWasmOnFulfilledData(suspender);
Handle<SharedFunctionInfo> shared =
isolate->factory()->NewSharedFunctionInfoForWasmOnFulfilled(
function_data);
Handle<Context> context(isolate->native_context());
Handle<Map> function_map = isolate->strict_function_map();
Handle<JSObject> on_fulfilled =
Factory::JSFunctionBuilder{isolate, shared, context}
.set_map(function_map)
.Build();
i::Handle<i::Object> argv[] = {on_fulfilled};
i::Handle<i::Object> argv[] = {handle(suspender->resume(), isolate)};
i::Handle<i::Object> result;
bool has_pending_exception =
!i::Execution::CallBuiltin(isolate, isolate->promise_then(), promise,

View File

@ -1810,6 +1810,17 @@ Handle<WasmSuspenderObject> WasmSuspenderObject::New(Isolate* isolate) {
suspender->set_continuation(ReadOnlyRoots(isolate).undefined_value());
suspender->set_parent(ReadOnlyRoots(isolate).undefined_value());
suspender->set_state(Inactive);
// Instantiate the callable object which resumes this Suspender. This will be
// used implicitly as the onFulfilled callback of the returned JS promise.
Handle<WasmOnFulfilledData> function_data =
isolate->factory()->NewWasmOnFulfilledData(suspender);
Handle<SharedFunctionInfo> shared =
isolate->factory()->NewSharedFunctionInfoForWasmOnFulfilled(
function_data);
Handle<Context> context(isolate->native_context());
Handle<JSObject> resume =
Factory::JSFunctionBuilder{isolate, shared, context}.Build();
suspender->set_resume(*resume);
return suspender;
}

View File

@ -103,6 +103,7 @@ extern class WasmContinuationObject extends Struct {
extern class WasmSuspenderObject extends JSObject {
continuation: WasmContinuationObject|Undefined;
parent: WasmSuspenderObject|Undefined;
resume: JSObject;
state: Smi; // 0: Inactive, 1: Active, 2: Suspended.
}