LibJS+LibWeb: Defer initialization of the Agent after VM constructor

This helps unwind a niggly depedency where the VM owns and constructs
the Heap and the Agent. But the agent wants to have customized
construction that depends on the heap. Solve this by defering
the initialization of the Agent to after we have constructed the
VM and the heap.
This commit is contained in:
Shannon Booth 2025-04-24 15:58:00 +12:00 committed by Andreas Kling
commit 7dd7e5b438
Notes: github-actions[bot] 2025-04-25 14:45:40 +00:00
3 changed files with 8 additions and 7 deletions

View file

@ -37,12 +37,12 @@
namespace JS {
NonnullRefPtr<VM> VM::create(OwnPtr<Agent> agent)
NonnullRefPtr<VM> VM::create()
{
ErrorMessages error_messages {};
error_messages[to_underlying(ErrorMessage::OutOfMemory)] = ErrorType::OutOfMemory.message();
auto vm = adopt_ref(*new VM(move(agent), move(error_messages)));
auto vm = adopt_ref(*new VM(move(error_messages)));
WellKnownSymbols well_known_symbols {
#define __JS_ENUMERATE(SymbolName, snake_name) \
@ -63,12 +63,11 @@ static constexpr auto make_single_ascii_character_strings(IndexSequence<code_poi
static constexpr auto single_ascii_character_strings = make_single_ascii_character_strings(MakeIndexSequence<128>());
VM::VM(OwnPtr<Agent> agent, ErrorMessages error_messages)
VM::VM(ErrorMessages error_messages)
: m_heap(this, [this](HashMap<GC::Cell*, GC::HeapRoot>& roots) {
gather_roots(roots);
})
, m_error_messages(move(error_messages))
, m_agent(move(agent))
{
m_bytecode_interpreter = make<Bytecode::Interpreter>(*this);

View file

@ -47,7 +47,7 @@ enum class EvalMode {
class VM : public RefCounted<VM> {
public:
static NonnullRefPtr<VM> create(OwnPtr<Agent> = {});
static NonnullRefPtr<VM> create();
~VM();
GC::Heap& heap() { return m_heap; }
@ -240,6 +240,7 @@ public:
Function<void(Promise&)> on_promise_rejection_handled;
Function<void(Object const&, PropertyKey const&)> on_unimplemented_property_access;
void set_agent(OwnPtr<Agent> agent) { m_agent = move(agent); }
Agent* agent() { return m_agent; }
Agent const* agent() const { return m_agent; }
@ -290,7 +291,7 @@ private:
#undef __JS_ENUMERATE
};
VM(OwnPtr<Agent>, ErrorMessages);
explicit VM(ErrorMessages);
void load_imported_module(ImportedModuleReferrer, ModuleRequest const&, GC::Ptr<GraphLoadingState::HostDefined>, ImportedModulePayload);
ThrowCompletionOr<void> link_and_eval_module(CyclicModule&);

View file

@ -76,7 +76,8 @@ void initialize_main_thread_vm(HTML::EventLoop::Type type)
{
VERIFY(!s_main_thread_vm);
s_main_thread_vm = JS::VM::create(make<HTML::SimilarOriginWindowAgent>());
s_main_thread_vm = JS::VM::create();
s_main_thread_vm->set_agent(make<HTML::SimilarOriginWindowAgent>());
auto& agent = as<HTML::Agent>(*s_main_thread_vm->agent());
agent.event_loop = s_main_thread_vm->heap().allocate<HTML::EventLoop>(type);