2021-09-11 15:20:07 -03:00
|
|
|
/*
|
2022-08-26 20:54:55 -03:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2021-09-11 15:20:07 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-08-26 20:54:55 -03:00
|
|
|
#include <AK/Badge.h>
|
2022-02-07 14:51:58 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2022-03-16 21:26:49 -03:00
|
|
|
#include <AK/StringView.h>
|
2021-10-14 08:04:10 -03:00
|
|
|
#include <AK/Weakable.h>
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/CellAllocator.h>
|
|
|
|
|
#include <LibGC/Heap.h>
|
2023-11-17 07:48:30 -03:00
|
|
|
#include <LibJS/Bytecode/Builtins.h>
|
2025-07-19 17:49:30 -03:00
|
|
|
#include <LibJS/Export.h>
|
2021-09-11 15:20:07 -03:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2022-08-26 20:54:55 -03:00
|
|
|
#include <LibJS/Runtime/Intrinsics.h>
|
2023-11-17 07:48:30 -03:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2021-09-11 15:20:07 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
// 9.3 Realms, https://tc39.es/ecma262/#realm-record
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API Realm final : public Cell {
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_CELL(Realm, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(Realm);
|
2022-08-28 17:11:20 -03:00
|
|
|
|
2021-09-11 15:20:07 -03:00
|
|
|
public:
|
2022-02-07 12:55:35 -03:00
|
|
|
struct HostDefined {
|
|
|
|
|
virtual ~HostDefined() = default;
|
2022-09-01 16:22:02 -03:00
|
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) { }
|
2025-04-18 05:24:45 -03:00
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
|
|
|
|
|
virtual bool is_principal_host_defined() const { return false; }
|
|
|
|
|
virtual bool is_synthetic_host_defined() const { return false; }
|
2022-02-07 12:55:35 -03:00
|
|
|
};
|
|
|
|
|
|
2024-11-13 13:50:17 -03:00
|
|
|
template<typename T, typename... Args>
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<T> create(Args&&... args)
|
2024-11-13 13:50:17 -03:00
|
|
|
{
|
2024-11-13 14:13:46 -03:00
|
|
|
auto object = heap().allocate<T>(forward<Args>(args)...);
|
2024-11-13 13:50:17 -03:00
|
|
|
static_cast<Cell*>(object)->initialize(*this);
|
|
|
|
|
return *object;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 21:09:20 -03:00
|
|
|
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value);
|
2021-09-11 15:20:07 -03:00
|
|
|
|
2022-08-28 11:03:45 -03:00
|
|
|
[[nodiscard]] Object& global_object() const { return *m_global_object; }
|
2024-11-14 12:01:23 -03:00
|
|
|
void set_global_object(GC::Ref<Object> global) { m_global_object = global; }
|
2024-10-26 06:07:09 -03:00
|
|
|
|
2021-09-11 15:20:07 -03:00
|
|
|
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
|
2024-11-14 12:01:23 -03:00
|
|
|
void set_global_environment(GC::Ref<GlobalEnvironment> environment) { m_global_environment = environment; }
|
2021-09-11 15:20:07 -03:00
|
|
|
|
2022-08-26 20:54:55 -03:00
|
|
|
[[nodiscard]] Intrinsics const& intrinsics() const { return *m_intrinsics; }
|
|
|
|
|
[[nodiscard]] Intrinsics& intrinsics() { return *m_intrinsics; }
|
|
|
|
|
void set_intrinsics(Badge<Intrinsics>, Intrinsics& intrinsics)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(!m_intrinsics);
|
|
|
|
|
m_intrinsics = &intrinsics;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 12:55:35 -03:00
|
|
|
HostDefined* host_defined() { return m_host_defined; }
|
2024-10-20 23:25:09 -03:00
|
|
|
HostDefined const* host_defined() const { return m_host_defined; }
|
|
|
|
|
|
2022-02-07 12:55:35 -03:00
|
|
|
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
void define_builtin(Bytecode::Builtin builtin, GC::Ref<NativeFunction> value)
|
2023-11-17 07:48:30 -03:00
|
|
|
{
|
|
|
|
|
m_builtins[to_underlying(builtin)] = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<NativeFunction> get_builtin_value(Bytecode::Builtin builtin)
|
2023-11-30 15:49:29 -03:00
|
|
|
{
|
2024-02-20 17:29:58 -03:00
|
|
|
return *m_builtins[to_underlying(builtin)];
|
2023-11-30 15:49:29 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-11 15:20:07 -03:00
|
|
|
private:
|
2022-08-28 18:51:28 -03:00
|
|
|
Realm() = default;
|
|
|
|
|
|
2021-09-11 15:20:07 -03:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<Intrinsics> m_intrinsics; // [[Intrinsics]]
|
|
|
|
|
GC::Ptr<Object> m_global_object; // [[GlobalObject]]
|
|
|
|
|
GC::Ptr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]]
|
|
|
|
|
OwnPtr<HostDefined> m_host_defined; // [[HostDefined]]
|
|
|
|
|
AK::Array<GC::Ptr<NativeFunction>, to_underlying(Bytecode::Builtin::__Count)> m_builtins;
|
2021-09-11 15:20:07 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|