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