Skip to content

Truly independent web browser

1.7 KB
Raw Blame
/*
 * Copyright (c) 2025, Shannon Booth <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>

namespace Web::HTML {

Bindings::WrapperWorld& Agent::main_world()
{
    if (!m_main_world)
        m_main_world = GC::Heap::the().allocate<Bindings::WrapperWorld>(Bindings::WrapperWorld::Type::Main);
    return *m_main_world;
}

Bindings::WrapperWorld const& Agent::main_world() const
{
    return const_cast<Agent&>(*this).main_world();
}

void Agent::spin_event_loop_until(GC::Root<GC::Function<bool()>> goal_condition)
{
    event_loop->spin_until(*goal_condition);
}

NonnullRefPtr<ScriptAbortHandle> Agent::script_abort_handle()
{
    if (!m_script_abort_handle)
        m_script_abort_handle = ScriptAbortHandle::create(JS::VM::the());
    return *m_script_abort_handle;
}

void Agent::detach_script_abort_handle()
{
    if (m_script_abort_handle)
        m_script_abort_handle->detach();
}

Agent& surrounding_agent()
{
    return *static_cast<Agent*>(JS::VM::the().agent());
}

// https://html.spec.whatwg.org/multipage/webappapis.html#relevant-agent
Agent& relevant_agent(JS::Object const& object)
{
    // The relevant agent for a platform object platformObject is platformObject's relevant Realm's agent.
    // Spec Note: This pointer is not yet defined in the JavaScript specification; see tc39/ecma262#1357.
    return *static_cast<Agent*>(relevant_realm(object).vm().agent());
}

Agent& relevant_agent(DOM::Node const& node)
{
    return *static_cast<Agent*>(relevant_realm(node).vm().agent());
}

}