1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef ScriptPreloader_inl_h
7 #define ScriptPreloader_inl_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/CheckedInt.h"
12 #include "mozilla/EndianUtils.h"
13 #include "mozilla/EnumSet.h"
14 #include "mozilla/Range.h"
15 #include "mozilla/ResultExtensions.h"
16 #include "mozilla/Unused.h"
17 #include "mozilla/dom/ScriptSettings.h"
27 using mozilla::dom::AutoJSAPI
;
29 static inline Result
<Ok
, nsresult
> Write(PRFileDesc
* fd
, const void* data
,
31 if (PR_Write(fd
, data
, len
) != len
) {
32 return Err(NS_ERROR_FAILURE
);
37 static inline Result
<Ok
, nsresult
> WritePadding(PRFileDesc
* fd
,
39 static const char paddingBytes
[8] = "PADBYTE";
40 MOZ_DIAGNOSTIC_ASSERT(padding
<= sizeof(paddingBytes
));
46 if (PR_Write(fd
, static_cast<const void*>(paddingBytes
), padding
) !=
48 return Err(NS_ERROR_FAILURE
);
53 struct MOZ_RAII AutoSafeJSAPI
: public AutoJSAPI
{
54 AutoSafeJSAPI() { Init(); }
60 // Wraps the iterator for a nsTHashTable so that it may be used as a range
61 // iterator. Each iterator result acts as a smart pointer to the hash element,
62 // and has a Remove() method which will remove the element from the hash.
64 // It also accepts an optional Matcher instance against which to filter the
65 // elements which should be iterated over.
69 // for (auto& elem : HashElemIter<HashType>(hash)) {
70 // if (elem->IsDead()) {
76 using Iterator
= typename
T::Iterator
;
77 using ElemType
= typename
T::UserDataType
;
80 Matcher
<ElemType
>* matcher_
;
84 explicit HashElemIter(T
& hash
, Matcher
<ElemType
>* matcher
= nullptr)
85 : hash_(hash
), matcher_(matcher
), iter_(hash
.Iter()) {}
88 friend class HashElemIter
<T
>;
90 HashElemIter
<T
>& iter_
;
93 Elem(HashElemIter
& iter
, bool done
) : iter_(iter
), done_(done
) {
97 Iterator
& iter() { return iter_
.iter_
; }
99 void skipNonMatching() {
100 if (iter_
.matcher_
) {
101 while (!done_
&& !iter_
.matcher_
->Matches(get())) {
103 done_
= iter().Done();
109 Elem
& operator*() { return *this; }
115 return iter().UserData();
118 const ElemType
get() const { return const_cast<Elem
*>(this)->get(); }
120 ElemType
operator->() { return get(); }
122 const ElemType
operator->() const { return get(); }
124 operator ElemType() { return get(); }
126 void Remove() { iter().Remove(); }
132 done_
= iter().Done();
138 bool operator!=(Elem
& other
) const {
139 return done_
!= other
.done_
|| this->get() != other
.get();
143 Elem
begin() { return Elem(*this, iter_
.Done()); }
145 Elem
end() { return Elem(*this, true); }
148 template <typename T
>
149 HashElemIter
<T
> IterHash(T
& hash
,
150 Matcher
<typename
T::UserDataType
>* matcher
= nullptr) {
151 return HashElemIter
<T
>(hash
, matcher
);
154 template <typename T
, typename F
>
155 bool Find(T
&& iter
, F
&& match
) {
156 for (auto& elem
: iter
) {
164 }; // namespace loader
165 }; // namespace mozilla
167 #endif // ScriptPreloader_inl_h