1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 08:25:07 +00:00
serenity/Userland/Libraries/LibJS/Runtime/Realm.h
Andreas Kling 8a03b17007 LibJS: Implement a more general InitializeHostDefinedRealm AO
The existing implementation of this AO lives in Interpreter::create(),
which makes it impossible to use without also constructing an
Interpreter.

This patch adds a new Realm::initialize_host_defined_realm() and takes
the global object and global this customization steps as Function
callback objects. This will be used by LibWeb to create realms during
Document construction.
2022-08-05 12:46:38 +02:00

48 lines
1.5 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/OwnPtr.h>
#include <AK/StringView.h>
#include <AK/Weakable.h>
#include <LibJS/Heap/Cell.h>
namespace JS {
// 9.3 Realms, https://tc39.es/ecma262/#realm-record
class Realm final
: public Cell
, public Weakable<Realm> {
public:
struct HostDefined {
virtual ~HostDefined() = default;
};
Realm() = default;
static Realm* create(VM&);
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Value(Realm&)> create_global_object, Function<Value(Realm&)> create_global_this_value);
void set_global_object(Value global_object, Value this_value);
[[nodiscard]] GlobalObject& global_object() const { return *m_global_object; }
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
HostDefined* host_defined() { return m_host_defined; }
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
private:
virtual StringView class_name() const override { return "Realm"sv; }
virtual void visit_edges(Visitor&) override;
GlobalObject* m_global_object { nullptr }; // [[GlobalObject]]
GlobalEnvironment* m_global_environment { nullptr }; // [[GlobalEnv]]
OwnPtr<HostDefined> m_host_defined; // [[HostDefined]]
};
}