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 /* JavaScript API. */
8 
9 #ifndef js_ContextOptions_h
10 #define js_ContextOptions_h
11 
12 #include "jstypes.h"  // JS_PUBLIC_API
13 
14 struct JS_PUBLIC_API JSContext;
15 
16 namespace JS {
17 
18 class JS_PUBLIC_API ContextOptions {
19  public:
ContextOptions()20   ContextOptions()
21       : asmJS_(true),
22         wasm_(true),
23         wasmForTrustedPrinciples_(true),
24         wasmVerbose_(false),
25         wasmBaseline_(true),
26         wasmIon_(true),
27         wasmCranelift_(false),
28         wasmReftypes_(true),
29         wasmGc_(false),
30         wasmMultiValue_(false),
31         wasmSimd_(false),
32         testWasmAwaitTier2_(false),
33         throwOnAsmJSValidationFailure_(false),
34         disableIon_(false),
35         disableEvalSecurityChecks_(false),
36         asyncStack_(true),
37         sourcePragmas_(true),
38         throwOnDebuggeeWouldRun_(true),
39         dumpStackOnDebuggeeWouldRun_(false),
40         strictMode_(false),
41 #ifdef JS_ENABLE_SMOOSH
42         trackNotImplemented_(false),
43         trySmoosh_(false),
44 #endif
45         fuzzing_(false) {
46   }
47 
asmJS()48   bool asmJS() const { return asmJS_; }
setAsmJS(bool flag)49   ContextOptions& setAsmJS(bool flag) {
50     asmJS_ = flag;
51     return *this;
52   }
toggleAsmJS()53   ContextOptions& toggleAsmJS() {
54     asmJS_ = !asmJS_;
55     return *this;
56   }
57 
wasm()58   bool wasm() const { return wasm_; }
setWasm(bool flag)59   ContextOptions& setWasm(bool flag) {
60     wasm_ = flag;
61     return *this;
62   }
toggleWasm()63   ContextOptions& toggleWasm() {
64     wasm_ = !wasm_;
65     return *this;
66   }
67 
wasmForTrustedPrinciples()68   bool wasmForTrustedPrinciples() const { return wasmForTrustedPrinciples_; }
setWasmForTrustedPrinciples(bool flag)69   ContextOptions& setWasmForTrustedPrinciples(bool flag) {
70     wasmForTrustedPrinciples_ = flag;
71     return *this;
72   }
73 
wasmVerbose()74   bool wasmVerbose() const { return wasmVerbose_; }
setWasmVerbose(bool flag)75   ContextOptions& setWasmVerbose(bool flag) {
76     wasmVerbose_ = flag;
77     return *this;
78   }
79 
wasmBaseline()80   bool wasmBaseline() const { return wasmBaseline_; }
setWasmBaseline(bool flag)81   ContextOptions& setWasmBaseline(bool flag) {
82     wasmBaseline_ = flag;
83     return *this;
84   }
85 
wasmIon()86   bool wasmIon() const { return wasmIon_; }
setWasmIon(bool flag)87   ContextOptions& setWasmIon(bool flag) {
88     wasmIon_ = flag;
89     return *this;
90   }
91 
wasmCranelift()92   bool wasmCranelift() const { return wasmCranelift_; }
93   // Defined out-of-line because it depends on a compile-time option
94   ContextOptions& setWasmCranelift(bool flag);
95 
testWasmAwaitTier2()96   bool testWasmAwaitTier2() const { return testWasmAwaitTier2_; }
setTestWasmAwaitTier2(bool flag)97   ContextOptions& setTestWasmAwaitTier2(bool flag) {
98     testWasmAwaitTier2_ = flag;
99     return *this;
100   }
101 
wasmReftypes()102   bool wasmReftypes() const { return wasmReftypes_; }
setWasmReftypes(bool flag)103   ContextOptions& setWasmReftypes(bool flag) {
104     wasmReftypes_ = flag;
105     return *this;
106   }
107 
wasmGc()108   bool wasmGc() const { return wasmGc_; }
109   // Defined out-of-line because it depends on a compile-time option
110   ContextOptions& setWasmGc(bool flag);
111 
wasmMultiValue()112   bool wasmMultiValue() const { return wasmMultiValue_; }
113   // Defined out-of-line because it depends on a compile-time option
114   ContextOptions& setWasmMultiValue(bool flag);
115 
wasmSimd()116   bool wasmSimd() const { return wasmSimd_; }
117   // Defined out-of-line because it depends on a compile-time option
118   ContextOptions& setWasmSimd(bool flag);
119 
throwOnAsmJSValidationFailure()120   bool throwOnAsmJSValidationFailure() const {
121     return throwOnAsmJSValidationFailure_;
122   }
setThrowOnAsmJSValidationFailure(bool flag)123   ContextOptions& setThrowOnAsmJSValidationFailure(bool flag) {
124     throwOnAsmJSValidationFailure_ = flag;
125     return *this;
126   }
toggleThrowOnAsmJSValidationFailure()127   ContextOptions& toggleThrowOnAsmJSValidationFailure() {
128     throwOnAsmJSValidationFailure_ = !throwOnAsmJSValidationFailure_;
129     return *this;
130   }
131 
132   // Override to allow disabling Ion for this context irrespective of the
133   // process-wide Ion-enabled setting. This must be set right after creating
134   // the context.
disableIon()135   bool disableIon() const { return disableIon_; }
setDisableIon()136   ContextOptions& setDisableIon() {
137     disableIon_ = true;
138     return *this;
139   }
140 
141   // Override to allow disabling the eval restriction security checks for
142   // this context.
disableEvalSecurityChecks()143   bool disableEvalSecurityChecks() const { return disableEvalSecurityChecks_; }
setDisableEvalSecurityChecks()144   ContextOptions& setDisableEvalSecurityChecks() {
145     disableEvalSecurityChecks_ = true;
146     return *this;
147   }
148 
asyncStack()149   bool asyncStack() const { return asyncStack_; }
setAsyncStack(bool flag)150   ContextOptions& setAsyncStack(bool flag) {
151     asyncStack_ = flag;
152     return *this;
153   }
154 
155   // Enable/disable support for parsing '//(#@) source(Mapping)?URL=' pragmas.
sourcePragmas()156   bool sourcePragmas() const { return sourcePragmas_; }
setSourcePragmas(bool flag)157   ContextOptions& setSourcePragmas(bool flag) {
158     sourcePragmas_ = flag;
159     return *this;
160   }
161 
throwOnDebuggeeWouldRun()162   bool throwOnDebuggeeWouldRun() const { return throwOnDebuggeeWouldRun_; }
setThrowOnDebuggeeWouldRun(bool flag)163   ContextOptions& setThrowOnDebuggeeWouldRun(bool flag) {
164     throwOnDebuggeeWouldRun_ = flag;
165     return *this;
166   }
167 
dumpStackOnDebuggeeWouldRun()168   bool dumpStackOnDebuggeeWouldRun() const {
169     return dumpStackOnDebuggeeWouldRun_;
170   }
setDumpStackOnDebuggeeWouldRun(bool flag)171   ContextOptions& setDumpStackOnDebuggeeWouldRun(bool flag) {
172     dumpStackOnDebuggeeWouldRun_ = flag;
173     return *this;
174   }
175 
strictMode()176   bool strictMode() const { return strictMode_; }
setStrictMode(bool flag)177   ContextOptions& setStrictMode(bool flag) {
178     strictMode_ = flag;
179     return *this;
180   }
toggleStrictMode()181   ContextOptions& toggleStrictMode() {
182     strictMode_ = !strictMode_;
183     return *this;
184   }
185 
186 #ifdef JS_ENABLE_SMOOSH
187   // Track Number of Not Implemented Calls by writing to a file
trackNotImplemented()188   bool trackNotImplemented() const { return trackNotImplemented_; }
setTrackNotImplemented(bool flag)189   ContextOptions& setTrackNotImplemented(bool flag) {
190     trackNotImplemented_ = flag;
191     return *this;
192   }
193 
194   // Try compiling SmooshMonkey frontend first, and fallback to C++
195   // implementation when it fails.
trySmoosh()196   bool trySmoosh() const { return trySmoosh_; }
setTrySmoosh(bool flag)197   ContextOptions& setTrySmoosh(bool flag) {
198     trySmoosh_ = flag;
199     return *this;
200   }
201 
202 #endif  // JS_ENABLE_SMOOSH
203 
fuzzing()204   bool fuzzing() const { return fuzzing_; }
205   // Defined out-of-line because it depends on a compile-time option
206   ContextOptions& setFuzzing(bool flag);
207 
disableOptionsForSafeMode()208   void disableOptionsForSafeMode() {
209     setAsmJS(false);
210     setWasm(false);
211     setWasmBaseline(false);
212     setWasmIon(false);
213     setWasmGc(false);
214     setWasmMultiValue(false);
215     setWasmSimd(false);
216   }
217 
218  private:
219   bool asmJS_ : 1;
220   bool wasm_ : 1;
221   bool wasmForTrustedPrinciples_ : 1;
222   bool wasmVerbose_ : 1;
223   bool wasmBaseline_ : 1;
224   bool wasmIon_ : 1;
225   bool wasmCranelift_ : 1;
226   bool wasmReftypes_ : 1;
227   bool wasmGc_ : 1;
228   bool wasmMultiValue_ : 1;
229   bool wasmSimd_ : 1;
230   bool testWasmAwaitTier2_ : 1;
231   bool throwOnAsmJSValidationFailure_ : 1;
232   bool disableIon_ : 1;
233   bool disableEvalSecurityChecks_ : 1;
234   bool asyncStack_ : 1;
235   bool sourcePragmas_ : 1;
236   bool throwOnDebuggeeWouldRun_ : 1;
237   bool dumpStackOnDebuggeeWouldRun_ : 1;
238   bool strictMode_ : 1;
239 #ifdef JS_ENABLE_SMOOSH
240   bool trackNotImplemented_ : 1;
241   bool trySmoosh_ : 1;
242 #endif
243   bool fuzzing_ : 1;
244 };
245 
246 JS_PUBLIC_API ContextOptions& ContextOptionsRef(JSContext* cx);
247 
248 }  // namespace JS
249 
250 #endif  // js_ContextOptions_h
251