Commit 7e0294dc authored by Andreas Haas's avatar Andreas Haas Committed by V8 LUCI CQ

[wasm][api] Introduce API to let the embedder resolve wasm promises

Asynchronous WebAssembly compilation returns the compilation result
through resolving a result promise. So far the result promise was
resolved through V8-internal APIs. This caused problems, because
resolving promises requires correct handling of microtasks, and
microtasks are controlled by the embedder, and not by V8.

This CL adds an API to allow the embedder to resolve the result
promise itself, and handle microtasks as necessary.

The use of the new API is available in a full CL, without API dance:
https://chromium-review.googlesource.com/c/v8/v8/+/3694975

R=cbruni@chromium.org

Bug: v8:12953
Change-Id: Ie9a56041f2d3e0c46664f1938e995f1e2c22f981
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3695584Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81141}
parent 9244f7d8
......@@ -12,6 +12,7 @@
#include "cppgc/common.h"
#include "v8-data.h" // NOLINT(build/include_directory)
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-promise.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
#if defined(V8_OS_WIN)
......@@ -312,6 +313,13 @@ using ApiImplementationCallback = void (*)(const FunctionCallbackInfo<Value>&);
// --- Callback for WebAssembly.compileStreaming ---
using WasmStreamingCallback = void (*)(const FunctionCallbackInfo<Value>&);
enum class WasmAsyncSuccess { kSuccess, kFail };
// --- Callback called when async WebAssembly operations finish ---
using WasmAsyncResolvePromiseCallback = void (*)(
Isolate* isolate, Local<Context> context, Local<Promise::Resolver> resolver,
Local<Value> result, WasmAsyncSuccess success);
// --- Callback for loading source map file for Wasm profiling support
using WasmLoadSourceMapCallback = Local<String> (*)(Isolate* isolate,
const char* name);
......
......@@ -1523,6 +1523,9 @@ class V8_EXPORT Isolate {
void SetWasmStreamingCallback(WasmStreamingCallback callback);
void SetWasmAsyncResolvePromiseCallback(
WasmAsyncResolvePromiseCallback callback);
void SetWasmLoadSourceMapCallback(WasmLoadSourceMapCallback callback);
void SetWasmSimdEnabledCallback(WasmSimdEnabledCallback callback);
......
......@@ -9390,6 +9390,10 @@ CALLBACK_SETTER(WasmInstanceCallback, ExtensionCallback, wasm_instance_callback)
CALLBACK_SETTER(WasmStreamingCallback, WasmStreamingCallback,
wasm_streaming_callback)
CALLBACK_SETTER(WasmAsyncResolvePromiseCallback,
WasmAsyncResolvePromiseCallback,
wasm_async_resolve_promise_callback)
CALLBACK_SETTER(WasmLoadSourceMapCallback, WasmLoadSourceMapCallback,
wasm_load_source_map_callback)
......
......@@ -5709,5 +5709,19 @@ ExternalPointer_t Isolate::EncodeWaiterQueueNodeAsExternalPointer(
}
#endif // V8_SANDBOXED_EXTERNAL_POINTERS
void DefaultWasmAsyncResolvePromiseCallback(
v8::Isolate* isolate, v8::Local<v8::Context> context,
v8::Local<v8::Promise::Resolver> resolver,
v8::Local<v8::Value> compilation_result, WasmAsyncSuccess success) {
MicrotasksScope microtasks_scope(isolate,
MicrotasksScope::kDoNotRunMicrotasks);
if (success == WasmAsyncSuccess::kSuccess) {
CHECK(resolver->Resolve(context, compilation_result).FromJust());
} else {
CHECK(resolver->Reject(context, compilation_result).FromJust());
}
}
} // namespace internal
} // namespace v8
......@@ -87,6 +87,11 @@ class AsyncEventDelegate;
namespace internal {
void DefaultWasmAsyncResolvePromiseCallback(
v8::Isolate* isolate, v8::Local<v8::Context> context,
v8::Local<v8::Promise::Resolver> resolver,
v8::Local<v8::Value> compilation_result, WasmAsyncSuccess success);
namespace heap {
class HeapTester;
} // namespace heap
......@@ -499,6 +504,8 @@ using DeprecatedLegacyOOMErrorCallback = void (*)(const char* location,
V(SharedArrayBufferConstructorEnabledCallback, \
sharedarraybuffer_constructor_enabled_callback, nullptr) \
V(WasmStreamingCallback, wasm_streaming_callback, nullptr) \
V(WasmAsyncResolvePromiseCallback, wasm_async_resolve_promise_callback, \
DefaultWasmAsyncResolvePromiseCallback) \
V(WasmLoadSourceMapCallback, wasm_load_source_map_callback, nullptr) \
V(WasmSimdEnabledCallback, wasm_simd_enabled_callback, nullptr) \
V(WasmExceptionsEnabledCallback, wasm_exceptions_enabled_callback, nullptr) \
......
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