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

Code before:
    ) -> ClassicScript {
        let source_code = Rc::new(DOMString::from(source.clone()));
        let cx = GlobalScope::get_cx();
        rooted!(in(*cx) let mut compiled_script = std::ptr::null_mut::<JSScript>());

        // TODO Step 1. If mutedErrors is true, then set baseURL to about:blank.

        // TODO Step 2. If scripting is disabled for settings, then set source to the empty string.

        // TODO Step 4. Set script's settings object to settings.
        // TODO Step 7. Set script's muted errors to mutedErrors.

        // TODO Step 9. Record classic script creation time given script and sourceURLForWindowScripts.

        // Step 10. Let result be ParseScript(source, settings's realm, script).
        compiled_script.set(compile_script(
            cx,
            &source,
            url.as_str(),
            line_number,
            introduction_type,
        ));

        // Step 11. If result is a list of errors, then:
        let record = if compiled_script.get().is_null() {
            // Set script's parse error and its error to rethrow to result[0].
            rooted!(in(*cx) let mut exception = UndefinedValue());

            assert!(unsafe { JS_GetPendingException(*cx, exception.handle_mut()) });
            unsafe { JS_ClearPendingException(*cx) };

            Err(RethrowError::new(Heap::boxed(exception.get())))
        } else {
            Ok(NonNull::new(*compiled_script).expect("Can't be null"))
        };

        // Step 3. Let script be a new classic script that this algorithm will subsequently initialize.
        // Step 5. Set script's base URL to baseURL.
        // Step 6. Set script's fetch options to options.
        // Step 12. Set script's record to result.
        let mut script = ClassicScript {
            record,
            url,
            fetch_options,
            source: source_code,
            external,
            unminified_dir: self.unminified_js_dir(),
        };
        unminify_js(&mut script);

        // Step 13. Return script.

Review comment (bug):
```suggestion
            // Step 11.1. Set script's parse error and its error to rethrow to result[0].
```
What about step 11.2? The algorithm appears to return the script, but we throw an error here.

Code after:
    ) -> ClassicScript {
        let source_code = Rc::new(DOMString::from(source.clone()));
        let cx = GlobalScope::get_cx();
        rooted!(in(*cx) let mut compiled_script = std::ptr::null_mut::<JSScript>());

        // TODO Step 1. If mutedErrors is true, then set baseURL to about:blank.

        // TODO Step 2. If scripting is disabled for settings, then set source to the empty string.

        // TODO Step 4. Set script's settings object to settings.
        // TODO Step 7. Set script's muted errors to mutedErrors.

        // TODO Step 9. Record classic script creation time given script and sourceURLForWindowScripts.

        // Step 10. Let result be ParseScript(source, settings's realm, script).
        compiled_script.set(compile_script(
            cx,
            &source,
            url.as_str(),
            line_number,
            introduction_type,
        ));

        // Step 11. If result is a list of errors, then:
        let record = if compiled_script.get().is_null() {
            // Step 11.1. Set script's parse error and its error to rethrow to result[0].
            // Step 11.2. Return script.
            rooted!(in(*cx) let mut exception = UndefinedValue());

            assert!(unsafe { JS_GetPendingException(*cx, exception.handle_mut()) });
            unsafe { JS_ClearPendingException(*cx) };

            Err(RethrowError::new(Heap::boxed(exception.get())))
        } else {
            Ok(NonNull::new(*compiled_script).expect("Can't be null"))
        };

        // Step 3. Let script be a new classic script that this algorithm will subsequently initialize.
        // Step 5. Set script's base URL to baseURL.
        // Step 6. Set script's fetch options to options.
        // Step 12. Set script's record to result.
        let mut script = ClassicScript {
            record,
            url,
            fetch_options,
            source: source_code,
            external,
            unminified_dir: self.unminified_js_dir(),
        };
        unminify_js(&mut script);