File: | var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h |
Warning: | line 1223, column 5 Dereference of undefined pointer value (loaded from field 'stack') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* THIS FILE IS AUTOGENERATED FROM TestDictionary.webidl BY Codegen.py - DO NOT EDIT */ | ||||||||||
2 | |||||||||||
3 | #include <type_traits> | ||||||||||
4 | #include "AtomList.h" | ||||||||||
5 | #include "MainThreadUtils.h" | ||||||||||
6 | #include "TestDictionaryBinding.h" | ||||||||||
7 | #include "js/CallAndConstruct.h" | ||||||||||
8 | #include "js/Exception.h" | ||||||||||
9 | #include "js/MapAndSet.h" | ||||||||||
10 | #include "js/Object.h" | ||||||||||
11 | #include "js/PropertyAndElement.h" | ||||||||||
12 | #include "js/PropertyDescriptor.h" | ||||||||||
13 | #include "js/experimental/JitInfo.h" | ||||||||||
14 | #include "mozilla/FloatingPoint.h" | ||||||||||
15 | #include "mozilla/OwningNonNull.h" | ||||||||||
16 | #include "mozilla/dom/BindingCallContext.h" | ||||||||||
17 | #include "mozilla/dom/BindingUtils.h" | ||||||||||
18 | #include "mozilla/dom/NonRefcountedDOMObject.h" | ||||||||||
19 | #include "mozilla/dom/PrimitiveConversions.h" | ||||||||||
20 | #include "mozilla/dom/ScriptSettings.h" | ||||||||||
21 | #include "mozilla/dom/SimpleGlobalObject.h" | ||||||||||
22 | |||||||||||
23 | namespace mozilla::dom { | ||||||||||
24 | |||||||||||
25 | namespace binding_detail {}; // Just to make sure it's known as a namespace | ||||||||||
26 | using namespace mozilla::dom::binding_detail; | ||||||||||
27 | |||||||||||
28 | |||||||||||
29 | |||||||||||
30 | GrandparentDict::GrandparentDict() | ||||||||||
31 | { | ||||||||||
32 | // Safe to pass a null context if we pass a null value | ||||||||||
33 | Init(nullptr, JS::NullHandleValue); | ||||||||||
| |||||||||||
34 | } | ||||||||||
35 | |||||||||||
36 | |||||||||||
37 | |||||||||||
38 | |||||||||||
39 | bool | ||||||||||
40 | GrandparentDict::InitIds(JSContext* cx, GrandparentDictAtoms* atomsCache) | ||||||||||
41 | { | ||||||||||
42 | MOZ_ASSERT(reinterpret_cast<jsid*>(atomsCache)->isVoid())do { static_assert( mozilla::detail::AssertionConditionType< decltype(reinterpret_cast<jsid*>(atomsCache)->isVoid ())>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(reinterpret_cast<jsid*>(atomsCache)->isVoid ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("reinterpret_cast<jsid*>(atomsCache)->isVoid()", "../TestDictionaryBinding.cpp" , 42); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reinterpret_cast<jsid*>(atomsCache)->isVoid()" ")"); do { *((volatile int*)__null) = 42; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
43 | |||||||||||
44 | // Initialize these in reverse order so that any failure leaves the first one | ||||||||||
45 | // uninitialized. | ||||||||||
46 | if (!atomsCache->someNum_id.init(cx, "someNum")) { | ||||||||||
47 | return false; | ||||||||||
48 | } | ||||||||||
49 | return true; | ||||||||||
50 | } | ||||||||||
51 | |||||||||||
52 | bool | ||||||||||
53 | GrandparentDict::Init(BindingCallContext& cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl) | ||||||||||
54 | { | ||||||||||
55 | // Passing a null JSContext is OK only if we're initing from null, | ||||||||||
56 | // Since in that case we will not have to do any property gets | ||||||||||
57 | // Also evaluate isNullOrUndefined in order to avoid false-positive | ||||||||||
58 | // checkers by static analysis tools | ||||||||||
59 | MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined())do { if (!cx) { do { static_assert( mozilla::detail::AssertionConditionType <decltype(val.isNull() && val.isNullOrUndefined()) >::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(val.isNull() && val.isNullOrUndefined()))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("val.isNull() && val.isNullOrUndefined()" , "../TestDictionaryBinding.cpp", 59); AnnotateMozCrashReason ("MOZ_ASSERT" "(" "val.isNull() && val.isNullOrUndefined()" ")"); do { *((volatile int*)__null) = 59; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | ||||||||||
60 | GrandparentDictAtoms* atomsCache = nullptr; | ||||||||||
61 | if (cx) { | ||||||||||
62 | atomsCache = GetAtomCache<GrandparentDictAtoms>(cx); | ||||||||||
63 | if (reinterpret_cast<jsid*>(atomsCache)->isVoid() && | ||||||||||
64 | !InitIds(cx, atomsCache)) { | ||||||||||
65 | return false; | ||||||||||
66 | } | ||||||||||
67 | } | ||||||||||
68 | |||||||||||
69 | if (!IsConvertibleToDictionary(val)) { | ||||||||||
70 | return cx.ThrowErrorMessage<MSG_CONVERSION_ERROR>(sourceDescription, "dictionary"); | ||||||||||
71 | } | ||||||||||
72 | |||||||||||
73 | bool isNull = val.isNullOrUndefined(); | ||||||||||
74 | // We only need these if !isNull, in which case we have |cx|. | ||||||||||
75 | Maybe<JS::Rooted<JSObject *> > object; | ||||||||||
76 | Maybe<JS::Rooted<JS::Value> > temp; | ||||||||||
77 | if (!isNull
| ||||||||||
78 | MOZ_ASSERT(cx)do { static_assert( mozilla::detail::AssertionConditionType< decltype(cx)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(cx))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("cx", "../TestDictionaryBinding.cpp" , 78); AnnotateMozCrashReason("MOZ_ASSERT" "(" "cx" ")"); do { *((volatile int*)__null) = 78; __attribute__((nomerge)) ::abort (); } while (false); } } while (false); | ||||||||||
79 | object.emplace(cx, &val.toObject()); | ||||||||||
80 | temp.emplace(cx); | ||||||||||
81 | } | ||||||||||
82 | if (!isNull
| ||||||||||
83 | if (!JS_GetPropertyById(cx, *object, atomsCache->someNum_id, temp.ptr())) { | ||||||||||
84 | return false; | ||||||||||
85 | } | ||||||||||
86 | } | ||||||||||
87 | if (!isNull
| ||||||||||
88 | mSomeNum.Construct(); | ||||||||||
89 | if (!ValueToPrimitive<double, eDefault>(cx, temp.ref(), "'someNum' member of GrandparentDict", &(mSomeNum.Value()))) { | ||||||||||
90 | return false; | ||||||||||
91 | } else if (!std::isfinite((mSomeNum.Value()))) { | ||||||||||
92 | cx.ThrowErrorMessage<MSG_NOT_FINITE>("'someNum' member of GrandparentDict"); | ||||||||||
93 | return false; | ||||||||||
94 | } | ||||||||||
95 | mIsAnyMemberPresent = true; | ||||||||||
96 | } | ||||||||||
97 | return true; | ||||||||||
98 | } | ||||||||||
99 | |||||||||||
100 | bool | ||||||||||
101 | GrandparentDict::Init(JSContext* cx_, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl) | ||||||||||
102 | { | ||||||||||
103 | // We don't want to use sourceDescription for our context here; | ||||||||||
104 | // that's not really what it's formatted for. | ||||||||||
105 | BindingCallContext cx(cx_, nullptr); | ||||||||||
106 | return Init(cx, val, sourceDescription, passedToJSImpl); | ||||||||||
107 | } | ||||||||||
108 | |||||||||||
109 | bool | ||||||||||
110 | GrandparentDict::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const | ||||||||||
111 | { | ||||||||||
112 | GrandparentDictAtoms* atomsCache = GetAtomCache<GrandparentDictAtoms>(cx); | ||||||||||
113 | if (reinterpret_cast<jsid*>(atomsCache)->isVoid() && | ||||||||||
114 | !InitIds(cx, atomsCache)) { | ||||||||||
115 | return false; | ||||||||||
116 | } | ||||||||||
117 | |||||||||||
118 | JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx)); | ||||||||||
119 | if (!obj) { | ||||||||||
120 | return false; | ||||||||||
121 | } | ||||||||||
122 | rval.set(JS::ObjectValue(*obj)); | ||||||||||
123 | |||||||||||
124 | if (mSomeNum.WasPassed()) { | ||||||||||
125 | do { | ||||||||||
126 | // block for our 'break' successCode and scope for 'temp' and 'currentValue' | ||||||||||
127 | JS::Rooted<JS::Value> temp(cx); | ||||||||||
128 | double const & currentValue = mSomeNum.InternalValue(); | ||||||||||
129 | temp.set(JS_NumberValue(double(currentValue))); | ||||||||||
130 | if (!JS_DefinePropertyById(cx, obj, atomsCache->someNum_id, temp, JSPROP_ENUMERATE)) { | ||||||||||
131 | return false; | ||||||||||
132 | } | ||||||||||
133 | break; | ||||||||||
134 | } while(false); | ||||||||||
135 | } | ||||||||||
136 | |||||||||||
137 | return true; | ||||||||||
138 | } | ||||||||||
139 | |||||||||||
140 | void | ||||||||||
141 | GrandparentDict::TraceDictionary(JSTracer* trc) | ||||||||||
142 | { | ||||||||||
143 | } | ||||||||||
144 | |||||||||||
145 | GrandparentDict& | ||||||||||
146 | GrandparentDict::operator=(const GrandparentDict& aOther) | ||||||||||
147 | { | ||||||||||
148 | DictionaryBase::operator=(aOther); | ||||||||||
149 | mSomeNum.Reset(); | ||||||||||
150 | if (aOther.mSomeNum.WasPassed()) { | ||||||||||
151 | mSomeNum.Construct(aOther.mSomeNum.Value()); | ||||||||||
152 | } | ||||||||||
153 | return *this; | ||||||||||
154 | } | ||||||||||
155 | |||||||||||
156 | |||||||||||
157 | } // namespace mozilla::dom |
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||||||||||
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ | ||||||||||
3 | /* This Source Code Form is subject to the terms of the Mozilla Public | ||||||||||
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this | ||||||||||
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||||||||||
6 | |||||||||||
7 | /* A class for optional values and in-place lazy construction. */ | ||||||||||
8 | |||||||||||
9 | #ifndef mozilla_Maybe_h | ||||||||||
10 | #define mozilla_Maybe_h | ||||||||||
11 | |||||||||||
12 | #include <functional> | ||||||||||
13 | #include <new> // for placement new | ||||||||||
14 | #include <ostream> | ||||||||||
15 | #include <type_traits> | ||||||||||
16 | #include <utility> | ||||||||||
17 | |||||||||||
18 | #include "mozilla/Alignment.h" | ||||||||||
19 | #include "mozilla/Assertions.h" | ||||||||||
20 | #include "mozilla/Attributes.h" | ||||||||||
21 | #include "mozilla/MaybeStorageBase.h" | ||||||||||
22 | #include "mozilla/MemoryChecking.h" | ||||||||||
23 | #include "mozilla/OperatorNewExtensions.h" | ||||||||||
24 | #include "mozilla/Poison.h" | ||||||||||
25 | #include "mozilla/ThreadSafety.h" | ||||||||||
26 | |||||||||||
27 | class nsCycleCollectionTraversalCallback; | ||||||||||
28 | |||||||||||
29 | template <typename T> | ||||||||||
30 | inline void CycleCollectionNoteChild( | ||||||||||
31 | nsCycleCollectionTraversalCallback& aCallback, T* aChild, const char* aName, | ||||||||||
32 | uint32_t aFlags); | ||||||||||
33 | |||||||||||
34 | namespace mozilla { | ||||||||||
35 | |||||||||||
36 | struct Nothing {}; | ||||||||||
37 | |||||||||||
38 | inline constexpr bool operator==(const Nothing&, const Nothing&) { | ||||||||||
39 | return true; | ||||||||||
40 | } | ||||||||||
41 | |||||||||||
42 | template <class T> | ||||||||||
43 | class Maybe; | ||||||||||
44 | |||||||||||
45 | namespace detail { | ||||||||||
46 | |||||||||||
47 | // You would think that poisoning Maybe instances could just be a call | ||||||||||
48 | // to mozWritePoison. Unfortunately, using a simple call to | ||||||||||
49 | // mozWritePoison generates poor code on MSVC for small structures. The | ||||||||||
50 | // generated code contains (always not-taken) branches and does a bunch | ||||||||||
51 | // of setup for `rep stos{l,q}`, even though we know at compile time | ||||||||||
52 | // exactly how many words we're poisoning. Instead, we're going to | ||||||||||
53 | // force MSVC to generate the code we want via recursive templates. | ||||||||||
54 | |||||||||||
55 | // Write the given poisonValue into p at offset*sizeof(uintptr_t). | ||||||||||
56 | template <size_t offset> | ||||||||||
57 | inline void WritePoisonAtOffset(void* p, const uintptr_t poisonValue) { | ||||||||||
58 | memcpy(static_cast<char*>(p) + offset * sizeof(poisonValue), &poisonValue, | ||||||||||
59 | sizeof(poisonValue)); | ||||||||||
60 | } | ||||||||||
61 | |||||||||||
62 | template <size_t Offset, size_t NOffsets> | ||||||||||
63 | struct InlinePoisoner { | ||||||||||
64 | static void poison(void* p, const uintptr_t poisonValue) { | ||||||||||
65 | WritePoisonAtOffset<Offset>(p, poisonValue); | ||||||||||
66 | InlinePoisoner<Offset + 1, NOffsets>::poison(p, poisonValue); | ||||||||||
67 | } | ||||||||||
68 | }; | ||||||||||
69 | |||||||||||
70 | template <size_t N> | ||||||||||
71 | struct InlinePoisoner<N, N> { | ||||||||||
72 | static void poison(void*, const uintptr_t) { | ||||||||||
73 | // All done! | ||||||||||
74 | } | ||||||||||
75 | }; | ||||||||||
76 | |||||||||||
77 | // We can't generate inline code for large structures, though, because we'll | ||||||||||
78 | // blow out recursive template instantiation limits, and the code would be | ||||||||||
79 | // bloated to boot. So provide a fallback to the out-of-line poisoner. | ||||||||||
80 | template <size_t ObjectSize> | ||||||||||
81 | struct OutOfLinePoisoner { | ||||||||||
82 | static MOZ_NEVER_INLINE__attribute__((noinline)) void poison(void* p, const uintptr_t) { | ||||||||||
83 | mozWritePoison(p, ObjectSize); | ||||||||||
84 | } | ||||||||||
85 | }; | ||||||||||
86 | |||||||||||
87 | template <typename T> | ||||||||||
88 | inline void PoisonObject(T* p) { | ||||||||||
89 | const uintptr_t POISON = mozPoisonValue(); | ||||||||||
90 | std::conditional_t<(sizeof(T) <= 8 * sizeof(POISON)), | ||||||||||
91 | InlinePoisoner<0, sizeof(T) / sizeof(POISON)>, | ||||||||||
92 | OutOfLinePoisoner<sizeof(T)>>::poison(p, POISON); | ||||||||||
93 | } | ||||||||||
94 | |||||||||||
95 | template <typename T> | ||||||||||
96 | struct MaybePoisoner { | ||||||||||
97 | static const size_t N = sizeof(T); | ||||||||||
98 | |||||||||||
99 | static void poison(void* aPtr) { | ||||||||||
100 | #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED1 | ||||||||||
101 | if (N >= sizeof(uintptr_t)) { | ||||||||||
102 | PoisonObject(static_cast<std::remove_cv_t<T>*>(aPtr)); | ||||||||||
103 | } | ||||||||||
104 | #endif | ||||||||||
105 | MOZ_MAKE_MEM_UNDEFINED(aPtr, N)do { } while (0); | ||||||||||
106 | } | ||||||||||
107 | }; | ||||||||||
108 | |||||||||||
109 | template <typename T, | ||||||||||
110 | bool TriviallyDestructibleAndCopyable = | ||||||||||
111 | IsTriviallyDestructibleAndCopyable<T>, | ||||||||||
112 | bool Copyable = std::is_copy_constructible_v<T>, | ||||||||||
113 | bool Movable = std::is_move_constructible_v<T>> | ||||||||||
114 | class Maybe_CopyMove_Enabler; | ||||||||||
115 | |||||||||||
116 | #define MOZ_MAYBE_COPY_OPS() \ | ||||||||||
117 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler& aOther) { \ | ||||||||||
118 | if (downcast(aOther).isSome()) { \ | ||||||||||
119 | downcast(*this).emplace(*downcast(aOther)); \ | ||||||||||
120 | } \ | ||||||||||
121 | } \ | ||||||||||
122 | \ | ||||||||||
123 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler& aOther) { \ | ||||||||||
124 | return downcast(*this).template operator= <T>(downcast(aOther)); \ | ||||||||||
125 | } | ||||||||||
126 | |||||||||||
127 | #define MOZ_MAYBE_MOVE_OPS() \ | ||||||||||
128 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { \ | ||||||||||
129 | if (downcast(aOther).isSome()) { \ | ||||||||||
130 | downcast(*this).emplace(std::move(*downcast(aOther))); \ | ||||||||||
131 | downcast(aOther).reset(); \ | ||||||||||
132 | } \ | ||||||||||
133 | } \ | ||||||||||
134 | \ | ||||||||||
135 | constexpr Maybe_CopyMove_Enabler& operator=( \ | ||||||||||
136 | Maybe_CopyMove_Enabler&& aOther) { \ | ||||||||||
137 | downcast(*this).template operator= <T>(std::move(downcast(aOther))); \ | ||||||||||
138 | \ | ||||||||||
139 | return *this; \ | ||||||||||
140 | } | ||||||||||
141 | |||||||||||
142 | #define MOZ_MAYBE_DOWNCAST() \ | ||||||||||
143 | static constexpr Maybe<T>& downcast(Maybe_CopyMove_Enabler& aObj) { \ | ||||||||||
144 | return static_cast<Maybe<T>&>(aObj); \ | ||||||||||
145 | } \ | ||||||||||
146 | static constexpr const Maybe<T>& downcast( \ | ||||||||||
147 | const Maybe_CopyMove_Enabler& aObj) { \ | ||||||||||
148 | return static_cast<const Maybe<T>&>(aObj); \ | ||||||||||
149 | } | ||||||||||
150 | |||||||||||
151 | template <typename T> | ||||||||||
152 | class Maybe_CopyMove_Enabler<T, true, true, true> { | ||||||||||
153 | public: | ||||||||||
154 | Maybe_CopyMove_Enabler() = default; | ||||||||||
155 | |||||||||||
156 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = default; | ||||||||||
157 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = default; | ||||||||||
158 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||
159 | downcast(aOther).reset(); | ||||||||||
160 | } | ||||||||||
161 | constexpr Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||
162 | downcast(aOther).reset(); | ||||||||||
163 | return *this; | ||||||||||
164 | } | ||||||||||
165 | |||||||||||
166 | private: | ||||||||||
167 | MOZ_MAYBE_DOWNCAST() | ||||||||||
168 | }; | ||||||||||
169 | |||||||||||
170 | template <typename T> | ||||||||||
171 | class Maybe_CopyMove_Enabler<T, true, false, true> { | ||||||||||
172 | public: | ||||||||||
173 | Maybe_CopyMove_Enabler() = default; | ||||||||||
174 | |||||||||||
175 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
176 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
177 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||
178 | downcast(aOther).reset(); | ||||||||||
179 | } | ||||||||||
180 | constexpr Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||
181 | downcast(aOther).reset(); | ||||||||||
182 | return *this; | ||||||||||
183 | } | ||||||||||
184 | |||||||||||
185 | private: | ||||||||||
186 | MOZ_MAYBE_DOWNCAST() | ||||||||||
187 | }; | ||||||||||
188 | |||||||||||
189 | template <typename T> | ||||||||||
190 | class Maybe_CopyMove_Enabler<T, false, true, true> { | ||||||||||
191 | public: | ||||||||||
192 | Maybe_CopyMove_Enabler() = default; | ||||||||||
193 | |||||||||||
194 | MOZ_MAYBE_COPY_OPS() | ||||||||||
195 | MOZ_MAYBE_MOVE_OPS() | ||||||||||
196 | |||||||||||
197 | private: | ||||||||||
198 | MOZ_MAYBE_DOWNCAST() | ||||||||||
199 | }; | ||||||||||
200 | |||||||||||
201 | template <typename T> | ||||||||||
202 | class Maybe_CopyMove_Enabler<T, false, false, true> { | ||||||||||
203 | public: | ||||||||||
204 | Maybe_CopyMove_Enabler() = default; | ||||||||||
205 | |||||||||||
206 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
207 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
208 | MOZ_MAYBE_MOVE_OPS() | ||||||||||
209 | |||||||||||
210 | private: | ||||||||||
211 | MOZ_MAYBE_DOWNCAST() | ||||||||||
212 | }; | ||||||||||
213 | |||||||||||
214 | template <typename T> | ||||||||||
215 | class Maybe_CopyMove_Enabler<T, false, true, false> { | ||||||||||
216 | public: | ||||||||||
217 | Maybe_CopyMove_Enabler() = default; | ||||||||||
218 | |||||||||||
219 | MOZ_MAYBE_COPY_OPS() | ||||||||||
220 | Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||
221 | Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||
222 | |||||||||||
223 | private: | ||||||||||
224 | MOZ_MAYBE_DOWNCAST() | ||||||||||
225 | }; | ||||||||||
226 | |||||||||||
227 | template <typename T, bool TriviallyDestructibleAndCopyable> | ||||||||||
228 | class Maybe_CopyMove_Enabler<T, TriviallyDestructibleAndCopyable, false, | ||||||||||
229 | false> { | ||||||||||
230 | public: | ||||||||||
231 | Maybe_CopyMove_Enabler() = default; | ||||||||||
232 | |||||||||||
233 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
234 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||
235 | Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||
236 | Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||
237 | }; | ||||||||||
238 | |||||||||||
239 | #undef MOZ_MAYBE_COPY_OPS | ||||||||||
240 | #undef MOZ_MAYBE_MOVE_OPS | ||||||||||
241 | #undef MOZ_MAYBE_DOWNCAST | ||||||||||
242 | |||||||||||
243 | template <typename T, bool TriviallyDestructibleAndCopyable = | ||||||||||
244 | IsTriviallyDestructibleAndCopyable<T>> | ||||||||||
245 | struct MaybeStorage; | ||||||||||
246 | |||||||||||
247 | template <typename T> | ||||||||||
248 | struct MaybeStorage<T, false> : MaybeStorageBase<T> { | ||||||||||
249 | protected: | ||||||||||
250 | char mIsSome = false; // not bool -- guarantees minimal space consumption | ||||||||||
251 | |||||||||||
252 | MaybeStorage() = default; | ||||||||||
253 | explicit MaybeStorage(const T& aVal) | ||||||||||
254 | : MaybeStorageBase<T>{aVal}, mIsSome{true} {} | ||||||||||
255 | explicit MaybeStorage(T&& aVal) | ||||||||||
256 | : MaybeStorageBase<T>{std::move(aVal)}, mIsSome{true} {} | ||||||||||
257 | |||||||||||
258 | template <typename... Args> | ||||||||||
259 | explicit MaybeStorage(std::in_place_t, Args&&... aArgs) | ||||||||||
260 | : MaybeStorageBase<T>{std::in_place, std::forward<Args>(aArgs)...}, | ||||||||||
261 | mIsSome{true} {} | ||||||||||
262 | |||||||||||
263 | public: | ||||||||||
264 | // Copy and move operations are no-ops, since copying is moving is implemented | ||||||||||
265 | // by Maybe_CopyMove_Enabler. | ||||||||||
266 | |||||||||||
267 | MaybeStorage(const MaybeStorage&) : MaybeStorageBase<T>{} {} | ||||||||||
268 | MaybeStorage& operator=(const MaybeStorage&) { return *this; } | ||||||||||
269 | MaybeStorage(MaybeStorage&&) : MaybeStorageBase<T>{} {} | ||||||||||
270 | MaybeStorage& operator=(MaybeStorage&&) { return *this; } | ||||||||||
271 | |||||||||||
272 | ~MaybeStorage() { | ||||||||||
273 | if (mIsSome
| ||||||||||
274 | this->addr()->T::~T(); | ||||||||||
275 | } | ||||||||||
276 | } | ||||||||||
277 | }; | ||||||||||
278 | |||||||||||
279 | template <typename T> | ||||||||||
280 | struct MaybeStorage<T, true> : MaybeStorageBase<T> { | ||||||||||
281 | protected: | ||||||||||
282 | char mIsSome = false; // not bool -- guarantees minimal space consumption | ||||||||||
283 | |||||||||||
284 | constexpr MaybeStorage() = default; | ||||||||||
285 | constexpr explicit MaybeStorage(const T& aVal) | ||||||||||
286 | : MaybeStorageBase<T>{aVal}, mIsSome{true} {} | ||||||||||
287 | constexpr explicit MaybeStorage(T&& aVal) | ||||||||||
288 | : MaybeStorageBase<T>{std::move(aVal)}, mIsSome{true} {} | ||||||||||
289 | |||||||||||
290 | template <typename... Args> | ||||||||||
291 | constexpr explicit MaybeStorage(std::in_place_t, Args&&... aArgs) | ||||||||||
292 | : MaybeStorageBase<T>{std::in_place, std::forward<Args>(aArgs)...}, | ||||||||||
293 | mIsSome{true} {} | ||||||||||
294 | }; | ||||||||||
295 | |||||||||||
296 | template <typename T> | ||||||||||
297 | struct IsMaybeImpl : std::false_type {}; | ||||||||||
298 | |||||||||||
299 | template <typename T> | ||||||||||
300 | struct IsMaybeImpl<Maybe<T>> : std::true_type {}; | ||||||||||
301 | |||||||||||
302 | template <typename T> | ||||||||||
303 | using IsMaybe = IsMaybeImpl<std::decay_t<T>>; | ||||||||||
304 | |||||||||||
305 | } // namespace detail | ||||||||||
306 | |||||||||||
307 | template <typename T, typename U = typename std::remove_cv< | ||||||||||
308 | typename std::remove_reference<T>::type>::type> | ||||||||||
309 | constexpr Maybe<U> Some(T&& aValue); | ||||||||||
310 | |||||||||||
311 | /* | ||||||||||
312 | * Maybe is a container class which contains either zero or one elements. It | ||||||||||
313 | * serves two roles. It can represent values which are *semantically* optional, | ||||||||||
314 | * augmenting a type with an explicit 'Nothing' value. In this role, it provides | ||||||||||
315 | * methods that make it easy to work with values that may be missing, along with | ||||||||||
316 | * equality and comparison operators so that Maybe values can be stored in | ||||||||||
317 | * containers. Maybe values can be constructed conveniently in expressions using | ||||||||||
318 | * type inference, as follows: | ||||||||||
319 | * | ||||||||||
320 | * void doSomething(Maybe<Foo> aFoo) { | ||||||||||
321 | * if (aFoo) // Make sure that aFoo contains a value... | ||||||||||
322 | * aFoo->takeAction(); // and then use |aFoo->| to access it. | ||||||||||
323 | * } // |*aFoo| also works! | ||||||||||
324 | * | ||||||||||
325 | * doSomething(Nothing()); // Passes a Maybe<Foo> containing no value. | ||||||||||
326 | * doSomething(Some(Foo(100))); // Passes a Maybe<Foo> containing |Foo(100)|. | ||||||||||
327 | * | ||||||||||
328 | * You'll note that it's important to check whether a Maybe contains a value | ||||||||||
329 | * before using it, using conversion to bool, |isSome()|, or |isNothing()|. You | ||||||||||
330 | * can avoid these checks, and sometimes write more readable code, using | ||||||||||
331 | * |valueOr()|, |ptrOr()|, and |refOr()|, which allow you to retrieve the value | ||||||||||
332 | * in the Maybe and provide a default for the 'Nothing' case. You can also use | ||||||||||
333 | * |apply()| to call a function only if the Maybe holds a value, and |map()| to | ||||||||||
334 | * transform the value in the Maybe, returning another Maybe with a possibly | ||||||||||
335 | * different type. | ||||||||||
336 | * | ||||||||||
337 | * Maybe's other role is to support lazily constructing objects without using | ||||||||||
338 | * dynamic storage. A Maybe directly contains storage for a value, but it's | ||||||||||
339 | * empty by default. |emplace()|, as mentioned above, can be used to construct a | ||||||||||
340 | * value in Maybe's storage. The value a Maybe contains can be destroyed by | ||||||||||
341 | * calling |reset()|; this will happen automatically if a Maybe is destroyed | ||||||||||
342 | * while holding a value. | ||||||||||
343 | * | ||||||||||
344 | * It's a common idiom in C++ to use a pointer as a 'Maybe' type, with a null | ||||||||||
345 | * value meaning 'Nothing' and any other value meaning 'Some'. You can convert | ||||||||||
346 | * from such a pointer to a Maybe value using 'ToMaybe()'. | ||||||||||
347 | * | ||||||||||
348 | * Maybe is inspired by similar types in the standard library of many other | ||||||||||
349 | * languages (e.g. Haskell's Maybe and Rust's Option). In the C++ world it's | ||||||||||
350 | * very similar to std::optional, which was proposed for C++14 and originated in | ||||||||||
351 | * Boost. The most important differences between Maybe and std::optional are: | ||||||||||
352 | * | ||||||||||
353 | * - std::optional<T> may be compared with T. We deliberately forbid that. | ||||||||||
354 | * - std::optional has |valueOr()|, equivalent to Maybe's |valueOr()|, but | ||||||||||
355 | * lacks corresponding methods for |refOr()| and |ptrOr()|. | ||||||||||
356 | * - std::optional lacks |map()| and |apply()|, making it less suitable for | ||||||||||
357 | * functional-style code. | ||||||||||
358 | * - std::optional lacks many convenience functions that Maybe has. Most | ||||||||||
359 | * unfortunately, it lacks equivalents of the type-inferred constructor | ||||||||||
360 | * functions |Some()| and |Nothing()|. | ||||||||||
361 | */ | ||||||||||
362 | template <class T> | ||||||||||
363 | class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS Maybe | ||||||||||
364 | : private detail::MaybeStorage<T>, | ||||||||||
365 | public detail::Maybe_CopyMove_Enabler<T> { | ||||||||||
366 | template <typename, bool, bool, bool> | ||||||||||
367 | friend class detail::Maybe_CopyMove_Enabler; | ||||||||||
368 | |||||||||||
369 | template <typename U, typename V> | ||||||||||
370 | friend constexpr Maybe<V> Some(U&& aValue); | ||||||||||
371 | |||||||||||
372 | struct SomeGuard {}; | ||||||||||
373 | |||||||||||
374 | template <typename U> | ||||||||||
375 | constexpr Maybe(U&& aValue, SomeGuard) | ||||||||||
376 | : detail::MaybeStorage<T>{std::forward<U>(aValue)} {} | ||||||||||
377 | |||||||||||
378 | using detail::MaybeStorage<T>::mIsSome; | ||||||||||
379 | using detail::MaybeStorage<T>::mStorage; | ||||||||||
380 | |||||||||||
381 | void poisonData() { detail::MaybePoisoner<T>::poison(&mStorage.val); } | ||||||||||
382 | |||||||||||
383 | public: | ||||||||||
384 | using ValueType = T; | ||||||||||
385 | |||||||||||
386 | MOZ_ALLOW_TEMPORARY constexpr Maybe() = default; | ||||||||||
387 | |||||||||||
388 | MOZ_ALLOW_TEMPORARY MOZ_IMPLICIT constexpr Maybe(Nothing) : Maybe{} {} | ||||||||||
389 | |||||||||||
390 | template <typename... Args> | ||||||||||
391 | constexpr explicit Maybe(std::in_place_t, Args&&... aArgs) | ||||||||||
392 | : detail::MaybeStorage<T>{std::in_place, std::forward<Args>(aArgs)...} {} | ||||||||||
393 | |||||||||||
394 | /** | ||||||||||
395 | * Maybe<T> can be copy-constructed from a Maybe<U> if T is constructible from | ||||||||||
396 | * a const U&. | ||||||||||
397 | */ | ||||||||||
398 | template <typename U, | ||||||||||
399 | std::enable_if_t<std::is_constructible_v<T, const U&>, bool> = true> | ||||||||||
400 | MOZ_IMPLICIT Maybe(const Maybe<U>& aOther) { | ||||||||||
401 | if (aOther.isSome()) { | ||||||||||
402 | emplace(*aOther); | ||||||||||
403 | } | ||||||||||
404 | } | ||||||||||
405 | |||||||||||
406 | template <typename U, std::enable_if_t<!std::is_constructible_v<T, const U&>, | ||||||||||
407 | bool> = true> | ||||||||||
408 | explicit Maybe(const Maybe<U>& aOther) = delete; | ||||||||||
409 | |||||||||||
410 | /** | ||||||||||
411 | * Maybe<T> can be move-constructed from a Maybe<U> if T is constructible from | ||||||||||
412 | * a U&&. | ||||||||||
413 | */ | ||||||||||
414 | template <typename U, | ||||||||||
415 | std::enable_if_t<std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||
416 | MOZ_IMPLICIT Maybe(Maybe<U>&& aOther) { | ||||||||||
417 | if (aOther.isSome()) { | ||||||||||
418 | emplace(std::move(*aOther)); | ||||||||||
419 | aOther.reset(); | ||||||||||
420 | } | ||||||||||
421 | } | ||||||||||
422 | template <typename U, | ||||||||||
423 | std::enable_if_t<!std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||
424 | explicit Maybe(Maybe<U>&& aOther) = delete; | ||||||||||
425 | |||||||||||
426 | template <typename U, | ||||||||||
427 | std::enable_if_t<std::is_constructible_v<T, const U&>, bool> = true> | ||||||||||
428 | Maybe& operator=(const Maybe<U>& aOther) { | ||||||||||
429 | if (aOther.isSome()) { | ||||||||||
430 | if (mIsSome) { | ||||||||||
431 | ref() = aOther.ref(); | ||||||||||
432 | } else { | ||||||||||
433 | emplace(*aOther); | ||||||||||
434 | } | ||||||||||
435 | } else { | ||||||||||
436 | reset(); | ||||||||||
437 | } | ||||||||||
438 | return *this; | ||||||||||
439 | } | ||||||||||
440 | |||||||||||
441 | template <typename U, std::enable_if_t<!std::is_constructible_v<T, const U&>, | ||||||||||
442 | bool> = true> | ||||||||||
443 | Maybe& operator=(const Maybe<U>& aOther) = delete; | ||||||||||
444 | |||||||||||
445 | template <typename U, | ||||||||||
446 | std::enable_if_t<std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||
447 | Maybe& operator=(Maybe<U>&& aOther) { | ||||||||||
448 | if (aOther.isSome()) { | ||||||||||
449 | if (mIsSome) { | ||||||||||
450 | ref() = std::move(aOther.ref()); | ||||||||||
451 | } else { | ||||||||||
452 | emplace(std::move(*aOther)); | ||||||||||
453 | } | ||||||||||
454 | aOther.reset(); | ||||||||||
455 | } else { | ||||||||||
456 | reset(); | ||||||||||
457 | } | ||||||||||
458 | |||||||||||
459 | return *this; | ||||||||||
460 | } | ||||||||||
461 | |||||||||||
462 | template <typename U, | ||||||||||
463 | std::enable_if_t<!std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||
464 | Maybe& operator=(Maybe<U>&& aOther) = delete; | ||||||||||
465 | |||||||||||
466 | constexpr Maybe& operator=(Nothing) { | ||||||||||
467 | reset(); | ||||||||||
468 | return *this; | ||||||||||
469 | } | ||||||||||
470 | |||||||||||
471 | /* Methods that check whether this Maybe contains a value */ | ||||||||||
472 | constexpr explicit operator bool() const { return isSome(); } | ||||||||||
473 | constexpr bool isSome() const { return mIsSome; } | ||||||||||
474 | constexpr bool isNothing() const { return !mIsSome; } | ||||||||||
475 | |||||||||||
476 | /* Returns the contents of this Maybe<T> by value. Unsafe unless |isSome()|. | ||||||||||
477 | */ | ||||||||||
478 | constexpr T value() const&; | ||||||||||
479 | constexpr T value() &&; | ||||||||||
480 | constexpr T value() const&&; | ||||||||||
481 | |||||||||||
482 | /** | ||||||||||
483 | * Move the contents of this Maybe<T> out of internal storage and return it | ||||||||||
484 | * without calling the destructor. The internal storage is also reset to | ||||||||||
485 | * avoid multiple calls. Unsafe unless |isSome()|. | ||||||||||
486 | */ | ||||||||||
487 | constexpr T extract() { | ||||||||||
488 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 488); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 488; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
489 | T v = std::move(mStorage.val); | ||||||||||
490 | reset(); | ||||||||||
491 | return v; | ||||||||||
492 | } | ||||||||||
493 | |||||||||||
494 | /** | ||||||||||
495 | * Returns the value (possibly |Nothing()|) by moving it out of this Maybe<T> | ||||||||||
496 | * and leaving |Nothing()| in its place. | ||||||||||
497 | */ | ||||||||||
498 | Maybe<T> take() { return std::exchange(*this, Nothing()); } | ||||||||||
499 | |||||||||||
500 | /* | ||||||||||
501 | * Returns the contents of this Maybe<T> by value. If |isNothing()|, returns | ||||||||||
502 | * the default value provided. | ||||||||||
503 | * | ||||||||||
504 | * Note: If the value passed to aDefault is not the result of a trivial | ||||||||||
505 | * expression, but expensive to evaluate, e.g. |valueOr(ExpensiveFunction())|, | ||||||||||
506 | * use |valueOrFrom| instead, e.g. | ||||||||||
507 | * |valueOrFrom([arg] { return ExpensiveFunction(arg); })|. This ensures | ||||||||||
508 | * that the expensive expression is only evaluated when its result will | ||||||||||
509 | * actually be used. | ||||||||||
510 | */ | ||||||||||
511 | template <typename V> | ||||||||||
512 | constexpr T valueOr(V&& aDefault) const { | ||||||||||
513 | if (isSome()) { | ||||||||||
514 | return ref(); | ||||||||||
515 | } | ||||||||||
516 | return std::forward<V>(aDefault); | ||||||||||
517 | } | ||||||||||
518 | |||||||||||
519 | /* | ||||||||||
520 | * Returns the contents of this Maybe<T> by value. If |isNothing()|, returns | ||||||||||
521 | * the value returned from the function or functor provided. | ||||||||||
522 | */ | ||||||||||
523 | template <typename F> | ||||||||||
524 | constexpr T valueOrFrom(F&& aFunc) const { | ||||||||||
525 | if (isSome()) { | ||||||||||
526 | return ref(); | ||||||||||
527 | } | ||||||||||
528 | return aFunc(); | ||||||||||
529 | } | ||||||||||
530 | |||||||||||
531 | /* Returns the contents of this Maybe<T> by pointer. Unsafe unless |isSome()|. | ||||||||||
532 | */ | ||||||||||
533 | T* ptr(); | ||||||||||
534 | constexpr const T* ptr() const; | ||||||||||
535 | |||||||||||
536 | /* | ||||||||||
537 | * Returns the contents of this Maybe<T> by pointer. If |isNothing()|, | ||||||||||
538 | * returns the default value provided. | ||||||||||
539 | */ | ||||||||||
540 | T* ptrOr(T* aDefault) { | ||||||||||
541 | if (isSome()) { | ||||||||||
542 | return ptr(); | ||||||||||
543 | } | ||||||||||
544 | return aDefault; | ||||||||||
545 | } | ||||||||||
546 | |||||||||||
547 | constexpr const T* ptrOr(const T* aDefault) const { | ||||||||||
548 | if (isSome()) { | ||||||||||
549 | return ptr(); | ||||||||||
550 | } | ||||||||||
551 | return aDefault; | ||||||||||
552 | } | ||||||||||
553 | |||||||||||
554 | /* | ||||||||||
555 | * Returns the contents of this Maybe<T> by pointer. If |isNothing()|, | ||||||||||
556 | * returns the value returned from the function or functor provided. | ||||||||||
557 | */ | ||||||||||
558 | template <typename F> | ||||||||||
559 | T* ptrOrFrom(F&& aFunc) { | ||||||||||
560 | if (isSome()) { | ||||||||||
561 | return ptr(); | ||||||||||
562 | } | ||||||||||
563 | return aFunc(); | ||||||||||
564 | } | ||||||||||
565 | |||||||||||
566 | template <typename F> | ||||||||||
567 | const T* ptrOrFrom(F&& aFunc) const { | ||||||||||
568 | if (isSome()) { | ||||||||||
569 | return ptr(); | ||||||||||
570 | } | ||||||||||
571 | return aFunc(); | ||||||||||
572 | } | ||||||||||
573 | |||||||||||
574 | constexpr T* operator->(); | ||||||||||
575 | constexpr const T* operator->() const; | ||||||||||
576 | |||||||||||
577 | /* Returns the contents of this Maybe<T> by ref. Unsafe unless |isSome()|. */ | ||||||||||
578 | constexpr T& ref() &; | ||||||||||
579 | constexpr const T& ref() const&; | ||||||||||
580 | constexpr T&& ref() &&; | ||||||||||
581 | constexpr const T&& ref() const&&; | ||||||||||
582 | |||||||||||
583 | /* | ||||||||||
584 | * Returns the contents of this Maybe<T> by ref. If |isNothing()|, returns | ||||||||||
585 | * the default value provided. | ||||||||||
586 | */ | ||||||||||
587 | constexpr T& refOr(T& aDefault) { | ||||||||||
588 | if (isSome()) { | ||||||||||
589 | return ref(); | ||||||||||
590 | } | ||||||||||
591 | return aDefault; | ||||||||||
592 | } | ||||||||||
593 | |||||||||||
594 | constexpr const T& refOr(const T& aDefault) const { | ||||||||||
595 | if (isSome()) { | ||||||||||
596 | return ref(); | ||||||||||
597 | } | ||||||||||
598 | return aDefault; | ||||||||||
599 | } | ||||||||||
600 | |||||||||||
601 | /* | ||||||||||
602 | * Returns the contents of this Maybe<T> by ref. If |isNothing()|, returns the | ||||||||||
603 | * value returned from the function or functor provided. | ||||||||||
604 | */ | ||||||||||
605 | template <typename F> | ||||||||||
606 | constexpr T& refOrFrom(F&& aFunc) { | ||||||||||
607 | if (isSome()) { | ||||||||||
608 | return ref(); | ||||||||||
609 | } | ||||||||||
610 | return aFunc(); | ||||||||||
611 | } | ||||||||||
612 | |||||||||||
613 | template <typename F> | ||||||||||
614 | constexpr const T& refOrFrom(F&& aFunc) const { | ||||||||||
615 | if (isSome()) { | ||||||||||
616 | return ref(); | ||||||||||
617 | } | ||||||||||
618 | return aFunc(); | ||||||||||
619 | } | ||||||||||
620 | |||||||||||
621 | constexpr T& operator*() &; | ||||||||||
622 | constexpr const T& operator*() const&; | ||||||||||
623 | constexpr T&& operator*() &&; | ||||||||||
624 | constexpr const T&& operator*() const&&; | ||||||||||
625 | |||||||||||
626 | /* If |isSome()|, runs the provided function or functor on the contents of | ||||||||||
627 | * this Maybe. */ | ||||||||||
628 | template <typename Func> | ||||||||||
629 | constexpr Maybe& apply(Func&& aFunc) & { | ||||||||||
630 | if (isSome()) { | ||||||||||
631 | std::forward<Func>(aFunc)(ref()); | ||||||||||
632 | } | ||||||||||
633 | return *this; | ||||||||||
634 | } | ||||||||||
635 | |||||||||||
636 | template <typename Func> | ||||||||||
637 | constexpr const Maybe& apply(Func&& aFunc) const& { | ||||||||||
638 | if (isSome()) { | ||||||||||
639 | std::forward<Func>(aFunc)(ref()); | ||||||||||
640 | } | ||||||||||
641 | return *this; | ||||||||||
642 | } | ||||||||||
643 | |||||||||||
644 | template <typename Func> | ||||||||||
645 | constexpr Maybe& apply(Func&& aFunc) && { | ||||||||||
646 | if (isSome()) { | ||||||||||
647 | std::forward<Func>(aFunc)(extract()); | ||||||||||
648 | } | ||||||||||
649 | return *this; | ||||||||||
650 | } | ||||||||||
651 | |||||||||||
652 | template <typename Func> | ||||||||||
653 | constexpr Maybe& apply(Func&& aFunc) const&& { | ||||||||||
654 | if (isSome()) { | ||||||||||
655 | std::forward<Func>(aFunc)(extract()); | ||||||||||
656 | } | ||||||||||
657 | return *this; | ||||||||||
658 | } | ||||||||||
659 | |||||||||||
660 | /* | ||||||||||
661 | * If |isSome()|, runs the provided function and returns the result wrapped | ||||||||||
662 | * in a Maybe. If |isNothing()|, returns an empty Maybe value with the same | ||||||||||
663 | * value type as what the provided function would have returned. | ||||||||||
664 | */ | ||||||||||
665 | template <typename Func> | ||||||||||
666 | constexpr auto map(Func&& aFunc) & { | ||||||||||
667 | if (isSome()) { | ||||||||||
668 | return Some(std::forward<Func>(aFunc)(ref())); | ||||||||||
669 | } | ||||||||||
670 | return Maybe<decltype(std::forward<Func>(aFunc)(ref()))>{}; | ||||||||||
671 | } | ||||||||||
672 | |||||||||||
673 | template <typename Func> | ||||||||||
674 | constexpr auto map(Func&& aFunc) const& { | ||||||||||
675 | if (isSome()) { | ||||||||||
676 | return Some(std::forward<Func>(aFunc)(ref())); | ||||||||||
677 | } | ||||||||||
678 | return Maybe<decltype(std::forward<Func>(aFunc)(ref()))>{}; | ||||||||||
679 | } | ||||||||||
680 | |||||||||||
681 | template <typename Func> | ||||||||||
682 | constexpr auto map(Func&& aFunc) && { | ||||||||||
683 | if (isSome()) { | ||||||||||
684 | return Some(std::forward<Func>(aFunc)(extract())); | ||||||||||
685 | } | ||||||||||
686 | return Maybe<decltype(std::forward<Func>(aFunc)(extract()))>{}; | ||||||||||
687 | } | ||||||||||
688 | |||||||||||
689 | template <typename Func> | ||||||||||
690 | constexpr auto map(Func&& aFunc) const&& { | ||||||||||
691 | if (isSome()) { | ||||||||||
692 | return Some(std::forward<Func>(aFunc)(extract())); | ||||||||||
693 | } | ||||||||||
694 | return Maybe<decltype(std::forward<Func>(aFunc)(extract()))>{}; | ||||||||||
695 | } | ||||||||||
696 | |||||||||||
697 | /* | ||||||||||
698 | * If |isSome()|, runs the provided function or functor on the contents of | ||||||||||
699 | * this Maybe and returns the result. Note that the provided function or | ||||||||||
700 | * functor must return a Maybe<U> of any type U. | ||||||||||
701 | * If |isNothing()|, returns an empty Maybe value with the same type as what | ||||||||||
702 | * the provided function would have returned. | ||||||||||
703 | */ | ||||||||||
704 | template <typename Func> | ||||||||||
705 | constexpr auto andThen(Func&& aFunc) & { | ||||||||||
706 | static_assert(std::is_invocable_v<Func, T&>); | ||||||||||
707 | using U = std::invoke_result_t<Func, T&>; | ||||||||||
708 | static_assert(detail::IsMaybe<U>::value); | ||||||||||
709 | if (isSome()) { | ||||||||||
710 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||
711 | } | ||||||||||
712 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||
713 | } | ||||||||||
714 | |||||||||||
715 | template <typename Func> | ||||||||||
716 | constexpr auto andThen(Func&& aFunc) const& { | ||||||||||
717 | static_assert(std::is_invocable_v<Func, const T&>); | ||||||||||
718 | using U = std::invoke_result_t<Func, const T&>; | ||||||||||
719 | static_assert(detail::IsMaybe<U>::value); | ||||||||||
720 | if (isSome()) { | ||||||||||
721 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||
722 | } | ||||||||||
723 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||
724 | } | ||||||||||
725 | |||||||||||
726 | template <typename Func> | ||||||||||
727 | constexpr auto andThen(Func&& aFunc) && { | ||||||||||
728 | static_assert(std::is_invocable_v<Func, T&&>); | ||||||||||
729 | using U = std::invoke_result_t<Func, T&&>; | ||||||||||
730 | static_assert(detail::IsMaybe<U>::value); | ||||||||||
731 | if (isSome()) { | ||||||||||
732 | return std::invoke(std::forward<Func>(aFunc), extract()); | ||||||||||
733 | } | ||||||||||
734 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||
735 | } | ||||||||||
736 | |||||||||||
737 | template <typename Func> | ||||||||||
738 | constexpr auto andThen(Func&& aFunc) const&& { | ||||||||||
739 | static_assert(std::is_invocable_v<Func, const T&&>); | ||||||||||
740 | using U = std::invoke_result_t<Func, const T&&>; | ||||||||||
741 | static_assert(detail::IsMaybe<U>::value); | ||||||||||
742 | if (isSome()) { | ||||||||||
743 | return std::invoke(std::forward<Func>(aFunc), extract()); | ||||||||||
744 | } | ||||||||||
745 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||
746 | } | ||||||||||
747 | |||||||||||
748 | /* | ||||||||||
749 | * If |isNothing()|, runs the provided function or functor and returns its | ||||||||||
750 | * result. If |isSome()|, returns the contained value wrapped in a Maybe. | ||||||||||
751 | */ | ||||||||||
752 | template <typename Func> | ||||||||||
753 | constexpr Maybe orElse(Func&& aFunc) & { | ||||||||||
754 | static_assert(std::is_invocable_v<Func>); | ||||||||||
755 | using U = std::invoke_result_t<Func>; | ||||||||||
756 | static_assert( | ||||||||||
757 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||
758 | if (isSome()) { | ||||||||||
759 | return *this; | ||||||||||
760 | } | ||||||||||
761 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||
762 | } | ||||||||||
763 | |||||||||||
764 | template <typename Func> | ||||||||||
765 | constexpr Maybe orElse(Func&& aFunc) const& { | ||||||||||
766 | static_assert(std::is_invocable_v<Func>); | ||||||||||
767 | using U = std::invoke_result_t<Func>; | ||||||||||
768 | static_assert( | ||||||||||
769 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||
770 | if (isSome()) { | ||||||||||
771 | return *this; | ||||||||||
772 | } | ||||||||||
773 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||
774 | } | ||||||||||
775 | |||||||||||
776 | template <typename Func> | ||||||||||
777 | constexpr Maybe orElse(Func&& aFunc) && { | ||||||||||
778 | static_assert(std::is_invocable_v<Func>); | ||||||||||
779 | using U = std::invoke_result_t<Func>; | ||||||||||
780 | static_assert( | ||||||||||
781 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||
782 | if (isSome()) { | ||||||||||
783 | return std::move(*this); | ||||||||||
784 | } | ||||||||||
785 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||
786 | } | ||||||||||
787 | |||||||||||
788 | template <typename Func> | ||||||||||
789 | constexpr Maybe orElse(Func&& aFunc) const&& { | ||||||||||
790 | static_assert(std::is_invocable_v<Func>); | ||||||||||
791 | using U = std::invoke_result_t<Func>; | ||||||||||
792 | static_assert( | ||||||||||
793 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||
794 | if (isSome()) { | ||||||||||
795 | return std::move(*this); | ||||||||||
796 | } | ||||||||||
797 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||
798 | } | ||||||||||
799 | |||||||||||
800 | /* If |isSome()|, empties this Maybe and destroys its contents. */ | ||||||||||
801 | constexpr void reset() { | ||||||||||
802 | if (isSome()) { | ||||||||||
803 | if constexpr (!std::is_trivially_destructible_v<T>) { | ||||||||||
804 | /* | ||||||||||
805 | * Static analyzer gets confused if we have Maybe<MutexAutoLock>, | ||||||||||
806 | * so we suppress thread-safety warnings here | ||||||||||
807 | */ | ||||||||||
808 | MOZ_PUSH_IGNORE_THREAD_SAFETYGCC diagnostic push
GCC diagnostic ignored "-Wthread-safety" | ||||||||||
809 | ref().T::~T(); | ||||||||||
810 | MOZ_POP_THREAD_SAFETYGCC diagnostic pop | ||||||||||
811 | poisonData(); | ||||||||||
812 | } | ||||||||||
813 | mIsSome = false; | ||||||||||
814 | } | ||||||||||
815 | } | ||||||||||
816 | |||||||||||
817 | /* | ||||||||||
818 | * Constructs a T value in-place in this empty Maybe<T>'s storage. The | ||||||||||
819 | * arguments to |emplace()| are the parameters to T's constructor. | ||||||||||
820 | */ | ||||||||||
821 | template <typename... Args> | ||||||||||
822 | constexpr void emplace(Args&&... aArgs); | ||||||||||
823 | |||||||||||
824 | template <typename U> | ||||||||||
825 | constexpr std::enable_if_t<std::is_same_v<T, U> && | ||||||||||
826 | std::is_copy_constructible_v<U> && | ||||||||||
827 | !std::is_move_constructible_v<U>> | ||||||||||
828 | emplace(U&& aArgs) { | ||||||||||
829 | emplace(aArgs); | ||||||||||
830 | } | ||||||||||
831 | |||||||||||
832 | friend std::ostream& operator<<(std::ostream& aStream, | ||||||||||
833 | const Maybe<T>& aMaybe) { | ||||||||||
834 | if (aMaybe) { | ||||||||||
835 | aStream << aMaybe.ref(); | ||||||||||
836 | } else { | ||||||||||
837 | aStream << "<Nothing>"; | ||||||||||
838 | } | ||||||||||
839 | return aStream; | ||||||||||
840 | } | ||||||||||
841 | }; | ||||||||||
842 | |||||||||||
843 | template <typename T> | ||||||||||
844 | class Maybe<T&> { | ||||||||||
845 | public: | ||||||||||
846 | constexpr Maybe() = default; | ||||||||||
847 | constexpr MOZ_IMPLICIT Maybe(Nothing) {} | ||||||||||
848 | |||||||||||
849 | void emplace(T& aRef) { mValue = &aRef; } | ||||||||||
850 | |||||||||||
851 | /* Methods that check whether this Maybe contains a value */ | ||||||||||
852 | constexpr explicit operator bool() const { return isSome(); } | ||||||||||
853 | constexpr bool isSome() const { return mValue; } | ||||||||||
854 | constexpr bool isNothing() const { return !mValue; } | ||||||||||
855 | |||||||||||
856 | T& ref() const { | ||||||||||
857 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 857); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 857; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
858 | return *mValue; | ||||||||||
859 | } | ||||||||||
860 | |||||||||||
861 | T* operator->() const { return &ref(); } | ||||||||||
862 | T& operator*() const { return ref(); } | ||||||||||
863 | |||||||||||
864 | // Deliberately not defining value and ptr accessors, as these may be | ||||||||||
865 | // confusing on a reference-typed Maybe. | ||||||||||
866 | |||||||||||
867 | // XXX Should we define refOr? | ||||||||||
868 | |||||||||||
869 | void reset() { mValue = nullptr; } | ||||||||||
870 | |||||||||||
871 | template <typename Func> | ||||||||||
872 | const Maybe& apply(Func&& aFunc) const { | ||||||||||
873 | if (isSome()) { | ||||||||||
874 | std::forward<Func>(aFunc)(ref()); | ||||||||||
875 | } | ||||||||||
876 | return *this; | ||||||||||
877 | } | ||||||||||
878 | |||||||||||
879 | template <typename Func> | ||||||||||
880 | auto map(Func&& aFunc) const { | ||||||||||
881 | Maybe<decltype(std::forward<Func>(aFunc)(ref()))> val; | ||||||||||
882 | if (isSome()) { | ||||||||||
883 | val.emplace(std::forward<Func>(aFunc)(ref())); | ||||||||||
884 | } | ||||||||||
885 | return val; | ||||||||||
886 | } | ||||||||||
887 | |||||||||||
888 | template <typename Func> | ||||||||||
889 | constexpr auto andThen(Func&& aFunc) const { | ||||||||||
890 | static_assert(std::is_invocable_v<Func, T&>); | ||||||||||
891 | using U = std::invoke_result_t<Func, T&>; | ||||||||||
892 | static_assert(detail::IsMaybe<U>::value); | ||||||||||
893 | if (isSome()) { | ||||||||||
894 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||
895 | } | ||||||||||
896 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||
897 | } | ||||||||||
898 | |||||||||||
899 | template <typename Func> | ||||||||||
900 | constexpr Maybe orElse(Func&& aFunc) const { | ||||||||||
901 | static_assert(std::is_invocable_v<Func>); | ||||||||||
902 | using U = std::invoke_result_t<Func>; | ||||||||||
903 | static_assert( | ||||||||||
904 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||
905 | if (isSome()) { | ||||||||||
906 | return *this; | ||||||||||
907 | } | ||||||||||
908 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||
909 | } | ||||||||||
910 | |||||||||||
911 | bool refEquals(const Maybe<T&>& aOther) const { | ||||||||||
912 | return mValue == aOther.mValue; | ||||||||||
913 | } | ||||||||||
914 | |||||||||||
915 | bool refEquals(const T& aOther) const { return mValue == &aOther; } | ||||||||||
916 | |||||||||||
917 | private: | ||||||||||
918 | T* mValue = nullptr; | ||||||||||
919 | }; | ||||||||||
920 | |||||||||||
921 | template <typename T> | ||||||||||
922 | constexpr T Maybe<T>::value() const& { | ||||||||||
923 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 923); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 923; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
924 | return ref(); | ||||||||||
925 | } | ||||||||||
926 | |||||||||||
927 | template <typename T> | ||||||||||
928 | constexpr T Maybe<T>::value() && { | ||||||||||
929 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 929); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 929; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
930 | return std::move(ref()); | ||||||||||
931 | } | ||||||||||
932 | |||||||||||
933 | template <typename T> | ||||||||||
934 | constexpr T Maybe<T>::value() const&& { | ||||||||||
935 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 935); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 935; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
936 | return std::move(ref()); | ||||||||||
937 | } | ||||||||||
938 | |||||||||||
939 | template <typename T> | ||||||||||
940 | T* Maybe<T>::ptr() { | ||||||||||
941 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 941); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 941; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
942 | return &ref(); | ||||||||||
943 | } | ||||||||||
944 | |||||||||||
945 | template <typename T> | ||||||||||
946 | constexpr const T* Maybe<T>::ptr() const { | ||||||||||
947 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 947); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 947; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
948 | return &ref(); | ||||||||||
949 | } | ||||||||||
950 | |||||||||||
951 | template <typename T> | ||||||||||
952 | constexpr T* Maybe<T>::operator->() { | ||||||||||
953 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 953); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 953; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
954 | return ptr(); | ||||||||||
955 | } | ||||||||||
956 | |||||||||||
957 | template <typename T> | ||||||||||
958 | constexpr const T* Maybe<T>::operator->() const { | ||||||||||
959 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 959); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 959; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
960 | return ptr(); | ||||||||||
961 | } | ||||||||||
962 | |||||||||||
963 | template <typename T> | ||||||||||
964 | constexpr T& Maybe<T>::ref() & { | ||||||||||
965 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 965); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 965; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
966 | return mStorage.val; | ||||||||||
967 | } | ||||||||||
968 | |||||||||||
969 | template <typename T> | ||||||||||
970 | constexpr const T& Maybe<T>::ref() const& { | ||||||||||
971 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 971); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 971; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
972 | return mStorage.val; | ||||||||||
973 | } | ||||||||||
974 | |||||||||||
975 | template <typename T> | ||||||||||
976 | constexpr T&& Maybe<T>::ref() && { | ||||||||||
977 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 977); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 977; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
978 | return std::move(mStorage.val); | ||||||||||
979 | } | ||||||||||
980 | |||||||||||
981 | template <typename T> | ||||||||||
982 | constexpr const T&& Maybe<T>::ref() const&& { | ||||||||||
983 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 983); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 983; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
984 | return std::move(mStorage.val); | ||||||||||
985 | } | ||||||||||
986 | |||||||||||
987 | template <typename T> | ||||||||||
988 | constexpr T& Maybe<T>::operator*() & { | ||||||||||
989 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 989); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 989; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
990 | return ref(); | ||||||||||
991 | } | ||||||||||
992 | |||||||||||
993 | template <typename T> | ||||||||||
994 | constexpr const T& Maybe<T>::operator*() const& { | ||||||||||
995 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 995); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 995; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
996 | return ref(); | ||||||||||
997 | } | ||||||||||
998 | |||||||||||
999 | template <typename T> | ||||||||||
1000 | constexpr T&& Maybe<T>::operator*() && { | ||||||||||
1001 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1001); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 1001; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
1002 | return std::move(ref()); | ||||||||||
1003 | } | ||||||||||
1004 | |||||||||||
1005 | template <typename T> | ||||||||||
1006 | constexpr const T&& Maybe<T>::operator*() const&& { | ||||||||||
1007 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1007); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 1007; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
1008 | return std::move(ref()); | ||||||||||
1009 | } | ||||||||||
1010 | |||||||||||
1011 | template <typename T> | ||||||||||
1012 | template <typename... Args> | ||||||||||
1013 | constexpr void Maybe<T>::emplace(Args&&... aArgs) { | ||||||||||
1014 | MOZ_RELEASE_ASSERT(!isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1014); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "!isSome()" ")"); do { *((volatile int*)__null) = 1014; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||
1015 | ::new (KnownNotNull, &mStorage.val) T(std::forward<Args>(aArgs)...); | ||||||||||
1016 | mIsSome = true; | ||||||||||
1017 | } | ||||||||||
1018 | |||||||||||
1019 | /* | ||||||||||
1020 | * Some() creates a Maybe<T> value containing the provided T value. If T has a | ||||||||||
1021 | * move constructor, it's used to make this as efficient as possible. | ||||||||||
1022 | * | ||||||||||
1023 | * Some() selects the type of Maybe it returns by removing any const, volatile, | ||||||||||
1024 | * or reference qualifiers from the type of the value you pass to it. This gives | ||||||||||
1025 | * it more intuitive behavior when used in expressions, but it also means that | ||||||||||
1026 | * if you need to construct a Maybe value that holds a const, volatile, or | ||||||||||
1027 | * reference value, you need to use emplace() instead. | ||||||||||
1028 | */ | ||||||||||
1029 | template <typename T, typename U> | ||||||||||
1030 | constexpr Maybe<U> Some(T&& aValue) { | ||||||||||
1031 | return {std::forward<T>(aValue), typename Maybe<U>::SomeGuard{}}; | ||||||||||
1032 | } | ||||||||||
1033 | |||||||||||
1034 | template <typename T> | ||||||||||
1035 | constexpr Maybe<T&> SomeRef(T& aValue) { | ||||||||||
1036 | Maybe<T&> value; | ||||||||||
1037 | value.emplace(aValue); | ||||||||||
1038 | return value; | ||||||||||
1039 | } | ||||||||||
1040 | |||||||||||
1041 | template <typename T> | ||||||||||
1042 | constexpr Maybe<T&> ToMaybeRef(T* const aPtr) { | ||||||||||
1043 | return aPtr ? SomeRef(*aPtr) : Nothing{}; | ||||||||||
1044 | } | ||||||||||
1045 | |||||||||||
1046 | template <typename T> | ||||||||||
1047 | Maybe<std::remove_cv_t<std::remove_reference_t<T>>> ToMaybe(T* aPtr) { | ||||||||||
1048 | if (aPtr) { | ||||||||||
1049 | return Some(*aPtr); | ||||||||||
1050 | } | ||||||||||
1051 | return Nothing(); | ||||||||||
1052 | } | ||||||||||
1053 | |||||||||||
1054 | /* | ||||||||||
1055 | * Two Maybe<T> values are equal if | ||||||||||
1056 | * - both are Nothing, or | ||||||||||
1057 | * - both are Some, and the values they contain are equal. | ||||||||||
1058 | */ | ||||||||||
1059 | template <typename T> | ||||||||||
1060 | constexpr bool operator==(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1061 | static_assert(!std::is_reference_v<T>, | ||||||||||
1062 | "operator== is not defined for Maybe<T&>, compare values or " | ||||||||||
1063 | "addresses explicitly instead"); | ||||||||||
1064 | if (aLHS.isNothing() != aRHS.isNothing()) { | ||||||||||
1065 | return false; | ||||||||||
1066 | } | ||||||||||
1067 | return aLHS.isNothing() || *aLHS == *aRHS; | ||||||||||
1068 | } | ||||||||||
1069 | |||||||||||
1070 | template <typename T> | ||||||||||
1071 | constexpr bool operator!=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1072 | return !(aLHS == aRHS); | ||||||||||
1073 | } | ||||||||||
1074 | |||||||||||
1075 | /* | ||||||||||
1076 | * We support comparison to Nothing to allow reasonable expressions like: | ||||||||||
1077 | * if (maybeValue == Nothing()) { ... } | ||||||||||
1078 | */ | ||||||||||
1079 | template <typename T> | ||||||||||
1080 | constexpr bool operator==(const Maybe<T>& aLHS, const Nothing& aRHS) { | ||||||||||
1081 | return aLHS.isNothing(); | ||||||||||
1082 | } | ||||||||||
1083 | |||||||||||
1084 | template <typename T> | ||||||||||
1085 | constexpr bool operator!=(const Maybe<T>& aLHS, const Nothing& aRHS) { | ||||||||||
1086 | return !(aLHS == aRHS); | ||||||||||
1087 | } | ||||||||||
1088 | |||||||||||
1089 | template <typename T> | ||||||||||
1090 | constexpr bool operator==(const Nothing& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1091 | return aRHS.isNothing(); | ||||||||||
1092 | } | ||||||||||
1093 | |||||||||||
1094 | template <typename T> | ||||||||||
1095 | constexpr bool operator!=(const Nothing& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1096 | return !(aLHS == aRHS); | ||||||||||
1097 | } | ||||||||||
1098 | |||||||||||
1099 | /* | ||||||||||
1100 | * Maybe<T> values are ordered in the same way T values are ordered, except that | ||||||||||
1101 | * Nothing comes before anything else. | ||||||||||
1102 | */ | ||||||||||
1103 | template <typename T> | ||||||||||
1104 | constexpr bool operator<(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1105 | if (aLHS.isNothing()) { | ||||||||||
1106 | return aRHS.isSome(); | ||||||||||
1107 | } | ||||||||||
1108 | if (aRHS.isNothing()) { | ||||||||||
1109 | return false; | ||||||||||
1110 | } | ||||||||||
1111 | return *aLHS < *aRHS; | ||||||||||
1112 | } | ||||||||||
1113 | |||||||||||
1114 | template <typename T> | ||||||||||
1115 | constexpr bool operator>(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1116 | return !(aLHS < aRHS || aLHS == aRHS); | ||||||||||
1117 | } | ||||||||||
1118 | |||||||||||
1119 | template <typename T> | ||||||||||
1120 | constexpr bool operator<=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1121 | return aLHS < aRHS || aLHS == aRHS; | ||||||||||
1122 | } | ||||||||||
1123 | |||||||||||
1124 | template <typename T> | ||||||||||
1125 | constexpr bool operator>=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||
1126 | return !(aLHS < aRHS); | ||||||||||
1127 | } | ||||||||||
1128 | |||||||||||
1129 | template <typename T> | ||||||||||
1130 | inline void ImplCycleCollectionTraverse( | ||||||||||
1131 | nsCycleCollectionTraversalCallback& aCallback, mozilla::Maybe<T>& aField, | ||||||||||
1132 | const char* aName, uint32_t aFlags = 0) { | ||||||||||
1133 | if (aField) { | ||||||||||
1134 | ImplCycleCollectionTraverse(aCallback, aField.ref(), aName, aFlags); | ||||||||||
1135 | } | ||||||||||
1136 | } | ||||||||||
1137 | |||||||||||
1138 | template <typename T> | ||||||||||
1139 | inline void ImplCycleCollectionUnlink(mozilla::Maybe<T>& aField) { | ||||||||||
1140 | if (aField) { | ||||||||||
1141 | ImplCycleCollectionUnlink(aField.ref()); | ||||||||||
1142 | } | ||||||||||
1143 | } | ||||||||||
1144 | |||||||||||
1145 | } // namespace mozilla | ||||||||||
1146 | |||||||||||
1147 | #endif /* mozilla_Maybe_h */ |
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | /* Internal storage class used e.g. by Maybe and Result. This file doesn't |
8 | * contain any public declarations. */ |
9 | |
10 | #ifndef mfbt_MaybeStorageBase_h |
11 | #define mfbt_MaybeStorageBase_h |
12 | |
13 | #include <type_traits> |
14 | #include <utility> |
15 | |
16 | namespace mozilla::detail { |
17 | |
18 | template <typename T> |
19 | constexpr bool IsTriviallyDestructibleAndCopyable = |
20 | std::is_trivially_destructible_v<T> && |
21 | (std::is_trivially_copy_constructible_v<T> || |
22 | !std::is_copy_constructible_v<T>); |
23 | |
24 | template <typename T, bool TriviallyDestructibleAndCopyable = |
25 | IsTriviallyDestructibleAndCopyable<T>> |
26 | struct MaybeStorageBase; |
27 | |
28 | template <typename T> |
29 | struct MaybeStorageBase<T, false> { |
30 | protected: |
31 | using NonConstT = std::remove_const_t<T>; |
32 | |
33 | union Union { |
34 | Union() {} |
35 | explicit Union(const T& aVal) : val{aVal} {} |
36 | template <typename U, |
37 | typename = std::enable_if_t<std::is_move_constructible_v<U>>> |
38 | explicit Union(U&& aVal) : val{std::forward<U>(aVal)} {} |
39 | template <typename... Args> |
40 | explicit Union(std::in_place_t, Args&&... aArgs) |
41 | : val{std::forward<Args>(aArgs)...} {} |
42 | |
43 | ~Union() {} |
44 | |
45 | NonConstT val; |
46 | } mStorage; |
47 | |
48 | public: |
49 | MaybeStorageBase() = default; |
50 | explicit MaybeStorageBase(const T& aVal) : mStorage{aVal} {} |
51 | explicit MaybeStorageBase(T&& aVal) : mStorage{std::move(aVal)} {} |
52 | template <typename... Args> |
53 | explicit MaybeStorageBase(std::in_place_t, Args&&... aArgs) |
54 | : mStorage{std::in_place, std::forward<Args>(aArgs)...} {} |
55 | |
56 | const T* addr() const { return &mStorage.val; } |
57 | T* addr() { return &mStorage.val; } |
58 | }; |
59 | |
60 | template <typename T> |
61 | struct MaybeStorageBase<T, true> { |
62 | protected: |
63 | using NonConstT = std::remove_const_t<T>; |
64 | |
65 | union Union { |
66 | constexpr Union() : dummy() {} |
67 | constexpr explicit Union(const T& aVal) : val{aVal} {} |
68 | constexpr explicit Union(T&& aVal) : val{std::move(aVal)} {} |
69 | template <typename... Args> |
70 | constexpr explicit Union(std::in_place_t, Args&&... aArgs) |
71 | : val{std::forward<Args>(aArgs)...} {} |
72 | |
73 | NonConstT val; |
74 | char dummy; |
75 | } mStorage; |
76 | |
77 | public: |
78 | constexpr MaybeStorageBase() = default; |
79 | constexpr explicit MaybeStorageBase(const T& aVal) : mStorage{aVal} {} |
80 | constexpr explicit MaybeStorageBase(T&& aVal) : mStorage{std::move(aVal)} {} |
81 | |
82 | template <typename... Args> |
83 | constexpr explicit MaybeStorageBase(std::in_place_t, Args&&... aArgs) |
84 | : mStorage{std::in_place, std::forward<Args>(aArgs)...} {} |
85 | |
86 | constexpr const T* addr() const { return &mStorage.val; } |
87 | constexpr T* addr() { return &mStorage.val; } |
88 | }; |
89 | |
90 | } // namespace mozilla::detail |
91 | |
92 | #endif |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #ifndef nsINode_h___ |
8 | #define nsINode_h___ |
9 | |
10 | #include "mozilla/DoublyLinkedList.h" |
11 | #include "mozilla/Likely.h" |
12 | #include "mozilla/UniquePtr.h" |
13 | #include "nsCOMPtr.h" // for member, local |
14 | #include "nsGkAtoms.h" // for nsGkAtoms::baseURIProperty |
15 | #include "mozilla/dom/NodeInfo.h" // member (in nsCOMPtr) |
16 | #include "nsIWeakReference.h" |
17 | #include "nsIMutationObserver.h" |
18 | #include "nsNodeInfoManager.h" // for use in NodePrincipal() |
19 | #include "nsPropertyTable.h" // for typedefs |
20 | #include "mozilla/ErrorResult.h" |
21 | #include "mozilla/LinkedList.h" |
22 | #include "mozilla/MemoryReporting.h" |
23 | #include "mozilla/dom/EventTarget.h" // for base class |
24 | #include "js/TypeDecls.h" // for Handle, Value, JSObject, JSContext |
25 | #include "mozilla/dom/DOMString.h" |
26 | #include "mozilla/dom/BindingDeclarations.h" |
27 | #include "mozilla/dom/NodeBinding.h" |
28 | #include "nsTHashtable.h" |
29 | #include <iosfwd> |
30 | |
31 | // Including 'windows.h' will #define GetClassInfo to something else. |
32 | #ifdef XP_WIN |
33 | # ifdef GetClassInfo |
34 | # undef GetClassInfo |
35 | # endif |
36 | #endif |
37 | |
38 | class AttrArray; |
39 | class nsAttrChildContentList; |
40 | template <typename T> |
41 | class nsCOMArray; |
42 | class nsDOMAttributeMap; |
43 | class nsGenericHTMLElement; |
44 | class nsIAnimationObserver; |
45 | class nsIContent; |
46 | class nsIContentSecurityPolicy; |
47 | class nsIFrame; |
48 | class nsIFormControl; |
49 | class nsIHTMLCollection; |
50 | class nsMultiMutationObserver; |
51 | class nsINode; |
52 | class nsINodeList; |
53 | class nsIPrincipal; |
54 | class nsIURI; |
55 | class nsNodeSupportsWeakRefTearoff; |
56 | class nsDOMMutationObserver; |
57 | class nsRange; |
58 | class nsWindowSizes; |
59 | |
60 | namespace mozilla { |
61 | class EventListenerManager; |
62 | struct StyleSelectorList; |
63 | template <typename T> |
64 | class Maybe; |
65 | class PresShell; |
66 | class TextEditor; |
67 | namespace dom { |
68 | /** |
69 | * @return true if aChar is what the WHATWG defines as a 'ascii whitespace'. |
70 | * https://infra.spec.whatwg.org/#ascii-whitespace |
71 | */ |
72 | inline bool IsSpaceCharacter(char16_t aChar) { |
73 | return aChar == ' ' || aChar == '\t' || aChar == '\n' || aChar == '\r' || |
74 | aChar == '\f'; |
75 | } |
76 | inline bool IsSpaceCharacter(char aChar) { |
77 | return aChar == ' ' || aChar == '\t' || aChar == '\n' || aChar == '\r' || |
78 | aChar == '\f'; |
79 | } |
80 | class AbstractRange; |
81 | class AccessibleNode; |
82 | template <typename T> |
83 | class AncestorsOfTypeIterator; |
84 | struct BoxQuadOptions; |
85 | struct ConvertCoordinateOptions; |
86 | class DocGroup; |
87 | class Document; |
88 | class DocumentFragment; |
89 | class DocumentOrShadowRoot; |
90 | class DOMPoint; |
91 | class DOMQuad; |
92 | class DOMRectReadOnly; |
93 | class Element; |
94 | class EventHandlerNonNull; |
95 | template <typename T> |
96 | class FlatTreeAncestorsOfTypeIterator; |
97 | template <typename T> |
98 | class InclusiveAncestorsOfTypeIterator; |
99 | template <typename T> |
100 | class InclusiveFlatTreeAncestorsOfTypeIterator; |
101 | class LinkStyle; |
102 | class MutationObservers; |
103 | template <typename T> |
104 | class Optional; |
105 | class OwningNodeOrString; |
106 | class SelectionNodeCache; |
107 | template <typename> |
108 | class Sequence; |
109 | class ShadowRoot; |
110 | class SVGUseElement; |
111 | class Text; |
112 | class TextOrElementOrDocument; |
113 | struct DOMPointInit; |
114 | struct GetRootNodeOptions; |
115 | enum class CallerType : uint32_t; |
116 | } // namespace dom |
117 | } // namespace mozilla |
118 | |
119 | #define NODE_FLAG_BIT(n_)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (n_))) \ |
120 | (nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (n_))) |
121 | |
122 | enum : uint32_t { |
123 | // This bit will be set if the node has a listener manager. |
124 | NODE_HAS_LISTENERMANAGER = NODE_FLAG_BIT(0)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (0))), |
125 | |
126 | // Whether this node has had any properties set on it |
127 | NODE_HAS_PROPERTIES = NODE_FLAG_BIT(1)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (1))), |
128 | |
129 | // Whether the node has some ancestor, possibly itself, that is native |
130 | // anonymous. This includes ancestors crossing XBL scopes, in cases when an |
131 | // XBL binding is attached to an element which has a native anonymous |
132 | // ancestor. This flag is set-once: once a node has it, it must not be |
133 | // removed. |
134 | // NOTE: Should only be used on nsIContent nodes |
135 | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE = NODE_FLAG_BIT(2)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (2))), |
136 | |
137 | // Whether this node is the root of a native anonymous (from the perspective |
138 | // of its parent) subtree. This flag is set-once: once a node has it, it |
139 | // must not be removed. |
140 | // NOTE: Should only be used on nsIContent nodes |
141 | NODE_IS_NATIVE_ANONYMOUS_ROOT = NODE_FLAG_BIT(3)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (3))), |
142 | |
143 | NODE_IS_EDITABLE = NODE_FLAG_BIT(4)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (4))), |
144 | |
145 | // Whether the node participates in a shadow tree. |
146 | NODE_IS_IN_SHADOW_TREE = NODE_FLAG_BIT(5)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (5))), |
147 | |
148 | // This node needs to go through frame construction to get a frame (or |
149 | // undisplayed entry). |
150 | NODE_NEEDS_FRAME = NODE_FLAG_BIT(6)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (6))), |
151 | |
152 | // At least one descendant in the flattened tree has NODE_NEEDS_FRAME set. |
153 | // This should be set on every node on the flattened tree path between the |
154 | // node(s) with NODE_NEEDS_FRAME and the root content. |
155 | NODE_DESCENDANTS_NEED_FRAMES = NODE_FLAG_BIT(7)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (7))), |
156 | |
157 | // Set if the node has the accesskey attribute set. |
158 | NODE_HAS_ACCESSKEY = NODE_FLAG_BIT(8)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (8))), |
159 | |
160 | NODE_HAS_BEEN_IN_UA_WIDGET = NODE_FLAG_BIT(9)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (9))), |
161 | |
162 | // Set if the node has a nonce value and a header delivered CSP. |
163 | NODE_HAS_NONCE_AND_HEADER_CSP = NODE_FLAG_BIT(10)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (10))), |
164 | |
165 | NODE_KEEPS_DOMARENA = NODE_FLAG_BIT(11)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (11))), |
166 | |
167 | NODE_MAY_HAVE_ELEMENT_CHILDREN = NODE_FLAG_BIT(12)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (12))), |
168 | |
169 | NODE_HAS_SCHEDULED_SELECTION_CHANGE_EVENT = NODE_FLAG_BIT(13)(nsWrapperCache::FlagsType(1U) << (WRAPPER_CACHE_FLAGS_BITS_USED + (13))), |
170 | |
171 | // Remaining bits are node type specific. |
172 | NODE_TYPE_SPECIFIC_BITS_OFFSET = 14 |
173 | }; |
174 | |
175 | // Flags for selectors that persist to the DOM node. |
176 | enum class NodeSelectorFlags : uint32_t { |
177 | // Node has an :empty or :-moz-only-whitespace selector |
178 | HasEmptySelector = 1 << 0, |
179 | |
180 | /// A child of the node has a selector such that any insertion, |
181 | /// removal, or appending of children requires restyling the parent, if the |
182 | /// parent is an element. If the parent is the shadow root, the child's |
183 | /// siblings are restyled. |
184 | HasSlowSelector = 1 << 1, |
185 | |
186 | /// A child of the node has a :first-child, :-moz-first-node, |
187 | /// :only-child, :last-child or :-moz-last-node selector. |
188 | HasEdgeChildSelector = 1 << 2, |
189 | |
190 | /// A child of the node has a selector such that any insertion or |
191 | /// removal of children requires restyling later siblings of that |
192 | /// element. Additionally (in this manner it is stronger than |
193 | /// NODE_HAS_SLOW_SELECTOR), if a child's style changes due to any |
194 | /// other content tree changes (e.g., the child changes to or from |
195 | /// matching :empty due to a grandchild insertion or removal), the |
196 | /// child's later siblings must also be restyled. |
197 | HasSlowSelectorLaterSiblings = 1 << 3, |
198 | |
199 | /// HasSlowSelector* was set by the presence of :nth (But not of). |
200 | HasSlowSelectorNth = 1 << 4, |
201 | |
202 | /// A child of this node might be matched by :nth-child(.. of <selector>) or |
203 | /// :nth-last-child(.. of <selector>). If a DOM mutation may have caused the |
204 | /// selector to either match or no longer match that child, the child's |
205 | /// siblings are restyled. |
206 | HasSlowSelectorNthOf = 1 << 5, |
207 | |
208 | /// All instances of :nth flags. |
209 | HasSlowSelectorNthAll = HasSlowSelectorNthOf | HasSlowSelectorNth, |
210 | |
211 | /// Set of selector flags that may trigger a restyle on DOM append, with |
212 | /// restyle on siblings or a single parent (And perhaps their subtrees). |
213 | AllSimpleRestyleFlagsForAppend = HasEmptySelector | HasSlowSelector | |
214 | HasEdgeChildSelector | HasSlowSelectorNthAll, |
215 | |
216 | /// Set of selector flags that may trigger a restyle as a result of any |
217 | /// DOM mutation. |
218 | AllSimpleRestyleFlags = |
219 | AllSimpleRestyleFlagsForAppend | HasSlowSelectorLaterSiblings, |
220 | |
221 | // This node was evaluated as an anchor for a relative selector. |
222 | RelativeSelectorAnchor = 1 << 6, |
223 | |
224 | // This node was evaluated as an anchor for a relative selector, and that |
225 | // relative selector was not the subject of the overall selector. |
226 | RelativeSelectorAnchorNonSubject = 1 << 7, |
227 | |
228 | // This node's sibling(s) performed a relative selector search to this node. |
229 | RelativeSelectorSearchDirectionSibling = 1 << 8, |
230 | |
231 | // This node's ancestor(s) performed a relative selector search to this node. |
232 | RelativeSelectorSearchDirectionAncestor = 1 << 9, |
233 | |
234 | // This node's sibling(s) and ancestor(s), and/or this node's ancestor's |
235 | // sibling(s) performed a relative selector search to this node. |
236 | RelativeSelectorSearchDirectionAncestorSibling = |
237 | RelativeSelectorSearchDirectionSibling | |
238 | RelativeSelectorSearchDirectionAncestor, |
239 | }; |
240 | |
241 | MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(NodeSelectorFlags)inline constexpr mozilla::CastableTypedEnumResult<NodeSelectorFlags > operator |( NodeSelectorFlags a, NodeSelectorFlags b) { typedef mozilla::CastableTypedEnumResult<NodeSelectorFlags> Result ; typedef mozilla::detail::UnsignedIntegerTypeForEnum<NodeSelectorFlags >::Type U; return Result(NodeSelectorFlags(U(a) | U(b))); } inline NodeSelectorFlags& operator |=(NodeSelectorFlags & a, NodeSelectorFlags b) { return a = a | b; } inline constexpr mozilla::CastableTypedEnumResult<NodeSelectorFlags> operator &( NodeSelectorFlags a, NodeSelectorFlags b) { typedef mozilla ::CastableTypedEnumResult<NodeSelectorFlags> Result; typedef mozilla::detail::UnsignedIntegerTypeForEnum<NodeSelectorFlags >::Type U; return Result(NodeSelectorFlags(U(a) & U(b) )); } inline NodeSelectorFlags& operator &=(NodeSelectorFlags & a, NodeSelectorFlags b) { return a = a & b; } inline constexpr mozilla::CastableTypedEnumResult<NodeSelectorFlags > operator ^( NodeSelectorFlags a, NodeSelectorFlags b) { typedef mozilla::CastableTypedEnumResult<NodeSelectorFlags> Result ; typedef mozilla::detail::UnsignedIntegerTypeForEnum<NodeSelectorFlags >::Type U; return Result(NodeSelectorFlags(U(a) ^ U(b))); } inline NodeSelectorFlags& operator ^=(NodeSelectorFlags & a, NodeSelectorFlags b) { return a = a ^ b; } inline constexpr mozilla::CastableTypedEnumResult<NodeSelectorFlags> operator ~(NodeSelectorFlags a) { typedef mozilla::CastableTypedEnumResult <NodeSelectorFlags> Result; typedef mozilla::detail::UnsignedIntegerTypeForEnum <NodeSelectorFlags>::Type U; return Result(NodeSelectorFlags (~(U(a)))); }; |
242 | |
243 | // Make sure we have space for our bits |
244 | #define ASSERT_NODE_FLAGS_SPACE(n)static_assert(WRAPPER_CACHE_FLAGS_BITS_USED + (n) <= sizeof (nsWrapperCache::FlagsType) * 8, "Not enough space for our bits" ) \ |
245 | static_assert(WRAPPER_CACHE_FLAGS_BITS_USED + (n) <= \ |
246 | sizeof(nsWrapperCache::FlagsType) * 8, \ |
247 | "Not enough space for our bits") |
248 | ASSERT_NODE_FLAGS_SPACE(NODE_TYPE_SPECIFIC_BITS_OFFSET)static_assert(WRAPPER_CACHE_FLAGS_BITS_USED + (NODE_TYPE_SPECIFIC_BITS_OFFSET ) <= sizeof(nsWrapperCache::FlagsType) * 8, "Not enough space for our bits" ); |
249 | |
250 | /** |
251 | * Class used to detect unexpected mutations. To use the class create an |
252 | * nsMutationGuard on the stack before unexpected mutations could occur. |
253 | * You can then at any time call Mutated to check if any unexpected mutations |
254 | * have occurred. |
255 | */ |
256 | class nsMutationGuard { |
257 | public: |
258 | nsMutationGuard() { mStartingGeneration = sGeneration; } |
259 | |
260 | /** |
261 | * Returns true if any unexpected mutations have occurred. You can pass in |
262 | * an 8-bit ignore count to ignore a number of expected mutations. |
263 | * |
264 | * We don't need to care about overflow because subtraction of uint64_t's is |
265 | * finding the difference between two elements of the group Z < 2^64. Once |
266 | * we know the difference between two elements we only need to check that is |
267 | * less than the given number of mutations to know less than that many |
268 | * mutations occured. Assuming constant 1ns mutations it would take 584 |
269 | * years for sGeneration to fully wrap around so we can ignore a guard living |
270 | * through a full wrap around. |
271 | */ |
272 | bool Mutated(uint8_t aIgnoreCount) { |
273 | return (sGeneration - mStartingGeneration) > aIgnoreCount; |
274 | } |
275 | |
276 | // This function should be called whenever a mutation that we want to keep |
277 | // track of happen. For now this is only done when children are added or |
278 | // removed, but we might do it for attribute changes too in the future. |
279 | static void DidMutate() { sGeneration++; } |
280 | |
281 | private: |
282 | // This is the value sGeneration had when the guard was constructed. |
283 | uint64_t mStartingGeneration; |
284 | |
285 | // This value is incremented on every mutation, for the life of the process. |
286 | static uint64_t sGeneration; |
287 | }; |
288 | |
289 | /** |
290 | * A class that implements nsIWeakReference |
291 | */ |
292 | class nsNodeWeakReference final : public nsIWeakReference { |
293 | public: |
294 | explicit nsNodeWeakReference(nsINode* aNode); |
295 | |
296 | // nsISupports |
297 | NS_DECL_ISUPPORTSpublic: virtual nsresult QueryInterface(const nsIID& aIID , void** aInstancePtr) override; virtual MozExternalRefCountType AddRef(void) override; virtual MozExternalRefCountType Release (void) override; using HasThreadSafeRefCnt = std::false_type; protected: nsAutoRefCnt mRefCnt; nsAutoOwningThread _mOwningThread ; public: |
298 | |
299 | // nsIWeakReference |
300 | NS_DECL_NSIWEAKREFERENCEvirtual nsresult QueryReferentFromScript(const nsIID & uuid , void * * result) override; virtual size_t SizeOfOnlyThis(mozilla ::MallocSizeOf aMallocSizeOf) override; |
301 | |
302 | void NoticeNodeDestruction() { mObject = nullptr; } |
303 | |
304 | private: |
305 | ~nsNodeWeakReference(); |
306 | }; |
307 | |
308 | // This should be used for any nsINode sub-class that has fields of its own |
309 | // that it needs to measure; any sub-class that doesn't use it will inherit |
310 | // AddSizeOfExcludingThis from its super-class. AddSizeOfIncludingThis() need |
311 | // not be defined, it is inherited from nsINode. |
312 | #define NS_DECL_ADDSIZEOFEXCLUDINGTHISvirtual void AddSizeOfExcludingThis(nsWindowSizes& aSizes , size_t* aNodeSize) const override; \ |
313 | virtual void AddSizeOfExcludingThis(nsWindowSizes& aSizes, \ |
314 | size_t* aNodeSize) const override; |
315 | |
316 | // IID for the nsINode interface |
317 | // Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs |
318 | #define NS_INODE_IID{ 0x70ba4547, 0x7699, 0x44fc, { 0xb3, 0x20, 0x52, 0xdb, 0xe3, 0xd1, 0xf9, 0x0a } } \ |
319 | { \ |
320 | 0x70ba4547, 0x7699, 0x44fc, { \ |
321 | 0xb3, 0x20, 0x52, 0xdb, 0xe3, 0xd1, 0xf9, 0x0a \ |
322 | } \ |
323 | } |
324 | |
325 | /** |
326 | * An internal interface that abstracts some DOMNode-related parts that both |
327 | * nsIContent and Document share. An instance of this interface has a list |
328 | * of nsIContent children and provides access to them. |
329 | */ |
330 | class nsINode : public mozilla::dom::EventTarget { |
331 | #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED1 |
332 | void AssertInvariantsOnNodeInfoChange(); |
333 | #endif |
334 | public: |
335 | using BoxQuadOptions = mozilla::dom::BoxQuadOptions; |
336 | using ConvertCoordinateOptions = mozilla::dom::ConvertCoordinateOptions; |
337 | using DocGroup = mozilla::dom::DocGroup; |
338 | using Document = mozilla::dom::Document; |
339 | using DOMPoint = mozilla::dom::DOMPoint; |
340 | using DOMPointInit = mozilla::dom::DOMPointInit; |
341 | using DOMQuad = mozilla::dom::DOMQuad; |
342 | using DOMRectReadOnly = mozilla::dom::DOMRectReadOnly; |
343 | using OwningNodeOrString = mozilla::dom::OwningNodeOrString; |
344 | using TextOrElementOrDocument = mozilla::dom::TextOrElementOrDocument; |
345 | using CallerType = mozilla::dom::CallerType; |
346 | using ErrorResult = mozilla::ErrorResult; |
347 | |
348 | // XXXbz Maybe we should codegen a class holding these constants and |
349 | // inherit from it... |
350 | static const auto ELEMENT_NODE = mozilla::dom::Node_Binding::ELEMENT_NODE; |
351 | static const auto ATTRIBUTE_NODE = mozilla::dom::Node_Binding::ATTRIBUTE_NODE; |
352 | static const auto TEXT_NODE = mozilla::dom::Node_Binding::TEXT_NODE; |
353 | static const auto CDATA_SECTION_NODE = |
354 | mozilla::dom::Node_Binding::CDATA_SECTION_NODE; |
355 | static const auto ENTITY_REFERENCE_NODE = |
356 | mozilla::dom::Node_Binding::ENTITY_REFERENCE_NODE; |
357 | static const auto ENTITY_NODE = mozilla::dom::Node_Binding::ENTITY_NODE; |
358 | static const auto PROCESSING_INSTRUCTION_NODE = |
359 | mozilla::dom::Node_Binding::PROCESSING_INSTRUCTION_NODE; |
360 | static const auto COMMENT_NODE = mozilla::dom::Node_Binding::COMMENT_NODE; |
361 | static const auto DOCUMENT_NODE = mozilla::dom::Node_Binding::DOCUMENT_NODE; |
362 | static const auto DOCUMENT_TYPE_NODE = |
363 | mozilla::dom::Node_Binding::DOCUMENT_TYPE_NODE; |
364 | static const auto DOCUMENT_FRAGMENT_NODE = |
365 | mozilla::dom::Node_Binding::DOCUMENT_FRAGMENT_NODE; |
366 | static const auto NOTATION_NODE = mozilla::dom::Node_Binding::NOTATION_NODE; |
367 | static const auto MAX_NODE_TYPE = NOTATION_NODE; |
368 | |
369 | void* operator new(size_t aSize, nsNodeInfoManager* aManager); |
370 | void* operator new(size_t aSize) = delete; |
371 | void operator delete(void* aPtr); |
372 | |
373 | template <class T> |
374 | using Sequence = mozilla::dom::Sequence<T>; |
375 | |
376 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_INODE_IID)template <typename T, typename U> struct COMTypeInfo; |
377 | |
378 | // The |aNodeSize| outparam on this function is where the actual node size |
379 | // value is put. It gets added to the appropriate value within |aSizes| by |
380 | // AddSizeOfNodeTree(). |
381 | // |
382 | // Among the sub-classes that inherit (directly or indirectly) from nsINode, |
383 | // measurement of the following members may be added later if DMD finds it is |
384 | // worthwhile: |
385 | // - nsGenericHTMLElement: mForm, mFieldSet |
386 | // - nsGenericHTMLFrameElement: mFrameLoader (bug 672539) |
387 | // - HTMLBodyElement: mContentStyleRule |
388 | // - HTMLDataListElement: mOptions |
389 | // - HTMLFieldSetElement: mElements, mDependentElements, mFirstLegend |
390 | // - HTMLFormElement: many! |
391 | // - HTMLFrameSetElement: mRowSpecs, mColSpecs |
392 | // - HTMLInputElement: mInputData, mFiles, mFileList, mStaticDocfileList |
393 | // - nsHTMLMapElement: mAreas |
394 | // - HTMLMediaElement: many! |
395 | // - nsHTMLOutputElement: mDefaultValue, mTokenList |
396 | // - nsHTMLRowElement: mCells |
397 | // - nsHTMLSelectElement: mOptions, mRestoreState |
398 | // - nsHTMLTableElement: mTBodies, mRows, mTableInheritedAttributes |
399 | // - nsHTMLTableSectionElement: mRows |
400 | // - nsHTMLTextAreaElement: mControllers, mState |
401 | // |
402 | // The following members don't need to be measured: |
403 | // - nsIContent: mPrimaryFrame, because it's non-owning and measured elsewhere |
404 | // |
405 | virtual void AddSizeOfExcludingThis(nsWindowSizes& aSizes, |
406 | size_t* aNodeSize) const; |
407 | |
408 | // SizeOfIncludingThis doesn't need to be overridden by sub-classes because |
409 | // sub-classes of nsINode are guaranteed to be laid out in memory in such a |
410 | // way that |this| points to the start of the allocated object, even in |
411 | // methods of nsINode's sub-classes, so aSizes.mState.mMallocSizeOf(this) is |
412 | // always safe to call no matter which object it was invoked on. |
413 | void AddSizeOfIncludingThis(nsWindowSizes& aSizes, size_t* aNodeSize) const; |
414 | |
415 | friend class nsNodeWeakReference; |
416 | friend class nsNodeSupportsWeakRefTearoff; |
417 | friend class AttrArray; |
418 | |
419 | #ifdef MOZILLA_INTERNAL_API1 |
420 | explicit nsINode(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); |
421 | #endif |
422 | |
423 | virtual ~nsINode(); |
424 | |
425 | bool IsContainerNode() const { |
426 | return IsElement() || IsDocument() || IsDocumentFragment(); |
427 | } |
428 | |
429 | /** |
430 | * Returns true if the node is a HTMLTemplate element. |
431 | */ |
432 | bool IsTemplateElement() const { return IsHTMLElement(nsGkAtoms::_template); } |
433 | |
434 | bool IsSlotable() const { return IsElement() || IsText(); } |
435 | |
436 | /** |
437 | * Returns true if this is a document node. |
438 | */ |
439 | bool IsDocument() const { |
440 | // One less pointer-chase than checking NodeType(). |
441 | return !GetParentNode() && IsInUncomposedDoc(); |
442 | } |
443 | |
444 | /** |
445 | * Return this node as a document. Asserts IsDocument(). |
446 | * |
447 | * This is defined inline in Document.h. |
448 | */ |
449 | inline Document* AsDocument(); |
450 | inline const Document* AsDocument() const; |
451 | |
452 | /** |
453 | * Returns true if this is a document fragment node. |
454 | */ |
455 | bool IsDocumentFragment() const { |
456 | return NodeType() == DOCUMENT_FRAGMENT_NODE; |
457 | } |
458 | |
459 | virtual bool IsHTMLFormControlElement() const { return false; } |
460 | |
461 | /** |
462 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-descendant |
463 | * |
464 | * @param aNode must not be nullptr. |
465 | */ |
466 | bool IsInclusiveDescendantOf(const nsINode* aNode) const; |
467 | |
468 | /** |
469 | * https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant |
470 | * |
471 | * @param aNode must not be nullptr. |
472 | */ |
473 | bool IsShadowIncludingInclusiveDescendantOf(const nsINode* aNode) const; |
474 | |
475 | /** |
476 | * Returns true if the given node is this node or one of its descendants |
477 | * in the "flat tree." |
478 | * |
479 | * @param aNode must not be nullptr. |
480 | */ |
481 | bool IsInclusiveFlatTreeDescendantOf(const nsINode* aNode) const; |
482 | |
483 | /** |
484 | * Return this node as a document fragment. Asserts IsDocumentFragment(). |
485 | * |
486 | * This is defined inline in DocumentFragment.h. |
487 | */ |
488 | inline mozilla::dom::DocumentFragment* AsDocumentFragment(); |
489 | inline const mozilla::dom::DocumentFragment* AsDocumentFragment() const; |
490 | |
491 | JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) final; |
492 | |
493 | /** |
494 | * Hook for constructing JS::ubi::Concrete specializations for memory |
495 | * reporting. Specializations are defined in NodeUbiReporting.h. |
496 | */ |
497 | virtual void ConstructUbiNode(void* storage) = 0; |
498 | |
499 | /** |
500 | * returns true if we are in priviliged code or |
501 | * layout.css.getBoxQuads.enabled == true. |
502 | */ |
503 | static bool HasBoxQuadsSupport(JSContext* aCx, JSObject* /* unused */); |
504 | |
505 | protected: |
506 | /** |
507 | * WrapNode is called from WrapObject to actually wrap this node, WrapObject |
508 | * does some additional checks and fix-up that's common to all nodes. WrapNode |
509 | * should just call the DOM binding's Wrap function. |
510 | * |
511 | * aGivenProto is the prototype to use (or null if the default one should be |
512 | * used) and should just be passed directly on to the DOM binding's Wrap |
513 | * function. |
514 | */ |
515 | virtual JSObject* WrapNode(JSContext* aCx, |
516 | JS::Handle<JSObject*> aGivenProto) = 0; |
517 | |
518 | public: |
519 | mozilla::dom::ParentObject GetParentObject() |
520 | const; // Implemented in Document.h |
521 | |
522 | /** |
523 | * Returns the first child of a node or the first child of |
524 | * a template element's content if the provided node is a |
525 | * template element. |
526 | */ |
527 | nsIContent* GetFirstChildOfTemplateOrNode(); |
528 | |
529 | /** |
530 | * Return the scope chain parent for this node, for use in things |
531 | * like event handler compilation. Returning null means to use the |
532 | * global object as the scope chain parent. |
533 | */ |
534 | virtual nsINode* GetScopeChainParent() const; |
535 | |
536 | MOZ_CAN_RUN_SCRIPT mozilla::dom::Element* GetParentFlexElement(); |
537 | |
538 | /** |
539 | * Returns the nearest inclusive open popover for a given node, see |
540 | * https://html.spec.whatwg.org/multipage/popover.html#nearest-inclusive-open-popover |
541 | */ |
542 | mozilla::dom::Element* GetNearestInclusiveOpenPopover() const; |
543 | |
544 | /** |
545 | * https://html.spec.whatwg.org/multipage/popover.html#nearest-inclusive-target-popover-for-invoker |
546 | */ |
547 | mozilla::dom::Element* GetNearestInclusiveTargetPopoverForInvoker() const; |
548 | |
549 | nsGenericHTMLElement* GetEffectiveInvokeTargetElement() const; |
550 | |
551 | /** |
552 | * https://html.spec.whatwg.org/multipage/popover.html#popover-target-element |
553 | */ |
554 | nsGenericHTMLElement* GetEffectivePopoverTargetElement() const; |
555 | |
556 | /** |
557 | * https://html.spec.whatwg.org/multipage/popover.html#topmost-clicked-popover |
558 | */ |
559 | mozilla::dom::Element* GetTopmostClickedPopover() const; |
560 | |
561 | bool IsNode() const final { return true; } |
562 | |
563 | NS_IMPL_FROMEVENTTARGET_HELPER(nsINode, IsNode())template <typename T> static auto FromEventTarget( T& aEventTarget) -> decltype(static_cast< nsINode*>(& aEventTarget)) { return aEventTarget.IsNode() ? static_cast< nsINode*>(&aEventTarget) : nullptr; } template <typename T> static nsINode* FromEventTarget( T* aEventTarget) { do { static_assert( mozilla::detail::AssertionConditionType< decltype(aEventTarget)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aEventTarget))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aEventTarget", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 563); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "aEventTarget" ")"); do { *((volatile int*)__null) = 563; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); return FromEventTarget (*aEventTarget); } template <typename T> static nsINode * FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; } template <typename T> static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const nsINode*>(&aEventTarget )) { return aEventTarget.IsNode() ? static_cast<const nsINode *>(&aEventTarget) : nullptr; } template <typename T > static const nsINode* FromEventTarget(const T* aEventTarget ) { do { static_assert( mozilla::detail::AssertionConditionType <decltype(aEventTarget)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aEventTarget))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aEventTarget", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 563); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "aEventTarget" ")"); do { *((volatile int*)__null) = 563; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); return FromEventTarget (*aEventTarget); } template <typename T> static const nsINode * FromEventTargetOrNull(const T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; } template <typename T> static nsINode* FromEventTarget(T&& aEventTarget ) { do { static_assert( mozilla::detail::AssertionConditionType <decltype(!!aEventTarget)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!!aEventTarget))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!!aEventTarget" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 563); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "!!aEventTarget" ")"); do { *((volatile int*)__null) = 563; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); return aEventTarget ->IsNode() ? static_cast<nsINode*>(static_cast<EventTarget *>(aEventTarget)) : nullptr; } template <typename T> static nsINode* FromEventTargetOrNull(T&& aEventTarget ) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr ; } |
564 | |
565 | /** |
566 | * Return whether the node is an Element node. Faster than using `NodeType()`. |
567 | */ |
568 | bool IsElement() const { return GetBoolFlag(NodeIsElement); } |
569 | |
570 | virtual bool IsTextControlElement() const { return false; } |
571 | virtual bool IsGenericHTMLFormControlElementWithState() const { |
572 | return false; |
573 | } |
574 | |
575 | // Returns non-null if this element subclasses `LinkStyle`. |
576 | virtual const mozilla::dom::LinkStyle* AsLinkStyle() const { return nullptr; } |
577 | mozilla::dom::LinkStyle* AsLinkStyle() { |
578 | return const_cast<mozilla::dom::LinkStyle*>( |
579 | static_cast<const nsINode*>(this)->AsLinkStyle()); |
580 | } |
581 | |
582 | /** |
583 | * Return this node as an Element. Should only be used for nodes |
584 | * for which IsElement() is true. This is defined inline in Element.h. |
585 | */ |
586 | inline mozilla::dom::Element* AsElement(); |
587 | inline const mozilla::dom::Element* AsElement() const; |
588 | |
589 | /** |
590 | * Return whether the node is an nsStyledElement instance or not. |
591 | */ |
592 | virtual bool IsStyledElement() const { return false; } |
593 | |
594 | /** |
595 | * Return this node as nsIContent. Should only be used for nodes for which |
596 | * IsContent() is true. |
597 | * |
598 | * The assertion in nsIContent's constructor makes this safe. |
599 | */ |
600 | nsIContent* AsContent() { |
601 | MOZ_ASSERT(IsContent())do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsContent())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsContent()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsContent()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 601); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsContent()" ")"); do { *((volatile int*)__null) = 601; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
602 | return reinterpret_cast<nsIContent*>(this); |
603 | } |
604 | const nsIContent* AsContent() const { |
605 | MOZ_ASSERT(IsContent())do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsContent())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsContent()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsContent()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 605); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsContent()" ")"); do { *((volatile int*)__null) = 605; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
606 | return reinterpret_cast<const nsIContent*>(this); |
607 | } |
608 | |
609 | /* |
610 | * Return whether the node is a Text node (which might be an actual |
611 | * textnode, or might be a CDATA section). |
612 | */ |
613 | bool IsText() const { |
614 | uint32_t nodeType = NodeType(); |
615 | return nodeType == TEXT_NODE || nodeType == CDATA_SECTION_NODE; |
616 | } |
617 | |
618 | /** |
619 | * Return this node as Text if it is one, otherwise null. This is defined |
620 | * inline in Text.h. |
621 | */ |
622 | inline mozilla::dom::Text* GetAsText(); |
623 | inline const mozilla::dom::Text* GetAsText() const; |
624 | |
625 | /** |
626 | * Return this node as Text. Asserts IsText(). This is defined inline in |
627 | * Text.h. |
628 | */ |
629 | inline mozilla::dom::Text* AsText(); |
630 | inline const mozilla::dom::Text* AsText() const; |
631 | |
632 | /** |
633 | * Return this node if the instance type inherits nsIFormControl, or an |
634 | * nsIFormControl instance which ia associated with this node. Otherwise, |
635 | * returns nullptr. |
636 | */ |
637 | [[nodiscard]] virtual nsIFormControl* GetAsFormControl() { return nullptr; } |
638 | [[nodiscard]] virtual const nsIFormControl* GetAsFormControl() const { |
639 | return nullptr; |
640 | } |
641 | |
642 | /* |
643 | * Return whether the node is a ProcessingInstruction node. |
644 | */ |
645 | bool IsProcessingInstruction() const { |
646 | return NodeType() == PROCESSING_INSTRUCTION_NODE; |
647 | } |
648 | |
649 | /* |
650 | * Return whether the node is a CharacterData node (text, cdata, |
651 | * comment, processing instruction) |
652 | */ |
653 | bool IsCharacterData() const { |
654 | uint32_t nodeType = NodeType(); |
655 | return nodeType == TEXT_NODE || nodeType == CDATA_SECTION_NODE || |
656 | nodeType == PROCESSING_INSTRUCTION_NODE || nodeType == COMMENT_NODE; |
657 | } |
658 | |
659 | /** |
660 | * Return whether the node is a Comment node. |
661 | */ |
662 | bool IsComment() const { return NodeType() == COMMENT_NODE; } |
663 | |
664 | /** |
665 | * Return whether the node is an Attr node. |
666 | */ |
667 | bool IsAttr() const { return NodeType() == ATTRIBUTE_NODE; } |
668 | |
669 | /** |
670 | * Return if this node has any children. |
671 | */ |
672 | bool HasChildren() const { return !!mFirstChild; } |
673 | |
674 | /** |
675 | * Get the number of children |
676 | * @return the number of children |
677 | */ |
678 | uint32_t GetChildCount() const { return mChildCount; } |
679 | |
680 | /** |
681 | * NOTE: this function is going to be removed soon (hopefully!) Don't use it |
682 | * in new code. |
683 | * |
684 | * Get a child by index |
685 | * @param aIndex the index of the child to get |
686 | * @return the child, or null if index out of bounds |
687 | */ |
688 | nsIContent* GetChildAt_Deprecated(uint32_t aIndex) const; |
689 | |
690 | /** |
691 | * Get the index of a child within this content. |
692 | * |
693 | * @param aPossibleChild the child to get the index of. |
694 | * @return the index of the child, or Nothing if not a child. Be aware that |
695 | * anonymous children (e.g. a <div> child of an <input> element) will |
696 | * result in Nothing. |
697 | * |
698 | * If the return value is Some, then calling GetChildAt_Deprecated() with |
699 | * that value will return aPossibleChild. |
700 | */ |
701 | mozilla::Maybe<uint32_t> ComputeIndexOf(const nsINode* aPossibleChild) const; |
702 | |
703 | /** |
704 | * Get the index of a child within this content's flat tree children. |
705 | * |
706 | * @param aPossibleChild the child to get the index of. |
707 | * @return the index of the child, or Nothing if not a child. Be aware that |
708 | * anonymous children (e.g. a <div> child of an <input> element) will |
709 | * result in Nothing. |
710 | */ |
711 | mozilla::Maybe<uint32_t> ComputeFlatTreeIndexOf( |
712 | const nsINode* aPossibleChild) const; |
713 | |
714 | /** |
715 | * Get the index of this within parent node (ComputeIndexInParentNode) or |
716 | * parent content (nsIContent) node (ComputeIndexInParentContent). |
717 | * |
718 | * @return the index of this node in the parent, or Nothing there is no |
719 | * parent (content) node or the parent does not have this node anymore |
720 | * (e.g., being removed from the parent). Be aware that anonymous |
721 | * children (e.g. a <div> child of an <input> element) will result in |
722 | * Nothing. |
723 | * |
724 | * If the return value is Some, then calling GetChildAt_Deprecated() with |
725 | * that value will return this. |
726 | */ |
727 | mozilla::Maybe<uint32_t> ComputeIndexInParentNode() const; |
728 | mozilla::Maybe<uint32_t> ComputeIndexInParentContent() const; |
729 | |
730 | /** |
731 | * Get the index of a child within this content. |
732 | * |
733 | * @param aPossibleChild the child to get the index of. |
734 | * @return the index of the child, or -1 if not a child. Be aware that |
735 | * anonymous children (e.g. a <div> child of an <input> element) will |
736 | * result in -1. |
737 | * |
738 | * If the return value is not -1, then calling GetChildAt_Deprecated() with |
739 | * that value will return aPossibleChild. |
740 | */ |
741 | int32_t ComputeIndexOf_Deprecated(const nsINode* aPossibleChild) const; |
742 | |
743 | /** |
744 | * Returns the "node document" of this node. |
745 | * |
746 | * https://dom.spec.whatwg.org/#concept-node-document |
747 | * |
748 | * Note that in the case that this node is a document node this method |
749 | * will return |this|. That is different to the Node.ownerDocument DOM |
750 | * attribute (implemented by nsINode::GetOwnerDocument) which is specified to |
751 | * be null in that case: |
752 | * |
753 | * https://dom.spec.whatwg.org/#dom-node-ownerdocument |
754 | * |
755 | * For all other cases OwnerDoc and GetOwnerDocument behave identically. |
756 | */ |
757 | Document* OwnerDoc() const MOZ_NONNULL_RETURN__attribute__((returns_nonnull)) { |
758 | return mNodeInfo->GetDocument(); |
759 | } |
760 | |
761 | /** |
762 | * Return the "owner document" of this node as an nsINode*. Implemented |
763 | * in Document.h. |
764 | */ |
765 | inline nsINode* OwnerDocAsNode() const MOZ_NONNULL_RETURN__attribute__((returns_nonnull)); |
766 | |
767 | /** |
768 | * Returns true if the content has an ancestor that is a document. |
769 | * |
770 | * @return whether this content is in a document tree |
771 | */ |
772 | bool IsInUncomposedDoc() const { return GetBoolFlag(IsInDocument); } |
773 | |
774 | /** |
775 | * Get the document that this content is currently in, if any. This will be |
776 | * null if the content has no ancestor that is a document. |
777 | * |
778 | * @return the current document |
779 | */ |
780 | |
781 | Document* GetUncomposedDoc() const { |
782 | return IsInUncomposedDoc() ? OwnerDoc() : nullptr; |
783 | } |
784 | |
785 | /** |
786 | * Returns true if we're connected, and thus GetComposedDoc() would return a |
787 | * non-null value. |
788 | */ |
789 | bool IsInComposedDoc() const { return GetBoolFlag(IsConnected); } |
790 | |
791 | /** |
792 | * This method returns the owner document if the node is connected to it |
793 | * (as defined in the DOM spec), otherwise it returns null. |
794 | * In other words, returns non-null even in the case the node is in |
795 | * Shadow DOM, if there is a possibly shadow boundary crossing path from |
796 | * the node to its owner document. |
797 | */ |
798 | Document* GetComposedDoc() const { |
799 | return IsInComposedDoc() ? OwnerDoc() : nullptr; |
800 | } |
801 | |
802 | /** |
803 | * Returns OwnerDoc() if the node is in uncomposed document and ShadowRoot if |
804 | * the node is in Shadow DOM. |
805 | */ |
806 | mozilla::dom::DocumentOrShadowRoot* GetContainingDocumentOrShadowRoot() const; |
807 | |
808 | /** |
809 | * Returns OwnerDoc() if the node is in uncomposed document and ShadowRoot if |
810 | * the node is in Shadow DOM and is in composed document. |
811 | */ |
812 | mozilla::dom::DocumentOrShadowRoot* GetUncomposedDocOrConnectedShadowRoot() |
813 | const; |
814 | |
815 | /** |
816 | * To be called when reference count of the node drops to zero. |
817 | */ |
818 | void LastRelease(); |
819 | |
820 | /** |
821 | * The values returned by this function are the ones defined for |
822 | * Node.nodeType |
823 | */ |
824 | uint16_t NodeType() const { return mNodeInfo->NodeType(); } |
825 | const nsString& NodeName() const { return mNodeInfo->NodeName(); } |
826 | const nsString& LocalName() const { return mNodeInfo->LocalName(); } |
827 | |
828 | /** |
829 | * Get the NodeInfo for this element |
830 | * @return the nodes node info |
831 | */ |
832 | inline mozilla::dom::NodeInfo* NodeInfo() const { return mNodeInfo; } |
833 | |
834 | /** |
835 | * Called when we have been adopted, and the information of the |
836 | * node has been changed. |
837 | * |
838 | * The new document can be reached via OwnerDoc(). |
839 | * |
840 | * If you override this method, |
841 | * please call up to the parent NodeInfoChanged. |
842 | * |
843 | * If you change this, change also the similar method in Link. |
844 | */ |
845 | virtual void NodeInfoChanged(Document* aOldDoc) { |
846 | #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED1 |
847 | AssertInvariantsOnNodeInfoChange(); |
848 | #endif |
849 | } |
850 | |
851 | inline bool IsInNamespace(int32_t aNamespace) const { |
852 | return mNodeInfo->NamespaceID() == aNamespace; |
853 | } |
854 | |
855 | /** |
856 | * Returns the DocGroup of the "node document" of this node. |
857 | */ |
858 | DocGroup* GetDocGroup() const; |
859 | |
860 | /** |
861 | * Print a debugger friendly descriptor of this element. This will describe |
862 | * the position of this element in the document. |
863 | */ |
864 | friend std::ostream& operator<<(std::ostream& aStream, const nsINode& aNode); |
865 | |
866 | protected: |
867 | // These 2 methods are useful for the recursive templates IsHTMLElement, |
868 | // IsSVGElement, etc. |
869 | inline bool IsNodeInternal() const { return false; } |
870 | |
871 | template <typename First, typename... Args> |
872 | inline bool IsNodeInternal(First aFirst, Args... aArgs) const { |
873 | return mNodeInfo->Equals(aFirst) || IsNodeInternal(aArgs...); |
874 | } |
875 | |
876 | public: |
877 | inline bool IsHTMLElement() const { |
878 | return IsElement() && IsInNamespace(kNameSpaceID_XHTML3); |
879 | } |
880 | |
881 | inline bool IsHTMLElement(const nsAtom* aTag) const { |
882 | return IsElement() && mNodeInfo->Equals(aTag, kNameSpaceID_XHTML3); |
883 | } |
884 | |
885 | template <typename First, typename... Args> |
886 | inline bool IsAnyOfHTMLElements(First aFirst, Args... aArgs) const { |
887 | return IsHTMLElement() && IsNodeInternal(aFirst, aArgs...); |
888 | } |
889 | |
890 | inline bool IsSVGElement() const { |
891 | return IsElement() && IsInNamespace(kNameSpaceID_SVG9); |
892 | } |
893 | |
894 | inline bool IsSVGElement(const nsAtom* aTag) const { |
895 | return IsElement() && mNodeInfo->Equals(aTag, kNameSpaceID_SVG9); |
896 | } |
897 | |
898 | template <typename First, typename... Args> |
899 | inline bool IsAnyOfSVGElements(First aFirst, Args... aArgs) const { |
900 | return IsSVGElement() && IsNodeInternal(aFirst, aArgs...); |
901 | } |
902 | |
903 | virtual bool IsSVGAnimationElement() const { return false; } |
904 | virtual bool IsSVGComponentTransferFunctionElement() const { return false; } |
905 | virtual bool IsSVGFilterPrimitiveElement() const { return false; } |
906 | virtual bool IsSVGFilterPrimitiveChildElement() const { return false; } |
907 | virtual bool IsSVGGeometryElement() const { return false; } |
908 | virtual bool IsSVGGraphicsElement() const { return false; } |
909 | |
910 | inline bool IsXULElement() const { |
911 | return IsElement() && IsInNamespace(kNameSpaceID_XUL8); |
912 | } |
913 | |
914 | inline bool IsXULElement(const nsAtom* aTag) const { |
915 | return IsElement() && mNodeInfo->Equals(aTag, kNameSpaceID_XUL8); |
916 | } |
917 | |
918 | template <typename First, typename... Args> |
919 | inline bool IsAnyOfXULElements(First aFirst, Args... aArgs) const { |
920 | return IsXULElement() && IsNodeInternal(aFirst, aArgs...); |
921 | } |
922 | |
923 | inline bool IsMathMLElement() const { |
924 | return IsElement() && IsInNamespace(kNameSpaceID_MathML6); |
925 | } |
926 | |
927 | inline bool IsMathMLElement(const nsAtom* aTag) const { |
928 | return IsElement() && mNodeInfo->Equals(aTag, kNameSpaceID_MathML6); |
929 | } |
930 | |
931 | template <typename First, typename... Args> |
932 | inline bool IsAnyOfMathMLElements(First aFirst, Args... aArgs) const { |
933 | return IsMathMLElement() && IsNodeInternal(aFirst, aArgs...); |
934 | } |
935 | |
936 | bool IsShadowRoot() const { |
937 | const bool isShadowRoot = IsInShadowTree() && !GetParentNode(); |
938 | MOZ_ASSERT_IF(isShadowRoot, IsDocumentFragment())do { if (isShadowRoot) { do { static_assert( mozilla::detail:: AssertionConditionType<decltype(IsDocumentFragment())>:: isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(IsDocumentFragment()))), 0))) { do { } while (false) ; MOZ_ReportAssertionFailure("IsDocumentFragment()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 938); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsDocumentFragment()" ")"); do { *((volatile int*)__null) = 938; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); |
939 | return isShadowRoot; |
940 | } |
941 | |
942 | bool IsHTMLHeadingElement() const { |
943 | return IsAnyOfHTMLElements(nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3, |
944 | nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6); |
945 | } |
946 | |
947 | /** |
948 | * Check whether the conditional processing attributes other than |
949 | * systemLanguage "return true" if they apply to and are specified |
950 | * on the given SVG element. Returns true if this element should be |
951 | * rendered, false if it should not. |
952 | */ |
953 | virtual bool PassesConditionalProcessingTests() const { return true; } |
954 | |
955 | /** |
956 | * Insert a content node before another or at the end. |
957 | * This method handles calling BindToTree on the child appropriately. |
958 | * |
959 | * @param aKid the content to insert |
960 | * @param aBeforeThis an existing node. Use nullptr if you want to |
961 | * add aKid at the end. |
962 | * @param aNotify whether to notify the document (current document for |
963 | * nsIContent, and |this| for Document) that the insert has occurred |
964 | * @param aRv The error, if any. |
965 | * Throw NS_ERROR_DOM_HIERARCHY_REQUEST_ERR if one attempts to have |
966 | * more than one element node as a child of a document. Doing this |
967 | * will also assert -- you shouldn't be doing it! Check with |
968 | * Document::GetRootElement() first if you're not sure. Apart from |
969 | * this one constraint, this doesn't do any checking on whether aKid is |
970 | * a valid child of |this|. |
971 | * Throw NS_ERROR_OUT_OF_MEMORY in some cases (from BindToTree). |
972 | */ |
973 | virtual void InsertChildBefore(nsIContent* aKid, nsIContent* aBeforeThis, |
974 | bool aNotify, mozilla::ErrorResult& aRv); |
975 | |
976 | /** |
977 | * Append a content node to the end of the child list. This method handles |
978 | * calling BindToTree on the child appropriately. |
979 | * |
980 | * @param aKid the content to append |
981 | * @param aNotify whether to notify the document (current document for |
982 | * nsIContent, and |this| for Document) that the append has occurred |
983 | * @param aRv The error, if any. |
984 | * Throw NS_ERROR_DOM_HIERARCHY_REQUEST_ERR if one attempts to have |
985 | * more than one element node as a child of a document. Doing this |
986 | * will also assert -- you shouldn't be doing it! Check with |
987 | * Document::GetRootElement() first if you're not sure. Apart from |
988 | * this one constraint, this doesn't do any checking on whether aKid is |
989 | * a valid child of |this|. |
990 | * Throw NS_ERROR_OUT_OF_MEMORY in some cases (from BindToTree). |
991 | */ |
992 | void AppendChildTo(nsIContent* aKid, bool aNotify, |
993 | mozilla::ErrorResult& aRv) { |
994 | InsertChildBefore(aKid, nullptr, aNotify, aRv); |
995 | } |
996 | |
997 | /** |
998 | * Remove a child from this node. This method handles calling UnbindFromTree |
999 | * on the child appropriately. |
1000 | * |
1001 | * @param aKid the content to remove |
1002 | * @param aNotify whether to notify the document (current document for |
1003 | * nsIContent, and |this| for Document) that the remove has occurred |
1004 | */ |
1005 | virtual void RemoveChildNode(nsIContent* aKid, bool aNotify); |
1006 | |
1007 | /** |
1008 | * Get a property associated with this node. |
1009 | * |
1010 | * @param aPropertyName name of property to get. |
1011 | * @param aStatus out parameter for storing resulting status. |
1012 | * Set to NS_PROPTABLE_PROP_NOT_THERE if the property |
1013 | * is not set. |
1014 | * @return the property. Null if the property is not set |
1015 | * (though a null return value does not imply the |
1016 | * property was not set, i.e. it can be set to null). |
1017 | */ |
1018 | void* GetProperty(const nsAtom* aPropertyName, |
1019 | nsresult* aStatus = nullptr) const; |
1020 | |
1021 | /** |
1022 | * Set a property to be associated with this node. This will overwrite an |
1023 | * existing value if one exists. The existing value is destroyed using the |
1024 | * destructor function given when that value was set. |
1025 | * |
1026 | * @param aPropertyName name of property to set. |
1027 | * @param aValue new value of property. |
1028 | * @param aDtor destructor function to be used when this property |
1029 | * is destroyed. |
1030 | * @param aTransfer if true the property will not be deleted when the |
1031 | * ownerDocument of the node changes, if false it |
1032 | * will be deleted. |
1033 | * |
1034 | * @return NS_PROPTABLE_PROP_OVERWRITTEN (success value) if the property |
1035 | * was already set |
1036 | * @throws NS_ERROR_OUT_OF_MEMORY if that occurs |
1037 | */ |
1038 | nsresult SetProperty(nsAtom* aPropertyName, void* aValue, |
1039 | NSPropertyDtorFunc aDtor = nullptr, |
1040 | bool aTransfer = false); |
1041 | |
1042 | /** |
1043 | * A generic destructor for property values allocated with new. |
1044 | */ |
1045 | template <class T> |
1046 | static void DeleteProperty(void*, nsAtom*, void* aPropertyValue, void*) { |
1047 | delete static_cast<T*>(aPropertyValue); |
1048 | } |
1049 | |
1050 | /** |
1051 | * Removes a property associated with this node. The value is destroyed using |
1052 | * the destruction function given when that value was set. |
1053 | * |
1054 | * @param aPropertyName name of property to destroy. |
1055 | */ |
1056 | void RemoveProperty(const nsAtom* aPropertyName); |
1057 | |
1058 | /** |
1059 | * Take a property associated with this node. The value will not be destroyed |
1060 | * but rather returned. It is the caller's responsibility to destroy the value |
1061 | * after that point. |
1062 | * |
1063 | * @param aPropertyName name of property to unset. |
1064 | * @param aStatus out parameter for storing resulting status. |
1065 | * Set to NS_PROPTABLE_PROP_NOT_THERE if the property |
1066 | * is not set. |
1067 | * @return the property. Null if the property is not set |
1068 | * (though a null return value does not imply the |
1069 | * property was not set, i.e. it can be set to null). |
1070 | */ |
1071 | void* TakeProperty(const nsAtom* aPropertyName, nsresult* aStatus = nullptr); |
1072 | |
1073 | bool HasProperties() const { return HasFlag(NODE_HAS_PROPERTIES); } |
1074 | |
1075 | /** |
1076 | * Return the principal of this node. This is guaranteed to never be a null |
1077 | * pointer. |
1078 | */ |
1079 | nsIPrincipal* NodePrincipal() const { |
1080 | return mNodeInfo->NodeInfoManager()->DocumentPrincipal(); |
1081 | } |
1082 | |
1083 | /** |
1084 | * Return the CSP of this node's document, if any. |
1085 | */ |
1086 | nsIContentSecurityPolicy* GetCsp() const; |
1087 | |
1088 | /** |
1089 | * Get the parent nsIContent for this node. |
1090 | * @return the parent, or null if no parent or the parent is not an nsIContent |
1091 | */ |
1092 | nsIContent* GetParent() const { |
1093 | return MOZ_LIKELY(GetBoolFlag(ParentIsContent))(__builtin_expect(!!(GetBoolFlag(ParentIsContent)), 1)) ? mParent->AsContent() |
1094 | : nullptr; |
1095 | } |
1096 | |
1097 | /** |
1098 | * Get the parent nsINode for this node. This can be either an nsIContent, a |
1099 | * Document or an Attr. |
1100 | * @return the parent node |
1101 | */ |
1102 | nsINode* GetParentNode() const { return mParent; } |
1103 | |
1104 | private: |
1105 | nsIContent* DoGetShadowHost() const; |
1106 | |
1107 | public: |
1108 | nsINode* GetParentOrShadowHostNode() const { |
1109 | if (MOZ_LIKELY(mParent)(__builtin_expect(!!(mParent), 1))) { |
1110 | return mParent; |
1111 | } |
1112 | // We could put this in nsIContentInlines.h or such to avoid this |
1113 | // reinterpret_cast, but it doesn't seem worth it. |
1114 | return IsInShadowTree() ? reinterpret_cast<nsINode*>(DoGetShadowHost()) |
1115 | : nullptr; |
1116 | } |
1117 | |
1118 | enum FlattenedParentType { eNormal, eForStyle, eForSelection }; |
1119 | |
1120 | /** |
1121 | * Returns the node that is the parent of this node in the flattened |
1122 | * tree. This differs from the normal parent if the node is filtered |
1123 | * into an insertion point, or if the node is a direct child of a |
1124 | * shadow root. |
1125 | * |
1126 | * @return the flattened tree parent |
1127 | */ |
1128 | inline nsINode* GetFlattenedTreeParentNode() const; |
1129 | |
1130 | nsINode* GetFlattenedTreeParentNodeNonInline() const; |
1131 | |
1132 | /** |
1133 | * Like GetFlattenedTreeParentNode, but returns the document for any native |
1134 | * anonymous content that was generated for ancestor frames of the document |
1135 | * element's primary frame, such as scrollbar elements created by the root |
1136 | * scroll frame. |
1137 | */ |
1138 | inline nsINode* GetFlattenedTreeParentNodeForStyle() const; |
1139 | |
1140 | /** |
1141 | * Similar to GetFlattenedTreeParentNode, it does two things differently |
1142 | * 1. For contents that are not in the flattened tree, use its |
1143 | * parent rather than nullptr. |
1144 | * 2. For contents that are slotted into a UA shadow tree, use its |
1145 | * parent rather than the slot element. |
1146 | */ |
1147 | inline nsIContent* GetFlattenedTreeParentNodeForSelection() const; |
1148 | |
1149 | inline mozilla::dom::Element* GetFlattenedTreeParentElement() const; |
1150 | inline mozilla::dom::Element* GetFlattenedTreeParentElementForStyle() const; |
1151 | |
1152 | /** |
1153 | * Get the parent nsINode for this node if it is an Element. |
1154 | * |
1155 | * Defined inline in Element.h |
1156 | * |
1157 | * @return the parent node |
1158 | */ |
1159 | inline mozilla::dom::Element* GetParentElement() const; |
1160 | |
1161 | /** |
1162 | * Get the parent Element of this node, traversing over a ShadowRoot |
1163 | * to its host if necessary. |
1164 | */ |
1165 | mozilla::dom::Element* GetParentElementCrossingShadowRoot() const; |
1166 | |
1167 | /** |
1168 | * Get closest element node for the node. Meaning that if the node is an |
1169 | * element node, returns itself. Otherwise, returns parent element or null. |
1170 | */ |
1171 | inline mozilla::dom::Element* GetAsElementOrParentElement() const; |
1172 | |
1173 | /** |
1174 | * Get inclusive ancestor element in the flattened tree. |
1175 | */ |
1176 | inline mozilla::dom::Element* GetInclusiveFlattenedTreeAncestorElement() |
1177 | const; |
1178 | |
1179 | /** |
1180 | * Get the root of the subtree this node belongs to. This never returns |
1181 | * null. It may return 'this' (e.g. for document nodes, and nodes that |
1182 | * are the roots of disconnected subtrees). |
1183 | */ |
1184 | nsINode* SubtreeRoot() const; |
1185 | |
1186 | /* |
1187 | * Get context object's shadow-including root if options's composed is true, |
1188 | * and context object's root otherwise. |
1189 | */ |
1190 | nsINode* GetRootNode(const mozilla::dom::GetRootNodeOptions& aOptions); |
1191 | |
1192 | virtual mozilla::EventListenerManager* GetExistingListenerManager() |
1193 | const override; |
1194 | virtual mozilla::EventListenerManager* GetOrCreateListenerManager() override; |
1195 | |
1196 | mozilla::Maybe<mozilla::dom::EventCallbackDebuggerNotificationType> |
1197 | GetDebuggerNotificationType() const override; |
1198 | |
1199 | bool ComputeDefaultWantsUntrusted(mozilla::ErrorResult& aRv) final; |
1200 | |
1201 | virtual bool IsApzAware() const override; |
1202 | |
1203 | virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindingsInternal() override; |
1204 | virtual nsIGlobalObject* GetOwnerGlobal() const override; |
1205 | |
1206 | using mozilla::dom::EventTarget::DispatchEvent; |
1207 | // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230) |
1208 | MOZ_CAN_RUN_SCRIPT_BOUNDARY bool DispatchEvent( |
1209 | mozilla::dom::Event& aEvent, mozilla::dom::CallerType aCallerType, |
1210 | mozilla::ErrorResult& aRv) override; |
1211 | |
1212 | MOZ_CAN_RUN_SCRIPT |
1213 | nsresult PostHandleEvent(mozilla::EventChainPostVisitor& aVisitor) override; |
1214 | |
1215 | /** |
1216 | * Adds a mutation observer to be notified when this node, or any of its |
1217 | * descendants, are modified. The node will hold a weak reference to the |
1218 | * observer, which means that it is the responsibility of the observer to |
1219 | * remove itself in case it dies before the node. If an observer is added |
1220 | * while observers are being notified, it may also be notified. In general, |
1221 | * adding observers while inside a notification is not a good idea. An |
1222 | * observer that is already observing the node must not be added without |
1223 | * being removed first. |
1224 | * |
1225 | * For mutation observers that implement nsIAnimationObserver, use |
1226 | * AddAnimationObserver instead. |
1227 | */ |
1228 | void AddMutationObserver(nsIMutationObserver* aMutationObserver) { |
1229 | nsSlots* s = Slots(); |
1230 | if (aMutationObserver) { |
1231 | NS_ASSERTION(!s->mMutationObservers.contains(aMutationObserver),do { if (!(!s->mMutationObservers.contains(aMutationObserver ))) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Observer already in the list" , "!s->mMutationObservers.contains(aMutationObserver)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1232); MOZ_PretendNoReturn(); } } while (0) |
1232 | "Observer already in the list")do { if (!(!s->mMutationObservers.contains(aMutationObserver ))) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Observer already in the list" , "!s->mMutationObservers.contains(aMutationObserver)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1232); MOZ_PretendNoReturn(); } } while (0); |
1233 | |
1234 | s->mMutationObservers.pushBack(aMutationObserver); |
1235 | } |
1236 | } |
1237 | |
1238 | void AddMutationObserver(nsMultiMutationObserver* aMultiMutationObserver); |
1239 | |
1240 | /** |
1241 | * Same as above, but only adds the observer if its not observing |
1242 | * the node already. |
1243 | * |
1244 | * For mutation observers that implement nsIAnimationObserver, use |
1245 | * AddAnimationObserverUnlessExists instead. |
1246 | */ |
1247 | void AddMutationObserverUnlessExists(nsIMutationObserver* aMutationObserver) { |
1248 | nsSlots* s = Slots(); |
1249 | if (aMutationObserver && |
1250 | !s->mMutationObservers.contains(aMutationObserver)) { |
1251 | s->mMutationObservers.pushBack(aMutationObserver); |
1252 | } |
1253 | } |
1254 | |
1255 | void AddMutationObserverUnlessExists( |
1256 | nsMultiMutationObserver* aMultiMutationObserver); |
1257 | /** |
1258 | * Same as AddMutationObserver, but for nsIAnimationObservers. This |
1259 | * additionally records on the document that animation observers have |
1260 | * been registered, which is used to determine whether notifications |
1261 | * must be fired when animations are added, removed or changed. |
1262 | */ |
1263 | void AddAnimationObserver(nsIAnimationObserver* aAnimationObserver); |
1264 | |
1265 | /** |
1266 | * Same as above, but only adds the observer if its not observing |
1267 | * the node already. |
1268 | */ |
1269 | void AddAnimationObserverUnlessExists( |
1270 | nsIAnimationObserver* aAnimationObserver); |
1271 | |
1272 | /** |
1273 | * Removes a mutation observer. |
1274 | */ |
1275 | void RemoveMutationObserver(nsIMutationObserver* aMutationObserver) { |
1276 | nsSlots* s = GetExistingSlots(); |
1277 | if (s) { |
1278 | s->mMutationObservers.remove(aMutationObserver); |
1279 | } |
1280 | } |
1281 | |
1282 | void RemoveMutationObserver(nsMultiMutationObserver* aMultiMutationObserver); |
1283 | |
1284 | mozilla::SafeDoublyLinkedList<nsIMutationObserver>* GetMutationObservers(); |
1285 | |
1286 | /** |
1287 | * Helper methods to access ancestor node(s) of type T. |
1288 | * The implementations of the methods are in mozilla/dom/AncestorIterator.h. |
1289 | */ |
1290 | template <typename T> |
1291 | inline mozilla::dom::AncestorsOfTypeIterator<T> AncestorsOfType() const; |
1292 | |
1293 | template <typename T> |
1294 | inline mozilla::dom::InclusiveAncestorsOfTypeIterator<T> |
1295 | InclusiveAncestorsOfType() const; |
1296 | |
1297 | template <typename T> |
1298 | inline mozilla::dom::FlatTreeAncestorsOfTypeIterator<T> |
1299 | FlatTreeAncestorsOfType() const; |
1300 | |
1301 | template <typename T> |
1302 | inline mozilla::dom::InclusiveFlatTreeAncestorsOfTypeIterator<T> |
1303 | InclusiveFlatTreeAncestorsOfType() const; |
1304 | |
1305 | template <typename T> |
1306 | T* FirstAncestorOfType() const; |
1307 | |
1308 | private: |
1309 | /** |
1310 | * Walks aNode, its attributes and, if aDeep is true, its descendant nodes. |
1311 | * If aClone is true the nodes will be cloned. If aNewNodeInfoManager is |
1312 | * not null, it is used to create new nodeinfos for the nodes. Also reparents |
1313 | * the XPConnect wrappers for the nodes into aReparentScope if non-null. |
1314 | * |
1315 | * @param aNode Node to adopt/clone. |
1316 | * @param aClone If true the node will be cloned and the cloned node will |
1317 | * be returned. |
1318 | * @param aDeep If true the function will be called recursively on |
1319 | * descendants of the node |
1320 | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
1321 | * nodeinfos for aNode and its attributes and |
1322 | * descendants. May be null if the nodeinfos |
1323 | * shouldn't be changed. |
1324 | * @param aReparentScope Scope into which wrappers should be reparented, or |
1325 | * null if no reparenting should be done. |
1326 | * @param aParent If aClone is true the cloned node will be appended to |
1327 | * aParent's children. May be null. If not null then aNode |
1328 | * must be an nsIContent. |
1329 | * @param aError The error, if any. |
1330 | * |
1331 | * @return If aClone is true then the cloned node will be returned, |
1332 | * unless an error occurred. In error conditions, null |
1333 | * will be returned. |
1334 | */ |
1335 | static already_AddRefed<nsINode> CloneAndAdopt( |
1336 | nsINode* aNode, bool aClone, bool aDeep, |
1337 | nsNodeInfoManager* aNewNodeInfoManager, |
1338 | JS::Handle<JSObject*> aReparentScope, nsINode* aParent, |
1339 | mozilla::ErrorResult& aError); |
1340 | |
1341 | public: |
1342 | /** |
1343 | * Walks the node, its attributes and descendant nodes. If aNewNodeInfoManager |
1344 | * is not null, it is used to create new nodeinfos for the nodes. Also |
1345 | * reparents the XPConnect wrappers for the nodes into aReparentScope if |
1346 | * non-null. |
1347 | * |
1348 | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
1349 | * nodeinfos for the node and its attributes and |
1350 | * descendants. May be null if the nodeinfos |
1351 | * shouldn't be changed. |
1352 | * @param aReparentScope New scope for the wrappers, or null if no reparenting |
1353 | * should be done. |
1354 | * @param aError The error, if any. |
1355 | */ |
1356 | void Adopt(nsNodeInfoManager* aNewNodeInfoManager, |
1357 | JS::Handle<JSObject*> aReparentScope, |
1358 | mozilla::ErrorResult& aError); |
1359 | |
1360 | /** |
1361 | * Clones the node, its attributes and, if aDeep is true, its descendant nodes |
1362 | * If aNewNodeInfoManager is not null, it is used to create new nodeinfos for |
1363 | * the clones. |
1364 | * |
1365 | * @param aDeep If true the function will be called recursively on |
1366 | * descendants of the node |
1367 | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
1368 | * nodeinfos for the node and its attributes and |
1369 | * descendants. May be null if the nodeinfos |
1370 | * shouldn't be changed. |
1371 | * @param aError The error, if any. |
1372 | * |
1373 | * @return The newly created node. Null in error conditions. |
1374 | */ |
1375 | already_AddRefed<nsINode> Clone(bool aDeep, |
1376 | nsNodeInfoManager* aNewNodeInfoManager, |
1377 | mozilla::ErrorResult& aError); |
1378 | |
1379 | /** |
1380 | * Clones this node. This needs to be overriden by all node classes. aNodeInfo |
1381 | * should be identical to this node's nodeInfo, except for the document which |
1382 | * may be different. When cloning an element, all attributes of the element |
1383 | * will be cloned. The children of the node will not be cloned. |
1384 | * |
1385 | * @param aNodeInfo the nodeinfo to use for the clone |
1386 | * @param aResult the clone |
1387 | */ |
1388 | virtual nsresult Clone(mozilla::dom::NodeInfo*, nsINode** aResult) const = 0; |
1389 | |
1390 | // A callback that gets called when we are forcefully unbound from a node (due |
1391 | // to the node going away). You shouldn't take a strong ref to the node from |
1392 | // the callback. |
1393 | using UnbindCallback = void (*)(nsISupports*, nsINode*); |
1394 | // We should keep alive these objects. |
1395 | struct BoundObject { |
1396 | nsCOMPtr<nsISupports> mObject; |
1397 | UnbindCallback mDtor = nullptr; |
1398 | |
1399 | BoundObject(nsISupports* aObject, UnbindCallback aDtor) |
1400 | : mObject(aObject), mDtor(aDtor) {} |
1401 | |
1402 | bool operator==(nsISupports* aOther) const { |
1403 | return mObject.get() == aOther; |
1404 | } |
1405 | }; |
1406 | |
1407 | // This class can be extended by subclasses that wish to store more |
1408 | // information in the slots. |
1409 | class nsSlots { |
1410 | public: |
1411 | nsSlots(); |
1412 | |
1413 | // If needed we could remove the vtable pointer this dtor causes by |
1414 | // putting a DestroySlots function on nsINode |
1415 | virtual ~nsSlots(); |
1416 | |
1417 | virtual void Traverse(nsCycleCollectionTraversalCallback&); |
1418 | virtual void Unlink(nsINode&); |
1419 | |
1420 | /** |
1421 | * A list of mutation observers |
1422 | */ |
1423 | mozilla::SafeDoublyLinkedList<nsIMutationObserver> mMutationObservers; |
1424 | |
1425 | /** |
1426 | * An object implementing NodeList for this content (childNodes) |
1427 | * @see NodeList |
1428 | * @see nsGenericHTMLElement::GetChildNodes |
1429 | */ |
1430 | RefPtr<nsAttrChildContentList> mChildNodes; |
1431 | |
1432 | /** |
1433 | * Weak reference to this node. This is cleared by the destructor of |
1434 | * nsNodeWeakReference. |
1435 | */ |
1436 | nsNodeWeakReference* MOZ_NON_OWNING_REF mWeakReference; |
1437 | |
1438 | /** A list of objects that we should keep alive. See Bind/UnbindObject. */ |
1439 | nsTArray<BoundObject> mBoundObjects; |
1440 | |
1441 | /** |
1442 | * A set of ranges which are in the selection and which have this node as |
1443 | * their endpoints' closest common inclusive ancestor |
1444 | * (https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor). This is |
1445 | * a UniquePtr instead of just a LinkedList, because that prevents us from |
1446 | * pushing DOMSlots up to the next allocation bucket size, at the cost of |
1447 | * some complexity. |
1448 | */ |
1449 | mozilla::UniquePtr<mozilla::LinkedList<mozilla::dom::AbstractRange>> |
1450 | mClosestCommonInclusiveAncestorRanges; |
1451 | }; |
1452 | |
1453 | /** |
1454 | * Functions for managing flags and slots |
1455 | */ |
1456 | #ifdef DEBUG1 |
1457 | nsSlots* DebugGetSlots() { return Slots(); } |
1458 | #endif |
1459 | |
1460 | void SetFlags(FlagsType aFlagsToSet) { |
1461 | NS_ASSERTION(do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1462 | !(aFlagsToSet &do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1463 | (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE |do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1464 | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME |do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1465 | NODE_HAS_BEEN_IN_UA_WIDGET)) ||do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1466 | IsContent(),do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0) |
1467 | "Flag only permitted on nsIContent nodes")do { if (!(!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Flag only permitted on nsIContent nodes" , "!(aFlagsToSet & (NODE_IS_NATIVE_ANONYMOUS_ROOT | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME | NODE_HAS_BEEN_IN_UA_WIDGET)) || IsContent()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1467); MOZ_PretendNoReturn(); } } while (0); |
1468 | nsWrapperCache::SetFlags(aFlagsToSet); |
1469 | } |
1470 | |
1471 | void UnsetFlags(FlagsType aFlagsToUnset) { |
1472 | NS_ASSERTION(!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET |do { if (!(!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT)))) { NS_DebugBreak(NS_DEBUG_ASSERTION , "Trying to unset write-only flags", "!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT))" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1474); MOZ_PretendNoReturn(); } } while (0) |
1473 | NODE_IS_NATIVE_ANONYMOUS_ROOT)),do { if (!(!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT)))) { NS_DebugBreak(NS_DEBUG_ASSERTION , "Trying to unset write-only flags", "!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT))" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1474); MOZ_PretendNoReturn(); } } while (0) |
1474 | "Trying to unset write-only flags")do { if (!(!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT)))) { NS_DebugBreak(NS_DEBUG_ASSERTION , "Trying to unset write-only flags", "!(aFlagsToUnset & (NODE_HAS_BEEN_IN_UA_WIDGET | NODE_IS_NATIVE_ANONYMOUS_ROOT))" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1474); MOZ_PretendNoReturn(); } } while (0); |
1475 | nsWrapperCache::UnsetFlags(aFlagsToUnset); |
1476 | } |
1477 | |
1478 | void SetEditableFlag(bool aEditable) { |
1479 | if (aEditable) { |
1480 | SetFlags(NODE_IS_EDITABLE); |
1481 | } else { |
1482 | UnsetFlags(NODE_IS_EDITABLE); |
1483 | } |
1484 | } |
1485 | |
1486 | inline bool IsEditable() const; |
1487 | |
1488 | /** |
1489 | * Check if this node is an editing host. For avoiding confusion, this always |
1490 | * returns false if the node is in the design mode document. |
1491 | */ |
1492 | inline bool IsEditingHost() const; |
1493 | |
1494 | /** |
1495 | * Check if this node is in design mode or not. When this returns true and: |
1496 | * - if this is a Document node, it's the design mode root. |
1497 | * - if this is a content node, it's connected, it's not in a shadow tree |
1498 | * (except shadow tree for UI widget and native anonymous subtree) and its |
1499 | * uncomposed document is in design mode. |
1500 | * Note that returning true does NOT mean the node or its children is |
1501 | * editable. E.g., when this node is in a shadow tree of a UA widget and its |
1502 | * host is in design mode. |
1503 | */ |
1504 | inline bool IsInDesignMode() const; |
1505 | |
1506 | /** |
1507 | * Returns true if |this| or any of its ancestors is native anonymous. |
1508 | */ |
1509 | bool IsInNativeAnonymousSubtree() const { |
1510 | return HasFlag(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE); |
1511 | } |
1512 | |
1513 | /** |
1514 | * If |this| or any ancestor is native anonymous, return the root of the |
1515 | * native anonymous subtree. Note that in case of nested native anonymous |
1516 | * content, this returns the innermost root, not the outermost. |
1517 | */ |
1518 | nsIContent* GetClosestNativeAnonymousSubtreeRoot() const { |
1519 | if (!IsInNativeAnonymousSubtree()) { |
1520 | MOZ_ASSERT(!HasBeenInUAWidget(), "UA widget implies anonymous")do { static_assert( mozilla::detail::AssertionConditionType< decltype(!HasBeenInUAWidget())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!HasBeenInUAWidget()))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("!HasBeenInUAWidget()" " (" "UA widget implies anonymous" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1520); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!HasBeenInUAWidget()" ") (" "UA widget implies anonymous" ")"); do { *((volatile int *)__null) = 1520; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
1521 | return nullptr; |
1522 | } |
1523 | MOZ_ASSERT(IsContent(), "How did non-content end up in NAC?")do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsContent())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsContent()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsContent()" " (" "How did non-content end up in NAC?" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1523); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsContent()" ") (" "How did non-content end up in NAC?" ")"); do { *((volatile int*)__null) = 1523; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
1524 | if (HasBeenInUAWidget()) { |
1525 | // reinterpret_cast because in this header we don't know ShadowRoot is an |
1526 | // nsIContent. ShadowRoot constructor asserts this is correct. |
1527 | return reinterpret_cast<nsIContent*>(GetContainingShadow()); |
1528 | } |
1529 | for (const nsINode* node = this; node; node = node->GetParentNode()) { |
1530 | if (node->IsRootOfNativeAnonymousSubtree()) { |
1531 | return const_cast<nsINode*>(node)->AsContent(); |
1532 | } |
1533 | } |
1534 | // FIXME(emilio): This should not happen, usually, but editor removes nodes |
1535 | // in native anonymous subtrees, and we don't clean nodes from the current |
1536 | // event content stack from ContentRemoved, so it can actually happen, see |
1537 | // bug 1510208. |
1538 | NS_WARNING("GetClosestNativeAnonymousSubtreeRoot on disconnected NAC!")NS_DebugBreak(NS_DEBUG_WARNING, "GetClosestNativeAnonymousSubtreeRoot on disconnected NAC!" , nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1538); |
1539 | return nullptr; |
1540 | } |
1541 | |
1542 | /** |
1543 | * If |this| or any ancestor is native anonymous, return the parent of the |
1544 | * native anonymous subtree. Note that in case of nested native anonymous |
1545 | * content, this returns the parent or host of the innermost root, not the |
1546 | * outermost. |
1547 | */ |
1548 | nsIContent* GetClosestNativeAnonymousSubtreeRootParentOrHost() const { |
1549 | // We could put this in nsIContentInlines.h or such to avoid this |
1550 | // reinterpret_cast, but it doesn't seem worth it. |
1551 | const auto* root = reinterpret_cast<const nsINode*>( |
1552 | GetClosestNativeAnonymousSubtreeRoot()); |
1553 | if (!root) { |
1554 | return nullptr; |
1555 | } |
1556 | if (nsIContent* parent = root->GetParent()) { |
1557 | return parent; |
1558 | } |
1559 | if (MOZ_UNLIKELY(root->IsInShadowTree())(__builtin_expect(!!(root->IsInShadowTree()), 0))) { |
1560 | return root->DoGetShadowHost(); |
1561 | } |
1562 | return nullptr; |
1563 | } |
1564 | |
1565 | /** |
1566 | * Gets the root of the node tree for this content if it is in a shadow tree. |
1567 | */ |
1568 | mozilla::dom::ShadowRoot* GetContainingShadow() const; |
1569 | /** |
1570 | * Gets the shadow host if this content is in a shadow tree. That is, the host |
1571 | * of |GetContainingShadow|, if its not null. |
1572 | * |
1573 | * @return The shadow host, if this is in shadow tree, or null. |
1574 | */ |
1575 | mozilla::dom::Element* GetContainingShadowHost() const; |
1576 | |
1577 | bool IsInSVGUseShadowTree() const { |
1578 | return !!GetContainingSVGUseShadowHost(); |
1579 | } |
1580 | |
1581 | mozilla::dom::SVGUseElement* GetContainingSVGUseShadowHost() const { |
1582 | if (!IsInShadowTree()) { |
1583 | return nullptr; |
1584 | } |
1585 | return DoGetContainingSVGUseShadowHost(); |
1586 | } |
1587 | |
1588 | // Whether this node has ever been part of a UA widget shadow tree. |
1589 | bool HasBeenInUAWidget() const { return HasFlag(NODE_HAS_BEEN_IN_UA_WIDGET); } |
1590 | |
1591 | // True for native anonymous content and for content in UA widgets. |
1592 | // Only nsIContent can fulfill this condition. |
1593 | bool ChromeOnlyAccess() const { return IsInNativeAnonymousSubtree(); } |
1594 | |
1595 | const nsIContent* GetChromeOnlyAccessSubtreeRootParent() const { |
1596 | return GetClosestNativeAnonymousSubtreeRootParentOrHost(); |
1597 | } |
1598 | |
1599 | bool IsInShadowTree() const { return HasFlag(NODE_IS_IN_SHADOW_TREE); } |
1600 | |
1601 | /** |
1602 | * Get whether this node is C++-generated anonymous content |
1603 | * @see nsIAnonymousContentCreator |
1604 | * @return whether this content is anonymous |
1605 | */ |
1606 | bool IsRootOfNativeAnonymousSubtree() const { |
1607 | NS_ASSERTION(do { if (!(!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Some flags seem to be missing!" , "!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1609); MOZ_PretendNoReturn(); } } while (0) |
1608 | !HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree(),do { if (!(!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Some flags seem to be missing!" , "!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1609); MOZ_PretendNoReturn(); } } while (0) |
1609 | "Some flags seem to be missing!")do { if (!(!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree ())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Some flags seem to be missing!" , "!HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT) || IsInNativeAnonymousSubtree()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1609); MOZ_PretendNoReturn(); } } while (0); |
1610 | return HasFlag(NODE_IS_NATIVE_ANONYMOUS_ROOT); |
1611 | } |
1612 | |
1613 | // Whether this node is the root of a ChromeOnlyAccess DOM subtree. |
1614 | bool IsRootOfChromeAccessOnlySubtree() const { |
1615 | return IsRootOfNativeAnonymousSubtree(); |
1616 | } |
1617 | |
1618 | /** Whether this is the container of a ::before pseudo-element. */ |
1619 | bool IsGeneratedContentContainerForBefore() const { |
1620 | return IsRootOfNativeAnonymousSubtree() && |
1621 | mNodeInfo->NameAtom() == nsGkAtoms::mozgeneratedcontentbefore; |
1622 | } |
1623 | |
1624 | /** Whether this is the container of an ::after pseudo-element. */ |
1625 | bool IsGeneratedContentContainerForAfter() const { |
1626 | return IsRootOfNativeAnonymousSubtree() && |
1627 | mNodeInfo->NameAtom() == nsGkAtoms::mozgeneratedcontentafter; |
1628 | } |
1629 | |
1630 | /** Whether this is the container of a ::marker pseudo-element. */ |
1631 | bool IsGeneratedContentContainerForMarker() const { |
1632 | return IsRootOfNativeAnonymousSubtree() && |
1633 | mNodeInfo->NameAtom() == nsGkAtoms::mozgeneratedcontentmarker; |
1634 | } |
1635 | |
1636 | /** |
1637 | * Returns true if |this| node is the closest common inclusive ancestor |
1638 | * (https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor) of the |
1639 | * start/end nodes of a Range in a Selection or a descendant of such a common |
1640 | * ancestor. This node is definitely not selected when |false| is returned, |
1641 | * but it may or may not be selected when |true| is returned. |
1642 | */ |
1643 | bool IsMaybeSelected() const { |
1644 | return IsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection() || |
1645 | IsClosestCommonInclusiveAncestorForRangeInSelection(); |
1646 | } |
1647 | |
1648 | /** |
1649 | * Return true if any part of (this, aStartOffset) .. (this, aEndOffset) |
1650 | * overlaps any nsRange in |
1651 | * GetClosestCommonInclusiveAncestorForRangeInSelection ranges (i.e. |
1652 | * where this is a descendant of a range's common inclusive ancestor node). |
1653 | * If a nsRange starts in (this, aEndOffset) or if it ends in |
1654 | * (this, aStartOffset) then it is non-overlapping and the result is false |
1655 | * for that nsRange. Collapsed ranges always counts as non-overlapping. |
1656 | * |
1657 | * @param aStartOffset has to be less or equal to aEndOffset. |
1658 | * @param aCache A cache which contains all fully selected nodes for each |
1659 | * selection. If present, this provides a fast path to check if |
1660 | * a node is fully selected. |
1661 | */ |
1662 | bool IsSelected(uint32_t aStartOffset, uint32_t aEndOffset, |
1663 | mozilla::dom::SelectionNodeCache* aCache = nullptr) const; |
1664 | |
1665 | /** |
1666 | * Get the root element of the text editor associated with this node or the |
1667 | * root element of the text editor of the ancestor 'TextControlElement' if |
1668 | * this is in its native anonymous subtree. I.e., this returns anonymous |
1669 | * `<div>` element of a `TextEditor`. Note that this can be used only for |
1670 | * getting root content of `<input>` or `<textarea>`. I.e., this method |
1671 | * doesn't support HTML editors. Note that this may create a `TextEditor` |
1672 | * instance, and it means that the `TextEditor` may modify its native |
1673 | * anonymous subtree and may run selection listeners. |
1674 | */ |
1675 | MOZ_CAN_RUN_SCRIPT mozilla::dom::Element* GetAnonymousRootElementOfTextEditor( |
1676 | mozilla::TextEditor** aTextEditor = nullptr); |
1677 | |
1678 | /** |
1679 | * Get the nearest selection root, ie. the node that will be selected if the |
1680 | * user does "Select All" while the focus is in this node. Note that if this |
1681 | * node is not in an editor, the result comes from the nsFrameSelection that |
1682 | * is related to aPresShell, so the result might not be the ancestor of this |
1683 | * node. Be aware that if this node and the computed selection limiter are |
1684 | * not in same subtree, this returns the root content of the closeset subtree. |
1685 | */ |
1686 | MOZ_CAN_RUN_SCRIPT nsIContent* GetSelectionRootContent( |
1687 | mozilla::PresShell* aPresShell, bool aAllowCrossShadowBoundary = false); |
1688 | |
1689 | bool HasScheduledSelectionChangeEvent() { |
1690 | return HasFlag(NODE_HAS_SCHEDULED_SELECTION_CHANGE_EVENT); |
1691 | } |
1692 | |
1693 | void SetHasScheduledSelectionChangeEvent() { |
1694 | SetFlags(NODE_HAS_SCHEDULED_SELECTION_CHANGE_EVENT); |
1695 | } |
1696 | |
1697 | void ClearHasScheduledSelectionChangeEvent() { |
1698 | UnsetFlags(NODE_HAS_SCHEDULED_SELECTION_CHANGE_EVENT); |
1699 | } |
1700 | |
1701 | nsINodeList* ChildNodes(); |
1702 | |
1703 | nsIContent* GetFirstChild() const { return mFirstChild; } |
1704 | |
1705 | nsIContent* GetLastChild() const; |
1706 | |
1707 | /** |
1708 | * Implementation is in Document.h, because it needs to cast from |
1709 | * Document* to nsINode*. |
1710 | */ |
1711 | Document* GetOwnerDocument() const; |
1712 | |
1713 | // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230) |
1714 | MOZ_CAN_RUN_SCRIPT_BOUNDARY void Normalize(); |
1715 | |
1716 | /** |
1717 | * Get the base URI for any relative URIs within this piece of |
1718 | * content. Generally, this is the document's base URI, but certain |
1719 | * content carries a local base for backward compatibility. |
1720 | * |
1721 | * @return the base URI. May return null. |
1722 | */ |
1723 | virtual nsIURI* GetBaseURI(bool aTryUseXHRDocBaseURI = false) const = 0; |
1724 | nsIURI* GetBaseURIObject() const; |
1725 | |
1726 | /** |
1727 | * Return true if the node may be apz aware. There are two cases. One is that |
1728 | * the node is apz aware (such as HTMLInputElement with number type). The |
1729 | * other is that the node has apz aware listeners. This is a non-virtual |
1730 | * function which calls IsNodeApzAwareInternal only when the MayBeApzAware is |
1731 | * set. We check the details in IsNodeApzAwareInternal which may be overriden |
1732 | * by child classes |
1733 | */ |
1734 | bool IsNodeApzAware() const { |
1735 | return NodeMayBeApzAware() ? IsNodeApzAwareInternal() : false; |
1736 | } |
1737 | |
1738 | /** |
1739 | * Override this function and set the flag MayBeApzAware in case the node has |
1740 | * to let APZC be aware of it. It's used when the node may handle the apz |
1741 | * aware events and may do preventDefault to stop APZC to do default actions. |
1742 | * |
1743 | * For example, instead of scrolling page by APZ, we handle mouse wheel event |
1744 | * in HTMLInputElement with number type as increasing / decreasing its value. |
1745 | */ |
1746 | virtual bool IsNodeApzAwareInternal() const; |
1747 | |
1748 | void GetTextContent(nsAString& aTextContent, mozilla::OOMReporter& aError) { |
1749 | GetTextContentInternal(aTextContent, aError); |
1750 | } |
1751 | void SetTextContent(const nsAString& aTextContent, |
1752 | nsIPrincipal* aSubjectPrincipal, |
1753 | mozilla::ErrorResult& aError) { |
1754 | SetTextContentInternal(aTextContent, aSubjectPrincipal, aError); |
1755 | } |
1756 | void SetTextContent(const nsAString& aTextContent, |
1757 | mozilla::ErrorResult& aError) { |
1758 | SetTextContentInternal(aTextContent, nullptr, aError); |
1759 | } |
1760 | |
1761 | mozilla::dom::Element* QuerySelector(const nsACString& aSelector, |
1762 | mozilla::ErrorResult& aResult); |
1763 | already_AddRefed<nsINodeList> QuerySelectorAll(const nsACString& aSelector, |
1764 | mozilla::ErrorResult& aResult); |
1765 | |
1766 | protected: |
1767 | // Document and ShadowRoot override this with its own (faster) version. |
1768 | // This should really only be called for elements and document fragments. |
1769 | mozilla::dom::Element* GetElementById(const nsAString& aId); |
1770 | |
1771 | void AppendChildToChildList(nsIContent* aKid); |
1772 | void InsertChildToChildList(nsIContent* aKid, nsIContent* aNextSibling); |
1773 | void DisconnectChild(nsIContent* aKid); |
1774 | |
1775 | public: |
1776 | void LookupPrefix(const nsAString& aNamespace, nsAString& aResult); |
1777 | bool IsDefaultNamespace(const nsAString& aNamespaceURI) { |
1778 | nsAutoString defaultNamespace; |
1779 | LookupNamespaceURI(u""_ns, defaultNamespace); |
1780 | return aNamespaceURI.Equals(defaultNamespace); |
1781 | } |
1782 | void LookupNamespaceURI(const nsAString& aNamespacePrefix, |
1783 | nsAString& aNamespaceURI); |
1784 | |
1785 | nsIContent* GetNextSibling() const { return mNextSibling; } |
1786 | nsIContent* GetPreviousSibling() const; |
1787 | |
1788 | /** |
1789 | * Return true if the node is being removed from the parent, it means that |
1790 | * the node still knows the container which it's disconnected from, but the |
1791 | * node has already been removed from the child node chain of the container. |
1792 | * I.e., Return true between a call of DisconnectChild of the parent and |
1793 | * a call of UnbindFromTree of the node. |
1794 | */ |
1795 | bool IsBeingRemoved() const { |
1796 | return mParent && !mNextSibling && !mPreviousOrLastSibling; |
1797 | } |
1798 | |
1799 | /** |
1800 | * Get the next node in the pre-order tree traversal of the DOM. If |
1801 | * aRoot is non-null, then it must be an ancestor of |this| |
1802 | * (possibly equal to |this|) and only nodes that are descendants of |
1803 | * aRoot, not including aRoot itself, will be returned. Returns |
1804 | * null if there are no more nodes to traverse. |
1805 | */ |
1806 | nsIContent* GetNextNode(const nsINode* aRoot = nullptr) const { |
1807 | return GetNextNodeImpl(aRoot, false); |
1808 | } |
1809 | |
1810 | /** |
1811 | * Get the next node in the pre-order tree traversal of the DOM but ignoring |
1812 | * the children of this node. If aRoot is non-null, then it must be an |
1813 | * ancestor of |this| (possibly equal to |this|) and only nodes that are |
1814 | * descendants of aRoot, not including aRoot itself, will be returned. |
1815 | * Returns null if there are no more nodes to traverse. |
1816 | */ |
1817 | nsIContent* GetNextNonChildNode(const nsINode* aRoot = nullptr) const { |
1818 | return GetNextNodeImpl(aRoot, true); |
1819 | } |
1820 | |
1821 | /** |
1822 | * Returns true if 'this' is either document or element or |
1823 | * document fragment and aOther is a descendant in the same |
1824 | * anonymous tree. |
1825 | */ |
1826 | bool Contains(const nsINode* aOther) const; |
1827 | |
1828 | bool UnoptimizableCCNode() const; |
1829 | |
1830 | /** |
1831 | * Fire a DOMNodeRemoved mutation event for all children of this node |
1832 | * TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230) |
1833 | */ |
1834 | MOZ_CAN_RUN_SCRIPT_BOUNDARY void FireNodeRemovedForChildren(); |
1835 | |
1836 | void QueueDevtoolsAnonymousEvent(bool aIsRemove); |
1837 | |
1838 | private: |
1839 | mozilla::dom::SVGUseElement* DoGetContainingSVGUseShadowHost() const; |
1840 | |
1841 | nsIContent* GetNextNodeImpl(const nsINode* aRoot, |
1842 | const bool aSkipChildren) const { |
1843 | #ifdef DEBUG1 |
1844 | if (aRoot) { |
1845 | // TODO: perhaps nsINode::IsInclusiveDescendantOf could be used instead. |
1846 | const nsINode* cur = this; |
1847 | for (; cur; cur = cur->GetParentNode()) |
1848 | if (cur == aRoot) break; |
1849 | NS_ASSERTION(cur, "aRoot not an ancestor of |this|?")do { if (!(cur)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "aRoot not an ancestor of |this|?" , "cur", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1849); MOZ_PretendNoReturn(); } } while (0); |
1850 | } |
1851 | #endif |
1852 | if (!aSkipChildren) { |
1853 | nsIContent* kid = GetFirstChild(); |
1854 | if (kid) { |
1855 | return kid; |
1856 | } |
1857 | } |
1858 | if (this == aRoot) { |
1859 | return nullptr; |
1860 | } |
1861 | const nsINode* cur = this; |
1862 | while (1) { |
1863 | nsIContent* next = cur->GetNextSibling(); |
1864 | if (next) { |
1865 | return next; |
1866 | } |
1867 | nsINode* parent = cur->GetParentNode(); |
1868 | if (parent == aRoot) { |
1869 | return nullptr; |
1870 | } |
1871 | cur = parent; |
1872 | } |
1873 | MOZ_ASSERT_UNREACHABLE("How did we get here?")do { static_assert( mozilla::detail::AssertionConditionType< decltype(false)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: " "How did we get here?" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1873); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "How did we get here?" ")"); do { *((volatile int*)__null) = 1873; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
1874 | } |
1875 | |
1876 | public: |
1877 | /** |
1878 | * Get the previous nsIContent in the pre-order tree traversal of the DOM. If |
1879 | * aRoot is non-null, then it must be an ancestor of |this| |
1880 | * (possibly equal to |this|) and only nsIContents that are descendants of |
1881 | * aRoot, including aRoot itself, will be returned. Returns |
1882 | * null if there are no more nsIContents to traverse. |
1883 | */ |
1884 | nsIContent* GetPrevNode(const nsINode* aRoot = nullptr) const { |
1885 | #ifdef DEBUG1 |
1886 | if (aRoot) { |
1887 | // TODO: perhaps nsINode::IsInclusiveDescendantOf could be used instead. |
1888 | const nsINode* cur = this; |
1889 | for (; cur; cur = cur->GetParentNode()) |
1890 | if (cur == aRoot) break; |
1891 | NS_ASSERTION(cur, "aRoot not an ancestor of |this|?")do { if (!(cur)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "aRoot not an ancestor of |this|?" , "cur", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 1891); MOZ_PretendNoReturn(); } } while (0); |
1892 | } |
1893 | #endif |
1894 | |
1895 | if (this == aRoot) { |
1896 | return nullptr; |
1897 | } |
1898 | nsIContent* cur = this->GetParent(); |
1899 | nsIContent* iter = this->GetPreviousSibling(); |
1900 | while (iter) { |
1901 | cur = iter; |
1902 | iter = reinterpret_cast<nsINode*>(iter)->GetLastChild(); |
1903 | } |
1904 | return cur; |
1905 | } |
1906 | |
1907 | /** |
1908 | * Boolean flags |
1909 | */ |
1910 | private: |
1911 | enum BooleanFlag { |
1912 | // Set if we're being used from -moz-element or observed via a mask, |
1913 | // clipPath, filter or use element. |
1914 | NodeHasDirectRenderingObservers, |
1915 | // Set if our parent chain (including this node itself) terminates |
1916 | // in a document |
1917 | IsInDocument, |
1918 | // Set if we're part of the composed doc. |
1919 | // https://dom.spec.whatwg.org/#connected |
1920 | IsConnected, |
1921 | // Set if mParent is an nsIContent |
1922 | ParentIsContent, |
1923 | // Set if this node is an Element |
1924 | NodeIsElement, |
1925 | // Set if the element has a non-empty id attribute. This can in rare |
1926 | // cases lie for nsXMLElement, such as when the node has been moved between |
1927 | // documents with different id mappings. |
1928 | ElementHasID, |
1929 | // Set if the element might have a class. |
1930 | ElementMayHaveClass, |
1931 | // Set if the element might have inline style. |
1932 | ElementMayHaveStyle, |
1933 | // Set if the element has a name attribute set. |
1934 | ElementHasName, |
1935 | // Set if the element has a part attribute set. |
1936 | ElementHasPart, |
1937 | // Set if the element might have a contenteditable attribute set. |
1938 | ElementMayHaveContentEditableAttr, |
1939 | // Set if the node is the closest common inclusive ancestor of the start/end |
1940 | // nodes of a Range that is in a Selection. |
1941 | NodeIsClosestCommonInclusiveAncestorForRangeInSelection, |
1942 | // Set if the node is a descendant of a node with the above bit set. |
1943 | NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection, |
1944 | // Set if CanSkipInCC check has been done for this subtree root. |
1945 | NodeIsCCMarkedRoot, |
1946 | // Maybe set if this node is in black subtree. |
1947 | NodeIsCCBlackTree, |
1948 | // Maybe set if the node is a root of a subtree |
1949 | // which needs to be kept in the purple buffer. |
1950 | NodeIsPurpleRoot, |
1951 | // Set if the element has some style states locked |
1952 | ElementHasLockedStyleStates, |
1953 | // Set if element has pointer locked |
1954 | ElementHasPointerLock, |
1955 | // Set if the node may have DOMMutationObserver attached to it. |
1956 | NodeMayHaveDOMMutationObserver, |
1957 | // Set if node is Content |
1958 | NodeIsContent, |
1959 | // Set if the node has animations or transitions |
1960 | ElementHasAnimations, |
1961 | // Set if node has a dir attribute with a valid value (ltr, rtl, or auto). |
1962 | // Note that we cannot compute this from the dir attribute event state |
1963 | // flags, because we can't use those to distinguish |
1964 | // <bdi dir="some-invalid-value"> and <bdi dir="auto">. |
1965 | NodeHasValidDirAttribute, |
1966 | // Set if a node in the node's parent chain has dir=auto and nothing |
1967 | // inbetween nor the node itself establishes its own direction. |
1968 | NodeAncestorHasDirAuto, |
1969 | // Set if the node or an ancestor is assigned to a dir=auto slot and |
1970 | // nothing between nor the node itself establishes its own direction. |
1971 | // Except for when the node assigned to the dir=auto slot establishes |
1972 | // its own direction, then the flag is still set. |
1973 | NodeAffectsDirAutoSlot, |
1974 | // Set if the node is handling a click. |
1975 | NodeHandlingClick, |
1976 | // Set if the element has a parser insertion mode other than "in body", |
1977 | // per the HTML5 "Parse state" section. |
1978 | ElementHasWeirdParserInsertionMode, |
1979 | // Parser sets this flag if it has notified about the node. |
1980 | ParserHasNotified, |
1981 | // Sets if the node is apz aware or we have apz aware listeners. |
1982 | MayBeApzAware, |
1983 | // Set if the element might have any kind of anonymous content children, |
1984 | // which would not be found through the element's children list. |
1985 | ElementMayHaveAnonymousChildren, |
1986 | // Set if element has CustomElementData. |
1987 | ElementHasCustomElementData, |
1988 | // Set if the element was created from prototype cache and |
1989 | // its l10n attributes haven't been changed. |
1990 | ElementCreatedFromPrototypeAndHasUnmodifiedL10n, |
1991 | // Guard value |
1992 | BooleanFlagCount |
1993 | }; |
1994 | |
1995 | void SetBoolFlag(BooleanFlag name, bool value) { |
1996 | static_assert(BooleanFlagCount <= 8 * sizeof(mBoolFlags), |
1997 | "Too many boolean flags"); |
1998 | mBoolFlags = (mBoolFlags & ~(1 << name)) | (value << name); |
1999 | } |
2000 | |
2001 | void SetBoolFlag(BooleanFlag name) { |
2002 | static_assert(BooleanFlagCount <= 8 * sizeof(mBoolFlags), |
2003 | "Too many boolean flags"); |
2004 | mBoolFlags |= (1 << name); |
2005 | } |
2006 | |
2007 | void ClearBoolFlag(BooleanFlag name) { |
2008 | static_assert(BooleanFlagCount <= 8 * sizeof(mBoolFlags), |
2009 | "Too many boolean flags"); |
2010 | mBoolFlags &= ~(1 << name); |
2011 | } |
2012 | |
2013 | bool GetBoolFlag(BooleanFlag name) const { |
2014 | static_assert(BooleanFlagCount <= 8 * sizeof(mBoolFlags), |
2015 | "Too many boolean flags"); |
2016 | return mBoolFlags & (1 << name); |
2017 | } |
2018 | |
2019 | public: |
2020 | bool HasDirectRenderingObservers() const { |
2021 | return GetBoolFlag(NodeHasDirectRenderingObservers); |
2022 | } |
2023 | void SetHasDirectRenderingObservers(bool aValue) { |
2024 | SetBoolFlag(NodeHasDirectRenderingObservers, aValue); |
2025 | } |
2026 | bool IsContent() const { return GetBoolFlag(NodeIsContent); } |
2027 | bool HasID() const { return GetBoolFlag(ElementHasID); } |
2028 | bool MayHaveClass() const { return GetBoolFlag(ElementMayHaveClass); } |
2029 | void SetMayHaveClass() { SetBoolFlag(ElementMayHaveClass); } |
2030 | bool MayHaveStyle() const { return GetBoolFlag(ElementMayHaveStyle); } |
2031 | bool HasName() const { return GetBoolFlag(ElementHasName); } |
2032 | bool HasPartAttribute() const { return GetBoolFlag(ElementHasPart); } |
2033 | bool MayHaveContentEditableAttr() const { |
2034 | return GetBoolFlag(ElementMayHaveContentEditableAttr); |
2035 | } |
2036 | /** |
2037 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2038 | */ |
2039 | bool IsClosestCommonInclusiveAncestorForRangeInSelection() const { |
2040 | return GetBoolFlag(NodeIsClosestCommonInclusiveAncestorForRangeInSelection); |
2041 | } |
2042 | /** |
2043 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2044 | */ |
2045 | void SetClosestCommonInclusiveAncestorForRangeInSelection() { |
2046 | SetBoolFlag(NodeIsClosestCommonInclusiveAncestorForRangeInSelection); |
2047 | } |
2048 | /** |
2049 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2050 | */ |
2051 | void ClearClosestCommonInclusiveAncestorForRangeInSelection() { |
2052 | ClearBoolFlag(NodeIsClosestCommonInclusiveAncestorForRangeInSelection); |
2053 | } |
2054 | /** |
2055 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2056 | */ |
2057 | bool IsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection() const { |
2058 | return GetBoolFlag( |
2059 | NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection); |
2060 | } |
2061 | /** |
2062 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2063 | */ |
2064 | void SetDescendantOfClosestCommonInclusiveAncestorForRangeInSelection() { |
2065 | SetBoolFlag( |
2066 | NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection); |
2067 | } |
2068 | /** |
2069 | * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor |
2070 | */ |
2071 | void ClearDescendantOfClosestCommonInclusiveAncestorForRangeInSelection() { |
2072 | ClearBoolFlag( |
2073 | NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection); |
2074 | } |
2075 | |
2076 | void SetCCMarkedRoot(bool aValue) { SetBoolFlag(NodeIsCCMarkedRoot, aValue); } |
2077 | bool CCMarkedRoot() const { return GetBoolFlag(NodeIsCCMarkedRoot); } |
2078 | void SetInCCBlackTree(bool aValue) { SetBoolFlag(NodeIsCCBlackTree, aValue); } |
2079 | bool InCCBlackTree() const { return GetBoolFlag(NodeIsCCBlackTree); } |
2080 | void SetIsPurpleRoot(bool aValue) { SetBoolFlag(NodeIsPurpleRoot, aValue); } |
2081 | bool IsPurpleRoot() const { return GetBoolFlag(NodeIsPurpleRoot); } |
2082 | bool MayHaveDOMMutationObserver() { |
2083 | return GetBoolFlag(NodeMayHaveDOMMutationObserver); |
2084 | } |
2085 | void SetMayHaveDOMMutationObserver() { |
2086 | SetBoolFlag(NodeMayHaveDOMMutationObserver, true); |
2087 | } |
2088 | bool HasListenerManager() { return HasFlag(NODE_HAS_LISTENERMANAGER); } |
2089 | bool HasPointerLock() const { return GetBoolFlag(ElementHasPointerLock); } |
2090 | void SetPointerLock() { SetBoolFlag(ElementHasPointerLock); } |
2091 | void ClearPointerLock() { ClearBoolFlag(ElementHasPointerLock); } |
2092 | bool MayHaveAnimations() const { return GetBoolFlag(ElementHasAnimations); } |
2093 | void SetMayHaveAnimations() { SetBoolFlag(ElementHasAnimations); } |
2094 | void ClearMayHaveAnimations() { ClearBoolFlag(ElementHasAnimations); } |
2095 | void SetHasValidDir() { SetBoolFlag(NodeHasValidDirAttribute); } |
2096 | void ClearHasValidDir() { ClearBoolFlag(NodeHasValidDirAttribute); } |
2097 | bool HasValidDir() const { return GetBoolFlag(NodeHasValidDirAttribute); } |
2098 | void SetAncestorHasDirAuto() { SetBoolFlag(NodeAncestorHasDirAuto); } |
2099 | void ClearAncestorHasDirAuto() { ClearBoolFlag(NodeAncestorHasDirAuto); } |
2100 | bool AncestorHasDirAuto() const { |
2101 | return GetBoolFlag(NodeAncestorHasDirAuto); |
2102 | } |
2103 | void SetAffectsDirAutoSlot() { SetBoolFlag(NodeAffectsDirAutoSlot); } |
2104 | void ClearAffectsDirAutoSlot() { ClearBoolFlag(NodeAffectsDirAutoSlot); } |
2105 | |
2106 | // Set if the node or an ancestor is assigned to a dir=auto slot. |
2107 | bool AffectsDirAutoSlot() const { |
2108 | return GetBoolFlag(NodeAffectsDirAutoSlot); |
2109 | } |
2110 | |
2111 | // Implemented in nsIContentInlines.h. |
2112 | inline bool NodeOrAncestorHasDirAuto() const; |
2113 | |
2114 | void SetParserHasNotified() { SetBoolFlag(ParserHasNotified); }; |
2115 | bool HasParserNotified() { return GetBoolFlag(ParserHasNotified); } |
2116 | |
2117 | void SetMayBeApzAware() { SetBoolFlag(MayBeApzAware); } |
2118 | bool NodeMayBeApzAware() const { return GetBoolFlag(MayBeApzAware); } |
2119 | |
2120 | void SetMayHaveAnonymousChildren() { |
2121 | SetBoolFlag(ElementMayHaveAnonymousChildren); |
2122 | } |
2123 | bool MayHaveAnonymousChildren() const { |
2124 | return GetBoolFlag(ElementMayHaveAnonymousChildren); |
2125 | } |
2126 | |
2127 | void SetHasCustomElementData() { SetBoolFlag(ElementHasCustomElementData); } |
2128 | bool HasCustomElementData() const { |
2129 | return GetBoolFlag(ElementHasCustomElementData); |
2130 | } |
2131 | |
2132 | void SetElementCreatedFromPrototypeAndHasUnmodifiedL10n() { |
2133 | SetBoolFlag(ElementCreatedFromPrototypeAndHasUnmodifiedL10n); |
2134 | } |
2135 | bool HasElementCreatedFromPrototypeAndHasUnmodifiedL10n() { |
2136 | return GetBoolFlag(ElementCreatedFromPrototypeAndHasUnmodifiedL10n); |
2137 | } |
2138 | void ClearElementCreatedFromPrototypeAndHasUnmodifiedL10n() { |
2139 | ClearBoolFlag(ElementCreatedFromPrototypeAndHasUnmodifiedL10n); |
2140 | } |
2141 | |
2142 | mozilla::dom::ShadowRoot* GetShadowRoot() const; |
2143 | |
2144 | // Return the shadow root of the node if it is a shadow host and |
2145 | // it meets the requirements for being a shadow host of a selection. |
2146 | // For example, <details>, <video> and <use> elements are not valid |
2147 | // shadow host for selection. |
2148 | mozilla::dom::ShadowRoot* GetShadowRootForSelection() const; |
2149 | |
2150 | protected: |
2151 | void SetParentIsContent(bool aValue) { SetBoolFlag(ParentIsContent, aValue); } |
2152 | void SetIsInDocument() { SetBoolFlag(IsInDocument); } |
2153 | void ClearInDocument() { ClearBoolFlag(IsInDocument); } |
2154 | void SetIsConnected(bool aConnected) { SetBoolFlag(IsConnected, aConnected); } |
2155 | void SetNodeIsContent() { SetBoolFlag(NodeIsContent); } |
2156 | void SetIsElement() { SetBoolFlag(NodeIsElement); } |
2157 | void SetHasID() { SetBoolFlag(ElementHasID); } |
2158 | void ClearHasID() { ClearBoolFlag(ElementHasID); } |
2159 | void SetMayHaveStyle() { SetBoolFlag(ElementMayHaveStyle); } |
2160 | void SetHasName() { SetBoolFlag(ElementHasName); } |
2161 | void ClearHasName() { ClearBoolFlag(ElementHasName); } |
2162 | void SetHasPartAttribute(bool aPart) { SetBoolFlag(ElementHasPart, aPart); } |
2163 | void SetMayHaveContentEditableAttr() { |
2164 | SetBoolFlag(ElementMayHaveContentEditableAttr); |
2165 | } |
2166 | void SetHasLockedStyleStates() { SetBoolFlag(ElementHasLockedStyleStates); } |
2167 | void ClearHasLockedStyleStates() { |
2168 | ClearBoolFlag(ElementHasLockedStyleStates); |
2169 | } |
2170 | bool HasLockedStyleStates() const { |
2171 | return GetBoolFlag(ElementHasLockedStyleStates); |
2172 | } |
2173 | void SetHasWeirdParserInsertionMode() { |
2174 | SetBoolFlag(ElementHasWeirdParserInsertionMode); |
2175 | } |
2176 | bool HasWeirdParserInsertionMode() const { |
2177 | return GetBoolFlag(ElementHasWeirdParserInsertionMode); |
2178 | } |
2179 | bool HandlingClick() const { return GetBoolFlag(NodeHandlingClick); } |
2180 | void SetHandlingClick() { SetBoolFlag(NodeHandlingClick); } |
2181 | void ClearHandlingClick() { ClearBoolFlag(NodeHandlingClick); } |
2182 | |
2183 | void SetSubtreeRootPointer(nsINode* aSubtreeRoot) { |
2184 | NS_ASSERTION(aSubtreeRoot, "aSubtreeRoot can never be null!")do { if (!(aSubtreeRoot)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "aSubtreeRoot can never be null!", "aSubtreeRoot", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2184); MOZ_PretendNoReturn(); } } while (0); |
2185 | NS_ASSERTION(!(IsContent() && IsInUncomposedDoc()) && !IsInShadowTree(),do { if (!(!(IsContent() && IsInUncomposedDoc()) && !IsInShadowTree())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Shouldn't be here!" , "!(IsContent() && IsInUncomposedDoc()) && !IsInShadowTree()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2186); MOZ_PretendNoReturn(); } } while (0) |
2186 | "Shouldn't be here!")do { if (!(!(IsContent() && IsInUncomposedDoc()) && !IsInShadowTree())) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Shouldn't be here!" , "!(IsContent() && IsInUncomposedDoc()) && !IsInShadowTree()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2186); MOZ_PretendNoReturn(); } } while (0); |
2187 | mSubtreeRoot = aSubtreeRoot; |
2188 | } |
2189 | |
2190 | void ClearSubtreeRootPointer() { mSubtreeRoot = nullptr; } |
2191 | |
2192 | public: |
2193 | // Makes nsINode object keep aObject alive. If a callback is provided, it's |
2194 | // called before deleting the node. |
2195 | void BindObject(nsISupports* aObject, UnbindCallback = nullptr); |
2196 | // After calling UnbindObject nsINode, object doesn't keep aObject alive |
2197 | // anymore. |
2198 | void UnbindObject(nsISupports* aObject); |
2199 | |
2200 | void GenerateXPath(nsAString& aResult); |
2201 | |
2202 | already_AddRefed<mozilla::dom::AccessibleNode> GetAccessibleNode(); |
2203 | |
2204 | /** |
2205 | * Returns the length of this node, as specified at |
2206 | * <http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-node-length> |
2207 | */ |
2208 | uint32_t Length() const; |
2209 | |
2210 | void GetNodeName(mozilla::dom::DOMString& aNodeName) { |
2211 | const nsString& nodeName = NodeName(); |
2212 | aNodeName.SetKnownLiveString(nodeName); |
2213 | } |
2214 | [[nodiscard]] nsresult GetBaseURI(nsAString& aBaseURI) const; |
2215 | // Return the base URI for the document. |
2216 | // The returned value may differ if the document is loaded via XHR, and |
2217 | // when accessed from chrome privileged script and |
2218 | // from content privileged script for compatibility. |
2219 | void GetBaseURIFromJS(nsAString& aBaseURI, CallerType aCallerType, |
2220 | ErrorResult& aRv) const; |
2221 | bool HasChildNodes() const { return HasChildren(); } |
2222 | |
2223 | // See nsContentUtils::PositionIsBefore for aThisIndex and aOtherIndex usage. |
2224 | uint16_t CompareDocumentPosition( |
2225 | nsINode& aOther, mozilla::Maybe<uint32_t>* aThisIndex = nullptr, |
2226 | mozilla::Maybe<uint32_t>* aOtherIndex = nullptr) const; |
2227 | void GetNodeValue(nsAString& aNodeValue) { GetNodeValueInternal(aNodeValue); } |
2228 | void SetNodeValue(const nsAString& aNodeValue, mozilla::ErrorResult& aError) { |
2229 | SetNodeValueInternal(aNodeValue, aError); |
2230 | } |
2231 | virtual void GetNodeValueInternal(nsAString& aNodeValue); |
2232 | virtual void SetNodeValueInternal(const nsAString& aNodeValue, |
2233 | mozilla::ErrorResult& aError) { |
2234 | // The DOM spec says that when nodeValue is defined to be null "setting it |
2235 | // has no effect", so we don't throw an exception. |
2236 | } |
2237 | void EnsurePreInsertionValidity(nsINode& aNewChild, nsINode* aRefChild, |
2238 | mozilla::ErrorResult& aError); |
2239 | nsINode* InsertBefore(nsINode& aNode, nsINode* aChild, |
2240 | mozilla::ErrorResult& aError) { |
2241 | return ReplaceOrInsertBefore(false, &aNode, aChild, aError); |
2242 | } |
2243 | |
2244 | /** |
2245 | * See <https://dom.spec.whatwg.org/#dom-node-appendchild>. |
2246 | */ |
2247 | nsINode* AppendChild(nsINode& aNode, mozilla::ErrorResult& aError) { |
2248 | return InsertBefore(aNode, nullptr, aError); |
2249 | } |
2250 | |
2251 | nsINode* ReplaceChild(nsINode& aNode, nsINode& aChild, |
2252 | mozilla::ErrorResult& aError) { |
2253 | return ReplaceOrInsertBefore(true, &aNode, &aChild, aError); |
2254 | } |
2255 | // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230) |
2256 | MOZ_CAN_RUN_SCRIPT_BOUNDARY nsINode* RemoveChild( |
2257 | nsINode& aChild, mozilla::ErrorResult& aError); |
2258 | already_AddRefed<nsINode> CloneNode(bool aDeep, mozilla::ErrorResult& aError); |
2259 | bool IsSameNode(nsINode* aNode); |
2260 | bool IsEqualNode(nsINode* aNode); |
2261 | void GetNamespaceURI(nsAString& aNamespaceURI) const { |
2262 | mNodeInfo->GetNamespaceURI(aNamespaceURI); |
2263 | } |
2264 | #ifdef MOZILLA_INTERNAL_API1 |
2265 | void GetPrefix(nsAString& aPrefix) { mNodeInfo->GetPrefix(aPrefix); } |
2266 | #endif |
2267 | void GetLocalName(mozilla::dom::DOMString& aLocalName) const { |
2268 | const nsString& localName = LocalName(); |
2269 | aLocalName.SetKnownLiveString(localName); |
2270 | } |
2271 | |
2272 | nsDOMAttributeMap* GetAttributes(); |
2273 | |
2274 | // Helper method to remove this node from its parent. This is not exposed |
2275 | // through WebIDL. |
2276 | // Only call this if the node has a parent node. |
2277 | nsresult RemoveFromParent() { |
2278 | nsINode* parent = GetParentNode(); |
2279 | mozilla::ErrorResult rv; |
2280 | parent->RemoveChild(*this, rv); |
2281 | return rv.StealNSResult(); |
2282 | } |
2283 | |
2284 | // ChildNode methods |
2285 | inline mozilla::dom::Element* GetPreviousElementSibling() const; |
2286 | inline mozilla::dom::Element* GetNextElementSibling() const; |
2287 | |
2288 | MOZ_CAN_RUN_SCRIPT void Before(const Sequence<OwningNodeOrString>& aNodes, |
2289 | ErrorResult& aRv); |
2290 | MOZ_CAN_RUN_SCRIPT void After(const Sequence<OwningNodeOrString>& aNodes, |
2291 | ErrorResult& aRv); |
2292 | MOZ_CAN_RUN_SCRIPT void ReplaceWith( |
2293 | const Sequence<OwningNodeOrString>& aNodes, ErrorResult& aRv); |
2294 | /** |
2295 | * Remove this node from its parent, if any. |
2296 | */ |
2297 | void Remove(); |
2298 | |
2299 | // ParentNode methods |
2300 | mozilla::dom::Element* GetFirstElementChild() const; |
2301 | mozilla::dom::Element* GetLastElementChild() const; |
2302 | |
2303 | already_AddRefed<nsIHTMLCollection> GetElementsByAttribute( |
2304 | const nsAString& aAttribute, const nsAString& aValue); |
2305 | already_AddRefed<nsIHTMLCollection> GetElementsByAttributeNS( |
2306 | const nsAString& aNamespaceURI, const nsAString& aAttribute, |
2307 | const nsAString& aValue, ErrorResult& aRv); |
2308 | |
2309 | MOZ_CAN_RUN_SCRIPT void Prepend(const Sequence<OwningNodeOrString>& aNodes, |
2310 | ErrorResult& aRv); |
2311 | MOZ_CAN_RUN_SCRIPT void Append(const Sequence<OwningNodeOrString>& aNodes, |
2312 | ErrorResult& aRv); |
2313 | MOZ_CAN_RUN_SCRIPT void ReplaceChildren( |
2314 | const Sequence<OwningNodeOrString>& aNodes, ErrorResult& aRv); |
2315 | MOZ_CAN_RUN_SCRIPT void ReplaceChildren(nsINode* aNode, ErrorResult& aRv); |
2316 | |
2317 | void GetBoxQuads(const BoxQuadOptions& aOptions, |
2318 | nsTArray<RefPtr<DOMQuad>>& aResult, CallerType aCallerType, |
2319 | ErrorResult& aRv); |
2320 | |
2321 | void GetBoxQuadsFromWindowOrigin(const BoxQuadOptions& aOptions, |
2322 | nsTArray<RefPtr<DOMQuad>>& aResult, |
2323 | ErrorResult& aRv); |
2324 | |
2325 | already_AddRefed<DOMQuad> ConvertQuadFromNode( |
2326 | DOMQuad& aQuad, const TextOrElementOrDocument& aFrom, |
2327 | const ConvertCoordinateOptions& aOptions, CallerType aCallerType, |
2328 | ErrorResult& aRv); |
2329 | already_AddRefed<DOMQuad> ConvertRectFromNode( |
2330 | DOMRectReadOnly& aRect, const TextOrElementOrDocument& aFrom, |
2331 | const ConvertCoordinateOptions& aOptions, CallerType aCallerType, |
2332 | ErrorResult& aRv); |
2333 | already_AddRefed<DOMPoint> ConvertPointFromNode( |
2334 | const DOMPointInit& aPoint, const TextOrElementOrDocument& aFrom, |
2335 | const ConvertCoordinateOptions& aOptions, CallerType aCallerType, |
2336 | ErrorResult& aRv); |
2337 | |
2338 | /** |
2339 | * See nsSlots::mClosestCommonInclusiveAncestorRanges. |
2340 | */ |
2341 | const mozilla::LinkedList<mozilla::dom::AbstractRange>* |
2342 | GetExistingClosestCommonInclusiveAncestorRanges() const { |
2343 | if (!HasSlots()) { |
2344 | return nullptr; |
2345 | } |
2346 | return GetExistingSlots()->mClosestCommonInclusiveAncestorRanges.get(); |
2347 | } |
2348 | |
2349 | /** |
2350 | * See nsSlots::mClosestCommonInclusiveAncestorRanges. |
2351 | */ |
2352 | mozilla::LinkedList<mozilla::dom::AbstractRange>* |
2353 | GetExistingClosestCommonInclusiveAncestorRanges() { |
2354 | if (!HasSlots()) { |
2355 | return nullptr; |
2356 | } |
2357 | return GetExistingSlots()->mClosestCommonInclusiveAncestorRanges.get(); |
2358 | } |
2359 | |
2360 | /** |
2361 | * See nsSlots::mClosestCommonInclusiveAncestorRanges. |
2362 | */ |
2363 | mozilla::UniquePtr<mozilla::LinkedList<mozilla::dom::AbstractRange>>& |
2364 | GetClosestCommonInclusiveAncestorRangesPtr() { |
2365 | return Slots()->mClosestCommonInclusiveAncestorRanges; |
2366 | } |
2367 | |
2368 | nsIWeakReference* GetExistingWeakReference() { |
2369 | return HasSlots() ? GetExistingSlots()->mWeakReference : nullptr; |
2370 | } |
2371 | |
2372 | protected: |
2373 | // Override this function to create a custom slots class. |
2374 | // Must not return null. |
2375 | virtual nsINode::nsSlots* CreateSlots(); |
2376 | |
2377 | bool HasSlots() const { return mSlots != nullptr; } |
2378 | |
2379 | nsSlots* GetExistingSlots() const { return mSlots; } |
2380 | |
2381 | nsSlots* Slots() { |
2382 | if (!HasSlots()) { |
2383 | mSlots = CreateSlots(); |
2384 | MOZ_ASSERT(mSlots)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mSlots)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mSlots))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mSlots", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2384); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mSlots" ")" ); do { *((volatile int*)__null) = 2384; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
2385 | } |
2386 | return GetExistingSlots(); |
2387 | } |
2388 | |
2389 | /** |
2390 | * Invalidate cached child array inside mChildNodes |
2391 | * of type nsParentNodeChildContentList. |
2392 | */ |
2393 | void InvalidateChildNodes(); |
2394 | |
2395 | virtual void GetTextContentInternal(nsAString& aTextContent, |
2396 | mozilla::OOMReporter& aError); |
2397 | virtual void SetTextContentInternal(const nsAString& aTextContent, |
2398 | nsIPrincipal* aSubjectPrincipal, |
2399 | mozilla::ErrorResult& aError) {} |
2400 | |
2401 | void EnsurePreInsertionValidity1(mozilla::ErrorResult& aError); |
2402 | void EnsurePreInsertionValidity2(bool aReplace, nsINode& aNewChild, |
2403 | nsINode* aRefChild, |
2404 | mozilla::ErrorResult& aError); |
2405 | // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230) |
2406 | MOZ_CAN_RUN_SCRIPT_BOUNDARY nsINode* ReplaceOrInsertBefore( |
2407 | bool aReplace, nsINode* aNewChild, nsINode* aRefChild, |
2408 | mozilla::ErrorResult& aError); |
2409 | |
2410 | /** |
2411 | * Returns the Element that should be used for resolving namespaces |
2412 | * on this node (ie the ownerElement for attributes, the documentElement for |
2413 | * documents, the node itself for elements and for other nodes the parentNode |
2414 | * if it is an element). |
2415 | */ |
2416 | virtual mozilla::dom::Element* GetNameSpaceElement() = 0; |
2417 | |
2418 | /** |
2419 | * Parse the given selector string into a servo SelectorList. |
2420 | * |
2421 | * Never returns null if aRv is not failing. |
2422 | * |
2423 | * Note that the selector list returned here is owned by the owner doc's |
2424 | * selector cache. |
2425 | */ |
2426 | const mozilla::StyleSelectorList* ParseSelectorList( |
2427 | const nsACString& aSelectorString, mozilla::ErrorResult&); |
2428 | |
2429 | public: |
2430 | /* Event stuff that documents and elements share. |
2431 | |
2432 | Note that we include DOCUMENT_ONLY_EVENT events here so that we |
2433 | can forward all the document stuff to this implementation. |
2434 | */ |
2435 | #define EVENT(name_, id_, type_, struct_) \ |
2436 | mozilla::dom::EventHandlerNonNull* GetOn##name_() { \ |
2437 | return GetEventHandler(nsGkAtoms::on##name_); \ |
2438 | } \ |
2439 | void SetOn##name_(mozilla::dom::EventHandlerNonNull* handler) { \ |
2440 | SetEventHandler(nsGkAtoms::on##name_, handler); \ |
2441 | } |
2442 | #define TOUCH_EVENT EVENT |
2443 | #define DOCUMENT_ONLY_EVENT EVENT |
2444 | #include "mozilla/EventNameList.h" |
2445 | #undef DOCUMENT_ONLY_EVENT |
2446 | #undef TOUCH_EVENT |
2447 | #undef EVENT |
2448 | |
2449 | NodeSelectorFlags GetSelectorFlags() const { |
2450 | return static_cast<NodeSelectorFlags>(mSelectorFlags.Get()); |
2451 | } |
2452 | |
2453 | protected: |
2454 | static bool Traverse(nsINode* tmp, nsCycleCollectionTraversalCallback& cb); |
2455 | static void Unlink(nsINode* tmp); |
2456 | |
2457 | RefPtr<mozilla::dom::NodeInfo> mNodeInfo; |
2458 | |
2459 | // mParent is an owning ref most of the time, except for the case of document |
2460 | // nodes, so it cannot be represented by nsCOMPtr, so mark is as |
2461 | // MOZ_OWNING_REF. |
2462 | nsINode* MOZ_OWNING_REF mParent; |
2463 | |
2464 | private: |
2465 | #ifndef BOOL_FLAGS_ON_WRAPPER_CACHE |
2466 | // Boolean flags. |
2467 | uint32_t mBoolFlags; |
2468 | #endif |
2469 | |
2470 | mozilla::RustCell<uint32_t> mSelectorFlags{0}; |
2471 | |
2472 | uint32_t mChildCount; |
2473 | |
2474 | protected: |
2475 | // mNextSibling and mFirstChild are strong references while |
2476 | // mPreviousOrLastSibling is a weak ref. |mFirstChild->mPreviousOrLastSibling| |
2477 | // points to the last child node. |
2478 | nsCOMPtr<nsIContent> mFirstChild; |
2479 | nsCOMPtr<nsIContent> mNextSibling; |
2480 | nsIContent* MOZ_NON_OWNING_REF mPreviousOrLastSibling; |
2481 | |
2482 | union { |
2483 | // Pointer to our primary frame. Might be null. |
2484 | nsIFrame* mPrimaryFrame; |
2485 | |
2486 | // Pointer to the root of our subtree. Might be null. |
2487 | // This reference is non-owning and safe, since it either points to the |
2488 | // object itself, or is reset by ClearSubtreeRootPointer. |
2489 | nsINode* MOZ_NON_OWNING_REF mSubtreeRoot; |
2490 | }; |
2491 | |
2492 | // Storage for more members that are usually not needed; allocated lazily. |
2493 | nsSlots* mSlots; |
2494 | }; |
2495 | |
2496 | NON_VIRTUAL_ADDREF_RELEASE(nsINode) |
2497 | |
2498 | inline nsINode* mozilla::dom::EventTarget::GetAsNode() { |
2499 | return IsNode() ? AsNode() : nullptr; |
2500 | } |
2501 | |
2502 | inline const nsINode* mozilla::dom::EventTarget::GetAsNode() const { |
2503 | return const_cast<mozilla::dom::EventTarget*>(this)->GetAsNode(); |
2504 | } |
2505 | |
2506 | inline nsINode* mozilla::dom::EventTarget::AsNode() { |
2507 | MOZ_DIAGNOSTIC_ASSERT(IsNode())do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsNode())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsNode()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsNode()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2507); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "IsNode()" ")"); do { *((volatile int*)__null) = 2507; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
2508 | return static_cast<nsINode*>(this); |
2509 | } |
2510 | |
2511 | inline const nsINode* mozilla::dom::EventTarget::AsNode() const { |
2512 | MOZ_DIAGNOSTIC_ASSERT(IsNode())do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsNode())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsNode()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsNode()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nsINode.h" , 2512); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "IsNode()" ")"); do { *((volatile int*)__null) = 2512; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
2513 | return static_cast<const nsINode*>(this); |
2514 | } |
2515 | |
2516 | // Useful inline function for getting a node given an nsIContent and a Document. |
2517 | // Returns the first argument cast to nsINode if it is non-null, otherwise |
2518 | // returns the second (which may be null). We use type variables instead of |
2519 | // nsIContent* and Document* because the actual types must be |
2520 | // known for the cast to work. |
2521 | template <class C, class D> |
2522 | inline nsINode* NODE_FROM(C& aContent, D& aDocument) { |
2523 | if (aContent) return static_cast<nsINode*>(aContent); |
2524 | return static_cast<nsINode*>(aDocument); |
2525 | } |
2526 | |
2527 | NS_DEFINE_STATIC_IID_ACCESSOR(nsINode, NS_INODE_IID)template <typename T> struct nsINode::COMTypeInfo<nsINode , T> { static const nsIID kIID __attribute__((visibility("hidden" ))); }; template <typename T> const nsIID nsINode::COMTypeInfo <nsINode, T>::kIID __attribute__((visibility("hidden")) ) = { 0x70ba4547, 0x7699, 0x44fc, { 0xb3, 0x20, 0x52, 0xdb, 0xe3 , 0xd1, 0xf9, 0x0a } }; |
2528 | |
2529 | inline nsISupports* ToSupports(nsINode* aPointer) { return aPointer; } |
2530 | |
2531 | // Some checks are faster to do on nsIContent or Element than on |
2532 | // nsINode, so spit out FromNode versions taking those types too. |
2533 | #define NS_IMPL_FROMNODE_GENERIC(_class, _check, _const)template <typename T> static auto FromNode( _const T& aNode) -> decltype(static_cast<_const _class*>(& aNode)) { return aNode._check ? static_cast<_const _class* >(&aNode) : nullptr; } template <typename T> static _const _class* FromNode(_const T* aNode) { return FromNode(* aNode); } template <typename T> static _const _class* FromNodeOrNull (_const T* aNode) { return aNode ? FromNode(*aNode) : nullptr ; } template <typename T> static auto FromEventTarget(_const T& aEventTarget) -> decltype(static_cast<_const _class *>(&aEventTarget)) { return aEventTarget.IsNode() && aEventTarget.AsNode()->_check ? static_cast<_const _class *>(&aEventTarget) : nullptr; } template <typename T > static _const _class* FromEventTarget(_const T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static _const _class* FromEventTargetOrNull(_const T* aEventTarget ) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } \ |
2534 | template <typename T> \ |
2535 | static auto FromNode( \ |
2536 | _const T& aNode) -> decltype(static_cast<_const _class*>(&aNode)) { \ |
2537 | return aNode._check ? static_cast<_const _class*>(&aNode) : nullptr; \ |
2538 | } \ |
2539 | template <typename T> \ |
2540 | static _const _class* FromNode(_const T* aNode) { \ |
2541 | return FromNode(*aNode); \ |
2542 | } \ |
2543 | template <typename T> \ |
2544 | static _const _class* FromNodeOrNull(_const T* aNode) { \ |
2545 | return aNode ? FromNode(*aNode) : nullptr; \ |
2546 | } \ |
2547 | template <typename T> \ |
2548 | static auto FromEventTarget(_const T& aEventTarget) \ |
2549 | -> decltype(static_cast<_const _class*>(&aEventTarget)) { \ |
2550 | return aEventTarget.IsNode() && aEventTarget.AsNode()->_check \ |
2551 | ? static_cast<_const _class*>(&aEventTarget) \ |
2552 | : nullptr; \ |
2553 | } \ |
2554 | template <typename T> \ |
2555 | static _const _class* FromEventTarget(_const T* aEventTarget) { \ |
2556 | return FromEventTarget(*aEventTarget); \ |
2557 | } \ |
2558 | template <typename T> \ |
2559 | static _const _class* FromEventTargetOrNull(_const T* aEventTarget) { \ |
2560 | return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; \ |
2561 | } |
2562 | |
2563 | #define NS_IMPL_FROMNODE_HELPER(_class, _check)template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode._check ? static_cast< _class*>(&aNode) : nullptr ; } template <typename T> static _class* FromNode( T* aNode ) { return FromNode(*aNode); } template <typename T> static _class* FromNodeOrNull( T* aNode) { return aNode ? FromNode( *aNode) : nullptr; } template <typename T> static auto FromEventTarget ( T& aEventTarget) -> decltype(static_cast< _class* >(&aEventTarget)) { return aEventTarget.IsNode() && aEventTarget.AsNode()->_check ? static_cast< _class*> (&aEventTarget) : nullptr; } template <typename T> static _class* FromEventTarget( T* aEventTarget) { return FromEventTarget (*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; } template <typename T> static auto FromNode( const T& aNode) -> decltype (static_cast<const _class*>(&aNode)) { return aNode ._check ? static_cast<const _class*>(&aNode) : nullptr ; } template <typename T> static const _class* FromNode (const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class*>(&aEventTarget) ) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->_check ? static_cast<const _class*>(&aEventTarget ) : nullptr; } template <typename T> static const _class * FromEventTarget(const T* aEventTarget) { return FromEventTarget (*aEventTarget); } template <typename T> static const _class * FromEventTargetOrNull(const T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode ->_check ? static_cast<_class*>(static_cast<nsINode *>(aNode)) : nullptr; } template <typename T> static _class* FromNodeOrNull(T&& aNode) { return aNode ? FromNode (aNode) : nullptr; } template <typename T> static _class * FromEventTarget(T&& aEventTarget) { return aEventTarget ->IsNode() && aEventTarget->AsNode()->_check ? static_cast<_class*>(static_cast<EventTarget*> (aEventTarget)) : nullptr; } template <typename T> static _class* FromEventTargetOrNull(T&& aEventTarget) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr; } \ |
2564 | NS_IMPL_FROMNODE_GENERIC(_class, _check, )template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode._check ? static_cast< _class*>(&aNode) : nullptr ; } template <typename T> static _class* FromNode( T* aNode ) { return FromNode(*aNode); } template <typename T> static _class* FromNodeOrNull( T* aNode) { return aNode ? FromNode( *aNode) : nullptr; } template <typename T> static auto FromEventTarget ( T& aEventTarget) -> decltype(static_cast< _class* >(&aEventTarget)) { return aEventTarget.IsNode() && aEventTarget.AsNode()->_check ? static_cast< _class*> (&aEventTarget) : nullptr; } template <typename T> static _class* FromEventTarget( T* aEventTarget) { return FromEventTarget (*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr; } \ |
2565 | NS_IMPL_FROMNODE_GENERIC(_class, _check, const)template <typename T> static auto FromNode( const T& aNode) -> decltype(static_cast<const _class*>(& aNode)) { return aNode._check ? static_cast<const _class*> (&aNode) : nullptr; } template <typename T> static const _class* FromNode(const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull (const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T> static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class *>(&aEventTarget)) { return aEventTarget.IsNode() && aEventTarget.AsNode()->_check ? static_cast<const _class *>(&aEventTarget) : nullptr; } template <typename T > static const _class* FromEventTarget(const T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static const _class* FromEventTargetOrNull(const T* aEventTarget ) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } \ |
2566 | \ |
2567 | template <typename T> \ |
2568 | static _class* FromNode(T&& aNode) { \ |
2569 | /* We need the double-cast in case aNode is a smartptr. Those */ \ |
2570 | /* can cast to superclasses of the type they're templated on, */ \ |
2571 | /* but not directly to subclasses. */ \ |
2572 | return aNode->_check ? static_cast<_class*>(static_cast<nsINode*>(aNode)) \ |
2573 | : nullptr; \ |
2574 | } \ |
2575 | template <typename T> \ |
2576 | static _class* FromNodeOrNull(T&& aNode) { \ |
2577 | return aNode ? FromNode(aNode) : nullptr; \ |
2578 | } \ |
2579 | template <typename T> \ |
2580 | static _class* FromEventTarget(T&& aEventTarget) { \ |
2581 | /* We need the double-cast in case aEventTarget is a smartptr. Those */ \ |
2582 | /* can cast to superclasses of the type they're templated on, */ \ |
2583 | /* but not directly to subclasses. */ \ |
2584 | return aEventTarget->IsNode() && aEventTarget->AsNode()->_check \ |
2585 | ? static_cast<_class*>(static_cast<EventTarget*>(aEventTarget)) \ |
2586 | : nullptr; \ |
2587 | } \ |
2588 | template <typename T> \ |
2589 | static _class* FromEventTargetOrNull(T&& aEventTarget) { \ |
2590 | return aEventTarget ? FromEventTarget(aEventTarget) : nullptr; \ |
2591 | } |
2592 | |
2593 | #define NS_IMPL_FROMNODE(_class, _nsid)template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.IsInNamespace(_nsid) ? static_cast< _class*>(& aNode) : nullptr; } template <typename T> static _class * FromNode( T* aNode) { return FromNode(*aNode); } template < typename T> static _class* FromNodeOrNull( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget( T& aEventTarget) -> decltype (static_cast< _class*>(&aEventTarget)) { return aEventTarget .IsNode() && aEventTarget.AsNode()->IsInNamespace( _nsid) ? static_cast< _class*>(&aEventTarget) : nullptr ; } template <typename T> static _class* FromEventTarget ( T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull ( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget ) : nullptr; } template <typename T> static auto FromNode ( const T& aNode) -> decltype(static_cast<const _class *>(&aNode)) { return aNode.IsInNamespace(_nsid) ? static_cast <const _class*>(&aNode) : nullptr; } template <typename T> static const _class* FromNode(const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode (*aNode) : nullptr; } template <typename T> static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast <const _class*>(&aEventTarget)) { return aEventTarget .IsNode() && aEventTarget.AsNode()->IsInNamespace( _nsid) ? static_cast<const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget (const T* aEventTarget) { return FromEventTarget(*aEventTarget ); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->IsInNamespace (_nsid) ? static_cast<_class*>(static_cast<nsINode*> (aNode)) : nullptr; } template <typename T> static _class * FromNodeOrNull(T&& aNode) { return aNode ? FromNode (aNode) : nullptr; } template <typename T> static _class * FromEventTarget(T&& aEventTarget) { return aEventTarget ->IsNode() && aEventTarget->AsNode()->IsInNamespace (_nsid) ? static_cast<_class*>(static_cast<EventTarget *>(aEventTarget)) : nullptr; } template <typename T> static _class* FromEventTargetOrNull(T&& aEventTarget ) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr ; } \ |
2594 | NS_IMPL_FROMNODE_HELPER(_class, IsInNamespace(_nsid))template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.IsInNamespace(_nsid) ? static_cast< _class*>(& aNode) : nullptr; } template <typename T> static _class * FromNode( T* aNode) { return FromNode(*aNode); } template < typename T> static _class* FromNodeOrNull( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget( T& aEventTarget) -> decltype (static_cast< _class*>(&aEventTarget)) { return aEventTarget .IsNode() && aEventTarget.AsNode()->IsInNamespace( _nsid) ? static_cast< _class*>(&aEventTarget) : nullptr ; } template <typename T> static _class* FromEventTarget ( T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull ( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget ) : nullptr; } template <typename T> static auto FromNode ( const T& aNode) -> decltype(static_cast<const _class *>(&aNode)) { return aNode.IsInNamespace(_nsid) ? static_cast <const _class*>(&aNode) : nullptr; } template <typename T> static const _class* FromNode(const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode (*aNode) : nullptr; } template <typename T> static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast <const _class*>(&aEventTarget)) { return aEventTarget .IsNode() && aEventTarget.AsNode()->IsInNamespace( _nsid) ? static_cast<const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget (const T* aEventTarget) { return FromEventTarget(*aEventTarget ); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->IsInNamespace (_nsid) ? static_cast<_class*>(static_cast<nsINode*> (aNode)) : nullptr; } template <typename T> static _class * FromNodeOrNull(T&& aNode) { return aNode ? FromNode (aNode) : nullptr; } template <typename T> static _class * FromEventTarget(T&& aEventTarget) { return aEventTarget ->IsNode() && aEventTarget->AsNode()->IsInNamespace (_nsid) ? static_cast<_class*>(static_cast<EventTarget *>(aEventTarget)) : nullptr; } template <typename T> static _class* FromEventTargetOrNull(T&& aEventTarget ) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr ; } |
2595 | |
2596 | #define NS_IMPL_FROMNODE_WITH_TAG(_class, _nsid, _tag)template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast < _class*>(&aNode) : nullptr; } template <typename T> static _class* FromNode( T* aNode) { return FromNode(* aNode); } template <typename T> static _class* FromNodeOrNull ( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T> static auto FromEventTarget( T& aEventTarget ) -> decltype(static_cast< _class*>(&aEventTarget )) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast < _class*>(&aEventTarget) : nullptr; } template < typename T> static _class* FromEventTarget( T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } template <typename T> static auto FromNode( const T & aNode) -> decltype(static_cast<const _class*>( &aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms:: _tag, _nsid) ? static_cast<const _class*>(&aNode) : nullptr; } template <typename T> static const _class* FromNode (const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class*>(&aEventTarget) ) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast <const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget(const T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->NodeInfo ()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast<_class *>(static_cast<nsINode*>(aNode)) : nullptr; } template <typename T> static _class* FromNodeOrNull(T&& aNode) { return aNode ? FromNode(aNode) : nullptr; } template <typename T> static _class* FromEventTarget(T&& aEventTarget) { return aEventTarget->IsNode() && aEventTarget ->AsNode()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid ) ? static_cast<_class*>(static_cast<EventTarget*> (aEventTarget)) : nullptr; } template <typename T> static _class* FromEventTargetOrNull(T&& aEventTarget) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr; } \ |
2597 | NS_IMPL_FROMNODE_HELPER(_class, NodeInfo()->Equals(nsGkAtoms::_tag, _nsid))template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast < _class*>(&aNode) : nullptr; } template <typename T> static _class* FromNode( T* aNode) { return FromNode(* aNode); } template <typename T> static _class* FromNodeOrNull ( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T> static auto FromEventTarget( T& aEventTarget ) -> decltype(static_cast< _class*>(&aEventTarget )) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast < _class*>(&aEventTarget) : nullptr; } template < typename T> static _class* FromEventTarget( T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } template <typename T> static auto FromNode( const T & aNode) -> decltype(static_cast<const _class*>( &aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms:: _tag, _nsid) ? static_cast<const _class*>(&aNode) : nullptr; } template <typename T> static const _class* FromNode (const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class*>(&aEventTarget) ) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast <const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget(const T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->NodeInfo ()->Equals(nsGkAtoms::_tag, _nsid) ? static_cast<_class *>(static_cast<nsINode*>(aNode)) : nullptr; } template <typename T> static _class* FromNodeOrNull(T&& aNode) { return aNode ? FromNode(aNode) : nullptr; } template <typename T> static _class* FromEventTarget(T&& aEventTarget) { return aEventTarget->IsNode() && aEventTarget ->AsNode()->NodeInfo()->Equals(nsGkAtoms::_tag, _nsid ) ? static_cast<_class*>(static_cast<EventTarget*> (aEventTarget)) : nullptr; } template <typename T> static _class* FromEventTargetOrNull(T&& aEventTarget) { return aEventTarget ? FromEventTarget(aEventTarget) : nullptr; } |
2598 | |
2599 | #define NS_IMPL_FROMNODE_HTML_WITH_TAG(_class, _tag)template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast < _class*>(&aNode) : nullptr; } template <typename T> static _class* FromNode( T* aNode) { return FromNode(* aNode); } template <typename T> static _class* FromNodeOrNull ( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T> static auto FromEventTarget( T& aEventTarget ) -> decltype(static_cast< _class*>(&aEventTarget )) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast < _class*>(&aEventTarget) : nullptr; } template < typename T> static _class* FromEventTarget( T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } template <typename T> static auto FromNode( const T & aNode) -> decltype(static_cast<const _class*>( &aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms:: _tag, 3) ? static_cast<const _class*>(&aNode) : nullptr ; } template <typename T> static const _class* FromNode (const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class*>(&aEventTarget) ) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast <const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget(const T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->NodeInfo ()->Equals(nsGkAtoms::_tag, 3) ? static_cast<_class*> (static_cast<nsINode*>(aNode)) : nullptr; } template < typename T> static _class* FromNodeOrNull(T&& aNode ) { return aNode ? FromNode(aNode) : nullptr; } template < typename T> static _class* FromEventTarget(T&& aEventTarget ) { return aEventTarget->IsNode() && aEventTarget-> AsNode()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast <_class*>(static_cast<EventTarget*>(aEventTarget) ) : nullptr; } template <typename T> static _class* FromEventTargetOrNull (T&& aEventTarget) { return aEventTarget ? FromEventTarget (aEventTarget) : nullptr; } \ |
2600 | NS_IMPL_FROMNODE_WITH_TAG(_class, kNameSpaceID_XHTML, _tag)template <typename T> static auto FromNode( T& aNode ) -> decltype(static_cast< _class*>(&aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast < _class*>(&aNode) : nullptr; } template <typename T> static _class* FromNode( T* aNode) { return FromNode(* aNode); } template <typename T> static _class* FromNodeOrNull ( T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T> static auto FromEventTarget( T& aEventTarget ) -> decltype(static_cast< _class*>(&aEventTarget )) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast < _class*>(&aEventTarget) : nullptr; } template < typename T> static _class* FromEventTarget( T* aEventTarget ) { return FromEventTarget(*aEventTarget); } template <typename T> static _class* FromEventTargetOrNull( T* aEventTarget) { return aEventTarget ? FromEventTarget(*aEventTarget) : nullptr ; } template <typename T> static auto FromNode( const T & aNode) -> decltype(static_cast<const _class*>( &aNode)) { return aNode.NodeInfo()->Equals(nsGkAtoms:: _tag, 3) ? static_cast<const _class*>(&aNode) : nullptr ; } template <typename T> static const _class* FromNode (const T* aNode) { return FromNode(*aNode); } template <typename T> static const _class* FromNodeOrNull(const T* aNode) { return aNode ? FromNode(*aNode) : nullptr; } template <typename T > static auto FromEventTarget(const T& aEventTarget) -> decltype(static_cast<const _class*>(&aEventTarget) ) { return aEventTarget.IsNode() && aEventTarget.AsNode ()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast <const _class*>(&aEventTarget) : nullptr; } template <typename T> static const _class* FromEventTarget(const T* aEventTarget) { return FromEventTarget(*aEventTarget); } template <typename T> static const _class* FromEventTargetOrNull (const T* aEventTarget) { return aEventTarget ? FromEventTarget (*aEventTarget) : nullptr; } template <typename T> static _class* FromNode(T&& aNode) { return aNode->NodeInfo ()->Equals(nsGkAtoms::_tag, 3) ? static_cast<_class*> (static_cast<nsINode*>(aNode)) : nullptr; } template < typename T> static _class* FromNodeOrNull(T&& aNode ) { return aNode ? FromNode(aNode) : nullptr; } template < typename T> static _class* FromEventTarget(T&& aEventTarget ) { return aEventTarget->IsNode() && aEventTarget-> AsNode()->NodeInfo()->Equals(nsGkAtoms::_tag, 3) ? static_cast <_class*>(static_cast<EventTarget*>(aEventTarget) ) : nullptr; } template <typename T> static _class* FromEventTargetOrNull (T&& aEventTarget) { return aEventTarget ? FromEventTarget (aEventTarget) : nullptr; } |
2601 | |
2602 | #endif /* nsINode_h___ */ |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- | |||
2 | * vim: set ts=8 sts=2 et sw=2 tw=80: | |||
3 | * This Source Code Form is subject to the terms of the Mozilla Public | |||
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
6 | ||||
7 | #ifndef js_RootingAPI_h | |||
8 | #define js_RootingAPI_h | |||
9 | ||||
10 | #include "mozilla/Attributes.h" | |||
11 | #include "mozilla/DebugOnly.h" | |||
12 | #include "mozilla/EnumeratedArray.h" | |||
13 | #include "mozilla/LinkedList.h" | |||
14 | #include "mozilla/Maybe.h" | |||
15 | ||||
16 | #include <tuple> | |||
17 | #include <type_traits> | |||
18 | #include <utility> | |||
19 | ||||
20 | #include "jspubtd.h" | |||
21 | ||||
22 | #include "js/ComparisonOperators.h" // JS::detail::DefineComparisonOps | |||
23 | #include "js/GCAnnotations.h" | |||
24 | #include "js/GCPolicyAPI.h" | |||
25 | #include "js/GCTypeMacros.h" // JS_FOR_EACH_PUBLIC_{,TAGGED_}GC_POINTER_TYPE | |||
26 | #include "js/HashTable.h" | |||
27 | #include "js/HeapAPI.h" // StackKindCount | |||
28 | #include "js/ProfilingStack.h" | |||
29 | #include "js/Realm.h" | |||
30 | #include "js/Stack.h" // JS::NativeStackLimit | |||
31 | #include "js/TypeDecls.h" | |||
32 | #include "js/UniquePtr.h" | |||
33 | ||||
34 | /* | |||
35 | * [SMDOC] Stack Rooting | |||
36 | * | |||
37 | * Moving GC Stack Rooting | |||
38 | * | |||
39 | * A moving GC may change the physical location of GC allocated things, even | |||
40 | * when they are rooted, updating all pointers to the thing to refer to its new | |||
41 | * location. The GC must therefore know about all live pointers to a thing, | |||
42 | * not just one of them, in order to behave correctly. | |||
43 | * | |||
44 | * The |Rooted| and |Handle| classes below are used to root stack locations | |||
45 | * whose value may be held live across a call that can trigger GC. For a | |||
46 | * code fragment such as: | |||
47 | * | |||
48 | * JSObject* obj = NewObject(cx); | |||
49 | * DoSomething(cx); | |||
50 | * ... = obj->lastProperty(); | |||
51 | * | |||
52 | * If |DoSomething()| can trigger a GC, the stack location of |obj| must be | |||
53 | * rooted to ensure that the GC does not move the JSObject referred to by | |||
54 | * |obj| without updating |obj|'s location itself. This rooting must happen | |||
55 | * regardless of whether there are other roots which ensure that the object | |||
56 | * itself will not be collected. | |||
57 | * | |||
58 | * If |DoSomething()| cannot trigger a GC, and the same holds for all other | |||
59 | * calls made between |obj|'s definitions and its last uses, then no rooting | |||
60 | * is required. | |||
61 | * | |||
62 | * SpiderMonkey can trigger a GC at almost any time and in ways that are not | |||
63 | * always clear. For example, the following innocuous-looking actions can | |||
64 | * cause a GC: allocation of any new GC thing; JSObject::hasProperty; | |||
65 | * JS_ReportError and friends; and ToNumber, among many others. The following | |||
66 | * dangerous-looking actions cannot trigger a GC: js_malloc, cx->malloc_, | |||
67 | * rt->malloc_, and friends and JS_ReportOutOfMemory. | |||
68 | * | |||
69 | * The following family of three classes will exactly root a stack location. | |||
70 | * Incorrect usage of these classes will result in a compile error in almost | |||
71 | * all cases. Therefore, it is very hard to be incorrectly rooted if you use | |||
72 | * these classes exclusively. These classes are all templated on the type T of | |||
73 | * the value being rooted. | |||
74 | * | |||
75 | * - Rooted<T> declares a variable of type T, whose value is always rooted. | |||
76 | * Rooted<T> may be automatically coerced to a Handle<T>, below. Rooted<T> | |||
77 | * should be used whenever a local variable's value may be held live across a | |||
78 | * call which can trigger a GC. | |||
79 | * | |||
80 | * - Handle<T> is a const reference to a Rooted<T>. Functions which take GC | |||
81 | * things or values as arguments and need to root those arguments should | |||
82 | * generally use handles for those arguments and avoid any explicit rooting. | |||
83 | * This has two benefits. First, when several such functions call each other | |||
84 | * then redundant rooting of multiple copies of the GC thing can be avoided. | |||
85 | * Second, if the caller does not pass a rooted value a compile error will be | |||
86 | * generated, which is quicker and easier to fix than when relying on a | |||
87 | * separate rooting analysis. | |||
88 | * | |||
89 | * - MutableHandle<T> is a non-const reference to Rooted<T>. It is used in the | |||
90 | * same way as Handle<T> and includes a |set(const T& v)| method to allow | |||
91 | * updating the value of the referenced Rooted<T>. A MutableHandle<T> can be | |||
92 | * created with an implicit cast from a Rooted<T>*. | |||
93 | * | |||
94 | * In some cases the small performance overhead of exact rooting (measured to | |||
95 | * be a few nanoseconds on desktop) is too much. In these cases, try the | |||
96 | * following: | |||
97 | * | |||
98 | * - Move all Rooted<T> above inner loops: this allows you to re-use the root | |||
99 | * on each iteration of the loop. | |||
100 | * | |||
101 | * - Pass Handle<T> through your hot call stack to avoid re-rooting costs at | |||
102 | * every invocation. | |||
103 | * | |||
104 | * The following diagram explains the list of supported, implicit type | |||
105 | * conversions between classes of this family: | |||
106 | * | |||
107 | * Rooted<T> ----> Handle<T> | |||
108 | * | ^ | |||
109 | * | | | |||
110 | * | | | |||
111 | * +---> MutableHandle<T> | |||
112 | * (via &) | |||
113 | * | |||
114 | * All of these types have an implicit conversion to raw pointers. | |||
115 | */ | |||
116 | ||||
117 | namespace js { | |||
118 | ||||
119 | class Nursery; | |||
120 | ||||
121 | // The defaulted Enable parameter for the following two types is for restricting | |||
122 | // specializations with std::enable_if. | |||
123 | template <typename T, typename Enable = void> | |||
124 | struct BarrierMethods {}; | |||
125 | ||||
126 | template <typename Element, typename Wrapper, typename Enable = void> | |||
127 | class WrappedPtrOperations {}; | |||
128 | ||||
129 | template <typename Element, typename Wrapper> | |||
130 | class MutableWrappedPtrOperations | |||
131 | : public WrappedPtrOperations<Element, Wrapper> {}; | |||
132 | ||||
133 | template <typename T, typename Wrapper> | |||
134 | class RootedOperations : public MutableWrappedPtrOperations<T, Wrapper> {}; | |||
135 | ||||
136 | template <typename T, typename Wrapper> | |||
137 | class HandleOperations : public WrappedPtrOperations<T, Wrapper> {}; | |||
138 | ||||
139 | template <typename T, typename Wrapper> | |||
140 | class MutableHandleOperations : public MutableWrappedPtrOperations<T, Wrapper> { | |||
141 | }; | |||
142 | ||||
143 | template <typename T, typename Wrapper> | |||
144 | class HeapOperations : public MutableWrappedPtrOperations<T, Wrapper> {}; | |||
145 | ||||
146 | // Cannot use FOR_EACH_HEAP_ABLE_GC_POINTER_TYPE, as this would import too many | |||
147 | // macros into scope | |||
148 | ||||
149 | // Add a 2nd template parameter to allow conditionally enabling partial | |||
150 | // specializations via std::enable_if. | |||
151 | template <typename T, typename Enable = void> | |||
152 | struct IsHeapConstructibleType : public std::false_type {}; | |||
153 | ||||
154 | #define JS_DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE(T)template <> struct IsHeapConstructibleType<T> : public std::true_type {}; \ | |||
155 | template <> \ | |||
156 | struct IsHeapConstructibleType<T> : public std::true_type {}; | |||
157 | JS_FOR_EACH_PUBLIC_GC_POINTER_TYPE(JS_DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE)template <> struct IsHeapConstructibleType<JS::BigInt *> : public std::true_type {}; template <> struct IsHeapConstructibleType <JS::Symbol*> : public std::true_type {}; template < > struct IsHeapConstructibleType<JSAtom*> : public std ::true_type {}; template <> struct IsHeapConstructibleType <JSFunction*> : public std::true_type {}; template < > struct IsHeapConstructibleType<JSLinearString*> : public std::true_type {}; template <> struct IsHeapConstructibleType <JSObject*> : public std::true_type {}; template <> struct IsHeapConstructibleType<JSScript*> : public std ::true_type {}; template <> struct IsHeapConstructibleType <JSString*> : public std::true_type {}; | |||
158 | JS_FOR_EACH_PUBLIC_TAGGED_GC_POINTER_TYPE(JS_DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE)template <> struct IsHeapConstructibleType<JS::Value > : public std::true_type {}; template <> struct IsHeapConstructibleType <JS::PropertyKey> : public std::true_type {}; | |||
159 | // Note that JS_DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE is left defined, to allow | |||
160 | // declaring other types (eg from js/public/experimental/TypedData.h) to | |||
161 | // be used with Heap<>. | |||
162 | ||||
163 | namespace gc { | |||
164 | struct Cell; | |||
165 | } /* namespace gc */ | |||
166 | ||||
167 | // Important: Return a reference so passing a Rooted<T>, etc. to | |||
168 | // something that takes a |const T&| is not a GC hazard. | |||
169 | #define DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); } \ | |||
170 | operator const T&() const { return get(); } \ | |||
171 | const T& operator->() const { return get(); } | |||
172 | ||||
173 | // Assignment operators on a base class are hidden by the implicitly defined | |||
174 | // operator= on the derived class. Thus, define the operator= directly on the | |||
175 | // class as we would need to manually pass it through anyway. | |||
176 | #define DECLARE_POINTER_ASSIGN_OPS(Wrapper, T)Wrapper& operator=(const T& p) { set(p); return *this ; } Wrapper& operator=(T&& p) { set(std::move(p)) ; return *this; } Wrapper& operator=(const Wrapper& other ) { set(other.get()); return *this; } \ | |||
177 | Wrapper& operator=(const T& p) { \ | |||
178 | set(p); \ | |||
179 | return *this; \ | |||
180 | } \ | |||
181 | Wrapper& operator=(T&& p) { \ | |||
182 | set(std::move(p)); \ | |||
183 | return *this; \ | |||
184 | } \ | |||
185 | Wrapper& operator=(const Wrapper& other) { \ | |||
186 | set(other.get()); \ | |||
187 | return *this; \ | |||
188 | } | |||
189 | ||||
190 | #define DELETE_ASSIGNMENT_OPS(Wrapper, T)template <typename S> Wrapper<T>& operator=(S ) = delete; Wrapper<T>& operator=(const Wrapper< T>&) = delete; \ | |||
191 | template <typename S> \ | |||
192 | Wrapper<T>& operator=(S) = delete; \ | |||
193 | Wrapper<T>& operator=(const Wrapper<T>&) = delete; | |||
194 | ||||
195 | #define DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr)const T* address() const { return &(ptr); } const T& get () const { return (ptr); } \ | |||
196 | const T* address() const { return &(ptr); } \ | |||
197 | const T& get() const { return (ptr); } | |||
198 | ||||
199 | #define DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr)T* address() { return &(ptr); } T& get() { return (ptr ); } \ | |||
200 | T* address() { return &(ptr); } \ | |||
201 | T& get() { return (ptr); } | |||
202 | ||||
203 | } /* namespace js */ | |||
204 | ||||
205 | namespace JS { | |||
206 | ||||
207 | JS_PUBLIC_API void HeapObjectPostWriteBarrier(JSObject** objp, JSObject* prev, | |||
208 | JSObject* next); | |||
209 | JS_PUBLIC_API void HeapObjectWriteBarriers(JSObject** objp, JSObject* prev, | |||
210 | JSObject* next); | |||
211 | JS_PUBLIC_API void HeapStringWriteBarriers(JSString** objp, JSString* prev, | |||
212 | JSString* next); | |||
213 | JS_PUBLIC_API void HeapBigIntWriteBarriers(JS::BigInt** bip, JS::BigInt* prev, | |||
214 | JS::BigInt* next); | |||
215 | JS_PUBLIC_API void HeapScriptWriteBarriers(JSScript** objp, JSScript* prev, | |||
216 | JSScript* next); | |||
217 | ||||
218 | /** | |||
219 | * SafelyInitialized<T>::create() creates a safely-initialized |T|, suitable for | |||
220 | * use as a default value in situations requiring a safe but arbitrary |T| | |||
221 | * value. Implemented as a static method of a struct to allow partial | |||
222 | * specialization for subclasses via the Enable template parameter. | |||
223 | */ | |||
224 | template <typename T, typename Enable = void> | |||
225 | struct SafelyInitialized { | |||
226 | static T create() { | |||
227 | // This function wants to presume that |T()| -- which value-initializes a | |||
228 | // |T| per C++11 [expr.type.conv]p2 -- will produce a safely-initialized, | |||
229 | // safely-usable T that it can return. | |||
230 | ||||
231 | #if defined(XP_WIN) || defined(XP_DARWIN) || \ | |||
232 | (defined(XP_UNIX1) && !defined(__clang__1)) | |||
233 | ||||
234 | // That presumption holds for pointers, where value initialization produces | |||
235 | // a null pointer. | |||
236 | constexpr bool IsPointer = std::is_pointer_v<T>; | |||
237 | ||||
238 | // For classes and unions we *assume* that if |T|'s default constructor is | |||
239 | // non-trivial it'll initialize correctly. (This is unideal, but C++ | |||
240 | // doesn't offer a type trait indicating whether a class's constructor is | |||
241 | // user-defined, which better approximates our desired semantics.) | |||
242 | constexpr bool IsNonTriviallyDefaultConstructibleClassOrUnion = | |||
243 | (std::is_class_v<T> || std::is_union_v<T>) && | |||
244 | !std::is_trivially_default_constructible_v<T>; | |||
245 | ||||
246 | static_assert(IsPointer || IsNonTriviallyDefaultConstructibleClassOrUnion, | |||
247 | "T() must evaluate to a safely-initialized T"); | |||
248 | ||||
249 | #endif | |||
250 | ||||
251 | return T(); | |||
252 | } | |||
253 | }; | |||
254 | ||||
255 | #ifdef JS_DEBUG1 | |||
256 | /** | |||
257 | * For generational GC, assert that an object is in the tenured generation as | |||
258 | * opposed to being in the nursery. | |||
259 | */ | |||
260 | extern JS_PUBLIC_API void AssertGCThingMustBeTenured(JSObject* obj); | |||
261 | extern JS_PUBLIC_API void AssertGCThingIsNotNurseryAllocable( | |||
262 | js::gc::Cell* cell); | |||
263 | #else | |||
264 | inline void AssertGCThingMustBeTenured(JSObject* obj) {} | |||
265 | inline void AssertGCThingIsNotNurseryAllocable(js::gc::Cell* cell) {} | |||
266 | #endif | |||
267 | ||||
268 | /** | |||
269 | * The Heap<T> class is a heap-stored reference to a JS GC thing for use outside | |||
270 | * the JS engine. All members of heap classes that refer to GC things should use | |||
271 | * Heap<T> (or possibly TenuredHeap<T>, described below). | |||
272 | * | |||
273 | * Heap<T> is an abstraction that hides some of the complexity required to | |||
274 | * maintain GC invariants for the contained reference. It uses operator | |||
275 | * overloading to provide a normal pointer interface, but adds barriers to | |||
276 | * notify the GC of changes. | |||
277 | * | |||
278 | * Heap<T> implements the following barriers: | |||
279 | * | |||
280 | * - Pre-write barrier (necessary for incremental GC). | |||
281 | * - Post-write barrier (necessary for generational GC). | |||
282 | * - Read barrier (necessary for cycle collector integration). | |||
283 | * | |||
284 | * Heap<T> may be moved or destroyed outside of GC finalization and hence may be | |||
285 | * used in dynamic storage such as a Vector. | |||
286 | * | |||
287 | * Heap<T> instances must be traced when their containing object is traced to | |||
288 | * keep the pointed-to GC thing alive. | |||
289 | * | |||
290 | * Heap<T> objects should only be used on the heap. GC references stored on the | |||
291 | * C/C++ stack must use Rooted/Handle/MutableHandle instead. | |||
292 | * | |||
293 | * Type T must be a public GC pointer type. | |||
294 | */ | |||
295 | template <typename T> | |||
296 | class MOZ_NON_MEMMOVABLE Heap : public js::HeapOperations<T, Heap<T>> { | |||
297 | static_assert(js::IsHeapConstructibleType<T>::value, | |||
298 | "Type T must be a public GC pointer type"); | |||
299 | ||||
300 | public: | |||
301 | using ElementType = T; | |||
302 | ||||
303 | Heap() : ptr(SafelyInitialized<T>::create()) { | |||
304 | // No barriers are required for initialization to the default value. | |||
305 | static_assert(sizeof(T) == sizeof(Heap<T>), | |||
306 | "Heap<T> must be binary compatible with T."); | |||
307 | } | |||
308 | explicit Heap(const T& p) : ptr(p) { | |||
309 | writeBarriers(SafelyInitialized<T>::create(), ptr); | |||
310 | } | |||
311 | ||||
312 | /* | |||
313 | * For Heap, move semantics are equivalent to copy semantics. However, we want | |||
314 | * the copy constructor to be explicit, and an explicit move constructor | |||
315 | * breaks common usage of move semantics, so we need to define both, even | |||
316 | * though they are equivalent. | |||
317 | */ | |||
318 | explicit Heap(const Heap<T>& other) : ptr(other.unbarrieredGet()) { | |||
319 | writeBarriers(SafelyInitialized<T>::create(), ptr); | |||
320 | } | |||
321 | Heap(Heap<T>&& other) : ptr(other.unbarrieredGet()) { | |||
322 | writeBarriers(SafelyInitialized<T>::create(), ptr); | |||
323 | } | |||
324 | ||||
325 | Heap& operator=(Heap<T>&& other) { | |||
326 | set(other.unbarrieredGet()); | |||
327 | other.set(SafelyInitialized<T>::create()); | |||
328 | return *this; | |||
329 | } | |||
330 | // Copy constructor defined by DECLARE_POINTER_ASSIGN_OPS. | |||
331 | ||||
332 | ~Heap() { writeBarriers(ptr, SafelyInitialized<T>::create()); } | |||
333 | ||||
334 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
335 | DECLARE_POINTER_ASSIGN_OPS(Heap<T>, T)Heap<T>& operator=(const T& p) { set(p); return *this; } Heap<T>& operator=(T&& p) { set(std ::move(p)); return *this; } Heap<T>& operator=(const Heap<T>& other) { set(other.get()); return *this; }; | |||
336 | ||||
337 | void exposeToActiveJS() const { js::BarrierMethods<T>::exposeToJS(ptr); } | |||
338 | ||||
339 | const T& get() const { | |||
340 | exposeToActiveJS(); | |||
341 | return ptr; | |||
342 | } | |||
343 | const T& unbarrieredGet() const { return ptr; } | |||
344 | ||||
345 | void set(const T& newPtr) { | |||
346 | T tmp = ptr; | |||
347 | ptr = newPtr; | |||
348 | writeBarriers(tmp, ptr); | |||
349 | } | |||
350 | void unbarrieredSet(const T& newPtr) { ptr = newPtr; } | |||
351 | ||||
352 | T* unsafeAddress() { return &ptr; } | |||
353 | const T* unsafeAddress() const { return &ptr; } | |||
354 | ||||
355 | explicit operator bool() const { | |||
356 | return bool(js::BarrierMethods<T>::asGCThingOrNull(ptr)); | |||
357 | } | |||
358 | ||||
359 | private: | |||
360 | void writeBarriers(const T& prev, const T& next) { | |||
361 | js::BarrierMethods<T>::writeBarriers(&ptr, prev, next); | |||
362 | } | |||
363 | ||||
364 | T ptr; | |||
365 | }; | |||
366 | ||||
367 | namespace detail { | |||
368 | ||||
369 | template <typename T> | |||
370 | struct DefineComparisonOps<Heap<T>> : std::true_type { | |||
371 | static const T& get(const Heap<T>& v) { return v.unbarrieredGet(); } | |||
372 | }; | |||
373 | ||||
374 | } // namespace detail | |||
375 | ||||
376 | static MOZ_ALWAYS_INLINEinline bool ObjectIsTenured(JSObject* obj) { | |||
377 | return !js::gc::IsInsideNursery(reinterpret_cast<js::gc::Cell*>(obj)); | |||
378 | } | |||
379 | ||||
380 | static MOZ_ALWAYS_INLINEinline bool ObjectIsTenured(const Heap<JSObject*>& obj) { | |||
381 | return ObjectIsTenured(obj.unbarrieredGet()); | |||
382 | } | |||
383 | ||||
384 | static MOZ_ALWAYS_INLINEinline bool ObjectIsMarkedGray(JSObject* obj) { | |||
385 | auto cell = reinterpret_cast<js::gc::Cell*>(obj); | |||
386 | if (js::gc::IsInsideNursery(cell)) { | |||
387 | return false; | |||
388 | } | |||
389 | ||||
390 | auto tenuredCell = reinterpret_cast<js::gc::TenuredCell*>(cell); | |||
391 | return js::gc::detail::CellIsMarkedGrayIfKnown(tenuredCell); | |||
392 | } | |||
393 | ||||
394 | static MOZ_ALWAYS_INLINEinline bool ObjectIsMarkedGray( | |||
395 | const JS::Heap<JSObject*>& obj) { | |||
396 | return ObjectIsMarkedGray(obj.unbarrieredGet()); | |||
397 | } | |||
398 | ||||
399 | // The following *IsNotGray functions take account of the eventual | |||
400 | // gray marking state at the end of any ongoing incremental GC by | |||
401 | // delaying the checks if necessary. | |||
402 | ||||
403 | #ifdef DEBUG1 | |||
404 | ||||
405 | inline void AssertCellIsNotGray(const js::gc::Cell* maybeCell) { | |||
406 | if (maybeCell) { | |||
407 | js::gc::detail::AssertCellIsNotGray(maybeCell); | |||
408 | } | |||
409 | } | |||
410 | ||||
411 | inline void AssertObjectIsNotGray(JSObject* maybeObj) { | |||
412 | AssertCellIsNotGray(reinterpret_cast<js::gc::Cell*>(maybeObj)); | |||
413 | } | |||
414 | ||||
415 | inline void AssertObjectIsNotGray(const JS::Heap<JSObject*>& obj) { | |||
416 | AssertObjectIsNotGray(obj.unbarrieredGet()); | |||
417 | } | |||
418 | ||||
419 | #else | |||
420 | ||||
421 | inline void AssertCellIsNotGray(js::gc::Cell* maybeCell) {} | |||
422 | inline void AssertObjectIsNotGray(JSObject* maybeObj) {} | |||
423 | inline void AssertObjectIsNotGray(const JS::Heap<JSObject*>& obj) {} | |||
424 | ||||
425 | #endif | |||
426 | ||||
427 | /** | |||
428 | * The TenuredHeap<T> class is similar to the Heap<T> class above in that it | |||
429 | * encapsulates the GC concerns of an on-heap reference to a JS object. However, | |||
430 | * it has two important differences: | |||
431 | * | |||
432 | * 1) Pointers which are statically known to only reference "tenured" objects | |||
433 | * can avoid the extra overhead of SpiderMonkey's post write barriers. | |||
434 | * | |||
435 | * 2) Objects in the "tenured" heap have stronger alignment restrictions than | |||
436 | * those in the "nursery", so it is possible to store flags in the lower | |||
437 | * bits of pointers known to be tenured. TenuredHeap wraps a normal tagged | |||
438 | * pointer with a nice API for accessing the flag bits and adds various | |||
439 | * assertions to ensure that it is not mis-used. | |||
440 | * | |||
441 | * GC things are said to be "tenured" when they are located in the long-lived | |||
442 | * heap: e.g. they have gained tenure as an object by surviving past at least | |||
443 | * one GC. For performance, SpiderMonkey allocates some things which are known | |||
444 | * to normally be long lived directly into the tenured generation; for example, | |||
445 | * global objects. Additionally, SpiderMonkey does not visit individual objects | |||
446 | * when deleting non-tenured objects, so object with finalizers are also always | |||
447 | * tenured; for instance, this includes most DOM objects. | |||
448 | * | |||
449 | * The considerations to keep in mind when using a TenuredHeap<T> vs a normal | |||
450 | * Heap<T> are: | |||
451 | * | |||
452 | * - It is invalid for a TenuredHeap<T> to refer to a non-tenured thing. | |||
453 | * - It is however valid for a Heap<T> to refer to a tenured thing. | |||
454 | * - It is not possible to store flag bits in a Heap<T>. | |||
455 | */ | |||
456 | template <typename T> | |||
457 | class TenuredHeap : public js::HeapOperations<T, TenuredHeap<T>> { | |||
458 | static_assert(js::IsHeapConstructibleType<T>::value, | |||
459 | "Type T must be a public GC pointer type"); | |||
460 | ||||
461 | public: | |||
462 | using ElementType = T; | |||
463 | ||||
464 | TenuredHeap() : bits(0) { | |||
465 | static_assert(sizeof(T) == sizeof(TenuredHeap<T>), | |||
466 | "TenuredHeap<T> must be binary compatible with T."); | |||
467 | } | |||
468 | ||||
469 | explicit TenuredHeap(T p) : bits(0) { unbarrieredSetPtr(p); } | |||
470 | explicit TenuredHeap(const TenuredHeap<T>& p) : bits(0) { | |||
471 | unbarrieredSetPtr(p.getPtr()); | |||
472 | } | |||
473 | ||||
474 | TenuredHeap<T>& operator=(T p) { | |||
475 | setPtr(p); | |||
476 | return *this; | |||
477 | } | |||
478 | TenuredHeap<T>& operator=(const TenuredHeap<T>& other) { | |||
479 | preWriteBarrier(); | |||
480 | bits = other.bits; | |||
481 | return *this; | |||
482 | } | |||
483 | ||||
484 | ~TenuredHeap() { preWriteBarrier(); } | |||
485 | ||||
486 | void setPtr(T newPtr) { | |||
487 | preWriteBarrier(); | |||
488 | unbarrieredSetPtr(newPtr); | |||
489 | } | |||
490 | void unbarrieredSetPtr(T newPtr) { | |||
491 | MOZ_ASSERT((reinterpret_cast<uintptr_t>(newPtr) & flagsMask) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((reinterpret_cast<uintptr_t>(newPtr) & flagsMask ) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((reinterpret_cast<uintptr_t>(newPtr) & flagsMask ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(reinterpret_cast<uintptr_t>(newPtr) & flagsMask) == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 491); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(newPtr) & flagsMask) == 0" ")"); do { *((volatile int*)__null) = 491; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
492 | MOZ_ASSERT(js::gc::IsCellPointerValidOrNull(newPtr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(js::gc::IsCellPointerValidOrNull(newPtr))>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(js::gc::IsCellPointerValidOrNull(newPtr)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("js::gc::IsCellPointerValidOrNull(newPtr)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 492); AnnotateMozCrashReason("MOZ_ASSERT" "(" "js::gc::IsCellPointerValidOrNull(newPtr)" ")"); do { *((volatile int*)__null) = 492; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
493 | if (newPtr) { | |||
494 | AssertGCThingMustBeTenured(newPtr); | |||
495 | } | |||
496 | bits = (bits & flagsMask) | reinterpret_cast<uintptr_t>(newPtr); | |||
497 | } | |||
498 | ||||
499 | void setFlags(uintptr_t flagsToSet) { | |||
500 | MOZ_ASSERT((flagsToSet & ~flagsMask) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((flagsToSet & ~flagsMask) == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!((flagsToSet & ~flagsMask ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(flagsToSet & ~flagsMask) == 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 500); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(flagsToSet & ~flagsMask) == 0" ")"); do { *((volatile int*)__null) = 500; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
501 | bits |= flagsToSet; | |||
502 | } | |||
503 | ||||
504 | void unsetFlags(uintptr_t flagsToUnset) { | |||
505 | MOZ_ASSERT((flagsToUnset & ~flagsMask) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((flagsToUnset & ~flagsMask) == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!((flagsToUnset & ~flagsMask ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(flagsToUnset & ~flagsMask) == 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 505); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(flagsToUnset & ~flagsMask) == 0" ")"); do { *((volatile int*)__null) = 505; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
506 | bits &= ~flagsToUnset; | |||
507 | } | |||
508 | ||||
509 | bool hasFlag(uintptr_t flag) const { | |||
510 | MOZ_ASSERT((flag & ~flagsMask) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((flag & ~flagsMask) == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!((flag & ~flagsMask) == 0 ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "(flag & ~flagsMask) == 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 510); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(flag & ~flagsMask) == 0" ")"); do { *((volatile int*)__null) = 510; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
511 | return (bits & flag) != 0; | |||
512 | } | |||
513 | ||||
514 | T unbarrieredGetPtr() const { return reinterpret_cast<T>(bits & ~flagsMask); } | |||
515 | uintptr_t getFlags() const { return bits & flagsMask; } | |||
516 | ||||
517 | void exposeToActiveJS() const { | |||
518 | js::BarrierMethods<T>::exposeToJS(unbarrieredGetPtr()); | |||
519 | } | |||
520 | T getPtr() const { | |||
521 | exposeToActiveJS(); | |||
522 | return unbarrieredGetPtr(); | |||
523 | } | |||
524 | ||||
525 | operator T() const { return getPtr(); } | |||
526 | T operator->() const { return getPtr(); } | |||
527 | ||||
528 | explicit operator bool() const { | |||
529 | return bool(js::BarrierMethods<T>::asGCThingOrNull(unbarrieredGetPtr())); | |||
530 | } | |||
531 | ||||
532 | private: | |||
533 | enum { | |||
534 | maskBits = 3, | |||
535 | flagsMask = (1 << maskBits) - 1, | |||
536 | }; | |||
537 | ||||
538 | void preWriteBarrier() { | |||
539 | if (T prev = unbarrieredGetPtr()) { | |||
540 | JS::IncrementalPreWriteBarrier(JS::GCCellPtr(prev)); | |||
541 | } | |||
542 | } | |||
543 | ||||
544 | uintptr_t bits; | |||
545 | }; | |||
546 | ||||
547 | namespace detail { | |||
548 | ||||
549 | template <typename T> | |||
550 | struct DefineComparisonOps<TenuredHeap<T>> : std::true_type { | |||
551 | static const T get(const TenuredHeap<T>& v) { return v.unbarrieredGetPtr(); } | |||
552 | }; | |||
553 | ||||
554 | } // namespace detail | |||
555 | ||||
556 | // std::swap uses a stack temporary, which prevents classes like Heap<T> | |||
557 | // from being declared MOZ_HEAP_CLASS. | |||
558 | template <typename T> | |||
559 | void swap(TenuredHeap<T>& aX, TenuredHeap<T>& aY) { | |||
560 | T tmp = aX; | |||
561 | aX = aY; | |||
562 | aY = tmp; | |||
563 | } | |||
564 | ||||
565 | template <typename T> | |||
566 | void swap(Heap<T>& aX, Heap<T>& aY) { | |||
567 | T tmp = aX; | |||
568 | aX = aY; | |||
569 | aY = tmp; | |||
570 | } | |||
571 | ||||
572 | static MOZ_ALWAYS_INLINEinline bool ObjectIsMarkedGray( | |||
573 | const JS::TenuredHeap<JSObject*>& obj) { | |||
574 | return ObjectIsMarkedGray(obj.unbarrieredGetPtr()); | |||
575 | } | |||
576 | ||||
577 | template <typename T> | |||
578 | class MutableHandle; | |||
579 | template <typename T> | |||
580 | class Rooted; | |||
581 | template <typename T, size_t N = SIZE_MAX(18446744073709551615UL)> | |||
582 | class RootedField; | |||
583 | template <typename T> | |||
584 | class PersistentRooted; | |||
585 | ||||
586 | /** | |||
587 | * Reference to a T that has been rooted elsewhere. This is most useful | |||
588 | * as a parameter type, which guarantees that the T lvalue is properly | |||
589 | * rooted. See "Move GC Stack Rooting" above. | |||
590 | * | |||
591 | * If you want to add additional methods to Handle for a specific | |||
592 | * specialization, define a HandleOperations<T> specialization containing them. | |||
593 | */ | |||
594 | template <typename T> | |||
595 | class MOZ_NONHEAP_CLASS Handle : public js::HandleOperations<T, Handle<T>> { | |||
596 | friend class MutableHandle<T>; | |||
597 | ||||
598 | public: | |||
599 | using ElementType = T; | |||
600 | ||||
601 | Handle(const Handle<T>&) = default; | |||
602 | ||||
603 | /* Creates a handle from a handle of a type convertible to T. */ | |||
604 | template <typename S> | |||
605 | MOZ_IMPLICIT Handle( | |||
606 | Handle<S> handle, | |||
607 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy = 0) { | |||
608 | static_assert(sizeof(Handle<T>) == sizeof(T*), | |||
609 | "Handle must be binary compatible with T*."); | |||
610 | ptr = reinterpret_cast<const T*>(handle.address()); | |||
611 | } | |||
612 | ||||
613 | MOZ_IMPLICIT Handle(decltype(nullptr)) { | |||
614 | static_assert(std::is_pointer_v<T>, | |||
615 | "nullptr_t overload not valid for non-pointer types"); | |||
616 | static void* const ConstNullValue = nullptr; | |||
617 | ptr = reinterpret_cast<const T*>(&ConstNullValue); | |||
618 | } | |||
619 | ||||
620 | MOZ_IMPLICIT Handle(MutableHandle<T> handle) { ptr = handle.address(); } | |||
621 | ||||
622 | /* | |||
623 | * Take care when calling this method! | |||
624 | * | |||
625 | * This creates a Handle from the raw location of a T. | |||
626 | * | |||
627 | * It should be called only if the following conditions hold: | |||
628 | * | |||
629 | * 1) the location of the T is guaranteed to be marked (for some reason | |||
630 | * other than being a Rooted), e.g., if it is guaranteed to be reachable | |||
631 | * from an implicit root. | |||
632 | * | |||
633 | * 2) the contents of the location are immutable, or at least cannot change | |||
634 | * for the lifetime of the handle, as its users may not expect its value | |||
635 | * to change underneath them. | |||
636 | */ | |||
637 | static constexpr Handle fromMarkedLocation(const T* p) { | |||
638 | return Handle(p, DeliberatelyChoosingThisOverload, | |||
639 | ImUsingThisOnlyInFromFromMarkedLocation); | |||
640 | } | |||
641 | ||||
642 | /* | |||
643 | * Construct a handle from an explicitly rooted location. This is the | |||
644 | * normal way to create a handle, and normally happens implicitly. | |||
645 | */ | |||
646 | template <typename S> | |||
647 | inline MOZ_IMPLICIT Handle( | |||
648 | const Rooted<S>& root, | |||
649 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy = 0); | |||
650 | ||||
651 | template <typename S> | |||
652 | inline MOZ_IMPLICIT Handle( | |||
653 | const PersistentRooted<S>& root, | |||
654 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy = 0); | |||
655 | ||||
656 | /* Construct a read only handle from a mutable handle. */ | |||
657 | template <typename S> | |||
658 | inline MOZ_IMPLICIT Handle( | |||
659 | MutableHandle<S>& root, | |||
660 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy = 0); | |||
661 | ||||
662 | template <size_t N, typename S> | |||
663 | inline MOZ_IMPLICIT Handle( | |||
664 | const RootedField<S, N>& rootedField, | |||
665 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy = 0); | |||
666 | ||||
667 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
668 | DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr)const T* address() const { return &(*ptr); } const T& get() const { return (*ptr); }; | |||
669 | ||||
670 | private: | |||
671 | Handle() = default; | |||
672 | DELETE_ASSIGNMENT_OPS(Handle, T)template <typename S> Handle<T>& operator=(S) = delete; Handle<T>& operator=(const Handle<T> &) = delete;; | |||
673 | ||||
674 | enum Disambiguator { DeliberatelyChoosingThisOverload = 42 }; | |||
675 | enum CallerIdentity { ImUsingThisOnlyInFromFromMarkedLocation = 17 }; | |||
676 | constexpr Handle(const T* p, Disambiguator, CallerIdentity) : ptr(p) {} | |||
677 | ||||
678 | const T* ptr; | |||
679 | }; | |||
680 | ||||
681 | namespace detail { | |||
682 | ||||
683 | template <typename T> | |||
684 | struct DefineComparisonOps<Handle<T>> : std::true_type { | |||
685 | static const T& get(const Handle<T>& v) { return v.get(); } | |||
686 | }; | |||
687 | ||||
688 | } // namespace detail | |||
689 | ||||
690 | /** | |||
691 | * Similar to a handle, but the underlying storage can be changed. This is | |||
692 | * useful for outparams. | |||
693 | * | |||
694 | * If you want to add additional methods to MutableHandle for a specific | |||
695 | * specialization, define a MutableHandleOperations<T> specialization containing | |||
696 | * them. | |||
697 | */ | |||
698 | template <typename T> | |||
699 | class MOZ_STACK_CLASS MutableHandle | |||
700 | : public js::MutableHandleOperations<T, MutableHandle<T>> { | |||
701 | public: | |||
702 | using ElementType = T; | |||
703 | ||||
704 | inline MOZ_IMPLICIT MutableHandle(Rooted<T>* root); | |||
705 | template <size_t N> | |||
706 | inline MOZ_IMPLICIT MutableHandle(RootedField<T, N>* root); | |||
707 | inline MOZ_IMPLICIT MutableHandle(PersistentRooted<T>* root); | |||
708 | ||||
709 | private: | |||
710 | // Disallow nullptr for overloading purposes. | |||
711 | MutableHandle(decltype(nullptr)) = delete; | |||
712 | ||||
713 | public: | |||
714 | MutableHandle(const MutableHandle<T>&) = default; | |||
715 | void set(const T& v) { | |||
716 | *ptr = v; | |||
717 | MOZ_ASSERT(GCPolicy<T>::isValid(*ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(*ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(* ptr)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(*ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 717); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(*ptr)" ")"); do { *((volatile int*)__null) = 717; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
718 | } | |||
719 | void set(T&& v) { | |||
720 | *ptr = std::move(v); | |||
721 | MOZ_ASSERT(GCPolicy<T>::isValid(*ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(*ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(* ptr)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(*ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 721); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(*ptr)" ")"); do { *((volatile int*)__null) = 721; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
722 | } | |||
723 | ||||
724 | /* | |||
725 | * This may be called only if the location of the T is guaranteed | |||
726 | * to be marked (for some reason other than being a Rooted), | |||
727 | * e.g., if it is guaranteed to be reachable from an implicit root. | |||
728 | * | |||
729 | * Create a MutableHandle from a raw location of a T. | |||
730 | */ | |||
731 | static MutableHandle fromMarkedLocation(T* p) { | |||
732 | MutableHandle h; | |||
733 | h.ptr = p; | |||
734 | return h; | |||
735 | } | |||
736 | ||||
737 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
738 | DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr)const T* address() const { return &(*ptr); } const T& get() const { return (*ptr); }; | |||
739 | DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(*ptr)T* address() { return &(*ptr); } T& get() { return (* ptr); }; | |||
740 | ||||
741 | private: | |||
742 | MutableHandle() = default; | |||
743 | DELETE_ASSIGNMENT_OPS(MutableHandle, T)template <typename S> MutableHandle<T>& operator =(S) = delete; MutableHandle<T>& operator=(const MutableHandle <T>&) = delete;; | |||
744 | ||||
745 | T* ptr; | |||
746 | }; | |||
747 | ||||
748 | namespace detail { | |||
749 | ||||
750 | template <typename T> | |||
751 | struct DefineComparisonOps<MutableHandle<T>> : std::true_type { | |||
752 | static const T& get(const MutableHandle<T>& v) { return v.get(); } | |||
753 | }; | |||
754 | ||||
755 | } // namespace detail | |||
756 | ||||
757 | } /* namespace JS */ | |||
758 | ||||
759 | namespace js { | |||
760 | ||||
761 | namespace detail { | |||
762 | ||||
763 | // Default implementations for barrier methods on GC thing pointers. | |||
764 | template <typename T> | |||
765 | struct PtrBarrierMethodsBase { | |||
766 | static T* initial() { return nullptr; } | |||
767 | static gc::Cell* asGCThingOrNull(T* v) { | |||
768 | if (!v) { | |||
769 | return nullptr; | |||
770 | } | |||
771 | MOZ_ASSERT(uintptr_t(v) > 32)do { static_assert( mozilla::detail::AssertionConditionType< decltype(uintptr_t(v) > 32)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(uintptr_t(v) > 32))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("uintptr_t(v) > 32" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 771); AnnotateMozCrashReason("MOZ_ASSERT" "(" "uintptr_t(v) > 32" ")"); do { *((volatile int*)__null) = 771; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
772 | return reinterpret_cast<gc::Cell*>(v); | |||
773 | } | |||
774 | static void exposeToJS(T* t) { | |||
775 | if (t) { | |||
776 | js::gc::ExposeGCThingToActiveJS(JS::GCCellPtr(t)); | |||
777 | } | |||
778 | } | |||
779 | static void readBarrier(T* t) { | |||
780 | if (t) { | |||
781 | js::gc::IncrementalReadBarrier(JS::GCCellPtr(t)); | |||
782 | } | |||
783 | } | |||
784 | }; | |||
785 | ||||
786 | } // namespace detail | |||
787 | ||||
788 | template <typename T> | |||
789 | struct BarrierMethods<T*> : public detail::PtrBarrierMethodsBase<T> { | |||
790 | static void writeBarriers(T** vp, T* prev, T* next) { | |||
791 | if (prev) { | |||
792 | JS::IncrementalPreWriteBarrier(JS::GCCellPtr(prev)); | |||
793 | } | |||
794 | if (next) { | |||
795 | JS::AssertGCThingIsNotNurseryAllocable( | |||
796 | reinterpret_cast<js::gc::Cell*>(next)); | |||
797 | } | |||
798 | } | |||
799 | }; | |||
800 | ||||
801 | template <> | |||
802 | struct BarrierMethods<JSObject*> | |||
803 | : public detail::PtrBarrierMethodsBase<JSObject> { | |||
804 | static void writeBarriers(JSObject** vp, JSObject* prev, JSObject* next) { | |||
805 | JS::HeapObjectWriteBarriers(vp, prev, next); | |||
806 | } | |||
807 | static void postWriteBarrier(JSObject** vp, JSObject* prev, JSObject* next) { | |||
808 | JS::HeapObjectPostWriteBarrier(vp, prev, next); | |||
809 | } | |||
810 | static void exposeToJS(JSObject* obj) { | |||
811 | if (obj) { | |||
812 | JS::ExposeObjectToActiveJS(obj); | |||
813 | } | |||
814 | } | |||
815 | }; | |||
816 | ||||
817 | template <> | |||
818 | struct BarrierMethods<JSFunction*> | |||
819 | : public detail::PtrBarrierMethodsBase<JSFunction> { | |||
820 | static void writeBarriers(JSFunction** vp, JSFunction* prev, | |||
821 | JSFunction* next) { | |||
822 | JS::HeapObjectWriteBarriers(reinterpret_cast<JSObject**>(vp), | |||
823 | reinterpret_cast<JSObject*>(prev), | |||
824 | reinterpret_cast<JSObject*>(next)); | |||
825 | } | |||
826 | static void exposeToJS(JSFunction* fun) { | |||
827 | if (fun) { | |||
828 | JS::ExposeObjectToActiveJS(reinterpret_cast<JSObject*>(fun)); | |||
829 | } | |||
830 | } | |||
831 | }; | |||
832 | ||||
833 | template <> | |||
834 | struct BarrierMethods<JSString*> | |||
835 | : public detail::PtrBarrierMethodsBase<JSString> { | |||
836 | static void writeBarriers(JSString** vp, JSString* prev, JSString* next) { | |||
837 | JS::HeapStringWriteBarriers(vp, prev, next); | |||
838 | } | |||
839 | }; | |||
840 | ||||
841 | template <> | |||
842 | struct BarrierMethods<JSScript*> | |||
843 | : public detail::PtrBarrierMethodsBase<JSScript> { | |||
844 | static void writeBarriers(JSScript** vp, JSScript* prev, JSScript* next) { | |||
845 | JS::HeapScriptWriteBarriers(vp, prev, next); | |||
846 | } | |||
847 | }; | |||
848 | ||||
849 | template <> | |||
850 | struct BarrierMethods<JS::BigInt*> | |||
851 | : public detail::PtrBarrierMethodsBase<JS::BigInt> { | |||
852 | static void writeBarriers(JS::BigInt** vp, JS::BigInt* prev, | |||
853 | JS::BigInt* next) { | |||
854 | JS::HeapBigIntWriteBarriers(vp, prev, next); | |||
855 | } | |||
856 | }; | |||
857 | ||||
858 | // Provide hash codes for Cell kinds that may be relocated and, thus, not have | |||
859 | // a stable address to use as the base for a hash code. Instead of the address, | |||
860 | // this hasher uses Cell::getUniqueId to provide exact matches and as a base | |||
861 | // for generating hash codes. | |||
862 | // | |||
863 | // Note: this hasher, like PointerHasher can "hash" a nullptr. While a nullptr | |||
864 | // would not likely be a useful key, there are some cases where being able to | |||
865 | // hash a nullptr is useful, either on purpose or because of bugs: | |||
866 | // (1) existence checks where the key may happen to be null and (2) some | |||
867 | // aggregate Lookup kinds embed a JSObject* that is frequently null and do not | |||
868 | // null test before dispatching to the hasher. | |||
869 | template <typename T> | |||
870 | struct JS_PUBLIC_API StableCellHasher { | |||
871 | using Key = T; | |||
872 | using Lookup = T; | |||
873 | ||||
874 | static bool maybeGetHash(const Lookup& l, mozilla::HashNumber* hashOut); | |||
875 | static bool ensureHash(const Lookup& l, HashNumber* hashOut); | |||
876 | static HashNumber hash(const Lookup& l); | |||
877 | static bool match(const Key& k, const Lookup& l); | |||
878 | // The rekey hash policy method is not provided since you dont't need to | |||
879 | // rekey any more when using this policy. | |||
880 | }; | |||
881 | ||||
882 | template <typename T> | |||
883 | struct JS_PUBLIC_API StableCellHasher<JS::Heap<T>> { | |||
884 | using Key = JS::Heap<T>; | |||
885 | using Lookup = T; | |||
886 | ||||
887 | static bool maybeGetHash(const Lookup& l, HashNumber* hashOut) { | |||
888 | return StableCellHasher<T>::maybeGetHash(l, hashOut); | |||
889 | } | |||
890 | static bool ensureHash(const Lookup& l, HashNumber* hashOut) { | |||
891 | return StableCellHasher<T>::ensureHash(l, hashOut); | |||
892 | } | |||
893 | static HashNumber hash(const Lookup& l) { | |||
894 | return StableCellHasher<T>::hash(l); | |||
895 | } | |||
896 | static bool match(const Key& k, const Lookup& l) { | |||
897 | return StableCellHasher<T>::match(k.unbarrieredGet(), l); | |||
898 | } | |||
899 | }; | |||
900 | ||||
901 | } // namespace js | |||
902 | ||||
903 | namespace mozilla { | |||
904 | ||||
905 | template <typename T> | |||
906 | struct FallibleHashMethods<js::StableCellHasher<T>> { | |||
907 | template <typename Lookup> | |||
908 | static bool maybeGetHash(Lookup&& l, HashNumber* hashOut) { | |||
909 | return js::StableCellHasher<T>::maybeGetHash(std::forward<Lookup>(l), | |||
910 | hashOut); | |||
911 | } | |||
912 | template <typename Lookup> | |||
913 | static bool ensureHash(Lookup&& l, HashNumber* hashOut) { | |||
914 | return js::StableCellHasher<T>::ensureHash(std::forward<Lookup>(l), | |||
915 | hashOut); | |||
916 | } | |||
917 | }; | |||
918 | ||||
919 | } // namespace mozilla | |||
920 | ||||
921 | namespace js { | |||
922 | ||||
923 | struct VirtualTraceable { | |||
924 | virtual ~VirtualTraceable() = default; | |||
925 | virtual void trace(JSTracer* trc, const char* name) = 0; | |||
926 | }; | |||
927 | ||||
928 | class StackRootedBase { | |||
929 | public: | |||
930 | StackRootedBase* previous() { return prev; } | |||
931 | ||||
932 | protected: | |||
933 | StackRootedBase** stack; | |||
934 | StackRootedBase* prev; | |||
935 | ||||
936 | template <typename T> | |||
937 | auto* derived() { | |||
938 | return static_cast<JS::Rooted<T>*>(this); | |||
939 | } | |||
940 | }; | |||
941 | ||||
942 | class PersistentRootedBase | |||
943 | : protected mozilla::LinkedListElement<PersistentRootedBase> { | |||
944 | protected: | |||
945 | friend class mozilla::LinkedList<PersistentRootedBase>; | |||
946 | friend class mozilla::LinkedListElement<PersistentRootedBase>; | |||
947 | ||||
948 | template <typename T> | |||
949 | auto* derived() { | |||
950 | return static_cast<JS::PersistentRooted<T>*>(this); | |||
951 | } | |||
952 | }; | |||
953 | ||||
954 | struct StackRootedTraceableBase : public StackRootedBase, | |||
955 | public VirtualTraceable {}; | |||
956 | ||||
957 | class PersistentRootedTraceableBase : public PersistentRootedBase, | |||
958 | public VirtualTraceable {}; | |||
959 | ||||
960 | template <typename Base, typename T> | |||
961 | class TypedRootedGCThingBase : public Base { | |||
962 | public: | |||
963 | void trace(JSTracer* trc, const char* name); | |||
964 | }; | |||
965 | ||||
966 | template <typename Base, typename T> | |||
967 | class TypedRootedTraceableBase : public Base { | |||
968 | public: | |||
969 | void trace(JSTracer* trc, const char* name) override { | |||
970 | auto* self = this->template derived<T>(); | |||
971 | JS::GCPolicy<T>::trace(trc, self->address(), name); | |||
972 | } | |||
973 | }; | |||
974 | ||||
975 | template <typename T> | |||
976 | struct RootedTraceableTraits { | |||
977 | using StackBase = TypedRootedTraceableBase<StackRootedTraceableBase, T>; | |||
978 | using PersistentBase = | |||
979 | TypedRootedTraceableBase<PersistentRootedTraceableBase, T>; | |||
980 | }; | |||
981 | ||||
982 | template <typename T> | |||
983 | struct RootedGCThingTraits { | |||
984 | using StackBase = TypedRootedGCThingBase<StackRootedBase, T>; | |||
985 | using PersistentBase = TypedRootedGCThingBase<PersistentRootedBase, T>; | |||
986 | }; | |||
987 | ||||
988 | } /* namespace js */ | |||
989 | ||||
990 | namespace JS { | |||
991 | ||||
992 | class JS_PUBLIC_API AutoGCRooter; | |||
993 | ||||
994 | enum class AutoGCRooterKind : uint8_t { | |||
995 | WrapperVector, /* js::AutoWrapperVector */ | |||
996 | Wrapper, /* js::AutoWrapperRooter */ | |||
997 | Custom, /* js::CustomAutoRooter */ | |||
998 | ||||
999 | Limit | |||
1000 | }; | |||
1001 | ||||
1002 | using RootedListHeads = mozilla::EnumeratedArray<RootKind, js::StackRootedBase*, | |||
1003 | size_t(RootKind::Limit)>; | |||
1004 | ||||
1005 | using AutoRooterListHeads = | |||
1006 | mozilla::EnumeratedArray<AutoGCRooterKind, AutoGCRooter*, | |||
1007 | size_t(AutoGCRooterKind::Limit)>; | |||
1008 | ||||
1009 | // Superclass of JSContext which can be used for rooting data in use by the | |||
1010 | // current thread but that does not provide all the functions of a JSContext. | |||
1011 | class RootingContext { | |||
1012 | // Stack GC roots for Rooted GC heap pointers. | |||
1013 | RootedListHeads stackRoots_; | |||
1014 | template <typename T> | |||
1015 | friend class Rooted; | |||
1016 | ||||
1017 | // Stack GC roots for AutoFooRooter classes. | |||
1018 | AutoRooterListHeads autoGCRooters_; | |||
1019 | friend class AutoGCRooter; | |||
1020 | ||||
1021 | // Gecko profiling metadata. | |||
1022 | // This isn't really rooting related. It's only here because we want | |||
1023 | // GetContextProfilingStackIfEnabled to be inlineable into non-JS code, and | |||
1024 | // we didn't want to add another superclass of JSContext just for this. | |||
1025 | js::GeckoProfilerThread geckoProfiler_; | |||
1026 | ||||
1027 | public: | |||
1028 | explicit RootingContext(js::Nursery* nursery); | |||
1029 | ||||
1030 | void traceStackRoots(JSTracer* trc); | |||
1031 | ||||
1032 | /* Implemented in gc/RootMarking.cpp. */ | |||
1033 | void traceAllGCRooters(JSTracer* trc); | |||
1034 | void traceWrapperGCRooters(JSTracer* trc); | |||
1035 | static void traceGCRooterList(JSTracer* trc, AutoGCRooter* head); | |||
1036 | ||||
1037 | void checkNoGCRooters(); | |||
1038 | ||||
1039 | js::GeckoProfilerThread& geckoProfiler() { return geckoProfiler_; } | |||
1040 | ||||
1041 | js::Nursery& nursery() const { | |||
1042 | MOZ_ASSERT(nursery_)do { static_assert( mozilla::detail::AssertionConditionType< decltype(nursery_)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(nursery_))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("nursery_", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1042); AnnotateMozCrashReason("MOZ_ASSERT" "(" "nursery_" ")" ); do { *((volatile int*)__null) = 1042; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1043 | return *nursery_; | |||
1044 | } | |||
1045 | ||||
1046 | protected: | |||
1047 | // The remaining members in this class should only be accessed through | |||
1048 | // JSContext pointers. They are unrelated to rooting and are in place so | |||
1049 | // that inlined API functions can directly access the data. | |||
1050 | ||||
1051 | /* The nursery. Null for non-main-thread contexts. */ | |||
1052 | js::Nursery* nursery_; | |||
1053 | ||||
1054 | /* The current zone. */ | |||
1055 | Zone* zone_; | |||
1056 | ||||
1057 | /* The current realm. */ | |||
1058 | Realm* realm_; | |||
1059 | ||||
1060 | public: | |||
1061 | /* Limit pointer for checking native stack consumption. */ | |||
1062 | JS::NativeStackLimit nativeStackLimit[StackKindCount]; | |||
1063 | ||||
1064 | #ifdef __wasi__ | |||
1065 | // For WASI we can't catch call-stack overflows with stack-pointer checks, so | |||
1066 | // we count recursion depth with RAII based AutoCheckRecursionLimit. | |||
1067 | uint32_t wasiRecursionDepth = 0u; | |||
1068 | ||||
1069 | static constexpr uint32_t wasiRecursionDepthLimit = 350u; | |||
1070 | #endif // __wasi__ | |||
1071 | ||||
1072 | static const RootingContext* get(const JSContext* cx) { | |||
1073 | return reinterpret_cast<const RootingContext*>(cx); | |||
1074 | } | |||
1075 | ||||
1076 | static RootingContext* get(JSContext* cx) { | |||
1077 | return reinterpret_cast<RootingContext*>(cx); | |||
1078 | } | |||
1079 | ||||
1080 | friend JS::Realm* js::GetContextRealm(const JSContext* cx); | |||
1081 | friend JS::Zone* js::GetContextZone(const JSContext* cx); | |||
1082 | }; | |||
1083 | ||||
1084 | class JS_PUBLIC_API AutoGCRooter { | |||
1085 | public: | |||
1086 | using Kind = AutoGCRooterKind; | |||
1087 | ||||
1088 | AutoGCRooter(JSContext* cx, Kind kind) | |||
1089 | : AutoGCRooter(JS::RootingContext::get(cx), kind) {} | |||
1090 | AutoGCRooter(RootingContext* cx, Kind kind) | |||
1091 | : down(cx->autoGCRooters_[kind]), | |||
1092 | stackTop(&cx->autoGCRooters_[kind]), | |||
1093 | kind_(kind) { | |||
1094 | MOZ_ASSERT(this != *stackTop)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != *stackTop)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != *stackTop))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != *stackTop" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1094); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != *stackTop" ")"); do { *((volatile int*)__null) = 1094; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1095 | *stackTop = this; | |||
1096 | } | |||
1097 | ||||
1098 | ~AutoGCRooter() { | |||
1099 | MOZ_ASSERT(this == *stackTop)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this == *stackTop)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this == *stackTop))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this == *stackTop" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1099); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this == *stackTop" ")"); do { *((volatile int*)__null) = 1099; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1100 | *stackTop = down; | |||
1101 | } | |||
1102 | ||||
1103 | void trace(JSTracer* trc); | |||
1104 | ||||
1105 | private: | |||
1106 | friend class RootingContext; | |||
1107 | ||||
1108 | AutoGCRooter* const down; | |||
1109 | AutoGCRooter** const stackTop; | |||
1110 | ||||
1111 | /* | |||
1112 | * Discriminates actual subclass of this being used. The meaning is | |||
1113 | * indicated by the corresponding value in the Kind enum. | |||
1114 | */ | |||
1115 | Kind kind_; | |||
1116 | ||||
1117 | /* No copy or assignment semantics. */ | |||
1118 | AutoGCRooter(AutoGCRooter& ida) = delete; | |||
1119 | void operator=(AutoGCRooter& ida) = delete; | |||
1120 | } JS_HAZ_ROOTED_BASE; | |||
1121 | ||||
1122 | /** | |||
1123 | * Custom rooting behavior for internal and external clients. | |||
1124 | * | |||
1125 | * Deprecated. Where possible, use Rooted<> instead. | |||
1126 | */ | |||
1127 | class MOZ_RAII JS_PUBLIC_API CustomAutoRooter : private AutoGCRooter { | |||
1128 | public: | |||
1129 | template <typename CX> | |||
1130 | explicit CustomAutoRooter(const CX& cx) | |||
1131 | : AutoGCRooter(cx, AutoGCRooter::Kind::Custom) {} | |||
1132 | ||||
1133 | friend void AutoGCRooter::trace(JSTracer* trc); | |||
1134 | ||||
1135 | protected: | |||
1136 | virtual ~CustomAutoRooter() = default; | |||
1137 | ||||
1138 | /** Supplied by derived class to trace roots. */ | |||
1139 | virtual void trace(JSTracer* trc) = 0; | |||
1140 | }; | |||
1141 | ||||
1142 | namespace detail { | |||
1143 | ||||
1144 | template <typename T> | |||
1145 | constexpr bool IsTraceable_v = | |||
1146 | MapTypeToRootKind<T>::kind == JS::RootKind::Traceable; | |||
1147 | ||||
1148 | template <typename T> | |||
1149 | using RootedTraits = | |||
1150 | std::conditional_t<IsTraceable_v<T>, js::RootedTraceableTraits<T>, | |||
1151 | js::RootedGCThingTraits<T>>; | |||
1152 | ||||
1153 | } /* namespace detail */ | |||
1154 | ||||
1155 | /** | |||
1156 | * Local variable of type T whose value is always rooted. This is typically | |||
1157 | * used for local variables, or for non-rooted values being passed to a | |||
1158 | * function that requires a handle, e.g. Foo(Root<T>(cx, x)). | |||
1159 | * | |||
1160 | * If you want to add additional methods to Rooted for a specific | |||
1161 | * specialization, define a RootedOperations<T> specialization containing them. | |||
1162 | */ | |||
1163 | template <typename T> | |||
1164 | class MOZ_RAII Rooted : public detail::RootedTraits<T>::StackBase, | |||
1165 | public js::RootedOperations<T, Rooted<T>> { | |||
1166 | inline void registerWithRootLists(RootedListHeads& roots) { | |||
1167 | this->stack = &roots[JS::MapTypeToRootKind<T>::kind]; | |||
1168 | this->prev = *this->stack; | |||
1169 | *this->stack = this; | |||
1170 | } | |||
1171 | ||||
1172 | inline RootedListHeads& rootLists(RootingContext* cx) { | |||
1173 | return cx->stackRoots_; | |||
1174 | } | |||
1175 | inline RootedListHeads& rootLists(JSContext* cx) { | |||
1176 | return rootLists(RootingContext::get(cx)); | |||
1177 | } | |||
1178 | ||||
1179 | public: | |||
1180 | using ElementType = T; | |||
1181 | ||||
1182 | // Construct an empty Rooted holding a safely initialized but empty T. | |||
1183 | // Requires T to have a copy constructor in order to copy the safely | |||
1184 | // initialized value. | |||
1185 | // | |||
1186 | // Note that for SFINAE to reject this method, the 2nd template parameter must | |||
1187 | // depend on RootingContext somehow even though we really only care about T. | |||
1188 | template <typename RootingContext, | |||
1189 | typename = std::enable_if_t<std::is_copy_constructible_v<T>, | |||
1190 | RootingContext>> | |||
1191 | explicit Rooted(const RootingContext& cx) | |||
1192 | : ptr(SafelyInitialized<T>::create()) { | |||
1193 | registerWithRootLists(rootLists(cx)); | |||
1194 | } | |||
1195 | ||||
1196 | // Provide an initial value. Requires T to be constructible from the given | |||
1197 | // argument. | |||
1198 | template <typename RootingContext, typename S> | |||
1199 | Rooted(const RootingContext& cx, S&& initial) | |||
1200 | : ptr(std::forward<S>(initial)) { | |||
1201 | MOZ_ASSERT(GCPolicy<T>::isValid(ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(ptr )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1201); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(ptr)" ")"); do { *((volatile int*)__null) = 1201; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1202 | registerWithRootLists(rootLists(cx)); | |||
1203 | } | |||
1204 | ||||
1205 | // (Traceables only) Construct the contained value from the given arguments. | |||
1206 | // Constructs in-place, so T does not need to be copyable or movable. | |||
1207 | // | |||
1208 | // Note that a copyable Traceable passed only a RootingContext will | |||
1209 | // choose the above SafelyInitialized<T> constructor, because otherwise | |||
1210 | // identical functions with parameter packs are considered less specialized. | |||
1211 | // | |||
1212 | // The SFINAE type must again depend on an inferred template parameter. | |||
1213 | template < | |||
1214 | typename RootingContext, typename... CtorArgs, | |||
1215 | typename = std::enable_if_t<detail::IsTraceable_v<T>, RootingContext>> | |||
1216 | explicit Rooted(const RootingContext& cx, CtorArgs... args) | |||
1217 | : ptr(std::forward<CtorArgs>(args)...) { | |||
1218 | MOZ_ASSERT(GCPolicy<T>::isValid(ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(ptr )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1218); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(ptr)" ")"); do { *((volatile int*)__null) = 1218; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1219 | registerWithRootLists(rootLists(cx)); | |||
1220 | } | |||
1221 | ||||
1222 | ~Rooted() { | |||
1223 | MOZ_ASSERT(*this->stack == this)do { static_assert( mozilla::detail::AssertionConditionType< decltype(*this->stack == this)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(*this->stack == this))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("*this->stack == this" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1223); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*this->stack == this" ")"); do { *((volatile int*)__null) = 1223; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| ||||
1224 | *this->stack = this->prev; | |||
1225 | } | |||
1226 | ||||
1227 | /* | |||
1228 | * This method is public for Rooted so that Codegen.py can use a Rooted | |||
1229 | * interchangeably with a MutableHandleValue. | |||
1230 | */ | |||
1231 | void set(const T& value) { | |||
1232 | ptr = value; | |||
1233 | MOZ_ASSERT(GCPolicy<T>::isValid(ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(ptr )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1233); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(ptr)" ")"); do { *((volatile int*)__null) = 1233; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1234 | } | |||
1235 | void set(T&& value) { | |||
1236 | ptr = std::move(value); | |||
1237 | MOZ_ASSERT(GCPolicy<T>::isValid(ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(ptr )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1237); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(ptr)" ")"); do { *((volatile int*)__null) = 1237; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1238 | } | |||
1239 | ||||
1240 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
1241 | DECLARE_POINTER_ASSIGN_OPS(Rooted<T>, T)Rooted<T>& operator=(const T& p) { set(p); return *this; } Rooted<T>& operator=(T&& p) { set (std::move(p)); return *this; } Rooted<T>& operator =(const Rooted<T>& other) { set(other.get()); return *this; }; | |||
1242 | ||||
1243 | T& get() { return ptr; } | |||
1244 | const T& get() const { return ptr; } | |||
1245 | ||||
1246 | T* address() { return &ptr; } | |||
1247 | const T* address() const { return &ptr; } | |||
1248 | ||||
1249 | private: | |||
1250 | T ptr; | |||
1251 | ||||
1252 | Rooted(const Rooted&) = delete; | |||
1253 | } JS_HAZ_ROOTED; | |||
1254 | ||||
1255 | namespace detail { | |||
1256 | ||||
1257 | template <typename T> | |||
1258 | struct DefineComparisonOps<Rooted<T>> : std::true_type { | |||
1259 | static const T& get(const Rooted<T>& v) { return v.get(); } | |||
1260 | }; | |||
1261 | ||||
1262 | } // namespace detail | |||
1263 | ||||
1264 | template <typename... Fs> | |||
1265 | using RootedTuple = Rooted<std::tuple<Fs...>>; | |||
1266 | ||||
1267 | // Reference to a field in a RootedTuple. This is a drop-in replacement for an | |||
1268 | // individual Rooted. | |||
1269 | // | |||
1270 | // This is very similar to a MutableHandle but with two differences: it has an | |||
1271 | // assignment operator so doesn't require set() to be called and its address | |||
1272 | // converts to a MutableHandle in the same way as a Rooted. | |||
1273 | // | |||
1274 | // The field is specified by the type parameter, optionally disambiguated by | |||
1275 | // supplying the field index too. | |||
1276 | // | |||
1277 | // Used like this: | |||
1278 | // | |||
1279 | // RootedTuple<JSObject*, JSString*> roots(cx); | |||
1280 | // RootedField<JSObject*> obj(roots); | |||
1281 | // RootedField<JSString*> str(roots); | |||
1282 | // | |||
1283 | // or: | |||
1284 | // | |||
1285 | // RootedTuple<JString*, JSObject*, JSObject*> roots(cx); | |||
1286 | // RootedField<JString*, 0> str(roots); | |||
1287 | // RootedField<JSObject*, 1> obj1(roots); | |||
1288 | // RootedField<JSObject*, 2> obj2(roots); | |||
1289 | template <typename T, size_t N /* = SIZE_MAX */> | |||
1290 | class MOZ_RAII RootedField : public js::RootedOperations<T, RootedField<T, N>> { | |||
1291 | T* ptr; | |||
1292 | friend class Handle<T>; | |||
1293 | friend class MutableHandle<T>; | |||
1294 | ||||
1295 | public: | |||
1296 | using ElementType = T; | |||
1297 | ||||
1298 | template <typename... Fs> | |||
1299 | explicit RootedField(RootedTuple<Fs...>& rootedTuple) { | |||
1300 | using Tuple = std::tuple<Fs...>; | |||
1301 | if constexpr (N == SIZE_MAX(18446744073709551615UL)) { | |||
1302 | ptr = &std::get<T>(rootedTuple.get()); | |||
1303 | } else { | |||
1304 | static_assert(N < std::tuple_size_v<Tuple>); | |||
1305 | static_assert(std::is_same_v<T, std::tuple_element_t<N, Tuple>>); | |||
1306 | ptr = &std::get<N>(rootedTuple.get()); | |||
1307 | } | |||
1308 | } | |||
1309 | template <typename... Fs, typename S> | |||
1310 | explicit RootedField(RootedTuple<Fs...>& rootedTuple, S&& value) | |||
1311 | : RootedField(rootedTuple) { | |||
1312 | *ptr = std::forward<S>(value); | |||
1313 | } | |||
1314 | ||||
1315 | T& get() { return *ptr; } | |||
1316 | const T& get() const { return *ptr; } | |||
1317 | void set(const T& value) { | |||
1318 | *ptr = value; | |||
1319 | MOZ_ASSERT(GCPolicy<T>::isValid(*ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(*ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(* ptr)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(*ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1319); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(*ptr)" ")"); do { *((volatile int*)__null) = 1319; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1320 | } | |||
1321 | void set(T&& value) { | |||
1322 | *ptr = std::move(value); | |||
1323 | MOZ_ASSERT(GCPolicy<T>::isValid(*ptr))do { static_assert( mozilla::detail::AssertionConditionType< decltype(GCPolicy<T>::isValid(*ptr))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(GCPolicy<T>::isValid(* ptr)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("GCPolicy<T>::isValid(*ptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1323); AnnotateMozCrashReason("MOZ_ASSERT" "(" "GCPolicy<T>::isValid(*ptr)" ")"); do { *((volatile int*)__null) = 1323; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1324 | } | |||
1325 | ||||
1326 | using WrapperT = RootedField<T, N>; | |||
1327 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
1328 | DECLARE_POINTER_ASSIGN_OPS(WrapperT, T)WrapperT& operator=(const T& p) { set(p); return *this ; } WrapperT& operator=(T&& p) { set(std::move(p) ); return *this; } WrapperT& operator=(const WrapperT& other) { set(other.get()); return *this; }; | |||
1329 | // DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr); | |||
1330 | // DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(*ptr); | |||
1331 | ||||
1332 | private: | |||
1333 | RootedField() = delete; | |||
1334 | RootedField(const RootedField& other) = delete; | |||
1335 | }; | |||
1336 | ||||
1337 | namespace detail { | |||
1338 | template <size_t N, typename T> | |||
1339 | struct DefineComparisonOps<JS::RootedField<T, N>> : std::true_type { | |||
1340 | static const T& get(const JS::RootedField<T, N>& v) { return v.get(); } | |||
1341 | }; | |||
1342 | } // namespace detail | |||
1343 | ||||
1344 | } /* namespace JS */ | |||
1345 | ||||
1346 | namespace js { | |||
1347 | ||||
1348 | /* | |||
1349 | * Inlinable accessors for JSContext. | |||
1350 | * | |||
1351 | * - These must not be available on the more restricted superclasses of | |||
1352 | * JSContext, so we can't simply define them on RootingContext. | |||
1353 | * | |||
1354 | * - They're perfectly ordinary JSContext functionality, so ought to be | |||
1355 | * usable without resorting to jsfriendapi.h, and when JSContext is an | |||
1356 | * incomplete type. | |||
1357 | */ | |||
1358 | inline JS::Realm* GetContextRealm(const JSContext* cx) { | |||
1359 | return JS::RootingContext::get(cx)->realm_; | |||
1360 | } | |||
1361 | ||||
1362 | inline JS::Compartment* GetContextCompartment(const JSContext* cx) { | |||
1363 | if (JS::Realm* realm = GetContextRealm(cx)) { | |||
1364 | return GetCompartmentForRealm(realm); | |||
1365 | } | |||
1366 | return nullptr; | |||
1367 | } | |||
1368 | ||||
1369 | inline JS::Zone* GetContextZone(const JSContext* cx) { | |||
1370 | return JS::RootingContext::get(cx)->zone_; | |||
1371 | } | |||
1372 | ||||
1373 | inline ProfilingStack* GetContextProfilingStackIfEnabled(JSContext* cx) { | |||
1374 | return JS::RootingContext::get(cx) | |||
1375 | ->geckoProfiler() | |||
1376 | .getProfilingStackIfEnabled(); | |||
1377 | } | |||
1378 | ||||
1379 | /** | |||
1380 | * Augment the generic Rooted<T> interface when T = JSObject* with | |||
1381 | * class-querying and downcasting operations. | |||
1382 | * | |||
1383 | * Given a Rooted<JSObject*> obj, one can view | |||
1384 | * Handle<StringObject*> h = obj.as<StringObject*>(); | |||
1385 | * as an optimization of | |||
1386 | * Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>()); | |||
1387 | * Handle<StringObject*> h = rooted; | |||
1388 | */ | |||
1389 | template <typename Container> | |||
1390 | class RootedOperations<JSObject*, Container> | |||
1391 | : public MutableWrappedPtrOperations<JSObject*, Container> { | |||
1392 | public: | |||
1393 | template <class U> | |||
1394 | JS::Handle<U*> as() const; | |||
1395 | }; | |||
1396 | ||||
1397 | /** | |||
1398 | * Augment the generic Handle<T> interface when T = JSObject* with | |||
1399 | * downcasting operations. | |||
1400 | * | |||
1401 | * Given a Handle<JSObject*> obj, one can view | |||
1402 | * Handle<StringObject*> h = obj.as<StringObject*>(); | |||
1403 | * as an optimization of | |||
1404 | * Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>()); | |||
1405 | * Handle<StringObject*> h = rooted; | |||
1406 | */ | |||
1407 | template <typename Container> | |||
1408 | class HandleOperations<JSObject*, Container> | |||
1409 | : public WrappedPtrOperations<JSObject*, Container> { | |||
1410 | public: | |||
1411 | template <class U> | |||
1412 | JS::Handle<U*> as() const; | |||
1413 | }; | |||
1414 | ||||
1415 | } /* namespace js */ | |||
1416 | ||||
1417 | namespace JS { | |||
1418 | ||||
1419 | template <typename T> | |||
1420 | template <typename S> | |||
1421 | inline Handle<T>::Handle( | |||
1422 | const Rooted<S>& root, | |||
1423 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy) { | |||
1424 | ptr = reinterpret_cast<const T*>(root.address()); | |||
1425 | } | |||
1426 | ||||
1427 | template <typename T> | |||
1428 | template <typename S> | |||
1429 | inline Handle<T>::Handle( | |||
1430 | const PersistentRooted<S>& root, | |||
1431 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy) { | |||
1432 | ptr = reinterpret_cast<const T*>(root.address()); | |||
1433 | } | |||
1434 | ||||
1435 | template <typename T> | |||
1436 | template <typename S> | |||
1437 | inline Handle<T>::Handle( | |||
1438 | MutableHandle<S>& root, | |||
1439 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy) { | |||
1440 | ptr = reinterpret_cast<const T*>(root.address()); | |||
1441 | } | |||
1442 | ||||
1443 | template <typename T> | |||
1444 | template <size_t N, typename S> | |||
1445 | inline Handle<T>::Handle( | |||
1446 | const RootedField<S, N>& rootedField, | |||
1447 | std::enable_if_t<std::is_convertible_v<S, T>, int> dummy) { | |||
1448 | ptr = reinterpret_cast<const T*>(rootedField.ptr); | |||
1449 | } | |||
1450 | ||||
1451 | template <typename T> | |||
1452 | inline MutableHandle<T>::MutableHandle(Rooted<T>* root) { | |||
1453 | static_assert(sizeof(MutableHandle<T>) == sizeof(T*), | |||
1454 | "MutableHandle must be binary compatible with T*."); | |||
1455 | ptr = root->address(); | |||
1456 | } | |||
1457 | ||||
1458 | template <typename T> | |||
1459 | template <size_t N> | |||
1460 | inline MutableHandle<T>::MutableHandle(RootedField<T, N>* rootedField) { | |||
1461 | ptr = rootedField->ptr; | |||
1462 | } | |||
1463 | ||||
1464 | template <typename T> | |||
1465 | inline MutableHandle<T>::MutableHandle(PersistentRooted<T>* root) { | |||
1466 | static_assert(sizeof(MutableHandle<T>) == sizeof(T*), | |||
1467 | "MutableHandle must be binary compatible with T*."); | |||
1468 | ptr = root->address(); | |||
1469 | } | |||
1470 | ||||
1471 | JS_PUBLIC_API void AddPersistentRoot(RootingContext* cx, RootKind kind, | |||
1472 | js::PersistentRootedBase* root); | |||
1473 | ||||
1474 | JS_PUBLIC_API void AddPersistentRoot(JSRuntime* rt, RootKind kind, | |||
1475 | js::PersistentRootedBase* root); | |||
1476 | ||||
1477 | /** | |||
1478 | * A copyable, assignable global GC root type with arbitrary lifetime, an | |||
1479 | * infallible constructor, and automatic unrooting on destruction. | |||
1480 | * | |||
1481 | * These roots can be used in heap-allocated data structures, so they are not | |||
1482 | * associated with any particular JSContext or stack. They are registered with | |||
1483 | * the JSRuntime itself, without locking. Initialization may take place on | |||
1484 | * construction, or in two phases if the no-argument constructor is called | |||
1485 | * followed by init(). | |||
1486 | * | |||
1487 | * Note that you must not use an PersistentRooted in an object owned by a JS | |||
1488 | * object: | |||
1489 | * | |||
1490 | * Whenever one object whose lifetime is decided by the GC refers to another | |||
1491 | * such object, that edge must be traced only if the owning JS object is traced. | |||
1492 | * This applies not only to JS objects (which obviously are managed by the GC) | |||
1493 | * but also to C++ objects owned by JS objects. | |||
1494 | * | |||
1495 | * If you put a PersistentRooted in such a C++ object, that is almost certainly | |||
1496 | * a leak. When a GC begins, the referent of the PersistentRooted is treated as | |||
1497 | * live, unconditionally (because a PersistentRooted is a *root*), even if the | |||
1498 | * JS object that owns it is unreachable. If there is any path from that | |||
1499 | * referent back to the JS object, then the C++ object containing the | |||
1500 | * PersistentRooted will not be destructed, and the whole blob of objects will | |||
1501 | * not be freed, even if there are no references to them from the outside. | |||
1502 | * | |||
1503 | * In the context of Firefox, this is a severe restriction: almost everything in | |||
1504 | * Firefox is owned by some JS object or another, so using PersistentRooted in | |||
1505 | * such objects would introduce leaks. For these kinds of edges, Heap<T> or | |||
1506 | * TenuredHeap<T> would be better types. It's up to the implementor of the type | |||
1507 | * containing Heap<T> or TenuredHeap<T> members to make sure their referents get | |||
1508 | * marked when the object itself is marked. | |||
1509 | */ | |||
1510 | template <typename T> | |||
1511 | class PersistentRooted : public detail::RootedTraits<T>::PersistentBase, | |||
1512 | public js::RootedOperations<T, PersistentRooted<T>> { | |||
1513 | void registerWithRootLists(RootingContext* cx) { | |||
1514 | MOZ_ASSERT(!initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!initialized()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!initialized()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1514); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!initialized()" ")"); do { *((volatile int*)__null) = 1514; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1515 | JS::RootKind kind = JS::MapTypeToRootKind<T>::kind; | |||
1516 | AddPersistentRoot(cx, kind, this); | |||
1517 | } | |||
1518 | ||||
1519 | void registerWithRootLists(JSRuntime* rt) { | |||
1520 | MOZ_ASSERT(!initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!initialized()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!initialized()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1520); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!initialized()" ")"); do { *((volatile int*)__null) = 1520; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1521 | JS::RootKind kind = JS::MapTypeToRootKind<T>::kind; | |||
1522 | AddPersistentRoot(rt, kind, this); | |||
1523 | } | |||
1524 | ||||
1525 | // Used when JSContext type is incomplete and so it is not known to inherit | |||
1526 | // from RootingContext. | |||
1527 | void registerWithRootLists(JSContext* cx) { | |||
1528 | registerWithRootLists(RootingContext::get(cx)); | |||
1529 | } | |||
1530 | ||||
1531 | public: | |||
1532 | using ElementType = T; | |||
1533 | ||||
1534 | PersistentRooted() : ptr(SafelyInitialized<T>::create()) {} | |||
1535 | ||||
1536 | template < | |||
1537 | typename RootHolder, | |||
1538 | typename = std::enable_if_t<std::is_copy_constructible_v<T>, RootHolder>> | |||
1539 | explicit PersistentRooted(const RootHolder& cx) | |||
1540 | : ptr(SafelyInitialized<T>::create()) { | |||
1541 | registerWithRootLists(cx); | |||
1542 | } | |||
1543 | ||||
1544 | template < | |||
1545 | typename RootHolder, typename U, | |||
1546 | typename = std::enable_if_t<std::is_constructible_v<T, U>, RootHolder>> | |||
1547 | PersistentRooted(const RootHolder& cx, U&& initial) | |||
1548 | : ptr(std::forward<U>(initial)) { | |||
1549 | registerWithRootLists(cx); | |||
1550 | } | |||
1551 | ||||
1552 | template <typename RootHolder, typename... CtorArgs, | |||
1553 | typename = std::enable_if_t<detail::IsTraceable_v<T>, RootHolder>> | |||
1554 | explicit PersistentRooted(const RootHolder& cx, CtorArgs... args) | |||
1555 | : ptr(std::forward<CtorArgs>(args)...) { | |||
1556 | registerWithRootLists(cx); | |||
1557 | } | |||
1558 | ||||
1559 | PersistentRooted(const PersistentRooted& rhs) : ptr(rhs.ptr) { | |||
1560 | /* | |||
1561 | * Copy construction takes advantage of the fact that the original | |||
1562 | * is already inserted, and simply adds itself to whatever list the | |||
1563 | * original was on - no JSRuntime pointer needed. | |||
1564 | * | |||
1565 | * This requires mutating rhs's links, but those should be 'mutable' | |||
1566 | * anyway. C++ doesn't let us declare mutable base classes. | |||
1567 | */ | |||
1568 | const_cast<PersistentRooted&>(rhs).setNext(this); | |||
1569 | } | |||
1570 | ||||
1571 | bool initialized() const { return this->isInList(); } | |||
1572 | ||||
1573 | void init(RootingContext* cx) { init(cx, SafelyInitialized<T>::create()); } | |||
1574 | void init(JSContext* cx) { init(RootingContext::get(cx)); } | |||
1575 | ||||
1576 | template <typename U> | |||
1577 | void init(RootingContext* cx, U&& initial) { | |||
1578 | ptr = std::forward<U>(initial); | |||
1579 | registerWithRootLists(cx); | |||
1580 | } | |||
1581 | template <typename U> | |||
1582 | void init(JSContext* cx, U&& initial) { | |||
1583 | ptr = std::forward<U>(initial); | |||
1584 | registerWithRootLists(RootingContext::get(cx)); | |||
1585 | } | |||
1586 | ||||
1587 | void reset() { | |||
1588 | if (initialized()) { | |||
1589 | set(SafelyInitialized<T>::create()); | |||
1590 | this->remove(); | |||
1591 | } | |||
1592 | } | |||
1593 | ||||
1594 | DECLARE_POINTER_CONSTREF_OPS(T)operator const T&() const { return get(); } const T& operator ->() const { return get(); }; | |||
1595 | DECLARE_POINTER_ASSIGN_OPS(PersistentRooted<T>, T)PersistentRooted<T>& operator=(const T& p) { set (p); return *this; } PersistentRooted<T>& operator= (T&& p) { set(std::move(p)); return *this; } PersistentRooted <T>& operator=(const PersistentRooted<T>& other) { set(other.get()); return *this; }; | |||
1596 | ||||
1597 | T& get() { return ptr; } | |||
1598 | const T& get() const { return ptr; } | |||
1599 | ||||
1600 | T* address() { | |||
1601 | MOZ_ASSERT(initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(initialized()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("initialized()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1601); AnnotateMozCrashReason("MOZ_ASSERT" "(" "initialized()" ")"); do { *((volatile int*)__null) = 1601; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1602 | return &ptr; | |||
1603 | } | |||
1604 | const T* address() const { return &ptr; } | |||
1605 | ||||
1606 | template <typename U> | |||
1607 | void set(U&& value) { | |||
1608 | MOZ_ASSERT(initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(initialized()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("initialized()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1608); AnnotateMozCrashReason("MOZ_ASSERT" "(" "initialized()" ")"); do { *((volatile int*)__null) = 1608; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1609 | ptr = std::forward<U>(value); | |||
1610 | } | |||
1611 | ||||
1612 | private: | |||
1613 | T ptr; | |||
1614 | } JS_HAZ_ROOTED; | |||
1615 | ||||
1616 | namespace detail { | |||
1617 | ||||
1618 | template <typename T> | |||
1619 | struct DefineComparisonOps<PersistentRooted<T>> : std::true_type { | |||
1620 | static const T& get(const PersistentRooted<T>& v) { return v.get(); } | |||
1621 | }; | |||
1622 | ||||
1623 | } // namespace detail | |||
1624 | ||||
1625 | } /* namespace JS */ | |||
1626 | ||||
1627 | namespace js { | |||
1628 | ||||
1629 | template <typename T, typename D, typename Container> | |||
1630 | class WrappedPtrOperations<UniquePtr<T, D>, Container> { | |||
1631 | const UniquePtr<T, D>& uniquePtr() const { | |||
1632 | return static_cast<const Container*>(this)->get(); | |||
1633 | } | |||
1634 | ||||
1635 | public: | |||
1636 | explicit operator bool() const { return !!uniquePtr(); } | |||
1637 | T* get() const { return uniquePtr().get(); } | |||
1638 | T* operator->() const { return get(); } | |||
1639 | T& operator*() const { return *uniquePtr(); } | |||
1640 | }; | |||
1641 | ||||
1642 | template <typename T, typename D, typename Container> | |||
1643 | class MutableWrappedPtrOperations<UniquePtr<T, D>, Container> | |||
1644 | : public WrappedPtrOperations<UniquePtr<T, D>, Container> { | |||
1645 | UniquePtr<T, D>& uniquePtr() { return static_cast<Container*>(this)->get(); } | |||
1646 | ||||
1647 | public: | |||
1648 | [[nodiscard]] typename UniquePtr<T, D>::Pointer release() { | |||
1649 | return uniquePtr().release(); | |||
1650 | } | |||
1651 | void reset(T* ptr = T()) { uniquePtr().reset(ptr); } | |||
1652 | }; | |||
1653 | ||||
1654 | template <typename T, typename Container> | |||
1655 | class WrappedPtrOperations<mozilla::Maybe<T>, Container> { | |||
1656 | const mozilla::Maybe<T>& maybe() const { | |||
1657 | return static_cast<const Container*>(this)->get(); | |||
1658 | } | |||
1659 | ||||
1660 | public: | |||
1661 | // This only supports a subset of Maybe's interface. | |||
1662 | bool isSome() const { return maybe().isSome(); } | |||
1663 | bool isNothing() const { return maybe().isNothing(); } | |||
1664 | const T value() const { return maybe().value(); } | |||
1665 | const T* operator->() const { return maybe().ptr(); } | |||
1666 | const T& operator*() const { return maybe().ref(); } | |||
1667 | }; | |||
1668 | ||||
1669 | template <typename T, typename Container> | |||
1670 | class MutableWrappedPtrOperations<mozilla::Maybe<T>, Container> | |||
1671 | : public WrappedPtrOperations<mozilla::Maybe<T>, Container> { | |||
1672 | mozilla::Maybe<T>& maybe() { return static_cast<Container*>(this)->get(); } | |||
1673 | ||||
1674 | public: | |||
1675 | // This only supports a subset of Maybe's interface. | |||
1676 | T* operator->() { return maybe().ptr(); } | |||
1677 | T& operator*() { return maybe().ref(); } | |||
1678 | void reset() { return maybe().reset(); } | |||
1679 | }; | |||
1680 | ||||
1681 | namespace gc { | |||
1682 | ||||
1683 | template <typename T, typename TraceCallbacks> | |||
1684 | void CallTraceCallbackOnNonHeap(T* v, const TraceCallbacks& aCallbacks, | |||
1685 | const char* aName, void* aClosure) { | |||
1686 | static_assert(sizeof(T) == sizeof(JS::Heap<T>), | |||
1687 | "T and Heap<T> must be compatible."); | |||
1688 | MOZ_ASSERT(v)do { static_assert( mozilla::detail::AssertionConditionType< decltype(v)>::isValid, "invalid assertion condition"); if ( (__builtin_expect(!!(!(!!(v))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("v", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1688); AnnotateMozCrashReason("MOZ_ASSERT" "(" "v" ")"); do { *((volatile int*)__null) = 1688; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
1689 | mozilla::DebugOnly<Cell*> cell = BarrierMethods<T>::asGCThingOrNull(*v); | |||
1690 | MOZ_ASSERT(cell)do { static_assert( mozilla::detail::AssertionConditionType< decltype(cell)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(cell))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("cell", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1690); AnnotateMozCrashReason("MOZ_ASSERT" "(" "cell" ")"); do { *((volatile int*)__null) = 1690; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1691 | MOZ_ASSERT(!IsInsideNursery(cell))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!IsInsideNursery(cell))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!IsInsideNursery(cell)))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("!IsInsideNursery(cell)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/RootingAPI.h" , 1691); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!IsInsideNursery(cell)" ")"); do { *((volatile int*)__null) = 1691; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1692 | JS::Heap<T>* asHeapT = reinterpret_cast<JS::Heap<T>*>(v); | |||
1693 | aCallbacks.Trace(asHeapT, aName, aClosure); | |||
1694 | } | |||
1695 | ||||
1696 | } /* namespace gc */ | |||
1697 | ||||
1698 | template <typename Wrapper, typename T1, typename T2> | |||
1699 | class WrappedPtrOperations<std::pair<T1, T2>, Wrapper> { | |||
1700 | const std::pair<T1, T2>& pair() const { | |||
1701 | return static_cast<const Wrapper*>(this)->get(); | |||
1702 | } | |||
1703 | ||||
1704 | public: | |||
1705 | const T1& first() const { return pair().first; } | |||
1706 | const T2& second() const { return pair().second; } | |||
1707 | }; | |||
1708 | ||||
1709 | template <typename Wrapper, typename T1, typename T2> | |||
1710 | class MutableWrappedPtrOperations<std::pair<T1, T2>, Wrapper> | |||
1711 | : public WrappedPtrOperations<std::pair<T1, T2>, Wrapper> { | |||
1712 | std::pair<T1, T2>& pair() { return static_cast<Wrapper*>(this)->get(); } | |||
1713 | ||||
1714 | public: | |||
1715 | T1& first() { return pair().first; } | |||
1716 | T2& second() { return pair().second; } | |||
1717 | }; | |||
1718 | ||||
1719 | } /* namespace js */ | |||
1720 | ||||
1721 | #endif /* js_RootingAPI_h */ |