summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Bindings/Intrinsics.h
blob: 896a143da5a6a3f802969092fe94ab6277a3f01d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Forward.h>
#include <AK/HashMap.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/HostDefined.h>

namespace Web::Bindings {

class Intrinsics final : public JS::Cell {
    JS_CELL(Intrinsics, JS::Cell);

public:
    Intrinsics(JS::Realm& realm)
        : m_realm(realm)
    {
    }

    template<typename NamespaceType>
    JS::Object& ensure_web_namespace(DeprecatedString const& namespace_name)
    {
        if (auto it = m_namespaces.find(namespace_name); it != m_namespaces.end())
            return *it->value;

        create_web_namespace<NamespaceType>(*m_realm);
        return *m_namespaces.find(namespace_name)->value;
    }

    template<typename PrototypeType>
    JS::Object& ensure_web_prototype(DeprecatedString const& class_name)
    {
        if (auto it = m_prototypes.find(class_name); it != m_prototypes.end())
            return *it->value;

        create_web_prototype_and_constructor<PrototypeType>(*m_realm);
        return *m_prototypes.find(class_name)->value;
    }

    template<typename PrototypeType>
    JS::NativeFunction& ensure_web_constructor(DeprecatedString const& class_name)
    {
        if (auto it = m_constructors.find(class_name); it != m_constructors.end())
            return *it->value;

        create_web_prototype_and_constructor<PrototypeType>(*m_realm);
        return *m_constructors.find(class_name)->value;
    }

private:
    virtual void visit_edges(JS::Cell::Visitor&) override;

    template<typename NamespaceType>
    void create_web_namespace(JS::Realm& realm);

    template<typename PrototypeType>
    void create_web_prototype_and_constructor(JS::Realm& realm);

    HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_namespaces;
    HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_prototypes;
    HashMap<DeprecatedString, JS::GCPtr<JS::NativeFunction>> m_constructors;
    JS::NonnullGCPtr<JS::Realm> m_realm;
};

[[nodiscard]] inline Intrinsics& host_defined_intrinsics(JS::Realm& realm)
{
    return *verify_cast<HostDefined>(realm.host_defined())->intrinsics;
}

template<typename T>
[[nodiscard]] JS::Object& ensure_web_namespace(JS::Realm& realm, DeprecatedString const& namespace_name)
{
    return host_defined_intrinsics(realm).ensure_web_namespace<T>(namespace_name);
}

template<typename T>
[[nodiscard]] JS::Object& ensure_web_prototype(JS::Realm& realm, DeprecatedString const& class_name)
{
    return host_defined_intrinsics(realm).ensure_web_prototype<T>(class_name);
}

template<typename T>
[[nodiscard]] JS::NativeFunction& ensure_web_constructor(JS::Realm& realm, DeprecatedString const& class_name)
{
    return host_defined_intrinsics(realm).ensure_web_constructor<T>(class_name);
}

}