servo: Merge #13755 - Invert conditional and return early from report_pending_exception (from fhartwig:early-return); r=KiChjang
authorFlorian Hartwig <florian.j.hartwig@gmail.com>
Thu, 13 Oct 2016 16:40:16 -0500
changeset 339916 cee68a736bee18ace452d738e190d3cb266b149d
parent 339915 4bbf1703b45d659168eea4241a7eed8d905a9ce6
child 339917 233fd3bda074b3e7ce5c30bd094f910c71aed801
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersKiChjang
servo: Merge #13755 - Invert conditional and return early from report_pending_exception (from fhartwig:early-return); r=KiChjang <!-- Please describe your changes on the following line: --> Previously, almost the entire body of `report_pending_exception` was in a conditional branch. We now return early if the function body does not need to be executed. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #13746 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [X] These changes do not require tests because they don't add any new functionality. <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3e12af581585b7ff988ab6ab9f3323f210b67dd1
servo/components/script/dom/bindings/error.rs
--- a/servo/components/script/dom/bindings/error.rs
+++ b/servo/components/script/dom/bindings/error.rs
@@ -197,62 +197,62 @@ impl ErrorInfo {
     }
 }
 
 /// Report a pending exception, thereby clearing it.
 ///
 /// The `dispatch_event` argument is temporary and non-standard; passing false
 /// prevents dispatching the `error` event.
 pub unsafe fn report_pending_exception(cx: *mut JSContext, dispatch_event: bool) {
-    if JS_IsExceptionPending(cx) {
-        rooted!(in(cx) let mut value = UndefinedValue());
-        if !JS_GetPendingException(cx, value.handle_mut()) {
-            JS_ClearPendingException(cx);
-            error!("Uncaught exception: JS_GetPendingException failed");
-            return;
-        }
+    if !JS_IsExceptionPending(cx) { return; }
 
+    rooted!(in(cx) let mut value = UndefinedValue());
+    if !JS_GetPendingException(cx, value.handle_mut()) {
         JS_ClearPendingException(cx);
-        let error_info = if value.is_object() {
-            rooted!(in(cx) let object = value.to_object());
-            let error_info = ErrorInfo::from_native_error(cx, object.handle())
-                .or_else(|| ErrorInfo::from_dom_exception(object.handle()));
-            match error_info {
-                Some(error_info) => error_info,
-                None => {
-                    error!("Uncaught exception: failed to extract information");
-                    return;
-                }
+        error!("Uncaught exception: JS_GetPendingException failed");
+        return;
+    }
+
+    JS_ClearPendingException(cx);
+    let error_info = if value.is_object() {
+        rooted!(in(cx) let object = value.to_object());
+        let error_info = ErrorInfo::from_native_error(cx, object.handle())
+            .or_else(|| ErrorInfo::from_dom_exception(object.handle()));
+        match error_info {
+            Some(error_info) => error_info,
+            None => {
+                error!("Uncaught exception: failed to extract information");
+                return;
             }
-        } else {
-            match USVString::from_jsval(cx, value.handle(), ()) {
-                Ok(ConversionResult::Success(USVString(string))) => {
-                    ErrorInfo {
-                        message: format!("uncaught exception: {}", string),
-                        filename: String::new(),
-                        lineno: 0,
-                        column: 0,
-                    }
-                },
-                _ => {
-                    panic!("Uncaught exception: failed to stringify primitive");
-                },
-            }
-        };
+        }
+    } else {
+        match USVString::from_jsval(cx, value.handle(), ()) {
+            Ok(ConversionResult::Success(USVString(string))) => {
+                ErrorInfo {
+                    message: format!("uncaught exception: {}", string),
+                    filename: String::new(),
+                    lineno: 0,
+                    column: 0,
+                }
+            },
+            _ => {
+                panic!("Uncaught exception: failed to stringify primitive");
+            },
+        }
+    };
 
-        error!("Error at {}:{}:{} {}",
-               error_info.filename,
-               error_info.lineno,
-               error_info.column,
-               error_info.message);
+    error!("Error at {}:{}:{} {}",
+           error_info.filename,
+           error_info.lineno,
+           error_info.column,
+           error_info.message);
 
-        if dispatch_event {
-            GlobalScope::from_context(cx)
-                .report_an_error(error_info, value.handle());
-        }
+    if dispatch_event {
+        GlobalScope::from_context(cx)
+            .report_an_error(error_info, value.handle());
     }
 }
 
 /// Throw an exception to signal that a `JSVal` can not be converted to any of
 /// the types in an IDL union type.
 pub unsafe fn throw_not_in_union(cx: *mut JSContext, names: &'static str) {
     assert!(!JS_IsExceptionPending(cx));
     let error = format!("argument could not be converted to any of: {}", names);