summaryrefslogtreecommitdiffstats
path: root/js/public/GCVariant.h
blob: 31ab23f54c41e729baa10523c8ebf7a30bf98184 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef js_GCVariant_h
#define js_GCVariant_h

#include "mozilla/Variant.h"

#include "js/GCPolicyAPI.h"
#include "js/RootingAPI.h"
#include "js/TracingAPI.h"

namespace JS {

// These template specializations allow Variant to be used inside GC wrappers.
//
// When matching on GC wrappers around Variants, matching should be done on
// the wrapper itself. The matcher class's methods should take Handles or
// MutableHandles. For example,
//
//   struct MyMatcher
//   {
//        using ReturnType = const char*;
//        ReturnType match(HandleObject o) { return "object"; }
//        ReturnType match(HandleScript s) { return "script"; }
//   };
//
//   Rooted<Variant<JSObject*, JSScript*>> v(cx, someScript);
//   MyMatcher mm;
//   v.match(mm);
//
// If you get compile errors about inability to upcast subclasses (e.g., from
// NativeObject* to JSObject*) and are inside js/src, be sure to also include
// "gc/Policy.h".

namespace detail {

template <typename... Ts>
struct GCVariantImplementation;

// The base case.
template <typename T>
struct GCVariantImplementation<T>
{
    template <typename ConcreteVariant>
    static void trace(JSTracer* trc, ConcreteVariant* v, const char* name) {
        T& thing = v->template as<T>();
        if (!mozilla::IsPointer<T>::value || thing)
            GCPolicy<T>::trace(trc, &thing, name);
    }

    template <typename Matcher, typename ConcreteVariant>
    static typename Matcher::ReturnType
    match(Matcher& matcher, Handle<ConcreteVariant> v) {
        const T& thing = v.get().template as<T>();
        return matcher.match(Handle<T>::fromMarkedLocation(&thing));
    }

    template <typename Matcher, typename ConcreteVariant>
    static typename Matcher::ReturnType
    match(Matcher& matcher, MutableHandle<ConcreteVariant> v) {
        T& thing = v.get().template as<T>();
        return matcher.match(MutableHandle<T>::fromMarkedLocation(&thing));
    }
};

// The inductive case.
template <typename T, typename... Ts>
struct GCVariantImplementation<T, Ts...>
{
    using Next = GCVariantImplementation<Ts...>;

    template <typename ConcreteVariant>
    static void trace(JSTracer* trc, ConcreteVariant* v, const char* name) {
        if (v->template is<T>()) {
            T& thing = v->template as<T>();
            if (!mozilla::IsPointer<T>::value || thing)
                GCPolicy<T>::trace(trc, &thing, name);
        } else {
            Next::trace(trc, v, name);
        }
    }

    template <typename Matcher, typename ConcreteVariant>
    static typename Matcher::ReturnType
    match(Matcher& matcher, Handle<ConcreteVariant> v) {
        if (v.get().template is<T>()) {
            const T& thing = v.get().template as<T>();
            return matcher.match(Handle<T>::fromMarkedLocation(&thing));
        }
        return Next::match(matcher, v);
    }

    template <typename Matcher, typename ConcreteVariant>
    static typename Matcher::ReturnType
    match(Matcher& matcher, MutableHandle<ConcreteVariant> v) {
        if (v.get().template is<T>()) {
            T& thing = v.get().template as<T>();
            return matcher.match(MutableHandle<T>::fromMarkedLocation(&thing));
        }
        return Next::match(matcher, v);
    }
};

} // namespace detail

template <typename... Ts>
struct GCPolicy<mozilla::Variant<Ts...>>
{
    using Impl = detail::GCVariantImplementation<Ts...>;

    // Variants do not provide initial(). They do not have a default initial
    // value and one must be provided.

    static void trace(JSTracer* trc, mozilla::Variant<Ts...>* v, const char* name) {
        Impl::trace(trc, v, name);
    }
};

} // namespace JS

namespace js {

template <typename Outer, typename... Ts>
class GCVariantOperations
{
    using Impl = JS::detail::GCVariantImplementation<Ts...>;
    using Variant = mozilla::Variant<Ts...>;

    const Variant& variant() const { return static_cast<const Outer*>(this)->get(); }

  public:
    template <typename T>
    bool is() const {
        return variant().template is<T>();
    }

    template <typename T>
    JS::Handle<T> as() const {
        return Handle<T>::fromMarkedLocation(&variant().template as<T>());
    }

    template <typename Matcher>
    typename Matcher::ReturnType
    match(Matcher& matcher) const {
        return Impl::match(matcher, JS::Handle<Variant>::fromMarkedLocation(&variant()));
    }
};

template <typename Outer, typename... Ts>
class MutableGCVariantOperations
  : public GCVariantOperations<Outer, Ts...>
{
    using Impl = JS::detail::GCVariantImplementation<Ts...>;
    using Variant = mozilla::Variant<Ts...>;

    const Variant& variant() const { return static_cast<const Outer*>(this)->get(); }
    Variant& variant() { return static_cast<Outer*>(this)->get(); }

  public:
    template <typename T>
    JS::MutableHandle<T> as() {
        return JS::MutableHandle<T>::fromMarkedLocation(&variant().template as<T>());
    }

    template <typename Matcher>
    typename Matcher::ReturnType
    match(Matcher& matcher) {
        return Impl::match(matcher, JS::MutableHandle<Variant>::fromMarkedLocation(&variant()));
    }
};

template <typename... Ts>
class RootedBase<mozilla::Variant<Ts...>>
  : public MutableGCVariantOperations<JS::Rooted<mozilla::Variant<Ts...>>, Ts...>
{ };

template <typename... Ts>
class MutableHandleBase<mozilla::Variant<Ts...>>
  : public MutableGCVariantOperations<JS::MutableHandle<mozilla::Variant<Ts...>>, Ts...>
{ };

template <typename... Ts>
class HandleBase<mozilla::Variant<Ts...>>
  : public GCVariantOperations<JS::Handle<mozilla::Variant<Ts...>>, Ts...>
{ };

template <typename... Ts>
class PersistentRootedBase<mozilla::Variant<Ts...>>
  : public MutableGCVariantOperations<JS::PersistentRooted<mozilla::Variant<Ts...>>, Ts...>
{ };

} // namespace js

#endif // js_GCVariant_h