mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 16:02:34 +00:00
fix(modules): parse JSON before evaluating module graphs
Parse JSON while creating document and worker module records, before any sibling module executes. Cache the parsed default export and preserve the original parse exception across concurrent and repeated imports. Retain JSON response URLs as private host metadata without replacing a later JavaScript throw location. Avoid preparing author stacks during JSON module creation, and preserve native Worker compile errors while retaining existing bootstrap fallback error kinds.
This commit is contained in:
@@ -63,7 +63,7 @@ pub(crate) use self::response_mime::{
|
||||
validate_css_module_response_mime, validate_json_module_response_mime,
|
||||
};
|
||||
pub(crate) use self::single_module_fetch::NativeModuleSingleFetchRequest;
|
||||
pub(crate) use self::synthetic_text::SyntheticTextModuleSource;
|
||||
pub(crate) use self::synthetic_text::{SyntheticTextModuleSource, SyntheticTextModuleValue};
|
||||
pub(crate) use self::tree_owner::{
|
||||
NativeModuleTreeDocumentOwnerAdapter, NativeModuleTreeFrameDocumentOwner,
|
||||
};
|
||||
|
||||
@@ -12,7 +12,14 @@ use super::{ModuleIdentityHash, ModuleMapKey, module_identity_hash_from_v8_modul
|
||||
pub(crate) struct SyntheticTextModuleSource {
|
||||
module: v8::Global<v8::Module>,
|
||||
key: ModuleMapKey,
|
||||
source: Arc<str>,
|
||||
value: SyntheticTextModuleValue,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum SyntheticTextModuleValue {
|
||||
Json(v8::Global<v8::Value>),
|
||||
Css(Arc<str>),
|
||||
Text(Arc<str>),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -25,7 +32,7 @@ impl SyntheticTextModuleSource {
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
module: v8::Local<'_, v8::Module>,
|
||||
key: ModuleMapKey,
|
||||
source: Arc<str>,
|
||||
value: SyntheticTextModuleValue,
|
||||
) -> Rc<Self> {
|
||||
let context = scope.get_current_context();
|
||||
let sources = context
|
||||
@@ -38,7 +45,7 @@ impl SyntheticTextModuleSource {
|
||||
let record = Rc::new(Self {
|
||||
module: v8::Global::new(scope, module),
|
||||
key,
|
||||
source,
|
||||
value,
|
||||
});
|
||||
let mut entries = sources.entries.borrow_mut();
|
||||
let candidates = entries
|
||||
@@ -67,8 +74,8 @@ impl SyntheticTextModuleSource {
|
||||
&self.key
|
||||
}
|
||||
|
||||
pub(crate) fn source(&self) -> &str {
|
||||
&self.source
|
||||
pub(crate) fn value(&self) -> &SyntheticTextModuleValue {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,9 +108,19 @@ mod tests {
|
||||
Url::parse("https://example.test/shared.json").unwrap(),
|
||||
ModuleAttributesKey::empty(),
|
||||
);
|
||||
let value = v8::Number::new(scope, 1.0);
|
||||
let first_value = SyntheticTextModuleValue::Json(v8::Global::new(
|
||||
scope,
|
||||
v8::Local::<v8::Value>::from(value),
|
||||
));
|
||||
let first_source =
|
||||
SyntheticTextModuleSource::register(scope, first, key.clone(), "1".into());
|
||||
let second_source = SyntheticTextModuleSource::register(scope, second, key, "2".into());
|
||||
SyntheticTextModuleSource::register(scope, first, key.clone(), first_value);
|
||||
let value = v8::Number::new(scope, 2.0);
|
||||
let second_value = SyntheticTextModuleValue::Json(v8::Global::new(
|
||||
scope,
|
||||
v8::Local::<v8::Value>::from(value),
|
||||
));
|
||||
let second_source = SyntheticTextModuleSource::register(scope, second, key, second_value);
|
||||
|
||||
// Inject a conflicting candidate deterministically instead of waiting
|
||||
// for a random V8 identity-hash collision.
|
||||
@@ -136,24 +153,25 @@ mod tests {
|
||||
Url::parse("https://example.test/value.json").unwrap(),
|
||||
ModuleAttributesKey::empty(),
|
||||
);
|
||||
let text: Arc<str> = "42".into();
|
||||
let weak_text = Arc::downgrade(&text);
|
||||
let source = SyntheticTextModuleSource::register(scope, module, key.clone(), text);
|
||||
let value = v8::Number::new(scope, 42.0);
|
||||
let value = SyntheticTextModuleValue::Json(v8::Global::new(
|
||||
scope,
|
||||
v8::Local::<v8::Value>::from(value),
|
||||
));
|
||||
let source = SyntheticTextModuleSource::register(scope, module, key.clone(), value);
|
||||
let weak = Rc::downgrade(&source);
|
||||
let record = ModuleRecordEntry::new(key, v8::Global::new(scope, module), Vec::new())
|
||||
.with_synthetic_text_module_source(source);
|
||||
let clone = record.clone();
|
||||
drop(record);
|
||||
assert_eq!(weak_text.strong_count(), 1);
|
||||
assert_eq!(
|
||||
SyntheticTextModuleSource::for_module(context, module)
|
||||
.unwrap()
|
||||
.source(),
|
||||
"42"
|
||||
);
|
||||
let retained = SyntheticTextModuleSource::for_module(context, module).unwrap();
|
||||
let SyntheticTextModuleValue::Json(value) = retained.value() else {
|
||||
panic!("expected retained JSON value");
|
||||
};
|
||||
assert_eq!(v8::Local::new(scope, value).number_value(scope), Some(42.0));
|
||||
drop(retained);
|
||||
drop(clone);
|
||||
assert!(weak.upgrade().is_none());
|
||||
assert!(weak_text.upgrade().is_none());
|
||||
assert!(SyntheticTextModuleSource::for_module(context, module).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -859,17 +859,18 @@ fn synthetic_text_module_evaluation_steps<'s>(
|
||||
else {
|
||||
return throw_synthetic_module_error(scope, "synthetic module source is not available");
|
||||
};
|
||||
let source = record.source();
|
||||
match record.key().kind() {
|
||||
ModuleKind::Json => evaluate_json_synthetic_module(scope, module, source),
|
||||
ModuleKind::Text => {
|
||||
match record.value() {
|
||||
crate::module_runtime::SyntheticTextModuleValue::Text(source) => {
|
||||
let value = v8_string(scope, source)?;
|
||||
set_synthetic_default_export(scope, module, value.into())
|
||||
}
|
||||
ModuleKind::Css => {
|
||||
crate::module_runtime::SyntheticTextModuleValue::Json(value) => {
|
||||
let value = v8::Local::new(scope, value);
|
||||
set_synthetic_default_export(scope, module, value)
|
||||
}
|
||||
crate::module_runtime::SyntheticTextModuleValue::Css(source) => {
|
||||
evaluate_css_synthetic_module(scope, module, record.key().url().as_str(), source)
|
||||
}
|
||||
_ => throw_synthetic_module_error(scope, "unexpected synthetic text module kind"),
|
||||
}
|
||||
}
|
||||
fn wasm_synthetic_module_evaluation_steps<'s>(
|
||||
@@ -891,18 +892,6 @@ fn wasm_synthetic_module_evaluation_steps<'s>(
|
||||
})
|
||||
}
|
||||
|
||||
fn evaluate_json_synthetic_module<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
module: v8::Local<'s, v8::Module>,
|
||||
source: &str,
|
||||
) -> Option<v8::Local<'s, v8::Value>> {
|
||||
let Some(json_source) = v8_string(scope, source) else {
|
||||
return throw_synthetic_module_error(scope, "failed to allocate JSON module source");
|
||||
};
|
||||
let value = v8::json::parse(scope, json_source)?;
|
||||
set_synthetic_default_export(scope, module, value)
|
||||
}
|
||||
|
||||
fn evaluate_css_synthetic_module<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
module: v8::Local<'s, v8::Module>,
|
||||
|
||||
@@ -259,6 +259,7 @@ impl ScriptVm {
|
||||
source: Arc<str>,
|
||||
source_url: &Url,
|
||||
) -> std::result::Result<(ModuleRecordEntry, ModuleIdentityHash), ModuleLoadError> {
|
||||
let mut exception_id = None;
|
||||
self.renderer_document_isolate
|
||||
.with_entered_renderer_document_isolate(|isolate| {
|
||||
let scope = pin!(v8::HandleScope::new(isolate));
|
||||
@@ -268,6 +269,33 @@ impl ScriptVm {
|
||||
let try_catch = pin!(v8::TryCatch::new(scope));
|
||||
let mut scope = try_catch.init();
|
||||
|
||||
let value = if key.kind() == ModuleKind::Json {
|
||||
let value = crate::module_runtime::parse_json_module(
|
||||
&mut scope,
|
||||
&source,
|
||||
source_url.as_str(),
|
||||
)
|
||||
.ok_or_else(|| {
|
||||
if let Some(exception) = scope.exception() {
|
||||
match retain_module_exception(&mut scope, exception) {
|
||||
Ok(id) => exception_id = Some(id),
|
||||
Err(error) => return error,
|
||||
}
|
||||
}
|
||||
let message = scope
|
||||
.message()
|
||||
.map(|message| message.get(&scope).to_rust_string_lossy(&scope))
|
||||
.unwrap_or_else(|| "failed to parse JSON module".to_owned());
|
||||
anyhow::anyhow!("{message}")
|
||||
})?;
|
||||
crate::module_runtime::SyntheticTextModuleValue::Json(v8::Global::new(
|
||||
&scope, value,
|
||||
))
|
||||
} else if key.kind() == ModuleKind::Text {
|
||||
crate::module_runtime::SyntheticTextModuleValue::Text(source)
|
||||
} else {
|
||||
crate::module_runtime::SyntheticTextModuleValue::Css(source)
|
||||
};
|
||||
let module_name = v8_string(&scope, source_url.as_str()).ok_or_else(|| {
|
||||
anyhow::anyhow!("failed to allocate v8 synthetic module name")
|
||||
})?;
|
||||
@@ -284,14 +312,23 @@ impl ScriptVm {
|
||||
&mut scope,
|
||||
module,
|
||||
key.clone(),
|
||||
source,
|
||||
value,
|
||||
);
|
||||
let compiled_module = v8::Global::new(scope.as_ref(), module);
|
||||
let entry = ModuleRecordEntry::new(key, compiled_module, Vec::new())
|
||||
.with_synthetic_text_module_source(evaluation_source);
|
||||
Ok((entry, identity))
|
||||
})
|
||||
.map_err(|error| ModuleLoadError::new(ModuleLoadStage::Compile, error.to_string()))
|
||||
.map_err(|error| {
|
||||
let error = ModuleLoadError::new(ModuleLoadStage::Compile, error.to_string());
|
||||
if let Some(id) = exception_id {
|
||||
error
|
||||
.with_exception_id(id)
|
||||
.with_error_constructor(ScriptErrorConstructorKind::SyntaxError)
|
||||
} else {
|
||||
error
|
||||
}
|
||||
})
|
||||
}
|
||||
pub(crate) fn instantiate_native_module_graph(
|
||||
&mut self,
|
||||
|
||||
@@ -1,6 +1,105 @@
|
||||
use super::super::{module_load_error_value, retain_module_exception};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn json_module_parse_errors_keep_the_response_origin_and_original_exception() {
|
||||
let mut vm = new_test_vm("https://json-errors.test/page.html");
|
||||
vm.eval("JSON.parse = () => { throw new Error('author JSON.parse'); };")
|
||||
.unwrap();
|
||||
let request_url = Url::parse("https://json-errors.test/redirect.json").unwrap();
|
||||
let response_url = Url::parse("https://json-errors.test/final/bad.json").unwrap();
|
||||
let error = vm
|
||||
.compile_native_module_record(
|
||||
ModuleMapKey::json_with_attributes(request_url, ModuleAttributesKey::empty()),
|
||||
&ModuleSource::text("{\n\"key\":\n}".to_owned()),
|
||||
&response_url,
|
||||
&ModuleFetchMetadata::default(),
|
||||
)
|
||||
.expect_err("invalid JSON must fail before instantiation or evaluation");
|
||||
assert_eq!(
|
||||
error.error_constructor(),
|
||||
Some(ScriptErrorConstructorKind::SyntaxError)
|
||||
);
|
||||
vm.with_default_context_scope(|scope, _| {
|
||||
let exception = module_load_error_value(scope, &error)?;
|
||||
let global = scope.get_current_context().global(scope);
|
||||
assert_eq!(
|
||||
global.set(scope, v8str(scope, "__original").into(), exception),
|
||||
Some(true)
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
vm.eval(
|
||||
r#"
|
||||
globalThis.__reads = 0;
|
||||
for (const name of ['fileName', 'lineNumber', 'columnNumber', 'stack']) {
|
||||
Object.defineProperty(__original, name, {get() { ++__reads; throw 1; }});
|
||||
}
|
||||
Object.freeze(__original);
|
||||
addEventListener('error', event => {
|
||||
globalThis.__result = [event.filename, event.lineno, event.colno,
|
||||
event.error === __original, event.error instanceof SyntaxError, __reads];
|
||||
event.preventDefault();
|
||||
});
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
vm.report_window_error_body(
|
||||
error.message(),
|
||||
Some("https://json-errors.test/root.mjs"),
|
||||
error.error_value(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
// JSON.parse was replaced above, so inspect the result with stringify only.
|
||||
assert_eq!(
|
||||
vm.eval("JSON.stringify(__result)").unwrap(),
|
||||
r#"["https://json-errors.test/final/bad.json",3,1,true,true,0]"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_module_parse_errors_do_not_override_a_later_javascript_throw_location() {
|
||||
let mut vm = new_test_vm("https://json-errors.test/page.html");
|
||||
let url = Url::parse("https://json-errors.test/bad.json").unwrap();
|
||||
let error = vm
|
||||
.compile_native_module_record(
|
||||
ModuleMapKey::json_with_attributes(url.clone(), ModuleAttributesKey::empty()),
|
||||
&ModuleSource::text("{".to_owned()),
|
||||
&url,
|
||||
&ModuleFetchMetadata::default(),
|
||||
)
|
||||
.unwrap_err();
|
||||
vm.with_default_context_scope(|scope, _| {
|
||||
let exception = module_load_error_value(scope, &error)?;
|
||||
let global = scope.get_current_context().global(scope);
|
||||
assert_eq!(
|
||||
global.set(scope, v8str(scope, "__original").into(), exception),
|
||||
Some(true)
|
||||
);
|
||||
let try_catch = pin!(v8::TryCatch::new(scope));
|
||||
let mut scope = try_catch.init();
|
||||
let source = v8str(
|
||||
&scope,
|
||||
"throw __original;\n//# sourceURL=https://json-errors.test/thrower.js",
|
||||
);
|
||||
let script = v8::Script::compile(&scope, source, None).unwrap();
|
||||
assert!(script.run(&scope).is_none());
|
||||
let exception = scope.exception();
|
||||
let message = scope.message();
|
||||
let report = crate::exception_reporting::build_event_handler_exception_report(
|
||||
&mut scope, exception, message, None,
|
||||
);
|
||||
assert_eq!(
|
||||
report.source.as_deref(),
|
||||
Some("https://json-errors.test/thrower.js")
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn compile_parse_error(vm: &mut ScriptVm, path: &str) -> ModuleLoadError {
|
||||
let url = Url::parse(path).unwrap();
|
||||
vm.compile_native_module_record(
|
||||
|
||||
@@ -174,7 +174,7 @@ fn dispatch_script_failure_error_body(
|
||||
}
|
||||
None => window_script_failure_error_value(scope, global, None, message_value),
|
||||
};
|
||||
// Retained V8 exceptions already carry engine-owned source information.
|
||||
// Retained exceptions carry V8 or loader-owned source information.
|
||||
// Read that metadata, not author-visible stack or location properties, and
|
||||
// do not replace a dependency's URL with the root script's fallback URL.
|
||||
let location = retained.then(|| {
|
||||
@@ -182,7 +182,11 @@ fn dispatch_script_failure_error_body(
|
||||
let filename = exception_message
|
||||
.get_script_resource_name(scope)
|
||||
.and_then(|value| v8::Local::<v8::String>::try_from(value).ok())
|
||||
.map(|value| value.to_rust_string_lossy(scope));
|
||||
.map(|value| value.to_rust_string_lossy(scope))
|
||||
.filter(|value| !value.is_empty())
|
||||
.or_else(|| {
|
||||
crate::module_runtime::json_module_exception_source_url(scope, error_value)
|
||||
});
|
||||
let line = exception_message
|
||||
.get_line_number(scope)
|
||||
.and_then(|line| u32::try_from(line).ok())
|
||||
|
||||
@@ -133,3 +133,23 @@ async fn worker_nested_json_errors_follow_declaration_order_and_reuse_native_exc
|
||||
server.assert_no_more_requests();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worker_json_data_module_parse_errors_keep_syntax_error_identity() {
|
||||
ensure_v8();
|
||||
let mut handle = spawn_worker(
|
||||
r#"
|
||||
const url = 'data:application/json,%7Bbad';
|
||||
const capture = () => import(url, {with:{type:'json'}}).catch(error => error);
|
||||
Promise.all([capture(), capture()]).then(async errors => {
|
||||
const repeated = await capture();
|
||||
postMessage([errors[0] instanceof SyntaxError,
|
||||
errors[0] === errors[1], errors[0] === repeated]);
|
||||
});
|
||||
"#
|
||||
.into(),
|
||||
"https://json-errors.test/worker.js".into(),
|
||||
);
|
||||
assert_eq!(recv_post_json(&mut handle).await, "[true,true,true]");
|
||||
handle.terminate_and_join();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user