Bug 743843 - Factor individual argument parsers into helper methods. r=Ms2ger
authorBobby Holley <bobbyholley@gmail.com>
Sun, 15 Apr 2012 17:51:36 -0700
changeset 91728 b31df4d9758427437227801653b3c726181f6610
parent 91727 d4a0adaa9328f709d06b45bda872107154d38b3a
child 91729 b80749916305ca8e0578e085389a75d25ecb0436
push id22472
push usereakhgari@mozilla.com
push dateMon, 16 Apr 2012 15:03:21 +0000
treeherdermozilla-central@0066df252596 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs743843
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 743843 - Factor individual argument parsers into helper methods. r=Ms2ger
js/xpconnect/src/XPCComponents.cpp
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -1851,58 +1851,81 @@ struct NS_STACK_CLASS ExceptionArgParser
     nsCOMPtr<nsISupports>   eData;
 
     // Parse the constructor arguments into the above |eFoo| parameter values.
     bool parse(uint32_t argc, JS::Value *argv) {
         // all params are optional - grab any passed in
         switch (argc) {
             default:    // more than 4 - ignore extra
                 // ...fall through...
-            case 4:     // argv[3] is object for eData
-                if (JSVAL_IS_NULL(argv[3])) {
-                    // do nothing, leave eData as null
-                } else {
-                    if (JSVAL_IS_PRIMITIVE(argv[3]) ||
-                        NS_FAILED(xpc->WrapJS(cx, JSVAL_TO_OBJECT(argv[3]),
-                                              NS_GET_IID(nsISupports),
-                                              (void**)getter_AddRefs(eData))))
-                        return false;
-                }
-                // ...fall through...
-            case 3:     // argv[2] is object for eStack
-                if (JSVAL_IS_NULL(argv[2])) {
-                    // do nothing, leave eStack as null
-                } else {
-                    if (JSVAL_IS_PRIMITIVE(argv[2]) ||
-                        NS_FAILED(xpc->WrapJS(cx, JSVAL_TO_OBJECT(argv[2]),
-                                              NS_GET_IID(nsIStackFrame),
-                                              (void**)getter_AddRefs(eStack))))
-                        return false;
-                }
-                // fall through...
-            case 2:     // argv[1] is nsresult for eResult
-                if (!JS_ValueToECMAInt32(cx, argv[1], (int32_t*) &eResult))
+            case 4:
+                if (!parseData(argv[3]))
+                    return false;
+            case 3:
+                if (!parseStack(argv[2]))
                     return false;
-                // ...fall through...
-            case 1:     // argv[0] is string for eMsg
-                {
-                    JSString* str = JS_ValueToString(cx, argv[0]);
-                    if (!str || !(eMsg = messageBytes.encode(cx, str)))
-                        return false;
-                }
-                // ...fall through...
+            case 2:
+                if (!parseResult(argv[1]))
+                    return false;
+            case 1:
+                if (!parseMessage(argv[0]))
+                    return false;
             case 0: // this case required so that 'default' does not include zero.
                 ;   // -- do nothing --
         }
 
         return true;
     }
 
   protected:
 
+    /*
+     * Parsing helpers.
+     */
+
+    bool parseMessage(JS::Value &v) {
+        JSString *str = JS_ValueToString(cx, v);
+        if (!str)
+           return false;
+        eMsg = messageBytes.encode(cx, str);
+        return !!eMsg;
+    }
+
+    bool parseResult(JS::Value &v) {
+        return JS_ValueToECMAInt32(cx, v, (int32_t*) &eResult);
+    }
+
+    bool parseStack(JS::Value &v) {
+        if (!v.isObject()) {
+            // eStack has already been initialized to null, which is what we want
+            // for any non-object values (including null).
+            return true;
+        }
+
+        return NS_SUCCEEDED(xpc->WrapJS(cx, JSVAL_TO_OBJECT(v),
+                                        NS_GET_IID(nsIStackFrame),
+                                        getter_AddRefs(eStack)));
+    }
+
+    bool parseData(JS::Value &v) {
+        if (!v.isObject()) {
+            // eData has already been initialized to null, which is what we want
+            // for any non-object values (including null).
+            return true;
+        }
+
+        return NS_SUCCEEDED(xpc->WrapJS(cx, &v.toObject(),
+                                        NS_GET_IID(nsISupports),
+                                        getter_AddRefs(eData)));
+    }
+
+    /*
+     * Internal data members.
+     */
+
     // If there's a non-default exception string, hold onto the allocated bytes.
     JSAutoByteString messageBytes;
 
     // Various bits and pieces that are helpful to have around.
     JSContext *cx;
     nsXPConnect *xpc;
 };