/src/serenity/Userland/Libraries/LibJS/Runtime/Realm.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> |
3 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Badge.h> |
11 | | #include <AK/OwnPtr.h> |
12 | | #include <AK/StringView.h> |
13 | | #include <AK/Weakable.h> |
14 | | #include <LibJS/Bytecode/Builtins.h> |
15 | | #include <LibJS/Heap/Cell.h> |
16 | | #include <LibJS/Heap/CellAllocator.h> |
17 | | #include <LibJS/Runtime/Intrinsics.h> |
18 | | #include <LibJS/Runtime/Value.h> |
19 | | |
20 | | namespace JS { |
21 | | |
22 | | // 9.3 Realms, https://tc39.es/ecma262/#realm-record |
23 | | class Realm final : public Cell { |
24 | | JS_CELL(Realm, Cell); |
25 | | JS_DECLARE_ALLOCATOR(Realm); |
26 | | |
27 | | public: |
28 | | struct HostDefined { |
29 | 0 | virtual ~HostDefined() = default; |
30 | | |
31 | 0 | virtual void visit_edges(Cell::Visitor&) { } |
32 | | }; |
33 | | |
34 | | static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value); |
35 | | |
36 | 73 | [[nodiscard]] Object& global_object() const { return *m_global_object; } |
37 | 44 | [[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; } |
38 | | |
39 | 0 | [[nodiscard]] Intrinsics const& intrinsics() const { return *m_intrinsics; } |
40 | 26.6k | [[nodiscard]] Intrinsics& intrinsics() { return *m_intrinsics; } |
41 | | void set_intrinsics(Badge<Intrinsics>, Intrinsics& intrinsics) |
42 | 51 | { |
43 | 51 | VERIFY(!m_intrinsics); |
44 | 51 | m_intrinsics = &intrinsics; |
45 | 51 | } |
46 | | |
47 | 0 | HostDefined* host_defined() { return m_host_defined; } |
48 | 0 | void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); } |
49 | | |
50 | | void define_builtin(Bytecode::Builtin builtin, NonnullGCPtr<NativeFunction> value) |
51 | 0 | { |
52 | 0 | m_builtins[to_underlying(builtin)] = value; |
53 | 0 | } |
54 | | |
55 | | NonnullGCPtr<NativeFunction> get_builtin_value(Bytecode::Builtin builtin) |
56 | 0 | { |
57 | 0 | return *m_builtins[to_underlying(builtin)]; |
58 | 0 | } |
59 | | |
60 | | private: |
61 | 51 | Realm() = default; |
62 | | |
63 | | virtual void visit_edges(Visitor&) override; |
64 | | |
65 | | GCPtr<Intrinsics> m_intrinsics; // [[Intrinsics]] |
66 | | GCPtr<Object> m_global_object; // [[GlobalObject]] |
67 | | GCPtr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]] |
68 | | OwnPtr<HostDefined> m_host_defined; // [[HostDefined]] |
69 | | AK::Array<GCPtr<NativeFunction>, to_underlying(Bytecode::Builtin::__Count)> m_builtins; |
70 | | }; |
71 | | |
72 | | } |