Source code

Revision control

Other Tools

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
/*
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 "js/UniquePtr.h"
18
#include "vm/JSContext.h"
19
20
namespace js {
21
class ErrorObject;
22
23
UniquePtr<JSErrorNotes::Note> CopyErrorNote(JSContext* cx,
24
JSErrorNotes::Note* note);
25
26
UniquePtr<JSErrorReport> CopyErrorReport(JSContext* cx, JSErrorReport* report);
27
28
JSString* 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 ErrorToException(JSContext* cx, JSErrorReport* reportp,
45
JSErrorCallback callback, void* userRef);
46
47
extern JSErrorReport* ErrorFromException(JSContext* cx, HandleObject obj);
48
49
/*
50
* Make a copy of errobj parented to cx's compartment's global.
51
*
52
* errobj may be in a different compartment than cx, but it must be an Error
53
* object (not a wrapper of one) and it must not be one of the standard error
54
* prototype objects (errobj->getPrivate() must not be nullptr).
55
*/
56
extern JSObject* CopyErrorObject(JSContext* cx,
57
JS::Handle<ErrorObject*> errobj);
58
59
static_assert(
60
JSEXN_ERR == 0 &&
61
JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError &&
62
JSProto_Error + JSEXN_EVALERR == JSProto_EvalError &&
63
JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError &&
64
JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError &&
65
JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError &&
66
JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError &&
67
JSProto_Error + JSEXN_URIERR == JSProto_URIError &&
68
JSProto_Error + JSEXN_DEBUGGEEWOULDRUN == JSProto_DebuggeeWouldRun &&
69
JSProto_Error + JSEXN_WASMCOMPILEERROR == JSProto_CompileError &&
70
JSProto_Error + JSEXN_WASMLINKERROR == JSProto_LinkError &&
71
JSProto_Error + JSEXN_WASMRUNTIMEERROR == JSProto_RuntimeError &&
72
JSEXN_WASMRUNTIMEERROR + 1 == JSEXN_WARN &&
73
JSEXN_WARN + 1 == JSEXN_NOTE && JSEXN_NOTE + 1 == JSEXN_LIMIT,
74
"GetExceptionProtoKey and ExnTypeFromProtoKey require that "
75
"each corresponding JSExnType and JSProtoKey value be separated "
76
"by the same constant value");
77
78
static inline JSProtoKey GetExceptionProtoKey(JSExnType exn) {
79
MOZ_ASSERT(JSEXN_ERR <= exn);
80
MOZ_ASSERT(exn < JSEXN_WARN);
81
return JSProtoKey(JSProto_Error + int(exn));
82
}
83
84
static inline JSExnType ExnTypeFromProtoKey(JSProtoKey key) {
85
JSExnType type = static_cast<JSExnType>(key - JSProto_Error);
86
MOZ_ASSERT(type >= JSEXN_ERR);
87
MOZ_ASSERT(type < JSEXN_ERROR_LIMIT);
88
return type;
89
}
90
91
static inline bool IsErrorProtoKey(JSProtoKey key) {
92
int type = key - JSProto_Error;
93
return type >= JSEXN_ERR && type < JSEXN_ERROR_LIMIT;
94
}
95
96
class AutoClearPendingException {
97
JSContext* cx;
98
99
public:
100
explicit AutoClearPendingException(JSContext* cxArg) : cx(cxArg) {}
101
102
~AutoClearPendingException() { JS_ClearPendingException(cx); }
103
};
104
105
extern const char* ValueToSourceForError(JSContext* cx, HandleValue val,
106
JS::UniqueChars& bytes);
107
108
bool GetInternalError(JSContext* cx, unsigned errorNumber,
109
MutableHandleValue error);
110
bool GetTypeError(JSContext* cx, unsigned errorNumber,
111
MutableHandleValue error);
112
113
} // namespace js
114
115
#endif /* jsexn_h */