From 29540f26eaab97055f4e6f0ba1319846d969fdc6 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 21 Sep 2026 12:58:12 -0700 Subject: [PATCH] deps: V8: cherry-pick 5f8109fccf06 and 7bd6db9dfd8a Original commit messages: [wasm] Add WasmModuleObject::Compile overload with compile-time imports Expose a public API to compile a Wasm module with compile-time imports with a flag type for the builtins and a specifier name for imported string constants. The existing single-argument WasmModuleObject::Compile is refactored to delegate to a shared helper, and a new overload accepts a CompileTimeImports struct mirroring the `{ builtins, importedStringConstants }` constructor options. Bug: v8:14179 Change-Id: I9877e8ea4d620152e98954c948ff9dc5eeb6577c Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7970437 Reviewed-by: Leszek Swirski Commit-Queue: Jakob Kummerow Reviewed-by: Jakob Kummerow Cr-Commit-Position: refs/heads/main@{#108394} [wasm] Generalize WasmModuleObject::Compile options with source URL Replaces the recently-added WasmModuleObject::CompileTimeImports struct with a CompileOptions struct carrying the compile-time import options plus a new source_url option, threading through to the existing source_url handling of SyncCompile for the script URL. This allows embedders compiling modules synchronously from bytes to attach a meaningful URL, as already possible for streaming compilation via WasmStreaming::SetUrl, for use in stack traces and developer tooling. Refactoring now to a single options struct mirrors the JS `WebAssembly.Module(bytes, compileOptions)` API, while enabling future compatibility. Change-Id: I4052f4a97e072467c00082df388e2eb4cb504cf6 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8127096 Commit-Queue: Dan Carney Reviewed-by: Dan Carney Reviewed-by: Leszek Swirski Reviewed-by: Jakob Kummerow Cr-Commit-Position: refs/heads/main@{#109045} The two commits are squashed since the second replaces the struct introduced by the first. Adapted to MemorySpan (pre-std::span API) and includes the optional source_url parameter of WasmEngine::SyncCompile from c0f790f1379 that the second commit depends on. Refs: https://github.com/v8/v8/commit/5f8109fccf06b3a846794d8c65ed097c81de9c90 Refs: https://github.com/v8/v8/commit/7bd6db9dfd8a6b7061272f3d7d5a5b21c7194d53 --- common.gypi | 2 +- deps/v8/include/v8-wasm.h | 45 +++- deps/v8/src/api/api.cc | 15 +- deps/v8/src/wasm/wasm-engine.cc | 6 +- deps/v8/src/wasm/wasm-engine.h | 3 +- deps/v8/src/wasm/wasm-js.cc | 34 ++- deps/v8/src/wasm/wasm-js.h | 7 +- .../test/unittests/api/api-wasm-unittest.cc | 201 ++++++++++++++++++ 8 files changed, 297 insertions(+), 16 deletions(-) diff --git a/common.gypi b/common.gypi index 71712d308472..7eaad1e5ea1e 100644 --- a/common.gypi +++ b/common.gypi @@ -43,7 +43,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.34', + 'v8_embedder_string': '-node.35', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/include/v8-wasm.h b/deps/v8/include/v8-wasm.h index 550cdfb32783..034912de0f8b 100644 --- a/deps/v8/include/v8-wasm.h +++ b/deps/v8/include/v8-wasm.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "v8-internal.h" // NOLINT(build/include_directory) @@ -106,12 +107,48 @@ class V8_EXPORT WasmModuleObject : public Object { */ CompiledWasmModule GetCompiledModule(); + /** + * Options that influence how a Wasm module is compiled. The compile-time + * import options mirror those accepted by the JS `WebAssembly.Module` + * constructor (`{ builtins, importedStringConstants }`). + */ + struct CompileOptions { + // Builtin compile-time imports, mirroring the strings accepted in the + // `builtins` array of the JS `WebAssembly.Module` constructor options. + // Combine values with bitwise-or to enable multiple builtins. + struct Builtins { + enum { + kNone = 0, + kJsString = 1 << 0, // "js-string" + }; + }; + // Bitwise-or of `Builtins` values to enable as compile-time imports. + int builtins = Builtins::kNone; + // If non-null, enable imported string constants from the named module + // (e.g. "wasm:js/string-constants"). The string must be null-terminated and + // remain valid for the duration of the compile call. + const char* imported_string_constants_module = nullptr; + // If non-empty, associated with the module's script as its source URL, for + // use in stack traces and developer tooling. If a script already exists in + // the isolate for the same module, its existing URL is retained. The + // string must remain valid for the duration of the compile call. + std::string_view source_url = {}; + }; + /** * Compile a Wasm module from the provided uncompiled bytes. */ static MaybeLocal Compile( Isolate* isolate, MemorySpan wire_bytes); + /** + * Compile a Wasm module from the provided uncompiled bytes, applying the + * given compile options. + */ + static MaybeLocal Compile( + Isolate* isolate, MemorySpan wire_bytes, + const CompileOptions& options); + V8_INLINE static WasmModuleObject* Cast(Value* value) { #ifdef V8_ENABLE_CHECKS CheckCast(value); @@ -224,13 +261,15 @@ class V8_EXPORT WasmStreaming final { class V8_EXPORT WasmModuleCompilation final { public: using ModuleCachingCallback = WasmStreaming::ModuleCachingCallback; + using CompileOptions = WasmModuleObject::CompileOptions; /** - * Start an asynchronous module compilation. This can be called on any thread. + * Start an asynchronous module compilation, applying the given compile + * options. This can be called on any thread. Providing + * {CompileOptions::source_url} is equivalent to calling {SetUrl}. * TODO(clemensb): Add some way to pass enabled features. - * TODO(clemensb): Add some way to pass compile time imports. */ - WasmModuleCompilation(); + explicit WasmModuleCompilation(const CompileOptions& options = {}); ~WasmModuleCompilation(); diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index eb3cd7dee28f..181d1a126faf 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -8853,7 +8853,15 @@ MaybeLocal WasmModuleObject::FromCompiledModule( MaybeLocal WasmModuleObject::Compile( Isolate* v8_isolate, MemorySpan wire_bytes) { + return Compile(v8_isolate, wire_bytes, CompileOptions{}); +} + +MaybeLocal WasmModuleObject::Compile( + Isolate* v8_isolate, MemorySpan wire_bytes, + const CompileOptions& options) { #if V8_ENABLE_WEBASSEMBLY + i::wasm::CompileTimeImports compile_imports = + i::wasm::CompileTimeImportsFromOptions(options); base::OwnedVector bytes = base::OwnedCopyOf(wire_bytes); i::Isolate* i_isolate = reinterpret_cast(v8_isolate); // We don't check for `IsWasmCodegenAllowed` here, because this function is @@ -8864,10 +8872,11 @@ MaybeLocal WasmModuleObject::Compile( i::wasm::ErrorThrower thrower(i_isolate, "WasmModuleObject::Compile()"); auto enabled_features = i::wasm::WasmEnabledFeatures::FromIsolate(i_isolate); - // TODO(14179): Provide an API method that supports compile options. maybe_compiled = i::wasm::GetWasmEngine()->SyncCompile( - i_isolate, enabled_features, i::wasm::CompileTimeImports{}, &thrower, - std::move(bytes)); + i_isolate, enabled_features, std::move(compile_imports), &thrower, + std::move(bytes), + base::Vector(options.source_url.data(), + options.source_url.size())); } CHECK_EQ(maybe_compiled.is_null(), i_isolate->has_exception()); if (maybe_compiled.is_null()) return {}; diff --git a/deps/v8/src/wasm/wasm-engine.cc b/deps/v8/src/wasm/wasm-engine.cc index 1380f8166b0f..c9184ef647c3 100644 --- a/deps/v8/src/wasm/wasm-engine.cc +++ b/deps/v8/src/wasm/wasm-engine.cc @@ -666,7 +666,8 @@ DirectHandle WasmEngine::FinalizeTranslatedAsmJs( MaybeDirectHandle WasmEngine::SyncCompile( Isolate* isolate, WasmEnabledFeatures enabled_features, CompileTimeImports compile_imports, ErrorThrower* thrower, - base::OwnedVector bytes) { + base::OwnedVector bytes, + base::Vector source_url) { int compilation_id = next_compilation_id_.fetch_add(1); TRACE_EVENT1("v8.wasm", "wasm.SyncCompile", "id", compilation_id); v8::metrics::Recorder::ContextId context_id = @@ -718,9 +719,8 @@ MaybeDirectHandle WasmEngine::SyncCompile( } #endif - constexpr base::Vector kNoSourceUrl; DirectHandle