bug 1466893 - Part 1: Implement ToNumeric operation. r=jandem
authorRobin Templeton <robin@igalia.com>
Tue, 12 Jun 2018 13:57:06 -0400
changeset 476616 631e90e9e0747845da585184217fa6f30525c14e
parent 476615 da4e49603752ddf885f7561e8268f7aaecacddc7
child 476617 7cdfba2700eb020b568fe2c34282d4154ba13577
push id9374
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:43:20 +0000
treeherdermozilla-beta@160e085dfb0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjandem
bugs1466893
milestone62.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 1466893 - Part 1: Implement ToNumeric operation. r=jandem ToNumeric is a generalized version of ToNumber that converts its argument to a numeric primitive value (either a Number or a BigInt).
js/src/jsnum.cpp
js/src/jsnum.h
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1602,16 +1602,44 @@ js::ToNumberSlow(JSContext* cx, HandleVa
         if (v.isBigInt())
             errnum = JSMSG_BIGINT_TO_NUMBER;
 #endif
         JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, errnum);
     }
     return false;
 }
 
+// BigInt proposal section 3.1.6
+bool
+js::ToNumericSlow(JSContext* cx, MutableHandleValue vp)
+{
+    MOZ_ASSERT(!vp.isNumber());
+#ifdef ENABLE_BIGINT
+    MOZ_ASSERT(!vp.isBigInt());
+#endif
+
+    // Step 1.
+    if (!vp.isPrimitive()) {
+        if (cx->helperThread())
+            return false;
+        if (!ToPrimitive(cx, JSTYPE_NUMBER, vp))
+            return false;
+    }
+
+    // Step 2.
+#ifdef ENABLE_BIGINT
+    if (vp.isBigInt()) {
+        return true;
+    }
+#endif
+
+    // Step 3.
+    return ToNumber(cx, vp);
+}
+
 /*
  * Convert a value to an int8_t, according to the WebIDL rules for byte
  * conversion. Return converted value in *out on success, false on failure.
  */
 JS_PUBLIC_API(bool)
 js::ToInt8Slow(JSContext *cx, const HandleValue v, int8_t *out)
 {
     MOZ_ASSERT(!v.isInt32());
--- a/js/src/jsnum.h
+++ b/js/src/jsnum.h
@@ -166,16 +166,32 @@ ToNumber(JSContext* cx, JS::MutableHandl
     extern JS_PUBLIC_API(bool) ToNumberSlow(JSContext* cx, HandleValue v, double* dp);
     if (!ToNumberSlow(cx, vp, &d))
         return false;
 
     vp.setNumber(d);
     return true;
 }
 
+bool
+ToNumericSlow(JSContext* cx, JS::MutableHandleValue vp);
+
+// BigInt proposal section 3.1.6
+MOZ_ALWAYS_INLINE MOZ_MUST_USE bool
+ToNumeric(JSContext* cx, JS::MutableHandleValue vp)
+{
+    if (vp.isNumber())
+        return true;
+#ifdef ENABLE_BIGINT
+    if (vp.isBigInt())
+        return true;
+#endif
+    return ToNumericSlow(cx, vp);
+}
+
 MOZ_MUST_USE bool
 num_parseInt(JSContext* cx, unsigned argc, Value* vp);
 
 }  /* namespace js */
 
 /*
  * Similar to strtod except that it replaces overflows with infinities of the
  * correct sign, and underflows with zeros of the correct sign.  Guaranteed to