Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / DOMException.h
blobc44cf904a04ab81161c224169ddca54db0980587
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 #ifndef mozilla_dom_DOMException_h__
8 #define mozilla_dom_DOMException_h__
10 // We intentionally shadow non-virtual methods, but gcc gets confused.
11 #ifdef __GNUC__
12 # pragma GCC diagnostic push
13 # pragma GCC diagnostic ignored "-Woverloaded-virtual"
14 #endif
16 #include <stdint.h>
17 #include "js/Value.h"
18 #include "jspubtd.h"
19 #include "nsCOMPtr.h"
20 #include "nsCycleCollectionParticipant.h"
21 #include "nsID.h"
22 #include "nsWrapperCache.h"
23 #include "nsIException.h"
24 #include "nsString.h"
25 #include "mozilla/dom/BindingDeclarations.h"
27 class nsIGlobalObject;
28 class nsIStackFrame;
30 nsresult NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult,
31 nsACString& aName,
32 nsACString& aMessage,
33 uint16_t* aCode = nullptr);
35 namespace mozilla {
36 class ErrorResult;
38 namespace dom {
40 class GlobalObject;
42 #define MOZILLA_EXCEPTION_IID \
43 { \
44 0x55eda557, 0xeba0, 0x4fe3, { \
45 0xae, 0x2e, 0xf3, 0x94, 0x49, 0x23, 0x62, 0xd6 \
46 } \
49 class Exception : public nsIException, public nsWrapperCache {
50 public:
51 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_EXCEPTION_IID)
53 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Exception)
55 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
56 NS_DECL_NSIEXCEPTION
58 const nsCString& GetMessageMoz() const { return mMessage; }
59 nsresult GetResult() const { return mResult; }
60 // DOMException wants different ToString behavior, so allow it to override.
61 virtual void ToString(JSContext* aCx, nsACString& aReturn);
63 // Cruft used by XPConnect for exceptions originating in JS implemented
64 // components.
65 bool StealJSVal(JS::Value* aVp);
66 void StowJSVal(JS::Value& aVp);
68 // WebIDL API
69 virtual JSObject* WrapObject(JSContext* cx,
70 JS::Handle<JSObject*> aGivenProto) override;
72 nsISupports* GetParentObject() const { return nullptr; }
74 void GetMessageMoz(nsString& retval);
76 uint32_t Result() const;
78 void GetName(nsAString& retval);
80 virtual void GetErrorMessage(nsAString& aRetVal) {
81 // Since GetName is non non-virtual and deals with different
82 // member variables in Exception vs. DOMException, have a virtual
83 // method to ensure the right error message creation.
84 nsAutoString name;
85 GetName(name);
86 CreateErrorMessage(name, aRetVal);
89 void GetFilename(JSContext* aCx, nsAString& aFilename);
91 uint32_t SourceId(JSContext* aCx) const;
93 uint32_t LineNumber(JSContext* aCx) const;
95 uint32_t ColumnNumber() const;
97 already_AddRefed<nsIStackFrame> GetLocation() const;
99 nsISupports* GetData() const;
101 void GetStack(JSContext* aCx, nsAString& aStack) const;
103 void Stringify(JSContext* aCx, nsString& retval);
105 Exception(const nsACString& aMessage, nsresult aResult,
106 const nsACString& aName, nsIStackFrame* aLocation,
107 nsISupports* aData);
108 Exception(nsCString&& aMessage, nsresult aResult, nsCString&& aName);
110 protected:
111 virtual ~Exception();
113 void CreateErrorMessage(const nsAString& aName, nsAString& aRetVal) {
114 // Create similar error message as what ErrorReport::init does in jsexn.cpp.
115 if (!aName.IsEmpty() && !mMessage.IsEmpty()) {
116 aRetVal.Assign(aName);
117 aRetVal.AppendLiteral(": ");
118 AppendUTF8toUTF16(mMessage, aRetVal);
119 } else if (!aName.IsEmpty()) {
120 aRetVal.Assign(aName);
121 } else if (!mMessage.IsEmpty()) {
122 CopyUTF8toUTF16(mMessage, aRetVal);
123 } else {
124 aRetVal.Truncate();
128 nsCString mMessage;
129 nsresult mResult;
130 nsCString mName;
131 nsCOMPtr<nsIStackFrame> mLocation;
132 nsCOMPtr<nsISupports> mData;
134 bool mHoldingJSVal;
135 JS::Heap<JS::Value> mThrownJSVal;
138 NS_DEFINE_STATIC_IID_ACCESSOR(Exception, MOZILLA_EXCEPTION_IID)
140 class DOMException : public Exception {
141 public:
142 DOMException(nsresult aRv, const nsACString& aMessage,
143 const nsACString& aName, uint16_t aCode,
144 nsIStackFrame* aLocation = nullptr);
145 DOMException(nsresult aRv, nsCString&& aMessage, nsCString&& aName,
146 uint16_t aCode);
148 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMException, Exception)
150 // nsWrapperCache overrides
151 virtual JSObject* WrapObject(JSContext* aCx,
152 JS::Handle<JSObject*> aGivenProto) override;
154 static already_AddRefed<DOMException> Constructor(
155 GlobalObject& /* unused */, const nsAString& aMessage,
156 const Optional<nsAString>& aName);
158 uint16_t Code() const { return mCode; }
160 // Intentionally shadow the Exception version.
161 void GetName(nsString& retval);
163 // Exception overrides
164 void ToString(JSContext* aCx, nsACString& aReturn) override;
166 virtual void GetErrorMessage(nsAString& aRetVal) override {
167 // See the comment in Exception::GetErrorMessage.
168 nsAutoString name;
169 GetName(name);
170 CreateErrorMessage(name, aRetVal);
173 static already_AddRefed<DOMException> Create(nsresult aRv);
175 static already_AddRefed<DOMException> Create(nsresult aRv,
176 const nsACString& aMessage);
178 static already_AddRefed<DOMException> ReadStructuredClone(
179 JSContext* aCx, nsIGlobalObject* aGlobal,
180 JSStructuredCloneReader* aReader);
181 bool WriteStructuredClone(JSContext* aCx,
182 JSStructuredCloneWriter* aWriter) const;
184 protected:
185 virtual ~DOMException() = default;
187 uint16_t mCode;
190 } // namespace dom
191 } // namespace mozilla
193 #ifdef __GNUC__
194 # pragma GCC diagnostic pop
195 #endif
197 #endif