Debian Codegraph @

js/src/shell/jsshell.h

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