feat(script): register import map (#37504)

- Register import map when preparing HTML script element
- Added `import_map` to `GlobalScope`

Testing: should pass existing WPT
Fixes: part of https://github.com/servo/servo/issues/37316

---------

Signed-off-by: Jason Tsai <git@pews.dev>
This commit is contained in:
Jason Tsai 2025-06-18 16:12:57 +09:00 committed by GitHub
parent d8c552f3ab
commit f97cdb4d12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 187 additions and 28 deletions

View file

@ -143,7 +143,9 @@ use crate::messaging::{CommonScriptMsg, ScriptEventLoopReceiver, ScriptEventLoop
use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
use crate::network_listener::{NetworkListener, PreInvoke};
use crate::realms::{AlreadyInRealm, InRealm, enter_realm};
use crate::script_module::{DynamicModuleList, ModuleScript, ModuleTree, ScriptFetchOptions};
use crate::script_module::{
DynamicModuleList, ImportMap, ModuleScript, ModuleTree, ScriptFetchOptions,
};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext, ThreadSafeJSContext};
use crate::script_thread::{ScriptThread, with_script_thread};
use crate::security_manager::CSPViolationReportTask;
@ -380,6 +382,12 @@ pub(crate) struct GlobalScope {
#[ignore_malloc_size_of = "Rc<T> is hard"]
notification_permission_request_callback_map:
DomRefCell<HashMap<String, Rc<NotificationPermissionCallback>>>,
/// An import map allows control over module specifier resolution.
/// For now, only Window global objects have their import map modified from the initial empty one.
///
/// <https://html.spec.whatwg.org/multipage/#import-maps>
import_map: DomRefCell<ImportMap>,
}
/// A wrapper for glue-code between the ipc router and the event-loop.
@ -781,6 +789,7 @@ impl GlobalScope {
byte_length_queuing_strategy_size_function: OnceCell::new(),
count_queuing_strategy_size_function: OnceCell::new(),
notification_permission_request_callback_map: Default::default(),
import_map: Default::default(),
}
}
@ -3759,6 +3768,10 @@ impl GlobalScope {
.queue(task);
}
}
pub(crate) fn import_map(&self) -> &DomRefCell<ImportMap> {
&self.import_map
}
}
/// Returns the Rust global scope from a JS global object.

View file

@ -49,7 +49,7 @@ use crate::dom::bindings::codegen::GenericBindings::HTMLElementBinding::HTMLElem
use crate::dom::bindings::codegen::UnionTypes::{
TrustedScriptOrString, TrustedScriptURLOrUSVString,
};
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomGlobal;
@ -76,8 +76,8 @@ use crate::fetch::create_a_potential_cors_request;
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
use crate::realms::enter_realm;
use crate::script_module::{
ModuleOwner, ScriptFetchOptions, fetch_external_module_script, fetch_inline_module_script,
parse_an_import_map_string,
ImportMap, ModuleOwner, ScriptFetchOptions, fetch_external_module_script,
fetch_inline_module_script, parse_an_import_map_string, register_import_map,
};
use crate::script_runtime::CanGc;
use crate::task_source::{SendableTaskSource, TaskSourceName};
@ -308,6 +308,7 @@ pub(crate) struct ScriptOrigin {
fetch_options: ScriptFetchOptions,
type_: ScriptType,
unminified_dir: Option<String>,
import_map: Fallible<ImportMap>,
}
impl ScriptOrigin {
@ -317,6 +318,7 @@ impl ScriptOrigin {
fetch_options: ScriptFetchOptions,
type_: ScriptType,
unminified_dir: Option<String>,
import_map: Fallible<ImportMap>,
) -> ScriptOrigin {
ScriptOrigin {
code: SourceCode::Text(text),
@ -325,6 +327,7 @@ impl ScriptOrigin {
fetch_options,
type_,
unminified_dir,
import_map,
}
}
@ -342,6 +345,7 @@ impl ScriptOrigin {
fetch_options,
type_,
unminified_dir,
import_map: Err(Error::NotFound),
}
}
@ -960,11 +964,12 @@ impl HTMLScriptElement {
match script_type {
ScriptType::Classic => {
let result = Ok(ScriptOrigin::internal(
Rc::clone(&text_rc),
base_url.clone(),
options.clone(),
text_rc,
base_url,
options,
script_type,
self.global().unminified_js_dir(),
Err(Error::NotFound),
));
if was_parser_inserted &&
@ -1003,12 +1008,23 @@ impl HTMLScriptElement {
ScriptType::ImportMap => {
// Step 32.1 Let result be the result of creating an import map
// parse result given source text and base URL.
let _result = parse_an_import_map_string(
let import_map_result = parse_an_import_map_string(
ModuleOwner::Window(Trusted::new(self)),
text_rc,
Rc::clone(&text_rc),
base_url.clone(),
can_gc,
);
let result = Ok(ScriptOrigin::internal(
text_rc,
base_url,
options,
script_type,
self.global().unminified_js_dir(),
import_map_result,
));
// Step 34.3
self.execute(result, can_gc);
},
}
}
@ -1125,8 +1141,8 @@ impl HTMLScriptElement {
self.run_a_module_script(&script, false, can_gc);
},
ScriptType::ImportMap => {
// TODO: Register an import map given el's relevant
// global object and el's result.
// Step 6.1 Register an import map given el's relevant global object and el's result.
register_import_map(&self.owner_global(), script.import_map, can_gc);
},
}