From 41720aeaac2450d2b1abfac19418f53bfd98ec7c Mon Sep 17 00:00:00 2001 From: Pale Moon Date: Sun, 19 Mar 2017 11:32:22 +0100 Subject: [PATCH] Add a way to "catch" an ErrorResult, and a way to safely convert an ErrorResult to an nsresult. --- dom/bindings/BindingUtils.cpp | 18 ++++++++++++++++++ dom/bindings/ErrorResult.h | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/dom/bindings/BindingUtils.cpp b/dom/bindings/BindingUtils.cpp index 5da10bc8b8..0284cfb049 100644 --- a/dom/bindings/BindingUtils.cpp +++ b/dom/bindings/BindingUtils.cpp @@ -331,6 +331,24 @@ ErrorResult::ReportNotEnoughArgsError(JSContext* cx, ThrowErrorMessage(cx, dom::MSG_MISSING_ARGUMENTS, errorMessage.get()); } +void +ErrorResult::SuppressException() +{ + WouldReportJSException(); + if (IsErrorWithMessage()) { + ClearMessage(); + } else if (IsJSException()) { + JSContext* cx = nsContentUtils::GetDefaultJSContextForThread(); + // Just steal it into a stack value (unrooting it in the process) + // that we then allow to die. + JS::Rooted temp(cx); + StealJSException(cx, &temp); + } + // We don't use AssignErrorCode, because we want to override existing error + // states, which AssignErrorCode is not allowed to do. + mResult = NS_OK; +} + namespace dom { bool diff --git a/dom/bindings/ErrorResult.h b/dom/bindings/ErrorResult.h index 5b2e73f440..be98601123 100644 --- a/dom/bindings/ErrorResult.h +++ b/dom/bindings/ErrorResult.h @@ -64,6 +64,20 @@ public: mResult = rv; } + // Use SuppressException when you want to suppress any exception that might be + // on the ErrorResult. After this call, the ErrorResult will be back a "no + // exception thrown" state. + void SuppressException(); + + // Use StealNSResult() when you want to safely convert the ErrorResult to an + // nsresult that you will then return to a caller. This will + // SuppressException(), since there will no longer be a way to report it. + nsresult StealNSResult() { + nsresult rv = ErrorCode(); + SuppressException(); + return rv; + } + void ThrowTypeError(const dom::ErrNum errorNumber, ...); void ThrowRangeError(const dom::ErrNum errorNumber, ...); void ReportErrorWithMessage(JSContext* cx);