Bug 645416, part 12 - Update ToBoolean for symbols. r=sfink.
authorJason Orendorff <jorendorff@mozilla.com>
Mon, 23 Jun 2014 10:56:50 -0500
changeset 190279 7171929b009012887c01b97d99766fe09a26029d
parent 190278 edcb8617d09fe1f1eec5749e3c4e2912688fe49f
child 190280 3140892419ab448be0b6aaafbb7788630e464d49
push id27004
push useremorley@mozilla.com
push dateTue, 24 Jun 2014 15:52:34 +0000
treeherdermozilla-central@7b174d47f3cc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfink
bugs645416
milestone33.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 645416, part 12 - Update ToBoolean for symbols. r=sfink.
js/src/jit-test/tests/symbol/not.js
js/src/jit-test/tests/symbol/truthiness.js
js/src/jsapi.h
js/src/tests/ecma_6/Symbol/conversions.js
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/symbol/not.js
@@ -0,0 +1,13 @@
+for (var i = 0; i < 9; i++)
+    assertEq(!Symbol(), false, "symbols are truthy");
+
+var a = [0, 0, 0, 0, 0, Symbol(), Symbol()];
+var b = [];
+function f(i, v) {
+    b[i] = !v;
+}
+for (var i = 0; i < a.length; i++)
+    f(i, a[i]);
+assertEq(b[b.length - 3], true);
+assertEq(b[b.length - 2], false);
+assertEq(b[b.length - 1], false);
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/symbol/truthiness.js
@@ -0,0 +1,13 @@
+for (var i = 0; i < 9; i++)
+    assertEq(Symbol() ? 1 : 0, 1, "symbols are truthy");
+
+var a = [0, 0, 0, 0, 0, Symbol(), Symbol()];
+var b = [];
+function f(i, v) {
+    b[i] = v ? "yes" : "no";
+}
+for (var i = 0; i < a.length; i++)
+    f(i, a[i]);
+assertEq(b[b.length - 3], "no");
+assertEq(b[b.length - 2], "yes");
+assertEq(b[b.length - 1], "yes");
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -1073,16 +1073,18 @@ ToBoolean(HandleValue v)
     if (v.isInt32())
         return v.toInt32() != 0;
     if (v.isNullOrUndefined())
         return false;
     if (v.isDouble()) {
         double d = v.toDouble();
         return !mozilla::IsNaN(d) && d != 0;
     }
+    if (v.isSymbol())
+        return true;
 
     /* The slow path handles strings and objects. */
     return js::ToBooleanSlow(v);
 }
 
 MOZ_ALWAYS_INLINE JSString*
 ToString(JSContext *cx, HandleValue v)
 {
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_6/Symbol/conversions.js
@@ -0,0 +1,19 @@
+// Section numbers cite ES6 rev 24 (2014 April 27).
+
+var symbols = [
+    Symbol(),
+    Symbol("one"),
+    Symbol.for("two"),
+    Symbol.iterator
+];
+
+for (var sym of symbols) {
+    // 7.1.2 ToBoolean
+    assertEq(Boolean(sym), true);
+    assertEq(!sym, false);
+    assertEq(sym || 13, sym);
+    assertEq(sym && 13, 13);
+}
+
+if (typeof reportCompare === "function")
+    reportCompare(0, 0);