/*
 * Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
 * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
 * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
 * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
 * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/NeverDestroyed.h>
#include <LibGC/DeferGC.h>
#include <LibJS/Module.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/FinalizationRegistry.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/ModuleRequest.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/SourceTextModule.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PromiseRejectionEvent.h>
#include <LibWeb/Bindings/WindowExposedInterfaces.h>
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
#include <LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
#include <LibWeb/HTML/Location.h>
#include <LibWeb/HTML/PromiseRejectionEvent.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Scripting/Fetching.h>
#include <LibWeb/HTML/Scripting/ModuleScript.h>
#include <LibWeb/HTML/Scripting/Script.h>
#include <LibWeb/HTML/Scripting/SimilarOriginWindowAgent.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Scripting/WorkerAgent.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/HTML/WorkletGlobalScope.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
#include <LibWeb/WebAssembly/WebAssembly.h>
#include <LibWeb/WebAssembly/WebAssemblyModule.h>
#include <LibWeb/WebIDL/AbstractOperations.h>

namespace Web::Bindings {

static auto& main_thread_vm_ptr()
{
    static NeverDestroyed<RefPtr<JS::VM>> vm;
    return *vm;
}

// https://html.spec.whatwg.org/multipage/webappapis.html#active-script
HTML::Script* active_script()
{
    // 1. Let record be GetActiveScriptOrModule().
    auto record = main_thread_vm().get_active_script_or_module();

    // 2. If record is null, return null.
    // 3. Return record.[[HostDefined]].
    return record.visit(
        [](GC::Ref<JS::Script>& js_script) -> HTML::Script* {
            return as<HTML::ClassicScript>(js_script->host_defined());
        },
        [](GC::Ref<JS::Module>& js_module) -> HTML::Script* {
            return as<HTML::ModuleScript>(js_module->host_defined());
        },
        [](Empty) -> HTML::Script* {
            return nullptr;
        });
}

static NonnullOwnPtr<JS::Agent> create_agent(GC::Heap& heap, AgentType type)
{
    switch (type) {
    case AgentType::SimilarOriginWindow:
        return HTML::SimilarOriginWindowAgent::create(heap);
    case AgentType::DedicatedWorker:
    case AgentType::SharedWorker:
        return HTML::WorkerAgent::create(heap, JS::Agent::CanBlock::Yes);
    case AgentType::ServiceWorker:
        return HTML::WorkerAgent::create(heap, JS::Agent::CanBlock::No);
    default:
        break;
    }
    VERIFY_NOT_REACHED();
}

void initialize_main_thread_vm(AgentType type)
{
    VERIFY(!main_thread_vm_ptr());

    main_thread_vm_ptr() = JS::VM::create();
    main_thread_vm_ptr()->set_agent(create_agent(main_thread_vm_ptr()->heap(), type));

    main_thread_vm_ptr()->on_unimplemented_property_access = [](auto const& object, auto const& property_key) {
        dbgln("FIXME: Unimplemented IDL interface: '{}.{}'", object.class_name(), property_key.to_utf16_string());
    };

    // NOTE: We intentionally leak the main thread JavaScript VM.
    //       This avoids doing an exhaustive garbage collection on process exit.
    main_thread_vm_ptr()->ref();

    // 8.1.6.1 HostEnsureCanAddPrivateElement(O), https://html.spec.whatwg.org/multipage/webappapis.html#the-hostensurecanaddprivateelement-implementation
    main_thread_vm_ptr()->host_ensure_can_add_private_element = [](JS::Object const& object) -> JS::ThrowCompletionOr<void> {
        // 1. If O is a WindowProxy object, or implements Location, then return ThrowCompletion(a new TypeError).
        if (is<HTML::WindowProxy>(object) || is<HTML::Location>(object))
            return main_thread_vm_ptr()->throw_completion<JS::TypeError>("Cannot add private elements to window or location object"_utf16);

        // 2. Return NormalCompletion(unused).
        return {};
    };

    // 8.1.6.2 HostEnsureCanCompileStrings(realm, parameterStrings, bodyString, codeString, compilationType, parameterArgs, bodyArg), https://html.spec.whatwg.org/multipage/webappapis.html#hostensurecancompilestrings(realm,-parameterstrings,-bodystring,-codestring,-compilationtype,-parameterargs,-bodyarg)
    main_thread_vm_ptr()->host_ensure_can_compile_strings = [](JS::Realm& realm, ReadonlySpan<Utf16String> parameter_strings, Utf16View body_string, Utf16View code_string, JS::CompilationType compilation_type, ReadonlySpan<JS::Value> parameter_args, JS::Value body_arg) -> JS::ThrowCompletionOr<void> {
        // 1. Perform ? EnsureCSPDoesNotBlockStringCompilation(realm, parameterStrings, bodyString, codeString, compilationType, parameterArgs, bodyArg). [CSP]
        return ContentSecurityPolicy::ensure_csp_does_not_block_string_compilation(realm, parameter_strings, body_string, code_string, compilation_type, parameter_args, body_arg);
    };

    // 8.1.6.3 HostGetCodeForEval(argument), https://html.spec.whatwg.org/multipage/webappapis.html#hostgetcodeforeval(argument)
    main_thread_vm_ptr()->host_get_code_for_eval = [](JS::Object const& argument) -> GC::Ptr<JS::PrimitiveString> {
        // 1. If argument is a TrustedScript object, then return argument's data.
        if (auto const* trusted_script = as_if<TrustedTypes::TrustedScript>(argument); trusted_script)
            return JS::PrimitiveString::create(argument.vm(), trusted_script->to_string());

        // 2. Otherwise, return no-code.
        return {};
    };

    // 8.1.5.3 HostPromiseRejectionTracker(promise, operation), https://html.spec.whatwg.org/multipage/webappapis.html#the-hostpromiserejectiontracker-implementation
    main_thread_vm_ptr()->host_promise_rejection_tracker = [](JS::Promise& promise, JS::Promise::RejectionOperation operation) {
        auto& vm = *main_thread_vm_ptr();

        // 1. Let script be the running script.
        //    The running script is the script in the [[HostDefined]] field in the ScriptOrModule component of the running JavaScript execution context.
        HTML::Script* script { nullptr };
        vm.running_execution_context().script_or_module.visit(
            [&script](GC::Ref<JS::Script>& js_script) {
                script = as<HTML::ClassicScript>(js_script->host_defined());
            },
            [&script](GC::Ref<JS::Module>& js_module) {
                script = as<HTML::ModuleScript>(js_module->host_defined());
            },
            [](Empty) {
            });

        // 2. If script is a classic script and script's muted errors is true, then return.
        // NOTE: is<T>() returns false if nullptr is passed.
        if (is<HTML::ClassicScript>(script)) {
            auto const& classic_script = static_cast<HTML::ClassicScript const&>(*script);
            if (classic_script.muted_errors() == HTML::ClassicScript::MutedErrors::Yes)
                return;
        }

        // 3. Let settingsObject be the current settings object.
        // 4. If script is not null, then set settings object to script's settings object.
        auto& settings_object = script ? script->settings_object() : HTML::current_settings_object();

        // 5. Let global be settingsObject's global object.
        auto& global_mixin = as<HTML::UniversalGlobalScopeMixin>(settings_object.global_object());
        auto& global = global_mixin.this_impl();

        switch (operation) {
        // 6. If operation is "reject",
        case JS::Promise::RejectionOperation::Reject:
            // 1. Append promise to global's about-to-be-notified rejected promises list.
            global_mixin.push_onto_about_to_be_notified_rejected_promises_list(promise);
            break;
        // 7. If operation is "handle",
        case JS::Promise::RejectionOperation::Handle: {
            // 1. If global's about-to-be-notified rejected promises list contains promise, then remove promise from that list and return.
            bool removed_about_to_be_notified_rejected_promise = global_mixin.remove_from_about_to_be_notified_rejected_promises_list(promise);
            if (removed_about_to_be_notified_rejected_promise)
                return;

            // 3. Remove promise from global's outstanding rejected promises weak set.
            bool removed_outstanding_rejected_promise = global_mixin.remove_from_outstanding_rejected_promises_weak_set(&promise);

            // 2. If global's outstanding rejected promises weak set does not contain promise, then return.
            // NOTE: This is done out of order because removed_outstanding_rejected_promise will be false if the promise wasn't in the set or true if it was and got removed.
            if (!removed_outstanding_rejected_promise)
                return;

            // 4. Queue a global task on the DOM manipulation task source given global to fire an event named rejectionhandled at global, using PromiseRejectionEvent,
            //    with the promise attribute initialized to promise, and the reason attribute initialized to the value of promise's [[PromiseResult]] internal slot.
            HTML::queue_global_task(HTML::Task::Source::DOMManipulation, global, GC::create_function(main_thread_vm_ptr()->heap(), [&global, &promise] {
                // FIXME: This currently assumes that global is a WindowObject.
                auto& window = as<HTML::Window>(global);

                Bindings::PromiseRejectionEventInit event_init {
                    {}, // Initialize the inherited Bindings::EventInit
                    /* .promise = */ promise,
                    /* .reason = */ promise.result(),
                };
                auto promise_rejection_event = HTML::PromiseRejectionEvent::create(HTML::relevant_realm(global), HTML::EventNames::rejectionhandled, event_init);
                window.dispatch_event(promise_rejection_event);
            }));
            break;
        }
        default:
            VERIFY_NOT_REACHED();
        }
    };

    // 8.1.5.4.1 HostCallJobCallback(callback, V, argumentsList), https://html.spec.whatwg.org/multipage/webappapis.html#hostcalljobcallback
    main_thread_vm_ptr()->host_call_job_callback = [](JS::JobCallback& callback, JS::Value this_value, ReadonlySpan<JS::Value> arguments_list) {
        auto& callback_host_defined = as<WebEngineCustomJobCallbackData>(*callback.custom_data());

        // 1. Let incumbent settings be callback.[[HostDefined]].[[IncumbentSettings]].
        auto& incumbent_settings = callback_host_defined.incumbent_settings;

        // 2. Let script execution context be callback.[[HostDefined]].[[ActiveScriptContext]].
        auto* script_execution_context = callback_host_defined.active_script_context.ptr();

        // 3. Prepare to run a callback with incumbent settings.
        HTML::prepare_to_run_callback(incumbent_settings);

        // 4. If script execution context is not null, then push script execution context onto the JavaScript execution context stack.
        if (script_execution_context)
            main_thread_vm_ptr()->push_execution_context(*script_execution_context);

        // 5. Let result be Call(callback.[[Callback]], V, argumentsList).
        auto result = JS::call(*main_thread_vm_ptr(), callback.callback(), this_value, arguments_list);

        // 6. If script execution context is not null, then pop script execution context from the JavaScript execution context stack.
        if (script_execution_context) {
            VERIFY(&main_thread_vm_ptr()->running_execution_context() == script_execution_context);
            main_thread_vm_ptr()->pop_execution_context();
        }

        // 7. Clean up after running a callback with incumbent settings.
        HTML::clean_up_after_running_callback(incumbent_settings);

        // 8. Return result.
        return result;
    };

    // 8.1.5.4.2 HostEnqueueFinalizationRegistryCleanupJob(finalizationRegistry), https://html.spec.whatwg.org/multipage/webappapis.html#hostenqueuefinalizationregistrycleanupjob
    main_thread_vm_ptr()->host_enqueue_finalization_registry_cleanup_job = [](JS::FinalizationRegistry& finalization_registry) {
        // 1. Let global be finalizationRegistry.[[Realm]]'s global object.
        auto& global = finalization_registry.realm().global_object();

        // 2. Queue a global task on the JavaScript engine task source given global to perform the following steps:
        HTML::queue_global_task(HTML::Task::Source::JavaScriptEngine, global, GC::create_function(main_thread_vm_ptr()->heap(), [&finalization_registry] {
            // 1. Let entry be finalizationRegistry.[[CleanupCallback]].[[Callback]].[[Realm]]'s environment settings object.
            // AD-HOC: The spec assumes [[Callback]] has a [[Realm]] internal slot, but Proxy and BoundFunction
            //         exotic objects do not. Use GetFunctionRealm to unwrap these exotic objects, falling back to
            //         the FinalizationRegistry's own realm on failure.
            // Spec issue: https://github.com/whatwg/html/issues/12326
            HTML::TemporaryExecutionContext context { finalization_registry.realm() };
            auto& callback = finalization_registry.cleanup_callback().callback();
            auto callback_realm = JS::get_function_realm(callback.vm(), callback);
            auto& realm = callback_realm.is_throw_completion() ? finalization_registry.realm() : *callback_realm.value();
            auto& entry = HTML::principal_realm_settings_object(realm);

            // 2. Prepare to run script with entry.
            HTML::prepare_to_run_script(entry);

            // 3. Let result be the result of performing CleanupFinalizationRegistry(finalizationRegistry).
            auto result = finalization_registry.cleanup();

            // 4. Clean up after running script with entry.
            HTML::clean_up_after_running_script(entry);

            // 5. If result is an abrupt completion, then report the exception given by result.[[Value]] for global.
            if (result.is_error())
                HTML::report_exception(result, finalization_registry.realm());
        }));
    };

    // 8.1.5.4.3 HostEnqueuePromiseJob(job, realm), https://html.spec.whatwg.org/multipage/webappapis.html#hostenqueuepromisejob
    main_thread_vm_ptr()->host_enqueue_promise_job = [](GC::Ref<GC::Function<JS::ThrowCompletionOr<JS::Value>()>> job, JS::Realm* realm) {
        auto& vm = *main_thread_vm_ptr();

        // IMPLEMENTATION DEFINED: The JS spec says we must take implementation defined steps to make the currently active script or module at the time of HostEnqueuePromiseJob being invoked
        //                         also be the active script or module of the job at the time of its invocation.
        //                         This means taking it here now and passing it through to the lambda.
        auto script_or_module = vm.get_active_script_or_module();

        // 1. If realm is not null, then let job settings be the settings object for realm. Otherwise, let job settings be null.
        GC::Ptr<HTML::EnvironmentSettingsObject> job_settings;
        if (realm)
            job_settings = &HTML::principal_realm_settings_object(*realm);

        // 2. Queue a microtask to perform the following steps:
        // This instance of "queue a microtask" uses the "implied document". The best fit for "implied document" here is "If the task is being queued by or for a script, then return the script's settings object's responsible document."
        // Do note that "implied document" from the spec is handwavy and the spec authors are trying to get rid of it: https://github.com/whatwg/html/issues/4980
        auto* script = active_script();

        auto& heap = realm ? realm->heap() : vm.heap();
        HTML::queue_a_microtask(script ? script->settings_object().responsible_document().ptr() : nullptr, GC::create_function(heap, [&vm, job_settings, job = move(job), script_or_module = move(script_or_module)] {
            // The dummy execution context has to be kept up here to keep it alive for the duration of the function.
            OwnPtr<JS::ExecutionContext> dummy_execution_context;

            if (job_settings) {
                // 1. If job settings is not null, then prepare to run script with job settings.
                HTML::prepare_to_run_script(*job_settings);

                // IMPLEMENTATION DEFINED: Additionally to preparing to run a script, we also prepare to run a callback here. This matches WebIDL's
                //                         invoke_callback() / call_user_object_operation() functions, and prevents a crash in host_make_job_callback()
                //                         when getting the incumbent settings object.
                HTML::prepare_to_run_callback(*job_settings);

                // IMPLEMENTATION DEFINED: Per the previous "implementation defined" comment, we must now make the script or module the active script or module.
                //                         Since the only active execution context currently is the realm execution context of job settings, lets attach it here.
                job_settings->realm_execution_context().script_or_module = script_or_module;
            } else {
                // FIXME: We need to setup a dummy execution context in case a JS::NativeFunction is called when processing the job.
                //        This is because JS::NativeFunction::call excepts something to be on the execution context stack to be able to get the caller context to initialize the environment.
                //        Do note that the JS spec gives _no_ guarantee that the execution context stack has something on it if HostEnqueuePromiseJob was called with a null realm: https://tc39.es/ecma262/#job-preparedtoevaluatecode
                dummy_execution_context = JS::ExecutionContext::create(0, ReadonlySpan<JS::Value> {}, 0);
                dummy_execution_context->script_or_module = script_or_module;
                vm.push_execution_context(*dummy_execution_context);
            }

            // 2. Let result be job().
            auto result = job->function()();

            // 3. If job settings is not null, then clean up after running script with job settings.
            if (job_settings) {
                // IMPLEMENTATION DEFINED: Disassociate the realm execution context from the script or module.
                job_settings->realm_execution_context().script_or_module = Empty {};

                // IMPLEMENTATION DEFINED: See comment above, we need to clean up the non-standard prepare_to_run_callback() call.
                HTML::clean_up_after_running_callback(*job_settings);

                HTML::clean_up_after_running_script(*job_settings);
            } else {
                // Pop off the dummy execution context. See the above FIXME block about why this is done.
                vm.pop_execution_context();
            }

            // 4. If result is an abrupt completion, then report the exception given by result.[[Value]].
            if (result.is_error())
                HTML::report_exception(result, job_settings->realm());
        }));
    };

    main_thread_vm_ptr()->host_promise_job_queue_is_empty = []() -> bool {
        return HTML::main_thread_event_loop().microtask_queue_empty();
    };

    // 8.1.5.4.4 HostMakeJobCallback(callable), https://html.spec.whatwg.org/multipage/webappapis.html#hostmakejobcallback
    main_thread_vm_ptr()->host_make_job_callback = [](JS::FunctionObject& callable) -> GC::Ref<JS::JobCallback> {
        // 1. Let incumbent settings be the incumbent settings object.
        auto& incumbent_settings = HTML::incumbent_settings_object();

        // 2. Let active script be the active script.
        auto* script = active_script();

        // 3. Let script execution context be null.
        OwnPtr<JS::ExecutionContext> script_execution_context;

        // 4. If active script is not null, set script execution context to a new JavaScript execution context, with its Function field set to null,
        //    its Realm field set to active script's settings object's realm, and its ScriptOrModule set to active script's record.
        if (script) {
            script_execution_context = JS::ExecutionContext::create(0, ReadonlySpan<JS::Value> {}, 0);
            script_execution_context->function = nullptr;
            script_execution_context->realm = &script->settings_object().realm();
            if (is<HTML::ClassicScript>(script)) {
                script_execution_context->script_or_module = GC::Ref<JS::Script>(*as<HTML::ClassicScript>(script)->script_record());
            } else if (is<HTML::ModuleScript>(script)) {
                script_execution_context->script_or_module = as<HTML::ModuleScript>(script)->record().visit(
                    [](Empty) -> JS::ScriptOrModule { return {}; },
                    [](auto& module) -> JS::ScriptOrModule { return GC::Ref<JS::Module> { module }; });
            } else {
                VERIFY_NOT_REACHED();
            }
        }

        // 5. Return the JobCallback Record { [[Callback]]: callable, [[HostDefined]]: { [[IncumbentSettings]]: incumbent settings, [[ActiveScriptContext]]: script execution context } }.
        auto host_defined = adopt_own(*new WebEngineCustomJobCallbackData(incumbent_settings, move(script_execution_context)));
        return JS::JobCallback::create(*main_thread_vm_ptr(), callable, move(host_defined));
    };

    // 8.1.6.7.1 HostGetImportMetaProperties(moduleRecord), https://html.spec.whatwg.org/multipage/webappapis.html#hostgetimportmetaproperties
    main_thread_vm_ptr()->host_get_import_meta_properties = [](JS::SourceTextModule& module_record) {
        auto& realm = module_record.realm();
        auto& vm = realm.vm();

        // 1. Let moduleScript be moduleRecord.[[HostDefined]].
        auto& module_script = *as<HTML::Script>(module_record.host_defined());

        // 2. Assert: moduleScript's base URL is not null, as moduleScript is a JavaScript module script.
        VERIFY(module_script.base_url().has_value());

        // 3. Let urlString be moduleScript's base URL, serialized.
        auto url_string = module_script.base_url()->serialize();

        // 4. Let steps be the following steps, given the argument specifier:
        auto steps = [module_script = GC::Ref { module_script }](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
            auto specifier = vm.argument(0);

            // 1. Set specifier to ? ToString(specifier).
            auto specifier_string = TRY(specifier.to_utf16_string(vm)).to_utf8_but_should_be_ported_to_utf16();

            // 2. Let url be the result of resolving a module specifier given moduleScript and specifier.
            auto url = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] {
                return HTML::resolve_module_specifier(*module_script, specifier_string);
            }));

            // 3. Return the serialization of url.
            return JS::PrimitiveString::create(vm, Utf16String::from_utf8(url.serialize()));
        };

        // 4. Let resolveFunction be ! CreateBuiltinFunction(steps, 1, "resolve", « »).
        auto resolve_function = JS::NativeFunction::create(realm, move(steps), 1, vm.names.resolve);

        // 5. Return « Record { [[Key]]: "url", [[Value]]: urlString }, Record { [[Key]]: "resolve", [[Value]]: resolveFunction } ».
        HashMap<JS::PropertyKey, JS::Value> meta;
        meta.set("url"_utf16_fly_string, JS::PrimitiveString::create(vm, Utf16String::from_utf8(url_string)));
        meta.set("resolve"_utf16_fly_string, resolve_function);

        return meta;
    };

    // 8.1.6.7.2 HostGetSupportedImportAttributes(), https://html.spec.whatwg.org/multipage/webappapis.html#hostgetsupportedimportassertions
    main_thread_vm_ptr()->host_get_supported_import_attributes = []() -> Vector<Utf16String> {
        // 1. Return « "type" ».
        return { "type"_utf16 };
    };

    // 8.1.6.7.3 HostLoadImportedModule(referrer, moduleRequest, loadState, payload), https://html.spec.whatwg.org/multipage/webappapis.html#hostloadimportedmodule
    main_thread_vm_ptr()->host_load_imported_module = [](JS::ImportedModuleReferrer referrer, JS::ModuleRequest const& module_request, GC::Ptr<JS::GraphLoadingState::HostDefined> load_state, JS::ImportedModulePayload payload) -> void {
        auto& vm = *main_thread_vm_ptr();

        // 1. Let settingsObject be the current settings object.
        GC::Ref<HTML::EnvironmentSettingsObject> settings_object = HTML::current_settings_object();

        // 2. If settingsObject's global object implements WorkletGlobalScope or ServiceWorkerGlobalScope and loadState is undefined, then:
        if ((is<HTML::WorkletGlobalScope>(settings_object->global_object()) || is<ServiceWorker::ServiceWorkerGlobalScope>(settings_object->global_object())) && !load_state) {
            // 1. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, ThrowCompletion(a new TypeError)).
            auto completion = JS::throw_completion(JS::TypeError::create(settings_object->realm(), "Dynamic Import not available for Worklets or ServiceWorkers"_utf16));
            JS::finish_loading_imported_module(referrer, module_request, payload, completion);

            // 2. Return.
            return;
        }

        // 3. Let referencingScript be null.
        Optional<HTML::Script&> referencing_script;

        // 4. Let originalFetchOptions be the default script fetch options.
        auto original_fetch_options = HTML::default_script_fetch_options();

        // 5. Let fetchReferrer be "client".
        Fetch::Infrastructure::Request::ReferrerType fetch_referrer = Fetch::Infrastructure::Request::Referrer::Client;

        // 6. If referrer is a Script Record or a Cyclic Module Record, then:
        if (referrer.has<GC::Ref<JS::Script>>() || referrer.has<GC::Ref<JS::CyclicModule>>()) {
            // 1. Set referencingScript to referrer.[[HostDefined]].
            referencing_script = as<HTML::Script>(referrer.has<GC::Ref<JS::Script>>() ? *referrer.get<GC::Ref<JS::Script>>()->host_defined() : *referrer.get<GC::Ref<JS::CyclicModule>>()->host_defined());

            // 2. Set settingsObject to referencingScript's settings object.
            settings_object = referencing_script->settings_object();

            // 3. Set fetchReferrer to referencingScript's base URL.
            fetch_referrer = referencing_script->base_url().value();

            // FIXME: 4. Set originalFetchOptions to referencingScript's fetch options.
        }

        // 7. If referrer is a Cyclic Module Record and moduleRequest is equal to the first element of referrer.[[RequestedModules]], then:
        if (referrer.has<GC::Ref<JS::CyclicModule>>()) {
            if (auto const& requested_modules = referrer.get<GC::Ref<JS::CyclicModule>>()->requested_modules(); !requested_modules.is_empty() && module_request == requested_modules.first()) {
                // 1. For each ModuleRequest record requested of referrer.[[RequestedModules]]:
                for (auto const& module_request : referrer.get<GC::Ref<JS::CyclicModule>>()->requested_modules()) {
                    // 1. If moduleRequest.[[Attributes]] contains a Record entry such that entry.[[Key]] is not "type", then:
                    for (auto const& attribute : module_request.attributes) {
                        if (attribute.key == "type"sv)
                            continue;

                        // 1. Let error be a new SyntaxError exception.
                        auto error = JS::SyntaxError::create(settings_object->realm(), "Module request attributes must only contain a type attribute"_utf16);

                        // 2. If loadState is not undefined and loadState.[[ErrorToRethrow]] is null, set loadState.[[ErrorToRethrow]] to error.
                        if (auto* load_state_as_fetch_context = as<HTML::FetchContext>(load_state.ptr());
                            load_state_as_fetch_context && load_state_as_fetch_context->error_to_rethrow.is_null()) {
                            load_state_as_fetch_context->error_to_rethrow = error;
                        }

                        // 3. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, ThrowCompletion(error)).
                        JS::finish_loading_imported_module(referrer, module_request, payload, JS::throw_completion(error));

                        // 4. Return.
                        return;
                    }
                }

                // 2. Resolve a module specifier given referencingScript and moduleRequest.[[Specifier]], catching any
                //    exceptions. If they throw an exception, let resolutionError be the thrown exception.
                auto maybe_exception = HTML::resolve_module_specifier(referencing_script, module_request.module_specifier.view().to_utf8_but_should_be_ported_to_utf16());

                // 3. If the previous step threw an exception, then:
                if (maybe_exception.is_exception()) {
                    // 1. If loadState is not undefined and loadState.[[ErrorToRethrow]] is null, set loadState.[[ErrorToRethrow]] to resolutionError.
                    if (auto* load_state_as_fetch_context = as<HTML::FetchContext>(load_state.ptr());
                        load_state_as_fetch_context && load_state_as_fetch_context->error_to_rethrow.is_null()) {
                        load_state_as_fetch_context->error_to_rethrow = exception_to_throw_completion(vm, maybe_exception.exception()).release_value();
                    }

                    // 2. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, ThrowCompletion(resolutionError)).
                    auto completion = exception_to_throw_completion(main_thread_vm(), maybe_exception.exception());
                    JS::finish_loading_imported_module(referrer, module_request, payload, completion);

                    // 3. Return.
                    return;
                }

                // 4. Let moduleType be the result of running the module type from module request steps given moduleRequest.
                auto module_type = HTML::module_type_from_module_request(module_request);

                // 5. If the result of running the module type allowed steps given moduleType and settingsObject is false, then:
                if (!HTML::module_type_allowed(settings_object, module_type)) {
                    // 1. Let error be a new TypeError exception.
                    auto error = JS::TypeError::create(settings_object->realm(), Utf16String::formatted("Module type '{}' is not supported", module_type));

                    // 2. If loadState is not undefined and loadState.[[ErrorToRethrow]] is null, set loadState.[[ErrorToRethrow]] to error.
                    if (auto* load_state_as_fetch_context = as<HTML::FetchContext>(load_state.ptr());
                        load_state_as_fetch_context && load_state_as_fetch_context->error_to_rethrow.is_null()) {
                        load_state_as_fetch_context->error_to_rethrow = error;
                    }

                    // 3. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, ThrowCompletion(error)).
                    JS::finish_loading_imported_module(referrer, module_request, payload, JS::throw_completion(error));

                    // 4. Return
                    return;
                }

                // NOTE: This step is essentially validating all of the requested module specifiers and type attributes
                //       when the first call to HostLoadImportedModule for a static module dependency list is made, to
                //       avoid further loading operations in the case any one of the dependencies has a static error. We
                //       treat a module with unresolvable module specifiers or unsupported type attributes the same as
                //       one that cannot be parsed; in both cases, a syntactic issue makes it impossible to ever
                //       contemplate linking the module later.
            }
        }

        // 8. Let url be the result of resolving a module specifier given referencingScript and moduleRequest.[[Specifier]],
        //    catching any exceptions. If they throw an exception, let resolutionError be the thrown exception.
        auto url = HTML::resolve_module_specifier(referencing_script, module_request.module_specifier.view().to_utf8_but_should_be_ported_to_utf16());

        // 9. If the previous step threw an exception, then:
        if (url.is_exception()) {
            // 1. If loadState is not undefined and loadState.[[ErrorToRethrow]] is null, set loadState.[[ErrorToRethrow]] to resolutionError.
            if (auto* load_state_as_fetch_context = as<HTML::FetchContext>(load_state.ptr());
                load_state_as_fetch_context && load_state_as_fetch_context->error_to_rethrow.is_null()) {
                load_state_as_fetch_context->error_to_rethrow = exception_to_throw_completion(vm, url.exception()).release_value();
            }

            // 2. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, ThrowCompletion(resolutionError)).
            auto completion = exception_to_throw_completion(main_thread_vm(), url.exception());
            HTML::TemporaryExecutionContext context { settings_object->realm() };
            JS::finish_loading_imported_module(referrer, module_request, payload, completion);

            // 3. Return.
            return;
        }

        // 10. Let fetchOptions be the result of getting the descendant script fetch options given originalFetchOptions, url, and settingsObject.
        auto fetch_options = HTML::get_descendant_script_fetch_options(original_fetch_options, url.value(), settings_object);

        // 11. Let destination be "script".
        auto destination = Fetch::Infrastructure::Request::Destination::Script;

        // 12. Let fetchClient be settingsObject.
        GC::Ref fetch_client { settings_object };

        // 15. If loadState is not undefined, then:
        HTML::PerformTheFetchHook perform_fetch;
        if (load_state) {
            auto& fetch_context = static_cast<HTML::FetchContext&>(*load_state);

            // 1. Set destination to loadState.[[Destination]].
            destination = fetch_context.destination;

            // 2. Set fetchClient to loadState.[[FetchClient]].
            fetch_client = fetch_context.fetch_client;

            // For step 13
            perform_fetch = fetch_context.perform_fetch;
        }

        auto on_single_fetch_complete = HTML::create_on_fetch_script_complete(settings_object->heap(), [referrer, settings_object, load_state, module_request, payload](GC::Ptr<HTML::Script> const& module_script) -> void {
            auto& realm = settings_object->realm();
            // onSingleFetchComplete given moduleScript is the following algorithm:
            // 1. Let completion be null.
            // NOTE: Our JS::Completion does not support non JS::Value types for its [[Value]], a such we
            //       use JS::ThrowCompletionOr here.

            auto& vm = realm.vm();
            GC::Ptr<JS::Module> module = nullptr;

            auto completion = [&]() -> JS::ThrowCompletionOr<GC::Ref<JS::Module>> {
                // 2. If moduleScript is null, then set completion to ThrowCompletion(a new TypeError).
                if (!module_script) {
                    return JS::throw_completion(JS::TypeError::create(realm, Utf16String::formatted("Loading imported module '{}' failed.", module_request.module_specifier)));
                }
                // 3. Otherwise, if moduleScript's parse error is not null, then:
                else if (!module_script->parse_error().is_null()) {
                    // 1. Let parseError be moduleScript's parse error.
                    auto parse_error = module_script->parse_error();

                    // 2. Set completion to ThrowCompletion(parseError).
                    auto completion = JS::throw_completion(parse_error);

                    // 3. If loadState is not undefined and loadState.[[ErrorToRethrow]] is null, set loadState.[[ErrorToRethrow]] to parseError.
                    if (auto* load_state_as_fetch_context = as<HTML::FetchContext>(load_state.ptr());
                        load_state_as_fetch_context && load_state_as_fetch_context->error_to_rethrow.is_null()) {
                        load_state_as_fetch_context->error_to_rethrow = parse_error;
                    }

                    return completion;
                }
                // 4. Otherwise, set completion to NormalCompletion(moduleScript's record).
                else {
                    module = static_cast<HTML::ModuleScript&>(*module_script).record().visit([](Empty) -> GC::Ptr<JS::Module> { return nullptr; }, [](GC::Ref<JS::Module> module) -> GC::Ptr<JS::Module> { return module; });
                    return JS::ThrowCompletionOr<GC::Ref<JS::Module>>(*module);
                }
            }();

            // 5. Perform FinishLoadingImportedModule(referrer, moduleRequest, payload, completion).
            // NON-STANDARD: To ensure that LibJS can find the module on the stack, we push a new execution context.

            auto& stack = vm.interpreter_stack();
            auto* stack_mark = stack.top();
            auto* module_execution_context = stack.allocate(0, ReadonlySpan<JS::Value> {}, 0);
            VERIFY(module_execution_context);
            module_execution_context->realm = realm;
            if (module)
                module_execution_context->script_or_module = GC::Ref { *module };
            vm.push_execution_context(*module_execution_context);

            JS::finish_loading_imported_module(referrer, module_request, payload, completion);

            vm.pop_execution_context();
            stack.deallocate(stack_mark);
        });

        // 16. Fetch a single imported module script given url, fetchClient, destination, fetchOptions, settingsObject, fetchReferrer,
        //     moduleRequest, and onSingleFetchComplete as defined below.
        //     If loadState is not undefined and loadState.[[PerformFetch]] is not null, pass loadState.[[PerformFetch]] along as well.
        HTML::fetch_single_imported_module_script(settings_object->realm(), url.release_value(), *fetch_client, destination, fetch_options, settings_object, fetch_referrer, module_request, perform_fetch, on_single_fetch_complete);
    };

    main_thread_vm_ptr()->host_unrecognized_date_string = [](Utf16View date) {
        dbgln("Unable to parse date string: \"{}\"", date);
    };

    main_thread_vm_ptr()->host_resize_array_buffer = [default_host_resize_array_buffer = move(main_thread_vm_ptr()->host_resize_array_buffer)](JS::ArrayBuffer& buffer, size_t new_byte_length) -> JS::ThrowCompletionOr<JS::HandledByHost> {
        auto wasm_handled = TRY(WebAssembly::Detail::host_resize_array_buffer(*main_thread_vm_ptr(), buffer, new_byte_length));
        if (wasm_handled == JS::HandledByHost::Handled)
            return JS::HandledByHost::Handled;

        return default_host_resize_array_buffer(buffer, new_byte_length);
    };

    main_thread_vm_ptr()->host_grow_shared_array_buffer = [default_host_grow_shared_array_buffer = move(main_thread_vm_ptr()->host_grow_shared_array_buffer)](JS::ArrayBuffer& buffer, size_t new_byte_length) -> JS::ThrowCompletionOr<JS::HandledByHost> {
        auto wasm_handled = TRY(WebAssembly::Detail::host_grow_shared_array_buffer(*main_thread_vm_ptr(), buffer, new_byte_length));
        if (wasm_handled == JS::HandledByHost::Handled)
            return JS::HandledByHost::Handled;

        return default_host_grow_shared_array_buffer(buffer, new_byte_length);
    };
}

