1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
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 /*
8  * JS runtime exception classes.
9  */
10 
11 #ifndef jsexn_h
12 #define jsexn_h
13 
14 #include "jsapi.h"
15 #include "NamespaceImports.h"
16 
17 #include "vm/JSContext.h"
18 
19 namespace js {
20 class ErrorObject;
21 
22 JSErrorNotes::Note* CopyErrorNote(JSContext* cx, JSErrorNotes::Note* note);
23 
24 JSErrorReport* CopyErrorReport(JSContext* cx, JSErrorReport* report);
25 
26 JSString* ComputeStackString(JSContext* cx);
27 
28 /*
29  * Given a JSErrorReport, check to see if there is an exception associated with
30  * the error number.  If there is, then create an appropriate exception object,
31  * set it as the pending exception, and set the JSREPORT_EXCEPTION flag on the
32  * error report.
33  *
34  * It's possible we fail (due to OOM or some other error) and end up setting
35  * cx->exception to a different exception. The original error described by
36  * *reportp typically won't be reported anywhere in this case.
37  *
38  * If the error code is unrecognized, or if we decided to do nothing in order to
39  * avoid recursion, we simply return and this error is just being swept under
40  * the rug.
41  */
42 extern void ErrorToException(JSContext* cx, JSErrorReport* reportp,
43                              JSErrorCallback callback, void* userRef);
44 
45 extern JSErrorReport* ErrorFromException(JSContext* cx, HandleObject obj);
46 
47 /*
48  * Make a copy of errobj parented to cx's compartment's global.
49  *
50  * errobj may be in a different compartment than cx, but it must be an Error
51  * object (not a wrapper of one) and it must not be one of the standard error
52  * prototype objects (errobj->getPrivate() must not be nullptr).
53  */
54 extern JSObject* CopyErrorObject(JSContext* cx,
55                                  JS::Handle<ErrorObject*> errobj);
56 
57 static_assert(
58     JSEXN_ERR == 0 &&
59         JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError &&
60         JSProto_Error + JSEXN_EVALERR == JSProto_EvalError &&
61         JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError &&
62         JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError &&
63         JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError &&
64         JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError &&
65         JSProto_Error + JSEXN_URIERR == JSProto_URIError &&
66         JSProto_Error + JSEXN_DEBUGGEEWOULDRUN == JSProto_DebuggeeWouldRun &&
67         JSProto_Error + JSEXN_WASMCOMPILEERROR == JSProto_CompileError &&
68         JSProto_Error + JSEXN_WASMLINKERROR == JSProto_LinkError &&
69         JSProto_Error + JSEXN_WASMRUNTIMEERROR == JSProto_RuntimeError &&
70         JSEXN_WASMRUNTIMEERROR + 1 == JSEXN_WARN &&
71         JSEXN_WARN + 1 == JSEXN_NOTE && JSEXN_NOTE + 1 == JSEXN_LIMIT,
72     "GetExceptionProtoKey and ExnTypeFromProtoKey require that "
73     "each corresponding JSExnType and JSProtoKey value be separated "
74     "by the same constant value");
75 
GetExceptionProtoKey(JSExnType exn)76 static inline JSProtoKey GetExceptionProtoKey(JSExnType exn) {
77   MOZ_ASSERT(JSEXN_ERR <= exn);
78   MOZ_ASSERT(exn < JSEXN_WARN);
79   return JSProtoKey(JSProto_Error + int(exn));
80 }
81 
ExnTypeFromProtoKey(JSProtoKey key)82 static inline JSExnType ExnTypeFromProtoKey(JSProtoKey key) {
83   JSExnType type = static_cast<JSExnType>(key - JSProto_Error);
84   MOZ_ASSERT(type >= JSEXN_ERR);
85   MOZ_ASSERT(type < JSEXN_ERROR_LIMIT);
86   return type;
87 }
88 
IsErrorProtoKey(JSProtoKey key)89 static inline bool IsErrorProtoKey(JSProtoKey key) {
90   int type = key - JSProto_Error;
91   return type >= JSEXN_ERR && type < JSEXN_ERROR_LIMIT;
92 }
93 
94 class AutoClearPendingException {
95   JSContext* cx;
96 
97  public:
AutoClearPendingException(JSContext * cxArg)98   explicit AutoClearPendingException(JSContext* cxArg) : cx(cxArg) {}
99 
~AutoClearPendingException()100   ~AutoClearPendingException() { JS_ClearPendingException(cx); }
101 };
102 
103 class AutoAssertNoPendingException {
104   mozilla::DebugOnly<JSContext*> cx;
105 
106  public:
AutoAssertNoPendingException(JSContext * cxArg)107   explicit AutoAssertNoPendingException(JSContext* cxArg) : cx(cxArg) {}
108 
~AutoAssertNoPendingException()109   ~AutoAssertNoPendingException() { MOZ_ASSERT(!JS_IsExceptionPending(cx)); }
110 };
111 
112 extern const char* ValueToSourceForError(JSContext* cx, HandleValue val,
113                                          JSAutoByteString& bytes);
114 
115 bool GetInternalError(JSContext* cx, unsigned errorNumber,
116                       MutableHandleValue error);
117 bool GetTypeError(JSContext* cx, unsigned errorNumber,
118                   MutableHandleValue error);
119 
120 }  // namespace js
121 
122 #endif /* jsexn_h */
123