Repository: servo/servo
File: components/script/script_module.rs (Rust)

Code before:

#[expect(unsafe_code)]
#[expect(non_snake_case)]
/// <https://tc39.es/ecma262/#sec-HostLoadImportedModule>
/// <https://html.spec.whatwg.org/multipage/#hostloadimportedmodule>
unsafe extern "C" fn HostResolveImportedModule(
    cx: *mut RawJSContext,
    reference_private: RawHandleValue,
    specifier: RawHandle<*mut JSObject>,
) -> *mut JSObject {
    // Safety: it is safe to construct a JSContext from engine hook.
    let mut cx = unsafe { JSContext::from_ptr(NonNull::new(cx).unwrap()) };
    let realm = CurrentRealm::assert(&mut cx);
    let global_scope = GlobalScope::from_current_realm(&realm);

    // Step 5.
    let module_data = unsafe { module_script_from_reference_private(&reference_private) };
    let jsstr = unsafe { GetModuleRequestSpecifier(&cx, Handle::from_raw(specifier)) };
    let module_type = unsafe { GetModuleRequestType(&cx, Handle::from_raw(specifier)) };

    let specifier = unsafe { jsstr_to_string(cx.raw_cx(), NonNull::new(jsstr).unwrap()) };
    let url = ModuleTree::resolve_module_specifier(
        &global_scope,
        module_data,
        DOMString::from(specifier),
        CanGc::note(),
    );

    // Step 6.
    assert!(url.is_ok());

    let parsed_url = url.unwrap();

    // Step 4 & 7.
    let module = global_scope.get_module_map_entry(&(parsed_url, module_type));

    // Step 9.
    assert!(module.as_ref().is_some_and(
        |status| matches!(status, ModuleStatus::Loaded(module_tree) if module_tree.is_some())
    ));

    let ModuleStatus::Loaded(Some(module_tree)) = module.unwrap() else {
        unreachable!()
    };

    let fetched_module_object = module_tree.get_record();

    // Step 8.
    assert!(fetched_module_object.is_some());

    // Step 10.

Review comment (suggestion):
Prefer using:
```suggestion
        CanGc::from_cx(cx),
```
so we have clearly marked where we have cx available.

Code after:
}

#[expect(unsafe_code)]
#[expect(non_snake_case)]
/// <https://tc39.es/ecma262/#sec-HostLoadImportedModule>
/// <https://html.spec.whatwg.org/multipage/#hostloadimportedmodule>
unsafe extern "C" fn HostResolveImportedModule(
    cx: *mut RawJSContext,
    reference_private: RawHandleValue,
    specifier: RawHandle<*mut JSObject>,
) -> *mut JSObject {
    // Safety: it is safe to construct a JSContext from engine hook.
    let mut cx = unsafe { JSContext::from_ptr(NonNull::new(cx).unwrap()) };
    let mut realm = CurrentRealm::assert(&mut cx);
    let global_scope = GlobalScope::from_current_realm(&realm);

    let cx = &mut realm;

    // Step 5.
    let module_data = unsafe { module_script_from_reference_private(&reference_private) };
    let jsstr = unsafe { GetModuleRequestSpecifier(cx, Handle::from_raw(specifier)) };
    let module_type = unsafe { GetModuleRequestType(cx, Handle::from_raw(specifier)) };

    let specifier = unsafe { jsstr_to_string(cx.raw_cx(), NonNull::new(jsstr).unwrap()) };
    let url = ModuleTree::resolve_module_specifier(
        &global_scope,
        module_data,
        DOMString::from(specifier),
        CanGc::from_cx(cx),
    );

    // Step 6.
    assert!(url.is_ok());

    let parsed_url = url.unwrap();

    // Step 4 & 7.
    let module = global_scope.get_module_map_entry(&(parsed_url, module_type));

    // Step 9.
    assert!(module.as_ref().is_some_and(
        |status| matches!(status, ModuleStatus::Loaded(module_tree) if module_tree.is_some())
    ));

    let ModuleStatus::Loaded(Some(module_tree)) = module.unwrap() else {
        unreachable!()
    };

    let fetched_module_object = module_tree.get_record();

    // Step 8.