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
extern bool enableWasmVerbose;
114
extern bool enableTestWasmAwaitTier2;
115
#ifdef ENABLE_WASM_BIGINT
116
extern bool enableWasmBigInt;
117
#endif
118
extern bool enableAsyncStacks;
119
extern bool enableStreams;
120
extern bool enableReadableByteStreams;
121
extern bool enableBYOBStreamReaders;
122
extern bool enableWritableStreams;
123
extern bool enableFields;
124
extern bool enableAwaitFix;
125
extern bool enableWeakRefs;
126
#ifdef JS_GC_ZEAL
127
extern uint32_t gZealBits;
128
extern uint32_t gZealFrequency;
129
#endif
130
extern bool printTiming;
131
extern RCFile* gErrFile;
132
extern RCFile* gOutFile;
133
extern bool reportWarnings;
134
extern bool compileOnly;
135
extern bool fuzzingSafe;
136
extern bool disableOOMFunctions;
137
extern bool defaultToSameCompartment;
138
139
#ifdef DEBUG
140
extern bool dumpEntrainedVariables;
141
extern bool OOM_printAllocationCount;
142
#endif
143
144
// Alias the global dstName to namespaceObj.srcName. For example, if dstName is
145
// "snarf", namespaceObj represents "os.file", and srcName is "readFile", then
146
// this is equivalent to the JS code:
147
//
148
// snarf = os.file.readFile;
149
//
150
// This provides a mechanism for namespacing the various JS shell helper
151
// functions without breaking backwards compatibility with things that use the
152
// global names.
153
bool CreateAlias(JSContext* cx, const char* dstName,
154
JS::HandleObject namespaceObj, const char* srcName);
155
156
enum class ScriptKind { Script, DecodeScript, Module };
157
158
class NonshrinkingGCObjectVector
159
: public GCVector<JSObject*, 0, SystemAllocPolicy> {
160
public:
161
void sweep() {
162
for (uint32_t i = 0; i < this->length(); i++) {
163
if (JS::GCPolicy<JSObject*>::needsSweep(&(*this)[i])) {
164
(*this)[i] = nullptr;
165
}
166
}
167
}
168
};
169
170
using MarkBitObservers = JS::WeakCache<NonshrinkingGCObjectVector>;
171
172
#ifdef SINGLESTEP_PROFILING
173
using StackChars = Vector<char16_t, 0, SystemAllocPolicy>;
174
#endif
175
176
class OffThreadJob;
177
178
// Per-context shell state.
179
struct ShellContext {
180
explicit ShellContext(JSContext* cx);
181
~ShellContext();
182
183
bool isWorker;
184
bool lastWarningEnabled;
185
186
// Track promise rejections and report unhandled rejections.
187
bool trackUnhandledRejections;
188
189
double timeoutInterval;
190
double startTime;
191
mozilla::Atomic<bool> serviceInterrupt;
192
mozilla::Atomic<bool> haveInterruptFunc;
193
JS::PersistentRootedValue interruptFunc;
194
JS::PersistentRootedValue lastWarning;
195
JS::PersistentRootedValue promiseRejectionTrackerCallback;
196
197
// Rejected promises that are not yet handled. Added when rejection
198
// happens, and removed when rejection is handled. This uses SetObject to
199
// report unhandled rejections in the rejected order.
200
JS::PersistentRooted<SetObject*> unhandledRejectedPromises;
201
202
#ifdef SINGLESTEP_PROFILING
203
Vector<StackChars, 0, SystemAllocPolicy> stacks;
204
#endif
205
206
/*
207
* Watchdog thread state.
208
*/
209
js::Mutex watchdogLock;
210
js::ConditionVariable watchdogWakeup;
211
mozilla::Maybe<js::Thread> watchdogThread;
212
mozilla::Maybe<mozilla::TimeStamp> watchdogTimeout;
213
214
js::ConditionVariable sleepWakeup;
215
216
int exitCode;
217
bool quitting;
218
219
JS::UniqueChars readLineBuf;
220
size_t readLineBufPos;
221
222
js::shell::RCFile** errFilePtr;
223
js::shell::RCFile** outFilePtr;
224
225
UniquePtr<ProfilingStack> geckoProfilingStack;
226
227
JS::UniqueChars moduleLoadPath;
228
229
UniquePtr<MarkBitObservers> markObservers;
230
231
// Off-thread parse state.
232
js::Monitor offThreadMonitor;
233
Vector<OffThreadJob*, 0, SystemAllocPolicy> offThreadJobs;
234
235
// Queued finalization group cleanup jobs.
236
using ObjectVector = GCVector<JSObject*, 0, SystemAllocPolicy>;
237
JS::PersistentRooted<ObjectVector> finalizationGroupsToCleanUp;
238
};
239
240
extern ShellContext* GetShellContext(JSContext* cx);
241
242
extern MOZ_MUST_USE bool PrintStackTrace(JSContext* cx,
243
JS::Handle<JSObject*> stackObj);
244
245
} /* namespace shell */
246
} /* namespace js */
247
248
#endif