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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * A set of structs for tracking exceptions that need to be thrown to JS:
9 * ErrorResult and IgnoredErrorResult.
11 * Conceptually, these structs represent either success or an exception in the
12 * process of being thrown. This means that a failing ErrorResult _must_ be
13 * handled in one of the following ways before coming off the stack:
15 * 1) Suppressed via SuppressException().
16 * 2) Converted to a pure nsresult return value via StealNSResult().
17 * 3) Converted to an actual pending exception on a JSContext via
18 * MaybeSetPendingException.
19 * 4) Converted to an exception JS::Value (probably to then reject a Promise
20 * with) via dom::ToJSValue.
22 * An IgnoredErrorResult will automatically do the first of those four things.
25 #ifndef mozilla_ErrorResult_h
26 #define mozilla_ErrorResult_h
33 #include "js/GCAnnotations.h"
34 #include "js/ErrorReport.h"
36 #include "mozilla/Assertions.h"
37 #include "mozilla/Attributes.h"
38 #include "mozilla/Utf8.h"
39 #include "nsISupportsImpl.h"
57 enum ErrNum
: uint16_t {
58 #define MSG_DEF(_name, _argc, _has_context, _exn, _str) _name,
59 #include "mozilla/dom/Errors.msg"
64 // Debug-only compile-time table of the number of arguments of each error, for
65 // use in static_assert.
66 #if defined(DEBUG) && (defined(__clang__) || defined(__GNUC__))
67 uint16_t constexpr ErrorFormatNumArgs
[] = {
68 # define MSG_DEF(_name, _argc, _has_context, _exn, _str) _argc,
69 # include "mozilla/dom/Errors.msg"
74 // Table of whether various error messages want a context arg.
75 bool constexpr ErrorFormatHasContext
[] = {
76 #define MSG_DEF(_name, _argc, _has_context, _exn, _str) _has_context,
77 #include "mozilla/dom/Errors.msg"
81 // Table of the kinds of exceptions error messages will produce.
82 JSExnType
constexpr ErrorExceptionType
[] = {
83 #define MSG_DEF(_name, _argc, _has_context, _exn, _str) _exn,
84 #include "mozilla/dom/Errors.msg"
88 uint16_t GetErrorArgCount(const ErrNum aErrorNumber
);
90 namespace binding_detail
{
91 void ThrowErrorMessage(JSContext
* aCx
, const unsigned aErrorNumber
, ...);
92 } // namespace binding_detail
94 template <ErrNum errorNumber
, typename
... Ts
>
95 inline bool ThrowErrorMessage(JSContext
* aCx
, Ts
&&... aArgs
) {
96 #if defined(DEBUG) && (defined(__clang__) || defined(__GNUC__))
97 static_assert(ErrorFormatNumArgs
[errorNumber
] == sizeof...(aArgs
),
98 "Pass in the right number of arguments");
100 binding_detail::ThrowErrorMessage(aCx
, static_cast<unsigned>(errorNumber
),
101 std::forward
<Ts
>(aArgs
)...);
105 template <typename CharT
>
106 struct TStringArrayAppender
{
107 static void Append(nsTArray
<nsTString
<CharT
>>& aArgs
, uint16_t aCount
) {
108 MOZ_RELEASE_ASSERT(aCount
== 0,
109 "Must give at least as many string arguments as are "
110 "required by the ErrNum.");
113 // Allow passing nsAString/nsACString instances for our args.
114 template <typename
... Ts
>
115 static void Append(nsTArray
<nsTString
<CharT
>>& aArgs
, uint16_t aCount
,
116 const nsTSubstring
<CharT
>& aFirst
, Ts
&&... aOtherArgs
) {
119 "There should not be more string arguments provided than are "
120 "required by the ErrNum.");
123 aArgs
.AppendElement(aFirst
);
124 Append(aArgs
, aCount
- 1, std::forward
<Ts
>(aOtherArgs
)...);
127 // Also allow passing literal instances for our args.
128 template <int N
, typename
... Ts
>
129 static void Append(nsTArray
<nsTString
<CharT
>>& aArgs
, uint16_t aCount
,
130 const CharT (&aFirst
)[N
], Ts
&&... aOtherArgs
) {
133 "There should not be more string arguments provided than are "
134 "required by the ErrNum.");
137 aArgs
.AppendElement(nsTLiteralString
<CharT
>(aFirst
));
138 Append(aArgs
, aCount
- 1, std::forward
<Ts
>(aOtherArgs
)...);
142 using StringArrayAppender
= TStringArrayAppender
<char16_t
>;
143 using CStringArrayAppender
= TStringArrayAppender
<char>;
149 class CopyableErrorResult
;
151 namespace binding_danger
{
154 * Templated implementation class for various ErrorResult-like things. The
155 * instantiations differ only in terms of their cleanup policies (used in the
156 * destructor), which they can specify via the template argument. Note that
157 * this means it's safe to reinterpret_cast between the instantiations unless
158 * you plan to invoke the destructor through such a cast pointer.
160 * A cleanup policy consists of two booleans: whether to assert that we've been
161 * reported or suppressed, and whether to then go ahead and suppress the
164 template <typename CleanupPolicy
>
171 mMightHaveUnreportedJSException(false),
172 mUnionState(HasNothing
)
178 AssertInOwningThread();
180 if (CleanupPolicy::assertHandled
) {
181 // Consumers should have called one of MaybeSetPendingException
182 // (possibly via ToJSValue), StealNSResult, and SuppressException
183 AssertReportedOrSuppressed();
186 if (CleanupPolicy::suppress
) {
190 // And now assert that we're in a good final state.
191 AssertReportedOrSuppressed();
194 TErrorResult(TErrorResult
&& aRHS
)
195 // Initialize mResult and whatever else we need to default-initialize, so
196 // the ClearUnionData call in our operator= will do the right thing
199 *this = std::move(aRHS
);
201 TErrorResult
& operator=(TErrorResult
&& aRHS
);
203 explicit TErrorResult(nsresult aRv
) : TErrorResult() { AssignErrorCode(aRv
); }
205 operator ErrorResult
&();
206 operator const ErrorResult
&() const;
207 operator OOMReporter
&();
209 // This method is deprecated. Consumers should Throw*Error with the
210 // appropriate DOMException name if they are throwing a DOMException. If they
211 // have a random nsresult which may or may not correspond to a DOMException
212 // type, they should consider using an appropriate DOMException with an
213 // informative message and calling the relevant Throw*Error.
214 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
Throw(nsresult rv
) {
215 MOZ_ASSERT(NS_FAILED(rv
), "Please don't try throwing success");
219 // Duplicate our current state on the given TErrorResult object. Any
220 // existing errors or messages on the target will be suppressed before
221 // cloning. Our own error state remains unchanged.
222 void CloneTo(TErrorResult
& aRv
) const;
224 // Use SuppressException when you want to suppress any exception that might be
225 // on the TErrorResult. After this call, the TErrorResult will be back a "no
226 // exception thrown" state.
227 void SuppressException();
229 // Use StealNSResult() when you want to safely convert the TErrorResult to
230 // an nsresult that you will then return to a caller. This will
231 // SuppressException(), since there will no longer be a way to report it.
232 nsresult
StealNSResult() {
233 nsresult rv
= ErrorCode();
235 // Don't propagate out our internal error codes that have special meaning.
236 if (rv
== NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR
||
237 rv
== NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR
||
238 rv
== NS_ERROR_INTERNAL_ERRORRESULT_JS_EXCEPTION
||
239 rv
== NS_ERROR_INTERNAL_ERRORRESULT_DOMEXCEPTION
) {
240 // What to pick here?
241 return NS_ERROR_DOM_INVALID_STATE_ERR
;
247 // Use MaybeSetPendingException to convert a TErrorResult to a pending
248 // exception on the given JSContext. This is the normal "throw an exception"
251 // The return value is false if the TErrorResult represents success, true
252 // otherwise. This does mean that in JSAPI method implementations you can't
253 // just use this as |return rv.MaybeSetPendingException(cx)| (though you could
254 // |return !rv.MaybeSetPendingException(cx)|), but in practice pretty much any
255 // consumer would want to do some more work on the success codepath. So
256 // instead the way you use this is:
258 // if (rv.MaybeSetPendingException(cx)) {
261 // go on to do something useful
263 // The success path is inline, since it should be the common case and we don't
264 // want to pay the price of a function call in some of the consumers of this
265 // method in the common case.
267 // Note that a true return value does NOT mean there is now a pending
268 // exception on aCx, due to uncatchable exceptions. It should still be
269 // considered equivalent to a JSAPI failure in terms of what callers should do
270 // after true is returned.
272 // After this call, the TErrorResult will no longer return true from Failed(),
273 // since the exception will have moved to the JSContext.
275 // If "context" is not null and our exception has a useful message string, the
276 // string "%s: ", with the value of "context" replacing %s, will be prepended
277 // to the message string. The passed-in string must be ASCII.
278 [[nodiscard
]] bool MaybeSetPendingException(
279 JSContext
* cx
, const char* description
= nullptr) {
280 WouldReportJSException();
285 SetPendingException(cx
, description
);
289 // Use StealExceptionFromJSContext to convert a pending exception on a
290 // JSContext to a TErrorResult. This function must be called only when a
291 // JSAPI operation failed. It assumes that lack of pending exception on the
292 // JSContext means an uncatchable exception was thrown.
294 // Codepaths that might call this method must call MightThrowJSException even
295 // if the relevant JSAPI calls do not fail.
297 // When this function returns, JS_IsExceptionPending(cx) will definitely be
299 void StealExceptionFromJSContext(JSContext
* cx
);
301 template <dom::ErrNum errorNumber
, typename
... Ts
>
302 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
303 ThrowTypeError(Ts
&&... messageArgs
) {
304 static_assert(dom::ErrorExceptionType
[errorNumber
] == JSEXN_TYPEERR
,
305 "Throwing a non-TypeError via ThrowTypeError");
306 ThrowErrorWithMessage
<errorNumber
>(NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR
,
307 std::forward
<Ts
>(messageArgs
)...);
310 // To be used when throwing a TypeError with a completely custom
311 // message string that's only used in one spot.
312 inline void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
313 ThrowTypeError(const nsACString
& aMessage
) {
314 this->template ThrowTypeError
<dom::MSG_ONE_OFF_TYPEERR
>(aMessage
);
317 // To be used when throwing a TypeError with a completely custom
318 // message string that's a string literal that's only used in one spot.
320 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
321 ThrowTypeError(const char (&aMessage
)[N
]) {
322 ThrowTypeError(nsLiteralCString(aMessage
));
325 template <dom::ErrNum errorNumber
, typename
... Ts
>
326 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
327 ThrowRangeError(Ts
&&... messageArgs
) {
328 static_assert(dom::ErrorExceptionType
[errorNumber
] == JSEXN_RANGEERR
,
329 "Throwing a non-RangeError via ThrowRangeError");
330 ThrowErrorWithMessage
<errorNumber
>(NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR
,
331 std::forward
<Ts
>(messageArgs
)...);
334 // To be used when throwing a RangeError with a completely custom
335 // message string that's only used in one spot.
336 inline void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
337 ThrowRangeError(const nsACString
& aMessage
) {
338 this->template ThrowRangeError
<dom::MSG_ONE_OFF_RANGEERR
>(aMessage
);
341 // To be used when throwing a RangeError with a completely custom
342 // message string that's a string literal that's only used in one spot.
344 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
345 ThrowRangeError(const char (&aMessage
)[N
]) {
346 ThrowRangeError(nsLiteralCString(aMessage
));
349 bool IsErrorWithMessage() const {
350 return ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR
||
351 ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR
;
354 // Facilities for throwing a preexisting JS exception value via this
355 // TErrorResult. The contract is that any code which might end up calling
356 // ThrowJSException() or StealExceptionFromJSContext() must call
357 // MightThrowJSException() even if no exception is being thrown. Code that
358 // conditionally calls ToJSValue on this TErrorResult only if Failed() must
359 // first call WouldReportJSException even if this TErrorResult has not failed.
361 // The exn argument to ThrowJSException can be in any compartment. It does
362 // not have to be in the compartment of cx. If someone later uses it, they
363 // will wrap it into whatever compartment they're working in, as needed.
364 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
365 ThrowJSException(JSContext
* cx
, JS::Handle
<JS::Value
> exn
);
366 bool IsJSException() const {
367 return ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_JS_EXCEPTION
;
370 // Facilities for throwing DOMExceptions of whatever type a spec calls for.
371 // If an empty message string is passed to one of these Throw*Error functions,
372 // the default message string for the relevant type of DOMException will be
373 // used. The passed-in string must be UTF-8.
374 #define DOMEXCEPTION(name, err) \
375 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG Throw##name( \
376 const nsACString& aMessage) { \
377 ThrowDOMException(err, aMessage); \
381 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG Throw##name( \
382 const char(&aMessage)[N]) { \
383 ThrowDOMException(err, aMessage); \
386 #include "mozilla/dom/DOMExceptionNames.h"
390 bool IsDOMException() const {
391 return ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_DOMEXCEPTION
;
394 // Flag on the TErrorResult that whatever needs throwing has been
395 // thrown on the JSContext already and we should not mess with it.
396 // If nothing was thrown, this becomes an uncatchable exception.
397 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
398 NoteJSContextException(JSContext
* aCx
);
400 // Check whether the TErrorResult says to just throw whatever is on
401 // the JSContext already.
402 bool IsJSContextException() {
403 return ErrorCode() == NS_ERROR_INTERNAL_ERRORRESULT_EXCEPTION_ON_JSCONTEXT
;
406 // Support for uncatchable exceptions.
407 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
ThrowUncatchableException() {
408 Throw(NS_ERROR_UNCATCHABLE_EXCEPTION
);
410 bool IsUncatchableException() const {
411 return ErrorCode() == NS_ERROR_UNCATCHABLE_EXCEPTION
;
414 void MOZ_ALWAYS_INLINE
MightThrowJSException() {
416 mMightHaveUnreportedJSException
= true;
419 void MOZ_ALWAYS_INLINE
WouldReportJSException() {
421 mMightHaveUnreportedJSException
= false;
425 // In the future, we can add overloads of Throw that take more
426 // interesting things, like strings or DOM exception types or
427 // something if desired.
429 // Backwards-compat to make conversion simpler. We don't call
430 // Throw() here because people can easily pass success codes to
431 // this. This operator is deprecated and ideally shouldn't be used.
432 void operator=(nsresult rv
) { AssignErrorCode(rv
); }
434 bool Failed() const { return NS_FAILED(mResult
); }
436 bool ErrorCodeIs(nsresult rv
) const { return mResult
== rv
; }
438 // For use in logging ONLY.
439 uint32_t ErrorCodeAsInt() const { return static_cast<uint32_t>(ErrorCode()); }
441 bool operator==(const ErrorResult
& aRight
) const;
444 nsresult
ErrorCode() const { return mResult
; }
446 // Helper methods for throwing DOMExceptions, for now. We can try to get rid
447 // of these once EME code is fixed to not use them and we decouple
448 // DOMExceptions from nsresult.
449 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
450 ThrowDOMException(nsresult rv
, const nsACString
& message
);
452 // Same thing, but using a string literal.
454 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
455 ThrowDOMException(nsresult rv
, const char (&aMessage
)[N
]) {
456 ThrowDOMException(rv
, nsLiteralCString(aMessage
));
459 // Allow Promise to call the above methods when it really needs to.
460 // Unfortunately, we can't have the definition of Promise here, so can't mark
461 // just it's MaybeRejectWithDOMException method as a friend. In any case,
462 // hopefully it's all temporary until we sort out the EME bits.
463 friend class dom::Promise
;
475 friend struct IPC::ParamTraits
<TErrorResult
>;
476 friend struct IPC::ParamTraits
<ErrorResult
>;
477 void SerializeMessage(IPC::Message
* aMsg
) const;
478 bool DeserializeMessage(const IPC::Message
* aMsg
, PickleIterator
* aIter
);
480 void SerializeDOMExceptionInfo(IPC::Message
* aMsg
) const;
481 bool DeserializeDOMExceptionInfo(const IPC::Message
* aMsg
,
482 PickleIterator
* aIter
);
484 // Helper method that creates a new Message for this TErrorResult,
485 // and returns the arguments array from that Message.
486 nsTArray
<nsCString
>& CreateErrorMessageHelper(const dom::ErrNum errorNumber
,
489 // Helper method to replace invalid UTF-8 characters with the replacement
490 // character. aValidUpTo is the number of characters that are known to be
491 // valid. The string might be truncated if we encounter an OOM error.
492 static void EnsureUTF8Validity(nsCString
& aValue
, size_t aValidUpTo
);
494 template <dom::ErrNum errorNumber
, typename
... Ts
>
495 void ThrowErrorWithMessage(nsresult errorType
, Ts
&&... messageArgs
) {
496 #if defined(DEBUG) && (defined(__clang__) || defined(__GNUC__))
497 static_assert(dom::ErrorFormatNumArgs
[errorNumber
] ==
498 sizeof...(messageArgs
) +
499 int(dom::ErrorFormatHasContext
[errorNumber
]),
500 "Pass in the right number of arguments");
505 nsTArray
<nsCString
>& messageArgsArray
=
506 CreateErrorMessageHelper(errorNumber
, errorType
);
507 uint16_t argCount
= dom::GetErrorArgCount(errorNumber
);
508 if (dom::ErrorFormatHasContext
[errorNumber
]) {
509 // Insert an empty string arg at the beginning and reduce our arg count to
510 // still be appended accordingly.
511 MOZ_ASSERT(argCount
> 0,
512 "Must have at least one arg if we have a context!");
513 MOZ_ASSERT(messageArgsArray
.Length() == 0,
514 "Why do we already have entries in the array?");
516 messageArgsArray
.AppendElement();
518 dom::CStringArrayAppender::Append(messageArgsArray
, argCount
,
519 std::forward
<Ts
>(messageArgs
)...);
520 for (nsCString
& arg
: messageArgsArray
) {
521 size_t validUpTo
= Utf8ValidUpTo(arg
);
522 if (validUpTo
!= arg
.Length()) {
523 EnsureUTF8Validity(arg
, validUpTo
);
527 mUnionState
= HasMessage
;
531 MOZ_ALWAYS_INLINE
void AssertInOwningThread() const {
533 if (CleanupPolicy::assertSameThread
) {
534 NS_ASSERT_OWNINGTHREAD(TErrorResult
);
539 void AssignErrorCode(nsresult aRv
) {
540 MOZ_ASSERT(aRv
!= NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR
,
541 "Use ThrowTypeError()");
542 MOZ_ASSERT(aRv
!= NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR
,
543 "Use ThrowRangeError()");
544 MOZ_ASSERT(!IsErrorWithMessage(), "Don't overwrite errors with message");
545 MOZ_ASSERT(aRv
!= NS_ERROR_INTERNAL_ERRORRESULT_JS_EXCEPTION
,
546 "Use ThrowJSException()");
547 MOZ_ASSERT(!IsJSException(), "Don't overwrite JS exceptions");
548 MOZ_ASSERT(aRv
!= NS_ERROR_INTERNAL_ERRORRESULT_DOMEXCEPTION
,
549 "Use Throw*Error for the appropriate DOMException name");
550 MOZ_ASSERT(!IsDOMException(), "Don't overwrite DOM exceptions");
551 MOZ_ASSERT(aRv
!= NS_ERROR_XPC_NOT_ENOUGH_ARGS
,
552 "May need to bring back ThrowNotEnoughArgsError");
553 MOZ_ASSERT(aRv
!= NS_ERROR_INTERNAL_ERRORRESULT_EXCEPTION_ON_JSCONTEXT
,
554 "Use NoteJSContextException");
559 void ClearDOMExceptionInfo();
561 // ClearUnionData will try to clear the data in our mExtra union. After this
562 // the union may be in an uninitialized state (e.g. mMessage or
563 // mDOMExceptionInfo may point to deleted memory, or mJSException may be a
564 // JS::Value containing an invalid gcthing) and the caller must either
565 // reinitialize it or change mResult to something that will not involve us
566 // touching the union anymore.
567 void ClearUnionData();
569 // Implementation of MaybeSetPendingException for the case when we're a
570 // failure result. See documentation of MaybeSetPendingException for the
571 // "context" argument.
572 void SetPendingException(JSContext
* cx
, const char* context
);
574 // Methods for setting various specific kinds of pending exceptions. See
575 // documentation of MaybeSetPendingException for the "context" argument.
576 void SetPendingExceptionWithMessage(JSContext
* cx
, const char* context
);
577 void SetPendingJSException(JSContext
* cx
);
578 void SetPendingDOMException(JSContext
* cx
, const char* context
);
579 void SetPendingGenericErrorException(JSContext
* cx
);
581 MOZ_ALWAYS_INLINE
void AssertReportedOrSuppressed() {
582 MOZ_ASSERT(!Failed());
583 MOZ_ASSERT(!mMightHaveUnreportedJSException
);
584 MOZ_ASSERT(mUnionState
== HasNothing
);
587 // Special values of mResult:
588 // NS_ERROR_INTERNAL_ERRORRESULT_TYPEERROR -- ThrowTypeError() called on us.
589 // NS_ERROR_INTERNAL_ERRORRESULT_RANGEERROR -- ThrowRangeError() called on us.
590 // NS_ERROR_INTERNAL_ERRORRESULT_JS_EXCEPTION -- ThrowJSException() called
592 // NS_ERROR_UNCATCHABLE_EXCEPTION -- ThrowUncatchableException called on us.
593 // NS_ERROR_INTERNAL_ERRORRESULT_DOMEXCEPTION -- ThrowDOMException() called
598 struct DOMExceptionInfo
;
600 // mMessage is set by ThrowErrorWithMessage and reported (and deallocated)
601 // by SetPendingExceptionWithMessage.
602 MOZ_INIT_OUTSIDE_CTOR
603 Message
* mMessage
; // valid when IsErrorWithMessage()
605 // mJSException is set (and rooted) by ThrowJSException and reported (and
606 // unrooted) by SetPendingJSException.
607 MOZ_INIT_OUTSIDE_CTOR
608 JS::Value mJSException
; // valid when IsJSException()
610 // mDOMExceptionInfo is set by ThrowDOMException and reported (and
611 // deallocated) by SetPendingDOMException.
612 MOZ_INIT_OUTSIDE_CTOR
613 DOMExceptionInfo
* mDOMExceptionInfo
; // valid when IsDOMException()
615 // |mJSException| has a non-trivial constructor and therefore MUST be
616 // placement-new'd into existence.
617 MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS
619 MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS
622 Message
* InitMessage(Message
* aMessage
) {
623 // The |new| here switches the active arm of |mExtra|, from the compiler's
624 // point of view. Mere assignment *won't* necessarily do the right thing!
625 new (&mExtra
.mMessage
) Message
*(aMessage
);
626 return mExtra
.mMessage
;
629 JS::Value
& InitJSException() {
630 // The |new| here switches the active arm of |mExtra|, from the compiler's
631 // point of view. Mere assignment *won't* necessarily do the right thing!
632 new (&mExtra
.mJSException
) JS::Value(); // sets to undefined
633 return mExtra
.mJSException
;
636 DOMExceptionInfo
* InitDOMExceptionInfo(DOMExceptionInfo
* aDOMExceptionInfo
) {
637 // The |new| here switches the active arm of |mExtra|, from the compiler's
638 // point of view. Mere assignment *won't* necessarily do the right thing!
639 new (&mExtra
.mDOMExceptionInfo
) DOMExceptionInfo
*(aDOMExceptionInfo
);
640 return mExtra
.mDOMExceptionInfo
;
644 // Used to keep track of codepaths that might throw JS exceptions,
645 // for assertion purposes.
646 bool mMightHaveUnreportedJSException
;
648 // Used to keep track of what's stored in our union right now. Note
649 // that this may be set to HasNothing even if our mResult suggests
650 // we should have something, if we have already cleaned up the
652 UnionState mUnionState
;
654 // The thread that created this TErrorResult
655 NS_DECL_OWNINGTHREAD
;
658 // Not to be implemented, to make sure people always pass this by
659 // reference, not by value.
660 TErrorResult(const TErrorResult
&) = delete;
661 void operator=(const TErrorResult
&) = delete;
664 struct JustAssertCleanupPolicy
{
665 static const bool assertHandled
= true;
666 static const bool suppress
= false;
667 static const bool assertSameThread
= true;
670 struct AssertAndSuppressCleanupPolicy
{
671 static const bool assertHandled
= true;
672 static const bool suppress
= true;
673 static const bool assertSameThread
= true;
676 struct JustSuppressCleanupPolicy
{
677 static const bool assertHandled
= false;
678 static const bool suppress
= true;
679 static const bool assertSameThread
= true;
682 struct ThreadSafeJustSuppressCleanupPolicy
{
683 static const bool assertHandled
= false;
684 static const bool suppress
= true;
685 static const bool assertSameThread
= false;
688 } // namespace binding_danger
690 // A class people should normally use on the stack when they plan to actually
691 // do something with the exception.
692 class ErrorResult
: public binding_danger::TErrorResult
<
693 binding_danger::AssertAndSuppressCleanupPolicy
> {
694 typedef binding_danger::TErrorResult
<
695 binding_danger::AssertAndSuppressCleanupPolicy
>
699 ErrorResult() = default;
701 ErrorResult(ErrorResult
&& aRHS
) = default;
702 // Explicitly allow moving out of a CopyableErrorResult into an ErrorResult.
703 // This is implemented below so it can see the definition of
704 // CopyableErrorResult.
705 inline explicit ErrorResult(CopyableErrorResult
&& aRHS
);
707 explicit ErrorResult(nsresult aRv
) : BaseErrorResult(aRv
) {}
709 // This operator is deprecated and ideally shouldn't be used.
710 void operator=(nsresult rv
) { BaseErrorResult::operator=(rv
); }
712 ErrorResult
& operator=(ErrorResult
&& aRHS
) = default;
714 // Not to be implemented, to make sure people always pass this by
715 // reference, not by value.
716 ErrorResult(const ErrorResult
&) = delete;
717 ErrorResult
& operator=(const ErrorResult
&) = delete;
720 template <typename CleanupPolicy
>
721 binding_danger::TErrorResult
<CleanupPolicy
>::operator ErrorResult
&() {
722 return *static_cast<ErrorResult
*>(
723 reinterpret_cast<TErrorResult
<AssertAndSuppressCleanupPolicy
>*>(this));
726 template <typename CleanupPolicy
>
727 binding_danger::TErrorResult
<CleanupPolicy
>::operator const ErrorResult
&()
729 return *static_cast<const ErrorResult
*>(
730 reinterpret_cast<const TErrorResult
<AssertAndSuppressCleanupPolicy
>*>(
734 // A class for use when an ErrorResult should just automatically be ignored.
735 // This doesn't inherit from ErrorResult so we don't make two separate calls to
736 // SuppressException.
737 class IgnoredErrorResult
: public binding_danger::TErrorResult
<
738 binding_danger::JustSuppressCleanupPolicy
> {};
740 // A class for use when an ErrorResult needs to be copied to a lambda, into
741 // an IPDL structure, etc. Since this will often involve crossing thread
742 // boundaries this class will assert if you try to copy a JS exception. Only
743 // use this if you are propagating internal errors. In general its best
744 // to use ErrorResult by default and only convert to a CopyableErrorResult when
746 class CopyableErrorResult
747 : public binding_danger::TErrorResult
<
748 binding_danger::ThreadSafeJustSuppressCleanupPolicy
> {
749 typedef binding_danger::TErrorResult
<
750 binding_danger::ThreadSafeJustSuppressCleanupPolicy
>
754 CopyableErrorResult() = default;
756 explicit CopyableErrorResult(const ErrorResult
& aRight
) : BaseErrorResult() {
757 auto val
= reinterpret_cast<const CopyableErrorResult
&>(aRight
);
761 CopyableErrorResult(CopyableErrorResult
&& aRHS
) = default;
763 explicit CopyableErrorResult(ErrorResult
&& aRHS
) : BaseErrorResult() {
764 // We must not copy JS exceptions since it can too easily lead to
765 // off-thread use. Assert this and fall back to a generic error
766 // in release builds.
767 MOZ_DIAGNOSTIC_ASSERT(
768 !aRHS
.IsJSException(),
769 "Attempt to copy from ErrorResult with a JS exception value.");
770 if (aRHS
.IsJSException()) {
771 aRHS
.SuppressException();
772 Throw(NS_ERROR_FAILURE
);
774 // We could avoid the cast here if we had a move constructor on
775 // TErrorResult templated on the cleanup policy type, but then we'd have
776 // to either inline the impl or force all possible instantiations or
777 // something. This is a bit simpler, and not that different from our copy
779 auto val
= reinterpret_cast<CopyableErrorResult
&&>(aRHS
);
784 explicit CopyableErrorResult(nsresult aRv
) : BaseErrorResult(aRv
) {}
786 // This operator is deprecated and ideally shouldn't be used.
787 void operator=(nsresult rv
) { BaseErrorResult::operator=(rv
); }
789 CopyableErrorResult
& operator=(CopyableErrorResult
&& aRHS
) = default;
791 CopyableErrorResult(const CopyableErrorResult
& aRight
) : BaseErrorResult() {
795 CopyableErrorResult
& operator=(const CopyableErrorResult
& aRight
) {
796 // We must not copy JS exceptions since it can too easily lead to
797 // off-thread use. Assert this and fall back to a generic error
798 // in release builds.
799 MOZ_DIAGNOSTIC_ASSERT(
801 "Attempt to copy to ErrorResult with a JS exception value.");
802 MOZ_DIAGNOSTIC_ASSERT(
803 !aRight
.IsJSException(),
804 "Attempt to copy from ErrorResult with a JS exception value.");
805 if (aRight
.IsJSException()) {
807 Throw(NS_ERROR_FAILURE
);
809 aRight
.CloneTo(*this);
814 // Disallow implicit converstion to non-const ErrorResult&, because that would
815 // allow people to throw exceptions on us while bypassing our checks for JS
817 operator ErrorResult
&() = delete;
819 // Allow conversion to ErrorResult&& so we can move out of ourselves into
821 operator ErrorResult
&&() && {
822 auto* val
= reinterpret_cast<ErrorResult
*>(this);
823 return std::move(*val
);
827 inline ErrorResult::ErrorResult(CopyableErrorResult
&& aRHS
)
828 : ErrorResult(reinterpret_cast<ErrorResult
&&>(aRHS
)) {}
831 namespace binding_detail
{
832 class FastErrorResult
: public mozilla::binding_danger::TErrorResult
<
833 mozilla::binding_danger::JustAssertCleanupPolicy
> {
835 } // namespace binding_detail
838 // We want an OOMReporter class that has the following properties:
840 // 1) Can be cast to from any ErrorResult-like type.
841 // 2) Has a fast destructor (because we want to use it from bindings).
842 // 3) Won't be randomly instantiated by non-binding code (because the fast
843 // destructor is not so safe).
844 // 4) Doesn't look ugly on the callee side (e.g. isn't in the binding_detail or
845 // binding_danger namespace).
847 // We do this by creating a class that can't actually be constructed directly
848 // but can be cast to from ErrorResult-like types, both implicitly and
850 class OOMReporter
: private dom::binding_detail::FastErrorResult
{
852 void MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG
ReportOOM() {
853 Throw(NS_ERROR_OUT_OF_MEMORY
);
856 // A method that turns a FastErrorResult into an OOMReporter, which we use in
857 // codegen to ensure that callees don't take an ErrorResult when they should
858 // only be taking an OOMReporter. The idea is that we can then just have a
859 // FastErrorResult on the stack and call this to produce the thing to pass to
861 static OOMReporter
& From(FastErrorResult
& aRv
) { return aRv
; }
864 // TErrorResult is a friend so its |operator OOMReporter&()| can work.
865 template <typename CleanupPolicy
>
866 friend class binding_danger::TErrorResult
;
868 OOMReporter() : dom::binding_detail::FastErrorResult() {}
871 template <typename CleanupPolicy
>
872 binding_danger::TErrorResult
<CleanupPolicy
>::operator OOMReporter
&() {
873 return *static_cast<OOMReporter
*>(
874 reinterpret_cast<TErrorResult
<JustAssertCleanupPolicy
>*>(this));
877 // A class for use when an ErrorResult should just automatically be
878 // ignored. This is designed to be passed as a temporary only, like
881 // foo->Bar(IgnoreErrors());
882 class MOZ_TEMPORARY_CLASS IgnoreErrors
{
884 operator ErrorResult
&() && { return mInner
; }
885 operator OOMReporter
&() && { return mInner
; }
888 // We don't use an ErrorResult member here so we don't make two separate calls
889 // to SuppressException (one from us, one from the ErrorResult destructor
891 binding_danger::TErrorResult
<binding_danger::JustSuppressCleanupPolicy
>
895 /******************************************************************************
896 ** Macros for checking results
897 ******************************************************************************/
899 #define ENSURE_SUCCESS(res, ret) \
901 if (res.Failed()) { \
904 "ENSURE_SUCCESS(%s, %s) failed with " \
906 #res, #ret, res.ErrorCodeAsInt()); \
907 NS_WARNING(msg.get()); \
912 #define ENSURE_SUCCESS_VOID(res) \
914 if (res.Failed()) { \
917 "ENSURE_SUCCESS_VOID(%s) failed with " \
919 #res, res.ErrorCodeAsInt()); \
920 NS_WARNING(msg.get()); \
925 } // namespace mozilla
927 #endif /* mozilla_ErrorResult_h */