Bug 645416, part 13 - Update ToNumber for symbols. r=sfink.
authorJason Orendorff <jorendorff@mozilla.com>
Mon, 23 Jun 2014 10:56:50 -0500
changeset 190280 3140892419ab448be0b6aaafbb7788630e464d49
parent 190279 7171929b009012887c01b97d99766fe09a26029d
child 190281 fa33d2ea3793de423336b9c5413981da7f14c604
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerssfink
bugs645416
milestone33.0a1
Bug 645416, part 13 - Update ToNumber for symbols. r=sfink. Not terribly interesting; converting a symbol to a number always produces NaN.
js/src/jit-test/tests/symbol/toNumber.js
js/src/jsnum.cpp
js/src/tests/ecma_6/Symbol/comparisons.js
js/src/tests/ecma_6/Symbol/conversions.js
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/symbol/toNumber.js
@@ -0,0 +1,52 @@
+load(libdir + "asserts.js");
+
+function add() {
+    assertEq(Symbol() + 2, NaN);
+}
+for (var i = 0; i < 9; i++)
+    add();
+
+function mul() {
+    assertEq(Symbol() * Symbol(), NaN);
+}
+for (var i = 0; i < 9; i++)
+    mul();
+
+function bit_or() {
+    assertEq(Symbol() | Symbol(), 0);
+}
+for (var i = 0; i < 9; i++)
+    bit_or();
+
+function bit_not() {
+    assertEq(~Symbol(), -1);
+}
+for (var i = 0; i < 9; i++)
+    bit_not();
+
+function plus() {
+    assertEq(+Symbol(), NaN);
+}
+for (var i = 0; i < 9; i++)
+    plus();
+
+
+function f(a, b) {
+    return a + b;
+}
+
+function testPoly() {
+    assertEq(f(20, 30), 50);
+    assertEq(f("one", "two"), "onetwo");
+    assertEq(f(Symbol("one"), Symbol("two")), NaN);
+    assertEq(f(Symbol("14"), 14), NaN);
+    assertEq(f(Symbol("14"), 13.719), NaN);
+    assertEq(f(14, Symbol("14")), NaN);
+    assertEq(f(13.719, Symbol("14")), NaN);
+}
+
+for (var i = 0; i < 9; i++)
+    testPoly();
+
+for (var i = 0; i < 9; i++)
+    assertThrowsInstanceOf(() => assertEq(f(Symbol("14"), "40"), NaN), TypeError);
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1558,17 +1558,17 @@ js::NonObjectToNumberSlow(ThreadSafeCont
         *out = v.toBoolean() ? 1.0 : 0.0;
         return true;
     }
     if (v.isNull()) {
         *out = 0.0;
         return true;
     }
 
-    JS_ASSERT(v.isUndefined());
+    JS_ASSERT(v.isUndefined() || v.isSymbol());
     *out = GenericNaN();
     return true;
 }
 
 #if defined(_MSC_VER)
 # pragma optimize("g", off)
 #endif
 
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_6/Symbol/comparisons.js
@@ -0,0 +1,26 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/ */
+
+var symbols = [
+    Symbol(),
+    Symbol("0"),
+    Symbol.for("snowman"),
+    Symbol.iterator
+];
+
+var values = [
+    undefined, null, 0, 3.14, -0, NaN, "", "alphabet", Symbol("0"),
+    {}, []
+];
+
+for (var comparator of ["==", "!=", "===", "!==", "<", "<=", ">", ">="]) {
+    var f = Function("a, b", "return a " + comparator + " b;");
+    var expected = (comparator[0] == '!');
+    for (var a of symbols) {
+        for (var b of values)
+            assertEq(f(a, b), expected);
+    }
+}
+
+if (typeof reportCompare === "function")
+    reportCompare(0, 0);
--- a/js/src/tests/ecma_6/Symbol/conversions.js
+++ b/js/src/tests/ecma_6/Symbol/conversions.js
@@ -8,12 +8,16 @@ var symbols = [
 ];
 
 for (var sym of symbols) {
     // 7.1.2 ToBoolean
     assertEq(Boolean(sym), true);
     assertEq(!sym, false);
     assertEq(sym || 13, sym);
     assertEq(sym && 13, 13);
+
+    // 7.1.3 ToNumber
+    assertEq(+sym, NaN);
+    assertEq(sym | 0, 0);
 }
 
 if (typeof reportCompare === "function")
     reportCompare(0, 0);