Skip to content

Truly independent web browser

10.7 KB
Raw Blame
/*
 * Copyright (c) 2021-2023, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Debug.h>
#include <LibCore/ElapsedTimer.h>
#include <LibGC/Heap.h>
#include <LibJS/ScriptCompilation.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
#include <LibWeb/WebIDL/DOMException.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(ClassicScript);

static void register_source(ClassicScript& script, ScriptRegistry::IsInlineSource is_inline_source, size_t source_line_number)
{
    auto script_record = script.script_record();
    if (!script_record)
        return;

    auto source_code = JS::top_level_source_code(*script_record);
    if (!source_code)
        return;

    register_javascript_source(script, source_code.release_nonnull(), ScriptRegistry::JavaScriptSource::Type::Script, is_inline_source, source_line_number);
}

// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
GC::Ref<ClassicScript> ClassicScript::create(ByteString filename, Utf16String source, EnvironmentSettingsObject& settings, URL::URL base_url, size_t source_line_number, MutedErrors muted_errors, ScriptRegistry::IsInlineSource is_inline_source)
{
    // 1. If mutedErrors is true, then set baseURL to about:blank.
    if (muted_errors == MutedErrors::Yes)
        base_url = URL::about_blank();

    // FIXME: 2. If scripting is disabled for settings and bypassDisabledScripting is false, then set source to the empty string.
    if (is_scripting_disabled(settings))
        source = {};

    // 3. Let script be a new classic script that this algorithm will subsequently initialize.
    // 4. Set script's settings object to settings.
    // 5. Set script's base URL to baseURL.
    auto script = GC::Heap::the().allocate<ClassicScript>(move(base_url), move(filename), settings);

    // 6. Set script's fetch options to options.
    // NB: Callers set the script's fetch options once it has been created.

    // 7. Set script's muted errors to mutedErrors.
    script->m_muted_errors = muted_errors;

    // 8. Set script's parse error and error to rethrow to null.
    script->set_parse_error(JS::js_null());
    script->set_error_to_rethrow(JS::js_null());

    // FIXME: 9. Record classic script creation time given script and sourceURLForWindowScripts.

    // 10. Let result be ParseScript(source, settings's realm, script).
    // Note: Passing script as the last parameter here ensures result.[[HostDefined]] will be script.
    auto parse_timer = Core::ElapsedTimer::start_new();
    auto source_code = JS::SourceCode::create(script->display_filename(), source);
    auto parsed = JS::ParsedProgram::parse(move(source), JS::ProgramType::Script, source_line_number);
    auto result = JS::create_script(move(parsed), move(source_code), settings.realm(), script->filename(), script.ptr());
    dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed_milliseconds());

    // 11. If result is a list of errors:
    if (result.is_error()) {
        auto& parse_error = result.error().first();
        dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to parse: {}", parse_error.to_utf16_string());

        // 1. Set script's parse error and its error to rethrow to result[0].
        script->set_parse_error(JS::SyntaxError::create(settings.realm(), parse_error.to_utf16_string()), parse_error.position);
        script->set_error_to_rethrow(script->parse_error());

        // 2. Return script.
        return script;
    }

    // 12. Set script's record to result.
    script->m_script_record = *result.release_value();
    register_source(*script, is_inline_source, source_line_number);

    // 13. Return script.
    return script;
}

GC::Ref<ClassicScript> ClassicScript::create_from_pre_parsed(ByteString filename, NonnullRefPtr<JS::SourceCode const> source_code, EnvironmentSettingsObject& settings, URL::URL base_url, JS::ParsedProgram parsed, MutedErrors muted_errors)
{
    auto& realm = settings.realm();

    if (muted_errors == MutedErrors::Yes)
        base_url = URL::about_blank();

    auto script = GC::Heap::the().allocate<ClassicScript>(move(base_url), move(filename), settings);

    script->m_muted_errors = muted_errors;
    script->set_parse_error(JS::js_null());
    script->set_error_to_rethrow(JS::js_null());

    auto parse_timer = Core::ElapsedTimer::start_new();
    auto result = JS::create_script(move(parsed), move(source_code), realm, script->filename(), script.ptr());
    dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Compiled pre-parsed {} in {}ms", script->filename(), parse_timer.elapsed_milliseconds());

    if (result.is_error()) {
        auto& parse_error = result.error().first();
        dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to compile: {}", parse_error.to_utf16_string());

        script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);
        script->set_error_to_rethrow(script->parse_error());

        return script;
    }

    script->m_script_record = *result.release_value();
    register_source(*script, ScriptRegistry::IsInlineSource::No, 1);

    return script;
}

GC::Ref<ClassicScript> ClassicScript::create_from_pre_compiled(ByteString filename, NonnullRefPtr<JS::SourceCode const> source_code, EnvironmentSettingsObject& settings, URL::URL base_url, JS::CompiledProgram compiled, MutedErrors muted_errors)
{
    auto& realm = settings.realm();

    if (muted_errors == MutedErrors::Yes)
        base_url = URL::about_blank();

    auto script = GC::Heap::the().allocate<ClassicScript>(move(base_url), move(filename), settings);

    script->m_muted_errors = muted_errors;
    script->set_parse_error(JS::js_null());
    script->set_error_to_rethrow(JS::js_null());

    auto parse_timer = Core::ElapsedTimer::start_new();
    auto result = JS::create_script(move(compiled), move(source_code), realm, script->filename(), script.ptr());
    dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Materialized pre-compiled {} in {}ms", script->filename(), parse_timer.elapsed_milliseconds());

    if (result.is_error()) {
        auto& parse_error = result.error().first();
        dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to materialize: {}", parse_error.to_utf16_string());

        script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_utf16_string()), parse_error.position);
        script->set_error_to_rethrow(script->parse_error());

        return script;
    }

    script->m_script_record = *result.release_value();
    register_source(*script, ScriptRegistry::IsInlineSource::No, 1);

    return script;
}

// https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, GC::Ptr<JS::Environment> lexical_environment_override)
{
    // 1. Let settings be the settings object of script.
    auto& settings = this->settings_object();
    auto& realm = settings.realm();

    // 2. Check if we can run script with settings. If this returns "do not run", then return NormalCompletion(empty).
    if (can_run_script(settings) == RunScriptDecision::DoNotRun)
        return JS::normal_completion(JS::js_undefined());

    // FIXME: 3. Record classic script execution start time given script.

    // 4. Prepare to run script given settings.
    prepare_to_run_script(settings);

    // 5. Let evaluationStatus be null.
    JS::Completion evaluation_status;

    // 6. If script's error to rethrow is not null, then set evaluationStatus to ThrowCompletion(script's error to rethrow).
    if (!error_to_rethrow().is_null()) {
        evaluation_status = JS::throw_completion(error_to_rethrow());
    }
    // 7. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
    else {
        auto timer = Core::ElapsedTimer::start_new();

        evaluation_status = vm().run(*m_script_record, lexical_environment_override);

        // FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.

        dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed_milliseconds());
    }

    // 8. If evaluationStatus is an abrupt completion, then:
    if (evaluation_status.is_abrupt()) {
        // 1. If rethrow errors is true and script's muted errors is false, then:
        if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::No) {
            // 1. Clean up after running script with settings.
            clean_up_after_running_script(settings);

            // 2. Rethrow evaluationStatus.[[Value]].
            return JS::throw_completion(evaluation_status.value());
        }

        // 2. If rethrow errors is true and script's muted errors is true, then:
        if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::Yes) {
            // 1. Clean up after running script with settings.
            clean_up_after_running_script(settings);

            // 2. Throw a "NetworkError" DOMException.
            return throw_completion(realm, WebIDL::NetworkError::create("Script error."_utf16));
        }

        // 3. Otherwise, rethrow errors is false. Perform the following steps:
        VERIFY(rethrow_errors == RethrowErrors::No);

        // 1. Report an exception given by evaluationStatus.[[Value]] for script's settings object's global object.
        report_an_exception(settings.global_object(), evaluation_status.value(), OmitError::No, this);

        // 2. Clean up after running script with settings.
        clean_up_after_running_script(settings);

        // 3. Return evaluationStatus.
        return evaluation_status;
    }

    // 9. Clean up after running script with settings.
    clean_up_after_running_script(settings);

    // 10. If evaluationStatus is a normal completion, then return evaluationStatus.
    VERIFY(!evaluation_status.is_abrupt());
    return evaluation_status;

    // FIXME: 11. If we've reached this point, evaluationStatus was left as null because the script was aborted prematurely during evaluation.
    //            Return ThrowCompletion(a new "QuotaExceededError" DOMException).
}

ClassicScript::ClassicScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& settings)
    : Script(move(base_url), move(filename), settings)
{
}

ClassicScript::ClassicScript(URL::URL base_url, ByteString filename, Utf16String display_filename, EnvironmentSettingsObject& settings)
    : Script(move(base_url), move(filename), move(display_filename), settings)
{
}

ClassicScript::~ClassicScript() = default;

void ClassicScript::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_script_record);
}

}