1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef frontend_FrontendContext_h
#define frontend_FrontendContext_h
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
#include "mozilla/Maybe.h" // mozilla::Maybe
#include <stddef.h> // size_t
#include "js/AllocPolicy.h" // SystemAllocPolicy, AllocFunction
#include "js/ErrorReport.h" // JSErrorCallback, JSErrorFormatString
#include "js/Vector.h" // Vector
#include "vm/ErrorReporting.h" // CompileError
#include "vm/MallocProvider.h" // MallocProvider
struct JSContext;
namespace js {
class FrontendContext;
namespace frontend {
class NameCollectionPool;
} // namespace frontend
struct FrontendErrors {
FrontendErrors() = default;
// Any errors or warnings produced during compilation. These are reported
// when finishing the script.
mozilla::Maybe<CompileError> error;
Vector<CompileError, 0, SystemAllocPolicy> warnings;
bool overRecursed = false;
bool outOfMemory = false;
bool allocationOverflow = false;
bool hadErrors() const {
return outOfMemory || overRecursed || allocationOverflow || error;
}
};
class FrontendAllocator : public MallocProvider<FrontendAllocator> {
private:
FrontendContext* const fc_;
public:
explicit FrontendAllocator(FrontendContext* fc) : fc_(fc) {}
void* onOutOfMemory(js::AllocFunction allocFunc, arena_id_t arena,
size_t nbytes, void* reallocPtr = nullptr);
void reportAllocationOverflow();
};
class FrontendContext {
private:
FrontendAllocator alloc_;
js::FrontendErrors errors_;
// NameCollectionPool can be either:
// * owned by this FrontendContext, or
// * borrowed from JSContext
frontend::NameCollectionPool* nameCollectionPool_;
bool ownNameCollectionPool_;
protected:
// (optional) Current JSContext to support main-thread-specific
// handling for error reporting, GC, and memory allocation.
//
// Set by setCurrentJSContext.
JSContext* maybeCx_ = nullptr;
public:
FrontendContext()
: alloc_(this),
nameCollectionPool_(nullptr),
ownNameCollectionPool_(false) {}
~FrontendContext();
bool allocateOwnedPool();
frontend::NameCollectionPool& nameCollectionPool() {
MOZ_ASSERT(
nameCollectionPool_,
"Either allocateOwnedPool or setCurrentJSContext must be called");
return *nameCollectionPool_;
}
FrontendAllocator* getAllocator() { return &alloc_; }
void setCurrentJSContext(JSContext* cx);
// Returns JSContext if any.
//
// This can be used only for:
// * Main-thread-specific operation, such as operating on JSAtom
// * Optional operation, such as providing better error message
JSContext* maybeCurrentJSContext() { return maybeCx_; }
enum class Warning { Suppress, Report };
void convertToRuntimeError(JSContext* cx, Warning warning = Warning::Report);
void linkWithJSContext(JSContext* cx);
mozilla::Maybe<CompileError>& maybeError() { return errors_.error; }
Vector<CompileError, 0, SystemAllocPolicy>& warnings() {
return errors_.warnings;
}
// Report CompileErrors
void reportError(js::CompileError&& err);
bool reportWarning(js::CompileError&& err);
// Report FrontendAllocator errors
void* onOutOfMemory(js::AllocFunction allocFunc, arena_id_t arena,
size_t nbytes, void* reallocPtr = nullptr);
void onAllocationOverflow();
void onOutOfMemory();
void onOverRecursed();
void recoverFromOutOfMemory();
const JSErrorFormatString* gcSafeCallback(JSErrorCallback callback,
void* userRef,
const unsigned errorNumber);
// Status of errors reported to this FrontendContext
bool hadOutOfMemory() const { return errors_.outOfMemory; }
bool hadOverRecursed() const { return errors_.overRecursed; }
bool hadAllocationOverflow() const { return errors_.allocationOverflow; }
bool hadErrors() const;
#ifdef __wasi__
void incWasiRecursionDepth();
void decWasiRecursionDepth();
bool checkWasiRecursionLimit();
#endif // __wasi__
private:
void ReportOutOfMemory();
void addPendingOutOfMemory();
};
// Automatically report any pending exception when leaving the scope.
class MOZ_STACK_CLASS AutoReportFrontendContext : public FrontendContext {
// The target JSContext to report the errors to.
JSContext* cx_;
Warning warning_;
public:
explicit AutoReportFrontendContext(JSContext* cx,
Warning warning = Warning::Report)
: FrontendContext(), cx_(cx), warning_(warning) {
setCurrentJSContext(cx_);
MOZ_ASSERT(cx_ == maybeCx_);
}
~AutoReportFrontendContext() {
if (cx_) {
convertToRuntimeErrorAndClear();
}
}
void clearAutoReport() { cx_ = nullptr; }
void convertToRuntimeErrorAndClear() {
convertToRuntimeError(cx_, warning_);
cx_ = nullptr;
}
};
/*
* Explicitly report any pending exception before leaving the scope.
*
* Before an instance of this class leaves the scope, you must call either
* failure() (if there are exceptions to report) or ok() (if there are no
* exceptions to report).
*/
class ManualReportFrontendContext : public FrontendContext {
JSContext* cx_;
#ifdef DEBUG
bool handled_ = false;
#endif
public:
explicit ManualReportFrontendContext(JSContext* cx)
: FrontendContext(), cx_(cx) {
setCurrentJSContext(cx_);
}
~ManualReportFrontendContext() { MOZ_ASSERT(handled_); }
void ok() {
#ifdef DEBUG
handled_ = true;
#endif
}
void failure() {
#ifdef DEBUG
handled_ = true;
#endif
convertToRuntimeError(cx_);
}
};
} // namespace js
#endif /* frontend_FrontendContext_h */
|