JS::VM& main_thread_vm()
{
    VERIFY(main_thread_vm_ptr());
    return *main_thread_vm_ptr();
}

// https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask
void queue_mutation_observer_microtask()
{
    auto& vm = main_thread_vm();
    auto& surrounding_agent = as<HTML::SimilarOriginWindowAgent>(*vm.agent());

    // 1. If the surrounding agent’s mutation observer microtask queued is true, then return.
    if (surrounding_agent.mutation_observer_microtask_queued)
        return;

    // 2. Set the surrounding agent’s mutation observer microtask queued to true.
    surrounding_agent.mutation_observer_microtask_queued = true;

    // 3. Queue a microtask to notify mutation observers.
    HTML::queue_a_microtask(nullptr, GC::create_function(vm.heap(), [&surrounding_agent]() {
        // https://dom.spec.whatwg.org/#notify-mutation-observers
        // 1. Set the surrounding agent’s mutation observer microtask queued to false.
        surrounding_agent.mutation_observer_microtask_queued = false;

        // 2. Let notifySet be a clone of the surrounding agent’s pending mutation observers.
        // 3. Empty the surrounding agent’s pending mutation observers.
        auto notify_set = move(surrounding_agent.pending_mutation_observers);

        // 4. Let signalSet be a clone of the surrounding agent’s signal slots.
        // 5. Empty the surrounding agent’s signal slots.
        auto signal_set = move(surrounding_agent.signal_slots);

        // 6. For each mo of notifySet:
        for (auto& mutation_observer : notify_set) {
            // 1. Let records be a clone of mo’s record queue.
            // 2. Empty mo’s record queue.
            auto records = mutation_observer->take_records();

            // 3. For each node of mo’s node list, remove all transient registered observers whose observer is mo from
            //    node’s registered observer list.
            for (auto& node : mutation_observer->node_list()) {
                // FIXME: Is this correct?
                if (!node)
                    continue;

                if (node->registered_observer_list()) {
                    node->registered_observer_list()->remove_all_matching([&mutation_observer](DOM::RegisteredObserver& registered_observer) {
                        return is<DOM::TransientRegisteredObserver>(registered_observer) && static_cast<DOM::TransientRegisteredObserver&>(registered_observer).observer().ptr() == mutation_observer;
                    });
                }
            }

            // 4. If records is not empty, then invoke mo’s callback with « records, mo » and "report", and with
            //    callback this value mo.
            if (!records.is_empty()) {
                auto& callback = mutation_observer->callback();
                auto& settings = callback.callback_context;

                auto wrapped_records = MUST(JS::Array::create(settings->realm(), 0));
                for (size_t i = 0; i < records.size(); ++i) {
                    auto& record = records.at(i);
                    auto property_index = JS::PropertyKey { i };
                    MUST(wrapped_records->create_data_property(property_index, record.ptr()));
                }

                (void)WebIDL::invoke_callback(callback, mutation_observer, WebIDL::ExceptionBehavior::Report, { { wrapped_records, mutation_observer } });
            }
        }

        // 7. For each slot of signalSet, fire an event named slotchange, with its bubbles attribute set to true, at slot.
        for (auto& slot : signal_set) {
            Bindings::EventInit event_init;
            event_init.bubbles = true;
            slot->dispatch_event(DOM::Event::create(slot->realm(), HTML::EventNames::slotchange, event_init));
        }
    }));
}

// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-new-javascript-realm
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM& vm, Function<JS::Object*(JS::Realm&)> create_global_object, Function<JS::Object*(JS::Realm&)> create_global_this_value)
{
    // 1. Perform InitializeHostDefinedRealm() with the provided customizations for creating the global object and the global this binding.
    // 2. Let realm execution context be the running JavaScript execution context.
    auto realm_execution_context = MUST(JS::Realm::initialize_host_defined_realm(vm, move(create_global_object), move(create_global_this_value)));

    // 3. Remove realm execution context from the JavaScript execution context stack.
    auto* popped_execution_context = vm.pop_execution_context();
    VERIFY(popped_execution_context == realm_execution_context.ptr());

    // NO-OP: 4. Let realm be realm execution context's Realm component.
    // NO-OP: 5. Set realm's agent to agent.

    // FIXME: 6. If agent's agent cluster's cross-origin isolation mode is "none", then:
    //          1. Let global be realm's global object.
    //          2. Let status be ! global.[[Delete]]("SharedArrayBuffer").
    //          3. Assert: status is true.

    // 7. Return realm execution context.
    return realm_execution_context;
}

// https://html.spec.whatwg.org/multipage/custom-elements.html#invoke-custom-element-reactions
void invoke_custom_element_reactions(Vector<GC::Weak<DOM::Element>>& element_queue)
{
    // 1. While queue is not empty:
    while (!element_queue.is_empty()) {
        // 1. Let element be the result of dequeuing from queue.
        auto element = element_queue.take_first();
        if (!element)
            continue;

        // 2. Let reactions be element's custom element reaction queue.
        auto* reactions = element->custom_element_reaction_queue();

        // 3. Repeat until reactions is empty:
        if (!reactions)
            continue;
        while (!reactions->is_empty()) {
            // 1. Remove the first element of reactions, and let reaction be that element. Switch on reaction's type:
            auto reaction = reactions->take_first();

            reaction.visit(
                [&](DOM::CustomElementUpgradeReaction const& custom_element_upgrade_reaction) -> void {
                    // -> upgrade reaction
                    //      Upgrade element using reaction's custom element definition.
                    auto maybe_exception = element->upgrade_element(*custom_element_upgrade_reaction.custom_element_definition);
                    // If this throws an exception, catch it, and report it for reaction's custom element definition's constructor's corresponding JavaScript object's associated realm's global object.
                    if (maybe_exception.is_error()) {
                        // FIXME: Should it be easier to get to report an exception from an IDL callback?
                        auto& callback = custom_element_upgrade_reaction.custom_element_definition->constructor();
                        auto& realm = callback.callback->shape().realm();
                        auto& global = realm.global_object();

                        auto& window_or_worker = as<HTML::WindowOrWorkerGlobalScopeMixin>(global);
                        window_or_worker.report_an_exception(maybe_exception.error_value());
                    }
                },
                [&](DOM::CustomElementCallbackReaction& custom_element_callback_reaction) -> void {
                    // -> callback reaction
                    //      Invoke reaction's callback function with reaction's arguments and "report", and callback this value set to element.
                    (void)WebIDL::invoke_callback(*custom_element_callback_reaction.callback, element.ptr(), WebIDL::ExceptionBehavior::Report, custom_element_callback_reaction.arguments);
                });
        }
    }
}

}
