Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / src / jsexn.h
blob5e681248c75d0e233cd3655591d2b3b10a5779df
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/. */
7 /*
8 * JS runtime exception classes.
9 */
11 #ifndef jsexn_h
12 #define jsexn_h
14 #include "mozilla/Assertions.h"
16 #include "jspubtd.h"
17 #include "jstypes.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];
30 namespace js {
32 class ErrorObject;
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
51 * in this case.
53 * Returns true if the error was converted to an exception. If the error code
54 * is unrecognized, we fail due to OOM, or if we decided to do nothing in order
55 * to avoid recursion, we return false and this error is just being swept under
56 * the rug.
58 extern bool ErrorToException(JSContext* cx, JSErrorReport* reportp,
59 JSErrorCallback callback, void* userRef);
61 extern JSErrorReport* ErrorFromException(JSContext* cx, HandleObject obj);
64 * Make a copy of errobj parented to cx's compartment's global.
66 * errobj may be in a different compartment than cx, but it must be an Error
67 * object (not a wrapper of one) and it must not be one of the standard error
68 * prototype objects (errobj->getPrivate() must not be nullptr).
70 extern JSObject* CopyErrorObject(JSContext* cx,
71 JS::Handle<ErrorObject*> errobj);
73 static_assert(
74 JSEXN_ERR == 0 &&
75 JSProto_Error + int(JSEXN_INTERNALERR) == JSProto_InternalError &&
76 JSProto_Error + int(JSEXN_AGGREGATEERR) == JSProto_AggregateError &&
77 JSProto_Error + int(JSEXN_EVALERR) == JSProto_EvalError &&
78 JSProto_Error + int(JSEXN_RANGEERR) == JSProto_RangeError &&
79 JSProto_Error + int(JSEXN_REFERENCEERR) == JSProto_ReferenceError &&
80 JSProto_Error + int(JSEXN_SYNTAXERR) == JSProto_SyntaxError &&
81 JSProto_Error + int(JSEXN_TYPEERR) == JSProto_TypeError &&
82 JSProto_Error + int(JSEXN_URIERR) == JSProto_URIError &&
83 JSProto_Error + int(JSEXN_DEBUGGEEWOULDRUN) ==
84 JSProto_DebuggeeWouldRun &&
85 JSProto_Error + int(JSEXN_WASMCOMPILEERROR) == JSProto_CompileError &&
86 JSProto_Error + int(JSEXN_WASMLINKERROR) == JSProto_LinkError &&
87 JSProto_Error + int(JSEXN_WASMRUNTIMEERROR) == JSProto_RuntimeError &&
88 JSEXN_WASMRUNTIMEERROR + 1 == JSEXN_WARN &&
89 JSEXN_WARN + 1 == JSEXN_NOTE && JSEXN_NOTE + 1 == JSEXN_LIMIT,
90 "GetExceptionProtoKey and ExnTypeFromProtoKey require that "
91 "each corresponding JSExnType and JSProtoKey value be separated "
92 "by the same constant value");
94 static inline constexpr JSProtoKey GetExceptionProtoKey(JSExnType exn) {
95 MOZ_ASSERT(JSEXN_ERR <= exn);
96 MOZ_ASSERT(exn < JSEXN_WARN);
97 return JSProtoKey(JSProto_Error + int(exn));
100 static inline JSExnType ExnTypeFromProtoKey(JSProtoKey key) {
101 JSExnType type = static_cast<JSExnType>(key - JSProto_Error);
102 MOZ_ASSERT(type >= JSEXN_ERR);
103 MOZ_ASSERT(type < JSEXN_ERROR_LIMIT);
104 return type;
107 static inline bool IsErrorProtoKey(JSProtoKey key) {
108 int type = key - JSProto_Error;
109 return type >= JSEXN_ERR && type < JSEXN_ERROR_LIMIT;
112 class AutoClearPendingException {
113 JSContext* cx;
115 public:
116 explicit AutoClearPendingException(JSContext* cxArg) : cx(cxArg) {}
118 ~AutoClearPendingException() { JS_ClearPendingException(cx); }
121 extern const char* ValueToSourceForError(JSContext* cx, HandleValue val,
122 JS::UniqueChars& bytes);
124 bool GetInternalError(JSContext* cx, unsigned errorNumber,
125 MutableHandleValue error);
126 bool GetTypeError(JSContext* cx, unsigned errorNumber,
127 MutableHandleValue error);
128 bool GetAggregateError(JSContext* cx, unsigned errorNumber,
129 MutableHandleValue error);
131 } // namespace js
133 #endif /* jsexn_h */