Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

vendor(v8): add Wasm module compile bridges

ldm090ac7ece551c
diff --git a/vendor/v8-152.2.0/src/lib.rs b/vendor/v8-152.2.0/src/lib.rs
index 49eef1337..a31423c22 100644
--- a/vendor/v8-152.2.0/src/lib.rs
+++ b/vendor/v8-152.2.0/src/lib.rs
@@ -204,6 +204,7 @@ pub use value_serializer::ValueSerializerHelper;
pub use value_serializer::ValueSerializerImpl;
pub use wasm::CompiledWasmModule;
pub use wasm::ModuleCachingInterface;
+pub use wasm::WasmCompileOptions;
pub use wasm::WasmModuleCompilation;
pub use wasm::WasmStreaming;
diff --git a/vendor/v8-152.2.0/src/module.rs b/vendor/v8-152.2.0/src/module.rs
index b7132dbc9..857b87526 100644
--- a/vendor/v8-152.2.0/src/module.rs
+++ b/vendor/v8-152.2.0/src/module.rs
@@ -257,6 +257,11 @@ unsafe extern "C" {
export_name: *const String,
export_value: *const Value,
) -> MaybeBool;
+ fn v8__Module__SetSyntheticModuleExportUninitialized(
+ this: *const Module,
+ isolate: *const RealIsolate,
+ export_name: *const String,
+ ) -> MaybeBool;
fn v8__Module__GetUnboundModuleScript(
this: *const Module,
) -> *const UnboundModuleScript;
@@ -573,6 +578,24 @@ impl Module {
.into()
}
+ /// Sets this module's export cell to V8's uninitialized binding sentinel.
+ #[must_use]
+ #[inline(always)]
+ pub fn set_synthetic_module_export_uninitialized<'s>(
+ &self,
+ scope: &mut PinScope<'s, '_>,
+ export_name: Local<'s, String>,
+ ) -> Option<bool> {
+ unsafe {
+ v8__Module__SetSyntheticModuleExportUninitialized(
+ self,
+ scope.get_isolate_ptr(),
+ &*export_name,
+ )
+ }
+ .into()
+ }
+
#[inline(always)]
pub fn get_unbound_module_script<'s>(
&self,
diff --git a/vendor/v8-152.2.0/src/module_ext.cc b/vendor/v8-152.2.0/src/module_ext.cc
new file mode 100644
index 000000000..87f6da197
--- /dev/null
+++ b/vendor/v8-152.2.0/src/module_ext.cc
@@ -0,0 +1,24 @@
+// Copyright 2026 the Moli authors. MIT license.
+
+#include "support.h"
+#include "v8.h"
+
+using namespace support;
+
+extern "C" {
+
+MaybeBool v8__Module__SetSyntheticModuleExportUninitialized(
+ const v8::Module& self,
+ v8::Isolate* isolate,
+ const v8::String* export_name) {
+ // Use the isolate root slot as the Local handle storage. Copying the tagged
+ // root value can misrepresent static roots in prebuilt V8 configurations.
+ auto root_slot = reinterpret_cast<const v8::Value* const*>(
+ v8::internal::Internals::GetRootSlot(
+ isolate, v8::internal::Internals::kTheHoleValueRootIndex));
+ auto value = *reinterpret_cast<const v8::Local<v8::Value>*>(&root_slot);
+ return maybe_to_maybe_bool(ptr_to_local(&self)->SetSyntheticModuleExport(
+ isolate, ptr_to_local(export_name), value));
+}
+
+} // extern "C"
diff --git a/vendor/v8-152.2.0/src/wasm.rs b/vendor/v8-152.2.0/src/wasm.rs
index 2311d2bf4..cc8cbacb5 100644
--- a/vendor/v8-152.2.0/src/wasm.rs
+++ b/vendor/v8-152.2.0/src/wasm.rs
@@ -182,6 +182,41 @@ impl WasmModuleObject {
})
}
}
+
+ /// Compiles a Wasm module with browser module-script compile-time imports.
+ #[inline(always)]
+ pub fn compile_with_options<'s>(
+ scope: &PinScope<'s, '_>,
+ wire_bytes: &[u8],
+ options: WasmCompileOptions<'_>,
+ ) -> Option<Local<'s, WasmModuleObject>> {
+ if options == WasmCompileOptions::default() {
+ return Self::compile(scope, wire_bytes);
+ }
+ let (string_constants_ptr, string_constants_len) = options
+ .imported_string_constants_module
+ .map_or((null(), 0), |module| {
+ (module.as_ptr() as *const char, module.len())
+ });
+ unsafe {
+ scope.cast_local(|sd| {
+ v8__WasmModuleObject__CompileWithOptions(
+ sd.get_isolate_ptr(),
+ wire_bytes.as_ptr(),
+ wire_bytes.len(),
+ options.js_string_builtins,
+ string_constants_ptr,
+ string_constants_len,
+ )
+ })
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
+pub struct WasmCompileOptions<'a> {
+ pub js_string_builtins: bool,
+ pub imported_string_constants_module: Option<&'a str>,
}
#[repr(C)]
@@ -572,6 +607,14 @@ unsafe extern "C" {
wire_bytes_data: *const u8,
length: usize,
) -> *mut WasmModuleObject;
+ fn v8__WasmModuleObject__CompileWithOptions(
+ isolate: *mut RealIsolate,
+ wire_bytes_data: *const u8,
+ length: usize,
+ js_string_builtins: bool,
+ imported_string_constants_module_data: *const char,
+ imported_string_constants_module_len: usize,
+ ) -> *mut WasmModuleObject;
fn v8__CompiledWasmModule__GetWireBytesRef(
this: *mut InternalCompiledWasmModule,
diff --git a/vendor/v8-152.2.0/src/wasm_ext.cc b/vendor/v8-152.2.0/src/wasm_ext.cc
new file mode 100644
index 000000000..12162221d
--- /dev/null
+++ b/vendor/v8-152.2.0/src/wasm_ext.cc
@@ -0,0 +1,192 @@
+// Copyright 2026 the Moli authors. MIT license.
+
+#include <cstring>
+
+#include "support.h"
+#include "v8.h"
+
+#if __has_include("v8/src/api/api-inl.h") && \
+ __has_include("v8/src/wasm/wasm-engine.h")
+#define MOLI_HAS_V8_INTERNAL_WASM_COMPILE 1
+#include "v8/src/api/api-inl.h"
+#include "v8/src/base/vector.h"
+#include "v8/src/wasm/wasm-engine.h"
+#include "v8/src/wasm/wasm-features.h"
+#include "v8/src/wasm/wasm-result.h"
+#else
+#define MOLI_HAS_V8_INTERNAL_WASM_COMPILE 0
+#endif
+
+using namespace support;
+
+namespace {
+
+#if !MOLI_HAS_V8_INTERNAL_WASM_COMPILE
+v8::MaybeLocal<v8::String> NewUtf8Literal(v8::Isolate* isolate,
+ const char* literal) {
+ return v8::String::NewFromUtf8(isolate, literal,
+ v8::NewStringType::kInternalized);
+}
+
+v8::MaybeLocal<v8::Value> GetProperty(v8::Isolate* isolate,
+ v8::Local<v8::Context> context,
+ v8::Local<v8::Object> object,
+ const char* name) {
+ v8::Local<v8::String> key;
+ if (!NewUtf8Literal(isolate, name).ToLocal(&key)) {
+ return v8::MaybeLocal<v8::Value>();
+ }
+ return object->Get(context, key);
+}
+
+v8::MaybeLocal<v8::WasmModuleObject> RethrowCaughtWasmCompileException(
+ v8::TryCatch* try_catch) {
+ if (try_catch->HasCaught()) {
+ try_catch->ReThrow();
+ }
+ return v8::MaybeLocal<v8::WasmModuleObject>();
+}
+
+v8::MaybeLocal<v8::WasmModuleObject> CompileViaIsolatedPublicConstructor(
+ v8::Isolate* isolate, v8::MemorySpan<const uint8_t> wire_bytes,
+ bool js_string_builtins, const char* string_constants_module_data,
+ size_t string_constants_module_len) {
+ bool has_string_constants = string_constants_module_data != nullptr;
+
+ v8::EscapableHandleScope handle_scope(isolate);
+ v8::TryCatch try_catch(isolate);
+ v8::Local<v8::Context> context = v8::Context::New(isolate);
+ v8::Context::Scope context_scope(context);
+
+ v8::Local<v8::Value> webassembly_value;
+ if (!GetProperty(isolate, context, context->Global(), "WebAssembly")
+ .ToLocal(&webassembly_value) ||
+ !webassembly_value->IsObject()) {
+ return RethrowCaughtWasmCompileException(&try_catch);
+ }
+
+ v8::Local<v8::Value> module_value;
+ if (!GetProperty(isolate, context, webassembly_value.As<v8::Object>(),
+ "Module")
+ .ToLocal(&module_value) ||
+ !module_value->IsFunction()) {
+ return RethrowCaughtWasmCompileException(&try_catch);
+ }
+
+ v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(
+ isolate, wire_bytes.size(),
+ v8::BackingStoreInitializationMode::kUninitialized);
+ if (wire_bytes.size() != 0) {
+ std::memcpy(buffer->Data(), wire_bytes.data(), wire_bytes.size());
+ }
+
+ v8::Local<v8::Uint8Array> bytes =
+ v8::Uint8Array::New(buffer, 0, wire_bytes.size());
+ v8::Local<v8::Object> options = v8::Object::New(isolate);
+ if (js_string_builtins) {
+ v8::Local<v8::Array> builtins = v8::Array::New(isolate, 1);
+ v8::Local<v8::String> js_string;
+ v8::Local<v8::String> builtins_key;
+ if (!NewUtf8Literal(isolate, "js-string").ToLocal(&js_string) ||
+ !NewUtf8Literal(isolate, "builtins").ToLocal(&builtins_key) ||
+ !builtins->Set(context, 0, js_string).FromMaybe(false) ||
+ !options->Set(context, builtins_key, builtins).FromMaybe(false)) {
+ return RethrowCaughtWasmCompileException(&try_catch);
+ }
+ }
+
+ if (has_string_constants) {
+ v8::Local<v8::String> constants_module;
+ v8::Local<v8::String> constants_key;
+ if (!v8::String::NewFromUtf8(
+ isolate, string_constants_module_data, v8::NewStringType::kNormal,
+ static_cast<int>(string_constants_module_len))
+ .ToLocal(&constants_module) ||
+ !NewUtf8Literal(isolate, "importedStringConstants")
+ .ToLocal(&constants_key) ||
+ !options->Set(context, constants_key, constants_module)
+ .FromMaybe(false)) {
+ return RethrowCaughtWasmCompileException(&try_catch);
+ }
+ }
+
+ v8::Local<v8::Value> args[] = {bytes, options};
+ v8::Local<v8::Object> module;
+ if (!module_value.As<v8::Function>()
+ ->NewInstance(context, std::size(args), args)
+ .ToLocal(&module) ||
+ !module->IsWasmModuleObject()) {
+ return RethrowCaughtWasmCompileException(&try_catch);
+ }
+
+ return handle_scope.Escape(module.As<v8::WasmModuleObject>());
+}
+#endif // !MOLI_HAS_V8_INTERNAL_WASM_COMPILE
+
+v8::MaybeLocal<v8::WasmModuleObject> CompileWithOptions(
+ v8::Isolate* v8_isolate, v8::MemorySpan<const uint8_t> wire_bytes,
+ bool js_string_builtins, const char* string_constants_module_data,
+ size_t string_constants_module_len) {
+#if MOLI_HAS_V8_INTERNAL_WASM_COMPILE
+#if V8_ENABLE_WEBASSEMBLY
+ // Source builds can use the same internal path as V8's public API and pass
+ // compile-time imports directly. Prebuilt rusty_v8 only ships public headers,
+ // so that configuration falls back to the public constructor path below.
+ v8::base::OwnedVector<const uint8_t> bytes =
+ v8::base::OwnedCopyOf(wire_bytes);
+ v8::internal::Isolate* isolate =
+ reinterpret_cast<v8::internal::Isolate*>(v8_isolate);
+ v8::internal::wasm::CompileTimeImports compile_imports;
+ if (js_string_builtins) {
+ compile_imports.Add(v8::internal::wasm::CompileTimeImport::kJsString);
+ }
+ if (string_constants_module_data != nullptr) {
+ compile_imports.Add(
+ v8::internal::wasm::CompileTimeImport::kStringConstants);
+ compile_imports.constants_module().assign(string_constants_module_data,
+ string_constants_module_len);
+ }
+ v8::internal::wasm::ErrorThrower thrower(
+ isolate, "WasmModuleObject::CompileWithOptions()");
+ auto enabled_features =
+ v8::internal::wasm::WasmEnabledFeatures::FromIsolate(isolate);
+ v8::internal::MaybeDirectHandle<v8::internal::WasmModuleObject>
+ maybe_compiled = v8::internal::wasm::GetWasmEngine()->SyncCompile(
+ isolate, enabled_features, std::move(compile_imports), &thrower,
+ std::move(bytes));
+ if (maybe_compiled.is_null()) {
+ return v8::MaybeLocal<v8::WasmModuleObject>();
+ }
+ return v8::Utils::ToLocal(maybe_compiled.ToHandleChecked());
+#else
+ v8::Utils::ApiCheck(false, "WasmModuleObject::CompileWithOptions",
+ "WebAssembly support is not enabled");
+ return v8::MaybeLocal<v8::WasmModuleObject>();
+#endif
+#else
+ // The public C++ API has no compile-options overload yet. Use a fresh V8
+ // context so the bridge exercises V8's built-in WebAssembly.Module
+ // constructor without observing or trusting the page's mutable global object.
+ return CompileViaIsolatedPublicConstructor(v8_isolate, wire_bytes,
+ js_string_builtins,
+ string_constants_module_data,
+ string_constants_module_len);
+#endif // MOLI_HAS_V8_INTERNAL_WASM_COMPILE
+}
+
+} // namespace
+
+extern "C" {
+
+const v8::WasmModuleObject* v8__WasmModuleObject__CompileWithOptions(
+ v8::Isolate* isolate, const uint8_t* wire_bytes_data, size_t length,
+ bool js_string_builtins, const char* string_constants_module_data,
+ size_t string_constants_module_len) {
+ v8::MemorySpan<const uint8_t> wire_bytes(wire_bytes_data, length);
+ return maybe_local_to_ptr(
+ CompileWithOptions(isolate, wire_bytes, js_string_builtins,
+ string_constants_module_data,
+ string_constants_module_len));
+}
+
+} // extern "C"