Bug 1040402 - Fix handling of |undefined| args in typed array view constructor. r=anba a=jcristau
authorJan de Mooij <jdemooij@mozilla.com>
Mon, 21 Nov 2016 13:36:45 +0100
changeset 353161 b0c105fc360fa893453ef47dfe61814f7efa15e7
parent 353160 a80f31823e23976f46297823e23251f2e2c6f8c4
child 353162 483b3196dc36107f3b9c9b49a92070d3c0d23b59
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersanba, jcristau
bugs1040402
milestone52.0a2
Bug 1040402 - Fix handling of |undefined| args in typed array view constructor. r=anba a=jcristau
js/src/tests/ecma_6/TypedArray/constructor-undefined-args.js
js/src/vm/TypedArrayObject.cpp
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_6/TypedArray/constructor-undefined-args.js
@@ -0,0 +1,14 @@
+// Bug 1040402
+
+var ab = new ArrayBuffer(16);
+
+assertEq(new Int32Array(ab).length, 4);
+assertEq(new Int32Array(ab, undefined).length, 4);
+assertEq(new Int32Array(ab, undefined, undefined).length, 4);
+assertEq(new Int32Array(ab, 0).length, 4);
+assertEq(new Int32Array(ab, 0, undefined).length, 4);
+assertEq(new Int32Array(ab, 4).length, 3);
+assertEq(new Int32Array(ab, 4, undefined).length, 3);
+
+if (typeof reportCompare === "function")
+    reportCompare(true, true);
--- a/js/src/vm/TypedArrayObject.cpp
+++ b/js/src/vm/TypedArrayObject.cpp
@@ -725,37 +725,36 @@ class TypedArrayObjectTemplate : public 
             return fromArray(cx, dataObj, newTarget);
 
         /* (ArrayBuffer, [byteOffset, [length]]) */
         RootedObject proto(cx);
         if (!GetPrototypeFromConstructor(cx, newTarget, &proto))
             return nullptr;
 
         int32_t byteOffset = 0;
-        int32_t length = -1;
-
-        if (args.length() > 1) {
+        if (args.hasDefined(1)) {
             if (!ToInt32(cx, args[1], &byteOffset))
                 return nullptr;
             if (byteOffset < 0) {
                 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                                           JSMSG_TYPED_ARRAY_NEGATIVE_ARG,
                                           "1");
                 return nullptr;
             }
-
-            if (args.length() > 2) {
-                if (!ToInt32(cx, args[2], &length))
-                    return nullptr;
-                if (length < 0) {
-                    JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
-                                              JSMSG_TYPED_ARRAY_NEGATIVE_ARG,
-                                              "2");
-                    return nullptr;
-                }
+        }
+
+        int32_t length = -1;
+        if (args.hasDefined(2)) {
+            if (!ToInt32(cx, args[2], &length))
+                return nullptr;
+            if (length < 0) {
+                JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
+                                          JSMSG_TYPED_ARRAY_NEGATIVE_ARG,
+                                          "2");
+                return nullptr;
             }
         }
 
         return fromBufferWithProto(cx, dataObj, byteOffset, length, proto);
     }
 
   public:
     static JSObject*