Bug 1346074 - Part 2: Move ToLengthClamped into jsarray.cpp. r=shu
authorAndré Bargull <andre.bargull@gmail.com>
Fri, 10 Mar 2017 23:40:17 +0100
changeset 347231 a01bd91a80c1f29cf5d97ed9a38f44dfeaf07ab9
parent 347230 a8338b879525e9a3c56d6eb065d1a5201eb0f5b2
child 347232 b2635d26a7b7c0f6cdb278c7f62503978e620f6c
push id31491
push usercbook@mozilla.com
push dateMon, 13 Mar 2017 14:24:00 +0000
treeherdermozilla-central@8d9fd089cabd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshu
bugs1346074
milestone55.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 1346074 - Part 2: Move ToLengthClamped into jsarray.cpp. r=shu
js/src/jsarray.cpp
js/src/jsnum.cpp
js/src/jsnum.h
--- a/js/src/jsarray.cpp
+++ b/js/src/jsarray.cpp
@@ -93,16 +93,42 @@ JS::IsArray(JSContext* cx, HandleObject 
         JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_PROXY_REVOKED);
         return false;
     }
 
     *isArray = answer == IsArrayAnswer::Array;
     return true;
 }
 
+// ES2017 7.1.15 ToLength, but clamped to the [0,2^32-2] range.
+static bool
+ToLengthClamped(JSContext* cx, HandleValue v, uint32_t* out)
+{
+    if (v.isInt32()) {
+        int32_t i = v.toInt32();
+        *out = i < 0 ? 0 : i;
+        return true;
+    }
+    double d;
+    if (v.isDouble()) {
+        d = v.toDouble();
+    } else {
+        if (!ToNumber(cx, v, &d))
+            return false;
+    }
+    d = JS::ToInteger(d);
+    if (d <= 0.0)
+        *out = 0;
+    else if (d < double(UINT32_MAX - 1))
+        *out = uint32_t(d);
+    else
+        *out = UINT32_MAX;
+    return true;
+}
+
 bool
 js::GetLengthProperty(JSContext* cx, HandleObject obj, uint32_t* lengthp)
 {
     if (obj->is<ArrayObject>()) {
         *lengthp = obj->as<ArrayObject>().length();
         return true;
     }
 
@@ -118,22 +144,19 @@ js::GetLengthProperty(JSContext* cx, Han
             return true;
         }
     }
 
     RootedValue value(cx);
     if (!GetProperty(cx, obj, obj, cx->names().length, &value))
         return false;
 
-    bool overflow;
-    if (!ToLengthClamped(cx, value, lengthp, &overflow)) {
-        if (!overflow)
-            return false;
-        *lengthp = UINT32_MAX;
-    }
+    if (!ToLengthClamped(cx, value, lengthp))
+        return false;
+
     return true;
 }
 
 /*
  * Determine if the id represents an array index.
  *
  * An id is an array index according to ECMA by (15.4):
  *
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1766,46 +1766,16 @@ js::ToUint16Slow(JSContext* cx, const Ha
     d = fmod(d, (double) m);
     if (d < 0)
         d += m;
     *out = (uint16_t) d;
     return true;
 }
 
 bool
-js::ToLengthClamped(JSContext* cx, HandleValue v, uint32_t* out, bool* overflow)
-{
-    if (v.isInt32()) {
-        int32_t i = v.toInt32();
-        *out = i < 0 ? 0 : i;
-        return true;
-    }
-    double d;
-    if (v.isDouble()) {
-        d = v.toDouble();
-    } else {
-        if (!ToNumber(cx, v, &d)) {
-            *overflow = false;
-            return false;
-        }
-    }
-    d = JS::ToInteger(d);
-    if (d <= 0.0) {
-        *out = 0;
-        return true;
-    }
-    if (d >= (double)0xFFFFFFFEU) {
-        *overflow = true;
-        return false;
-    }
-    *out = (uint32_t)d;
-    return true;
-}
-
-bool
 js::NonStandardToIndex(JSContext* cx, HandleValue v, uint64_t* index)
 {
     // Fast common case.
     if (v.isInt32()) {
         int32_t i = v.toInt32();
         if (i >= 0) {
             *index = i;
             return true;
--- a/js/src/jsnum.h
+++ b/js/src/jsnum.h
@@ -261,22 +261,16 @@ ToInteger(JSContext* cx, HandleValue v, 
         extern JS_PUBLIC_API(bool) ToNumberSlow(JSContext* cx, HandleValue v, double* dp);
         if (!ToNumberSlow(cx, v, dp))
             return false;
     }
     *dp = JS::ToInteger(*dp);
     return true;
 }
 
-/* ES6 7.1.15 ToLength, but clamped to the [0,2^32-2] range.  If the
- * return value is false then *overflow will be true iff the value was
- * not clampable to uint32_t range.
- */
-MOZ_MUST_USE bool ToLengthClamped(JSContext* cx, HandleValue v, uint32_t* out, bool* overflow);
-
 /* Non-standard convert and range check an index value as for SIMD, and Atomics
  * operations, eg ES7 24.2.1.1, DataView's GetViewValue():
  *
  *   1. numericIndex = ToNumber(argument)            (may throw TypeError)
  *   2. intIndex = ToInteger(numericIndex)
  *   3. if intIndex != numericIndex throw RangeError
  *
  * This function additionally bounds the range to the non-negative contiguous