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

Code before:
/// <https://tc39.es/ecma262/#sec-ContinueDynamicImport>
fn continue_dynamic_import(
    cx: &mut JSContext,
    promise: Rc<Promise>,
    module_completion: Result<Rc<ModuleTree>, RethrowError>,
) {
    // Step 1. If moduleCompletion is an abrupt completion, then
    if let Err(exception) = module_completion {
        // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « moduleCompletion.[[Value]] »).
        promise.reject(cx.into(), exception.handle(), CanGc::from_cx(cx));

        // b. Return unused.
        return;
    }

    let realm = CurrentRealm::assert(cx);
    let global = GlobalScope::from_current_realm(&realm);

    // Step 2. Let module be moduleCompletion.[[Value]].
    let module = module_completion.unwrap();
    let record = ModuleObject::new(module.get_record().map(|module| module.handle()).unwrap());

    // Step 3. Let loadPromise be module.LoadRequestedModules().
    let load_promise = load_requested_modules(cx, module, None);

    let realm = enter_realm(&*global);
    let comp = InRealm::Entered(&realm);
    let _ais = AutoIncumbentScript::new(&global);

    // Step 4. Let rejectedClosure be a new Abstract Closure with parameters (reason)
    // that captures promiseCapability and performs the following steps when called:
    // Step 5. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
    // Note: implemented by OnRejectedHandler.

    let global_scope = global.clone();
    let inner_promise = promise.clone();
    let fulfilled_promise = promise.clone();

    // Step 6. Let linkAndEvaluateClosure be a new Abstract Closure with no parameters that captures
    // module, promiseCapability, and onRejected and performs the following steps when called:
    // Step 7. Let linkAndEvaluate be CreateBuiltinFunction(linkAndEvaluateClosure, 0, "", « »).
    let link_and_evaluate = ModuleHandler::new_boxed(Box::new(
        task!(link_and_evaluate: |cx, global_scope: DomRoot<GlobalScope>, inner_promise: Rc<Promise>, record: ModuleObject| {
            let mut realm = AutoRealm::new(
                cx,
                std::ptr::NonNull::new(global_scope.reflector().get_jsobject().get()).unwrap(),
            );
            let cx = &mut *realm;
            // a. Let link be Completion(module.Link()).
            let link = unsafe { ModuleLink(cx, record.handle()) };


Review comment (suggestion):
We have:
```suggestion
    let realm = enter_auto_realm(cx, global);
```
that will return safer AutoRealm

Code after:
    // 4. Return unused.
}

/// <https://tc39.es/ecma262/#sec-ContinueDynamicImport>
fn continue_dynamic_import(
    cx: &mut CurrentRealm,
    promise: Rc<Promise>,
    module_completion: Result<Rc<ModuleTree>, RethrowError>,
) {
    // Step 1. If moduleCompletion is an abrupt completion, then
    if let Err(exception) = module_completion {
        // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « moduleCompletion.[[Value]] »).
        promise.reject(cx.into(), exception.handle(), CanGc::from_cx(cx));

        // b. Return unused.
        return;
    }

    let realm = CurrentRealm::assert(cx);
    let global = GlobalScope::from_current_realm(&realm);

    // Step 2. Let module be moduleCompletion.[[Value]].
    let module = module_completion.unwrap();
    let record = ModuleObject::new(module.get_record().map(|module| module.handle()).unwrap());

    // Step 3. Let loadPromise be module.LoadRequestedModules().
    let load_promise = load_requested_modules(cx, module, None);

    // Step 4. Let rejectedClosure be a new Abstract Closure with parameters (reason)
    // that captures promiseCapability and performs the following steps when called:
    // Step 5. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
    // Note: implemented by OnRejectedHandler.

    let global_scope = global.clone();
    let inner_promise = promise.clone();
    let fulfilled_promise = promise.clone();

    // Step 6. Let linkAndEvaluateClosure be a new Abstract Closure with no parameters that captures
    // module, promiseCapability, and onRejected and performs the following steps when called:
    // Step 7. Let linkAndEvaluate be CreateBuiltinFunction(linkAndEvaluateClosure, 0, "", « »).
    let link_and_evaluate = ModuleHandler::new_boxed(Box::new(
        task!(link_and_evaluate: |cx, global_scope: DomRoot<GlobalScope>, inner_promise: Rc<Promise>, record: ModuleObject| {
            let mut realm = AutoRealm::new(
                cx,
                std::ptr::NonNull::new(global_scope.reflector().get_jsobject().get()).unwrap(),
            );
            let in_realm_proof = (&mut realm.current_realm()).into();
            let cx = &mut *realm;
            // a. Let link be Completion(module.Link()).
            let link = unsafe { ModuleLink(cx, record.handle()) };