Source code

Revision control

Other Tools

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 jsshell_js_h
8
#define jsshell_js_h
9
10
#include "mozilla/Atomics.h"
11
#include "mozilla/Maybe.h"
12
#include "mozilla/TimeStamp.h"
13
#include "mozilla/Variant.h"
14
15
#include "jsapi.h"
16
17
#include "builtin/MapObject.h"
18
#include "js/GCVector.h"
19
#include "threading/ConditionVariable.h"
20
#include "threading/LockGuard.h"
21
#include "threading/Mutex.h"
22
#include "threading/Thread.h"
23
#include "vm/GeckoProfiler.h"
24
#include "vm/Monitor.h"
25
26
// Some platform hooks must be implemented for single-step profiling.
27
#if defined(JS_SIMULATOR_ARM) || defined(JS_SIMULATOR_MIPS64) || \
28
defined(JS_SIMULATOR_MIPS32)
29
# define SINGLESTEP_PROFILING
30
#endif
31
32
namespace js {
33
namespace shell {
34
35
enum JSShellErrNum {
36
#define MSG_DEF(name, count, exception, format) name,
37
#include "jsshell.msg"
38
#undef MSG_DEF
39
JSShellErr_Limit
40
};
41
42
const JSErrorFormatString* my_GetErrorMessage(void* userRef,
43
const unsigned errorNumber);
44
45
void WarningReporter(JSContext* cx, JSErrorReport* report);
46
47
class MOZ_STACK_CLASS AutoReportException {
48
JSContext* cx;
49
50
public:
51
explicit AutoReportException(JSContext* cx) : cx(cx) {}
52
~AutoReportException();
53
};
54
55
bool GenerateInterfaceHelp(JSContext* cx, JS::HandleObject obj,
56
const char* name);
57
58
JSString* FileAsString(JSContext* cx, JS::HandleString pathnameStr);
59
60
class AutoCloseFile {
61
private:
62
FILE* f_;
63
64
public:
65
explicit AutoCloseFile(FILE* f) : f_(f) {}
66
~AutoCloseFile() { (void)release(); }
67
bool release() {
68
bool success = true;
69
if (f_ && f_ != stdin && f_ != stdout && f_ != stderr) {
70
success = !fclose(f_);
71
}
72
f_ = nullptr;
73
return success;
74
}
75
};
76
77
// Reference counted file.
78
struct RCFile {
79
FILE* fp;
80
uint32_t numRefs;
81
82
RCFile() : fp(nullptr), numRefs(0) {}
83
explicit RCFile(FILE* fp) : fp(fp), numRefs(0) {}
84
85
void acquire() { numRefs++; }
86
87
// Starts out with a ref count of zero.
88
static RCFile* create(JSContext* cx, const char* filename, const char* mode);
89
90
void close();
91
bool isOpen() const { return fp; }
92
bool release();
93
};
94
95
// Shell command-line arguments and count.
96
extern int sArgc;
97
extern char** sArgv;
98
99
// Shell state set once at startup.
100
extern bool enableDeferredMode;
101
extern bool enableCodeCoverage;
102
extern bool enableDisassemblyDumps;
103
extern bool offthreadCompilation;
104
extern bool enableAsmJS;
105
extern bool enableWasm;
106
extern bool enableSharedMemory;
107
extern bool enableWasmBaseline;
108
extern bool enableWasmIon;
109
extern bool enableWasmCranelift;
110
#ifdef ENABLE_WASM_GC
111
extern bool enableWasmGc;
112
#endif
113
#ifdef ENABLE_WASM_MULTI_VALUE
114
extern bool enableWasmMultiValue;
115
#endif
116
extern bool enableWasmVerbose;
117
extern bool enableTestWasmAwaitTier2;
118
#ifdef ENABLE_WASM_BIGINT
119
extern bool enableWasmBigInt;
120
#endif
121
extern bool enableAsyncStacks;
122
extern bool enableStreams;
123
extern bool enableReadableByteStreams;
124
extern bool enableBYOBStreamReaders;
125
extern bool enableWritableStreams;
126
extern bool enableReadableStreamPipeTo;
127
extern bool enableWeakRefs;
128
extern bool enableToSource;
129
extern bool enablePropertyErrorMessageFix;
130
#ifdef JS_GC_ZEAL
131
extern uint32_t gZealBits;
132
extern uint32_t gZealFrequency;
133
#endif
134
extern bool printTiming;
135
extern RCFile* gErrFile;
136
extern RCFile* gOutFile;
137
extern bool reportWarnings;
138
extern bool compileOnly;
139
extern bool fuzzingSafe;
140
extern bool disableOOMFunctions;
141
extern bool defaultToSameCompartment;
142
143
#ifdef DEBUG
144
extern bool dumpEntrainedVariables;
145
extern bool OOM_printAllocationCount;
146
#endif
147
148
// Alias the global dstName to namespaceObj.srcName. For example, if dstName is
149
// "snarf", namespaceObj represents "os.file", and srcName is "readFile", then
150
// this is equivalent to the JS code:
151
//
152
// snarf = os.file.readFile;
153
//
154
// This provides a mechanism for namespacing the various JS shell helper
155
// functions without breaking backwards compatibility with things that use the
156
// global names.
157
bool CreateAlias(JSContext* cx, const char* dstName,
158
JS::HandleObject namespaceObj, const char* srcName);
159
160
enum class ScriptKind { Script, DecodeScript, Module };
161
162
class NonshrinkingGCObjectVector
163
: public GCVector<JSObject*, 0, SystemAllocPolicy> {
164
public:
165
void sweep() {
166
for (uint32_t i = 0; i < this->length(); i++) {
167
if (JS::GCPolicy<JSObject*>::needsSweep(&(*this)[i])) {
168
(*this)[i] = nullptr;
169
}
170
}
171
}
172
};
173
174
using MarkBitObservers = JS::WeakCache<NonshrinkingGCObjectVector>;
175
176
#ifdef SINGLESTEP_PROFILING
177
using StackChars = Vector<char16_t, 0, SystemAllocPolicy>;
178
#endif
179
180
class OffThreadJob;
181
182
// Per-context shell state.
183
struct ShellContext {
184
explicit ShellContext(JSContext* cx);
185
~ShellContext();
186
187
bool isWorker;
188
bool lastWarningEnabled;
189
190
// Track promise rejections and report unhandled rejections.
191
bool trackUnhandledRejections;
192
193
double timeoutInterval;
194
double startTime;
195
mozilla::Atomic<bool> serviceInterrupt;
196
mozilla::Atomic<bool> haveInterruptFunc;
197
JS::PersistentRootedValue interruptFunc;
198
JS::PersistentRootedValue lastWarning;
199
JS::PersistentRootedValue promiseRejectionTrackerCallback;
200
201
// Rejected promises that are not yet handled. Added when rejection
202
// happens, and removed when rejection is handled. This uses SetObject to
203
// report unhandled rejections in the rejected order.
204
JS::PersistentRooted<SetObject*> unhandledRejectedPromises;
205
206
#ifdef SINGLESTEP_PROFILING
207
Vector<StackChars, 0, SystemAllocPolicy> stacks;
208
#endif
209
210
/*
211
* Watchdog thread state.
212
*/
213
js::Mutex watchdogLock;
214
js::ConditionVariable watchdogWakeup;
215
mozilla::Maybe<js::Thread> watchdogThread;
216
mozilla::Maybe<mozilla::TimeStamp> watchdogTimeout;
217
218
js::ConditionVariable sleepWakeup;
219
220
int exitCode;
221
bool quitting;
222
223
JS::UniqueChars readLineBuf;
224
size_t readLineBufPos;
225
226
js::shell::RCFile** errFilePtr;
227
js::shell::RCFile** outFilePtr;
228
229
UniquePtr<ProfilingStack> geckoProfilingStack;
230
231
JS::UniqueChars moduleLoadPath;
232
233
UniquePtr<MarkBitObservers> markObservers;
234
235
// Off-thread parse state.
236
js::Monitor offThreadMonitor;
237
Vector<OffThreadJob*, 0, SystemAllocPolicy> offThreadJobs;
238
239
// Queued finalization registry cleanup jobs.
240
using ObjectVector = GCVector<JSObject*, 0, SystemAllocPolicy>;
241
JS::PersistentRooted<ObjectVector> finalizationRegistriesToCleanUp;
242
};
243
244
extern ShellContext* GetShellContext(JSContext* cx);
245
246
extern MOZ_MUST_USE bool PrintStackTrace(JSContext* cx,
247
JS::Handle<JSObject*> stackObj);
248
249
} /* namespace shell */
250
} /* namespace js */
251
252
#endif