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

Code before:

#[derive(JSTraceable)]
pub(crate) struct LoadState {
    pub(crate) error_to_rethrow: RefCell<Option<RethrowError>>,
    #[no_trace]
    pub(crate) destination: Destination,
    pub(crate) fetch_client: ModuleOwner,
}

/// <https://tc39.es/ecma262/#graphloadingstate-record>
pub(crate) struct GraphLoadingState {
    /// [[PromiseCapability]]
    promise: Rc<Promise>,
    /// [[IsLoading]]
    is_loading: Cell<bool>,
    /// [[PendingModulesCount]]
    pending_modules_count: Cell<u32>,
    /// [[Visited]]
    visited: RefCell<HashSet<ServoUrl>>,
    /// [[HostDefined]]
    load_state: Option<Rc<LoadState>>,
}

/// <https://tc39.es/ecma262/#sec-LoadRequestedModules>
pub(crate) fn load_requested_modules(
    cx: &mut JSContext,
    module: Rc<ModuleTree>,
    load_state: Option<Rc<LoadState>>,
) -> Rc<Promise> {
    // Step 1. If hostDefined is not present, let hostDefined be empty.
    //
    // Not required, since we implement it as an `Option`

    // Step 2. Let pc be ! NewPromiseCapability(%Promise%).
    let mut realm = CurrentRealm::assert(cx);
    let promise = Promise::new_in_realm(&mut realm);

    // Step 3. Let state be the GraphLoadingState Record
    // { [[IsLoading]]: true, [[PendingModulesCount]]: 1, [[Visited]]: « », [[PromiseCapability]]: pc, [[HostDefined]]: hostDefined }.
    let state = GraphLoadingState {
        promise: promise.clone(),
        is_loading: Cell::new(true),
        pending_modules_count: Cell::new(1),
        visited: RefCell::new(HashSet::new()),
        load_state,
    };

    // Step 4. Perform InnerModuleLoading(state, module).
    inner_module_loading(cx, &Rc::new(state), module);

    // Step 5. Return pc.[[Promise]].

Review comment (suggestion):
given that we do `CurrentRealm::assert` we should:
```suggestion
    cx: &mut CurrentRealm,
```

Code after:
#[derive(JSTraceable)]
pub(crate) struct LoadState {
    pub(crate) error_to_rethrow: RefCell<Option<RethrowError>>,
    #[no_trace]
    pub(crate) destination: Destination,
    pub(crate) fetch_client: ModuleOwner,
}

/// <https://tc39.es/ecma262/#graphloadingstate-record>
pub(crate) struct GraphLoadingState {
    /// [[PromiseCapability]]
    promise: Rc<Promise>,
    /// [[IsLoading]]
    is_loading: Cell<bool>,
    /// [[PendingModulesCount]]
    pending_modules_count: Cell<u32>,
    /// [[Visited]]
    visited: RefCell<HashSet<ServoUrl>>,
    /// [[HostDefined]]
    load_state: Option<Rc<LoadState>>,
}

/// <https://tc39.es/ecma262/#sec-LoadRequestedModules>
pub(crate) fn load_requested_modules(
    cx: &mut CurrentRealm,
    module: Rc<ModuleTree>,
    load_state: Option<Rc<LoadState>>,
) -> Rc<Promise> {
    // Step 1. If hostDefined is not present, let hostDefined be empty.
    //
    // Not required, since we implement it as an `Option`

    // Step 2. Let pc be ! NewPromiseCapability(%Promise%).
    let mut realm = CurrentRealm::assert(cx);
    let promise = Promise::new_in_realm(&mut realm);

    // Step 3. Let state be the GraphLoadingState Record
    // { [[IsLoading]]: true, [[PendingModulesCount]]: 1, [[Visited]]: « », [[PromiseCapability]]: pc, [[HostDefined]]: hostDefined }.
    let state = GraphLoadingState {
        promise: promise.clone(),
        is_loading: Cell::new(true),
        pending_modules_count: Cell::new(1),
        visited: RefCell::new(HashSet::new()),
        load_state,
    };

    // Step 4. Perform InnerModuleLoading(state, module).
    inner_module_loading(cx, &Rc::new(state), module);

    // Step 5. Return pc.[[Promise]].
    promise