Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(wasm): retain child intrinsic prototype identities

ldm0929b3ac32838
diff --git a/moli-renderer-v8/src/native_bridge/child_window_surface/webassembly_realm.rs b/moli-renderer-v8/src/native_bridge/child_window_surface/webassembly_realm.rs
index b46e62823..2b3d55565 100644
--- a/moli-renderer-v8/src/native_bridge/child_window_surface/webassembly_realm.rs
+++ b/moli-renderer-v8/src/native_bridge/child_window_surface/webassembly_realm.rs
@@ -11,13 +11,6 @@ struct ChildWindowWebAssemblyConstructorDataDeclaration<'scope> {
native_constructor: v8::Local<'scope, v8::Function>,
}
-#[derive(WebApiObject)]
-#[webapi(plain)]
-struct ChildWindowInstancePrototypeDeclaration<'scope> {
- #[webapi(data_property)]
- constructor: v8::Local<'scope, v8::Function>,
-}
-
const CHILD_WEBASSEMBLY_CONSTRUCTOR_NAMES: &[(&str, i32)] = &[
("Module", 1),
("Instance", 1),
@@ -29,12 +22,11 @@ const CHILD_WEBASSEMBLY_CONSTRUCTOR_NAMES: &[(&str, i32)] = &[
("RuntimeError", 1),
];
-/// Adapts V8's isolate-owned WebAssembly constructors to observable
-/// per-realm constructor and NewTarget prototype semantics.
+/// Adapts WebAssembly constructors to the observable Function and NewTarget
+/// realm semantics while retaining V8's intrinsic instance prototypes.
///
/// This is not a child-only Web API declaration. It is an embedder workaround
-/// kept at the realm-state boundary until V8 supplies the same identities for
-/// these contexts directly.
+/// kept at the realm-state boundary.
pub(super) fn install_child_webassembly_realm_adapter(
scope: &mut v8::PinScope<'_, '_>,
window: v8::Local<'_, v8::Object>,
@@ -109,23 +101,27 @@ fn install_child_webassembly_constructor(
set_object_slot(scope, child_webassembly, name, constructor.into());
return;
};
- let Some(child_instance_prototype) = ChildWindowInstancePrototypeDeclaration::new(constructor)
- .bind(scope)
- .ok()
- else {
- set_object_slot(scope, child_webassembly, name, constructor.into());
- return;
- };
- let _ = child_instance_prototype.set_prototype(scope, native_instance_prototype.into());
- let _ = constructor.set(
+ // V8-created exceptions, compiled modules and instances use this intrinsic
+ // prototype directly. An extra prototype layer would exclude those values
+ // from the public constructor's instanceof and constructor identity checks.
+ let _ = constructor.define_own_property(
scope,
v8str(scope, "prototype").into(),
- child_instance_prototype.into(),
+ native_instance_prototype.into(),
+ v8::PropertyAttribute::READ_ONLY
+ | v8::PropertyAttribute::DONT_ENUM
+ | v8::PropertyAttribute::DONT_DELETE,
+ );
+ let _ = native_instance_prototype.define_own_property(
+ scope,
+ v8str(scope, "constructor").into(),
+ constructor.into(),
+ v8::PropertyAttribute::DONT_ENUM,
);
crate::context_bootstrap::set_current_context_webassembly_default_prototype(
scope,
name,
- child_instance_prototype,
+ native_instance_prototype,
);
set_object_slot(scope, child_webassembly, name, constructor.into());
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index 6c5618bd6..5f01fecaa 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -9243,6 +9243,69 @@ lightFrame.name = 'lightTarget';
assert_eq!(result, "false|true|true|true|true");
}
+#[test]
+fn child_webassembly_native_values_use_public_intrinsic_prototypes() {
+ let mut vm = new_storage_test_vm("https://child-wasm-intrinsics.test/");
+ assert_eq!(
+ vm.eval(
+ r#"
+(() => {
+ const names = ['Module', 'Instance', 'Memory', 'Table', 'Global',
+ 'CompileError', 'LinkError', 'RuntimeError'];
+ const parentConstructors = names.map(name => WebAssembly[name]);
+ const frame = document.createElement('iframe');
+ const sibling = document.createElement('iframe');
+ (document.body || document.documentElement || document).appendChild(frame);
+ (document.body || document.documentElement || document).appendChild(sibling);
+ const child = frame.contentWindow;
+ const wasm = child.WebAssembly;
+ const other = sibling.contentWindow.WebAssembly;
+ const results = {};
+ results.isolated = names.every((name, index) =>
+ wasm[name].prototype !== other[name].prototype &&
+ wasm[name].prototype !== parentConstructors[index].prototype &&
+ wasm[name].prototype.constructor === wasm[name] &&
+ other[name].prototype.constructor === other[name] &&
+ parentConstructors[index].prototype.constructor === parentConstructors[index]);
+ results.errorParent = ['CompileError', 'LinkError', 'RuntimeError'].every(name =>
+ Object.getPrototypeOf(wasm[name].prototype) === child.Error.prototype);
+ results.descriptors = names.every(name => {
+ const descriptor = Object.getOwnPropertyDescriptor(wasm[name], 'prototype');
+ return !descriptor.writable && !descriptor.enumerable && !descriptor.configurable;
+ });
+ function thrownInChild(name, operation) {
+ try { operation(); return false; }
+ catch (error) {
+ return error.constructor === wasm[name] && error instanceof wasm[name] &&
+ Object.getPrototypeOf(error) === wasm[name].prototype &&
+ !(error instanceof WebAssembly[name]);
+ }
+ }
+ results.compileError = thrownInChild('CompileError', () =>
+ new wasm.Module(new Uint8Array([0])));
+ const importing = new wasm.Module(new Uint8Array([
+ 0,97,115,109,1,0,0,0,1,4,1,96,0,0,2,7,1,1,109,1,102,0,0
+ ]));
+ results.linkError = thrownInChild('LinkError', () =>
+ new wasm.Instance(importing, {m: {f: 1}}));
+ const trap = new wasm.Module(new Uint8Array([
+ 0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,7,5,1,1,102,0,0,
+ 10,5,1,3,0,0,11
+ ]));
+ results.runtimeError = thrownInChild('RuntimeError', () =>
+ new wasm.Instance(trap).exports.f());
+ const sentinel = new RangeError('author getter');
+ try { new wasm.Memory({get initial() { throw sentinel; }}); }
+ catch (error) { results.authorException = error === sentinel; }
+ return JSON.stringify(results);
+})()
+ "#
+ )
+ .unwrap(),
+ r#"{"isolated":true,"errorParent":true,"descriptors":true,"compileError":true,"linkError":true,"runtimeError":true,"authorException":true}"#
+ );
+}
+
#[test]
fn child_webassembly_constructors_use_newtarget_child_realm_default_prototype() {
let mut vm = new_storage_test_vm("https://child-wasm-newtarget.test/");