Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(modules): preserve constructors through module trees

ldm0bc998c574852
diff --git a/moli-module-script-tree/src/types.rs b/moli-module-script-tree/src/types.rs
index f78993866..593bf485b 100644
--- a/moli-module-script-tree/src/types.rs
+++ b/moli-module-script-tree/src/types.rs
@@ -551,8 +551,11 @@ impl ModuleLoadError {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModuleErrorConstructorKind {
+ Error,
SyntaxError,
TypeError,
+ WebAssemblyCompileError,
+ WebAssemblyLinkError,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
diff --git a/moli-renderer-v8/src/module_runtime/graph.rs b/moli-renderer-v8/src/module_runtime/graph.rs
index 03104ae4a..0444581c9 100644
--- a/moli-renderer-v8/src/module_runtime/graph.rs
+++ b/moli-renderer-v8/src/module_runtime/graph.rs
@@ -1900,15 +1900,19 @@ fn chromium_error(error: ModuleLoadError) -> module_tree::ModuleLoadError {
module_tree::ModuleLoadError::new(chromium_load_stage(error.stage()), error.message());
if let Some(constructor) = error.error_constructor() {
let constructor = match constructor {
+ ScriptErrorConstructorKind::Error => module_tree::ModuleErrorConstructorKind::Error,
ScriptErrorConstructorKind::SyntaxError => {
module_tree::ModuleErrorConstructorKind::SyntaxError
}
ScriptErrorConstructorKind::TypeError => {
module_tree::ModuleErrorConstructorKind::TypeError
}
- ScriptErrorConstructorKind::Error
- | ScriptErrorConstructorKind::WebAssemblyCompileError
- | ScriptErrorConstructorKind::WebAssemblyLinkError => return converted,
+ ScriptErrorConstructorKind::WebAssemblyCompileError => {
+ module_tree::ModuleErrorConstructorKind::WebAssemblyCompileError
+ }
+ ScriptErrorConstructorKind::WebAssemblyLinkError => {
+ module_tree::ModuleErrorConstructorKind::WebAssemblyLinkError
+ }
};
converted = converted.with_error_constructor(constructor);
}
@@ -1927,10 +1931,17 @@ fn local_error_constructor(
constructor: module_tree::ModuleErrorConstructorKind,
) -> ScriptErrorConstructorKind {
match constructor {
+ module_tree::ModuleErrorConstructorKind::Error => ScriptErrorConstructorKind::Error,
module_tree::ModuleErrorConstructorKind::SyntaxError => {
ScriptErrorConstructorKind::SyntaxError
}
module_tree::ModuleErrorConstructorKind::TypeError => ScriptErrorConstructorKind::TypeError,
+ module_tree::ModuleErrorConstructorKind::WebAssemblyCompileError => {
+ ScriptErrorConstructorKind::WebAssemblyCompileError
+ }
+ module_tree::ModuleErrorConstructorKind::WebAssemblyLinkError => {
+ ScriptErrorConstructorKind::WebAssemblyLinkError
+ }
}
}
diff --git a/moli-renderer-v8/src/script_vm/native_module.rs b/moli-renderer-v8/src/script_vm/native_module.rs
index ed2937d76..953d86bd5 100644
--- a/moli-renderer-v8/src/script_vm/native_module.rs
+++ b/moli-renderer-v8/src/script_vm/native_module.rs
@@ -5053,6 +5053,7 @@ fn module_import_phase(phase: v8::ModuleImportPhase) -> ModuleImportPhase {
#[cfg(test)]
mod tests {
+ mod constructor_preservation;
use std::pin::pin;
use super::{
diff --git a/moli-renderer-v8/src/script_vm/native_module/tests/constructor_preservation.rs b/moli-renderer-v8/src/script_vm/native_module/tests/constructor_preservation.rs
new file mode 100644
index 000000000..8e5dca830
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/native_module/tests/constructor_preservation.rs
@@ -0,0 +1,151 @@
+use super::*;
+use crate::types::ScriptErrorConstructorKind;
+
+fn graph_error(vm: &mut ScriptVm, specifier: &str) -> ModuleLoadError {
+ let mut job = dynamic_import_job_in_vm(
+ vm,
+ specifier,
+ Url::parse("https://module-errors.test/page.html").unwrap(),
+ ModuleImportPhase::Evaluation,
+ );
+ job.advance_dynamic_import_owner_lane(vm)
+ .err()
+ .expect("module graph should fail")
+}
+
+fn reject_with_error(vm: &mut ScriptVm, error: &ModuleLoadError) -> v8::Global<v8::Value> {
+ let request = dynamic_import_request_in_vm(
+ vm,
+ "./bad.mjs",
+ Url::parse("https://module-errors.test/page.html").unwrap(),
+ ModuleImportPhase::Evaluation,
+ );
+ let resolver = request.resolver().clone();
+ vm.renderer_document_isolate
+ .with_entered_renderer_document_isolate(|isolate| {
+ let scope = pin!(v8::HandleScope::new(isolate));
+ let scope = &mut scope.init();
+ let context = v8::Local::new(scope, &vm.page_default_context);
+ let scope = &mut v8::ContextScope::new(scope, context);
+ v8::Local::new(scope, &resolver)
+ .get_promise(scope)
+ .mark_as_handled();
+ Ok(())
+ })
+ .unwrap();
+ vm.reject_native_dynamic_module_import_with_error_selected_task_body(request, error)
+ .unwrap();
+ vm.renderer_document_isolate
+ .with_entered_renderer_document_isolate(|isolate| {
+ let scope = pin!(v8::HandleScope::new(isolate));
+ let scope = &mut scope.init();
+ let context = v8::Local::new(scope, &vm.page_default_context);
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let promise = v8::Local::new(scope, &resolver).get_promise(scope);
+ assert_eq!(promise.state(), v8::PromiseState::Rejected);
+ Ok(v8::Global::new(scope, promise.result(scope)))
+ })
+ .unwrap()
+}
+
+#[test]
+fn cached_module_graph_errors_preserve_their_native_constructors() {
+ for (kind, constructor) in [
+ (ScriptErrorConstructorKind::Error, "Error"),
+ (ScriptErrorConstructorKind::SyntaxError, "SyntaxError"),
+ (ScriptErrorConstructorKind::TypeError, "TypeError"),
+ (
+ ScriptErrorConstructorKind::WebAssemblyCompileError,
+ "WebAssembly.CompileError",
+ ),
+ (
+ ScriptErrorConstructorKind::WebAssemblyLinkError,
+ "WebAssembly.LinkError",
+ ),
+ ] {
+ let mut vm = new_test_vm("https://module-errors.test/page.html");
+ let url = Url::parse("https://module-errors.test/cached.mjs").unwrap();
+ vm.document_runtime.mark_native_module_failed(
+ ModuleMapKey::java_script(url.clone()),
+ ModuleLoadError::new(ModuleLoadStage::Instantiate, "cached module failure")
+ .with_error_constructor(kind),
+ );
+ let error = graph_error(&mut vm, url.as_str());
+ let rejection = reject_with_error(&mut vm, &error);
+ vm.with_default_context_scope(|scope, _| {
+ let global = scope.get_current_context().global(scope);
+ let rejection = v8::Local::new(scope, &rejection);
+ assert_eq!(
+ global.set(scope, v8str(scope, "__rejected").into(), rejection),
+ Some(true)
+ );
+ Ok(())
+ })
+ .unwrap();
+ assert_eq!(
+ vm.eval(&format!("String(__rejected.constructor === {constructor})"))
+ .unwrap(),
+ "true",
+ "a cached module graph failure must retain {constructor}"
+ );
+ }
+}
+
+#[test]
+fn wasm_compile_error_survives_module_graph_and_rejection() {
+ let mut vm = new_test_vm("https://module-errors.test/page.html");
+ let url = Url::parse("https://module-errors.test/invalid.wasm").unwrap();
+ let mut job = dynamic_import_job_in_vm(
+ &mut vm,
+ url.as_str(),
+ Url::parse("https://module-errors.test/page.html").unwrap(),
+ ModuleImportPhase::Evaluation,
+ );
+ let NativeModuleGraphJobAdvance::NeedFetches(mut requests) =
+ job.advance_dynamic_import_owner_lane(&mut vm).unwrap()
+ else {
+ panic!("the Wasm module must start a fetch");
+ };
+ assert_eq!(requests.len(), 1);
+ let error = job
+ .finish_dynamic_import_fetch_for_request(
+ &mut vm,
+ &requests.pop().unwrap(),
+ Ok(ModuleGraphFetchedSource::new(
+ url,
+ false,
+ ModuleSource::binary(vec![0]),
+ )),
+ )
+ .err()
+ .expect("V8 must reject the invalid Wasm bytes");
+ assert_eq!(error.stage(), ModuleLoadStage::Compile);
+ assert_eq!(
+ error.error_constructor(),
+ Some(ScriptErrorConstructorKind::WebAssemblyCompileError)
+ );
+ vm.eval(
+ r#"
+globalThis.__originalCompileError = WebAssembly.CompileError;
+globalThis.WebAssembly = { get CompileError() { throw new Error('author getter'); } };
+"#,
+ )
+ .unwrap();
+ let rejection = reject_with_error(&mut vm, &error);
+ vm.with_default_context_scope(|scope, _| {
+ let global = scope.get_current_context().global(scope);
+ let rejection = v8::Local::new(scope, &rejection);
+ assert_eq!(
+ global.set(scope, v8str(scope, "__rejected").into(), rejection),
+ Some(true)
+ );
+ Ok(())
+ })
+ .unwrap();
+ assert_eq!(
+ vm.eval("String(__rejected.constructor === __originalCompileError)")
+ .unwrap(),
+ "true",
+ "import rejection must retain CompileError without reading page constructors"
+ );
+}