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/. */
8 * JS runtime exception classes.
14 #include "mozilla/Assertions.h"
18 #include "NamespaceImports.h"
20 #include "js/ErrorReport.h"
21 #include "js/Exception.h"
22 #include "js/friend/ErrorMessages.h" // JSErr_Limit
23 #include "js/RootingAPI.h"
24 #include "js/TypeDecls.h"
25 #include "js/UniquePtr.h"
26 #include "js/Utility.h"
28 extern const JSErrorFormatString js_ErrorFormatString
[JSErr_Limit
];
34 UniquePtr
<JSErrorNotes::Note
> CopyErrorNote(JSContext
* cx
,
35 JSErrorNotes::Note
* note
);
37 UniquePtr
<JSErrorReport
> CopyErrorReport(JSContext
* cx
, JSErrorReport
* report
);
39 bool CaptureStack(JSContext
* cx
, MutableHandleObject stack
);
41 JSString
* ComputeStackString(JSContext
* cx
);
44 * Given a JSErrorReport, check to see if there is an exception associated with
45 * the error number. If there is, then create an appropriate Error object,
46 * set it as the pending exception.
48 * It's possible we fail (due to OOM or some other error) and end up setting
49 * JSContext::unwrappedException to a different exception.
50 * The original error described by reportp typically won't be reported anywhere
53 * If the error code is unrecognized, or if we decided to do nothing in order to
54 * avoid recursion, we simply return and this error is just being swept under
57 extern void ErrorToException(JSContext
* cx
, JSErrorReport
* reportp
,
58 JSErrorCallback callback
, void* userRef
);
60 extern JSErrorReport
* ErrorFromException(JSContext
* cx
, HandleObject obj
);
63 * Make a copy of errobj parented to cx's compartment's global.
65 * errobj may be in a different compartment than cx, but it must be an Error
66 * object (not a wrapper of one) and it must not be one of the standard error
67 * prototype objects (errobj->getPrivate() must not be nullptr).
69 extern JSObject
* CopyErrorObject(JSContext
* cx
,
70 JS::Handle
<ErrorObject
*> errobj
);
74 JSProto_Error
+ JSEXN_INTERNALERR
== JSProto_InternalError
&&
75 JSProto_Error
+ JSEXN_AGGREGATEERR
== JSProto_AggregateError
&&
76 JSProto_Error
+ JSEXN_EVALERR
== JSProto_EvalError
&&
77 JSProto_Error
+ JSEXN_RANGEERR
== JSProto_RangeError
&&
78 JSProto_Error
+ JSEXN_REFERENCEERR
== JSProto_ReferenceError
&&
79 JSProto_Error
+ JSEXN_SYNTAXERR
== JSProto_SyntaxError
&&
80 JSProto_Error
+ JSEXN_TYPEERR
== JSProto_TypeError
&&
81 JSProto_Error
+ JSEXN_URIERR
== JSProto_URIError
&&
82 JSProto_Error
+ JSEXN_DEBUGGEEWOULDRUN
== JSProto_DebuggeeWouldRun
&&
83 JSProto_Error
+ JSEXN_WASMCOMPILEERROR
== JSProto_CompileError
&&
84 JSProto_Error
+ JSEXN_WASMLINKERROR
== JSProto_LinkError
&&
85 JSProto_Error
+ JSEXN_WASMRUNTIMEERROR
== JSProto_RuntimeError
&&
86 JSEXN_WASMRUNTIMEERROR
+ 1 == JSEXN_WARN
&&
87 JSEXN_WARN
+ 1 == JSEXN_NOTE
&& JSEXN_NOTE
+ 1 == JSEXN_LIMIT
,
88 "GetExceptionProtoKey and ExnTypeFromProtoKey require that "
89 "each corresponding JSExnType and JSProtoKey value be separated "
90 "by the same constant value");
92 static inline constexpr JSProtoKey
GetExceptionProtoKey(JSExnType exn
) {
93 MOZ_ASSERT(JSEXN_ERR
<= exn
);
94 MOZ_ASSERT(exn
< JSEXN_WARN
);
95 return JSProtoKey(JSProto_Error
+ int(exn
));
98 static inline JSExnType
ExnTypeFromProtoKey(JSProtoKey key
) {
99 JSExnType type
= static_cast<JSExnType
>(key
- JSProto_Error
);
100 MOZ_ASSERT(type
>= JSEXN_ERR
);
101 MOZ_ASSERT(type
< JSEXN_ERROR_LIMIT
);
105 static inline bool IsErrorProtoKey(JSProtoKey key
) {
106 int type
= key
- JSProto_Error
;
107 return type
>= JSEXN_ERR
&& type
< JSEXN_ERROR_LIMIT
;
110 class AutoClearPendingException
{
114 explicit AutoClearPendingException(JSContext
* cxArg
) : cx(cxArg
) {}
116 ~AutoClearPendingException() { JS_ClearPendingException(cx
); }
119 extern const char* ValueToSourceForError(JSContext
* cx
, HandleValue val
,
120 JS::UniqueChars
& bytes
);
122 bool GetInternalError(JSContext
* cx
, unsigned errorNumber
,
123 MutableHandleValue error
);
124 bool GetTypeError(JSContext
* cx
, unsigned errorNumber
,
125 MutableHandleValue error
);
126 bool GetAggregateError(JSContext
* cx
, unsigned errorNumber
,
127 MutableHandleValue error
);