8190391: nashorn "!!" of nonzero even integer var becomes false when returned
authorhannesw
Mon, 13 Nov 2017 15:54:20 +0100
changeset 47734 d0c784151182
parent 47733 fbfe06b70e16
child 47735 45af799ceb6a
8190391: nashorn "!!" of nonzero even integer var becomes false when returned Reviewed-by: sundar, hannesw Contributed-by: priya.lakshmi.muthuswamy@oracle.com
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/types/IntType.java
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSType.java
test/nashorn/script/basic/JDK-8190391.js
test/nashorn/script/basic/JDK-8190391.js.EXPECTED
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/types/IntType.java	Mon Nov 13 14:03:36 2017 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/types/IntType.java	Mon Nov 13 15:54:20 2017 +0100
@@ -138,7 +138,7 @@
         } else if (to.isLong()) {
             method.visitInsn(I2L);
         } else if (to.isBoolean()) {
-            //nop
+            invokestatic(method, JSType.TO_BOOLEAN_I);
         } else if (to.isString()) {
             invokestatic(method, TO_STRING);
         } else if (to.isObject()) {
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSType.java	Mon Nov 13 14:03:36 2017 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/JSType.java	Mon Nov 13 15:54:20 2017 +0100
@@ -89,6 +89,9 @@
     /** JavaScript compliant conversion function from number to boolean */
     public static final Call TO_BOOLEAN_D = staticCall(JSTYPE_LOOKUP, JSType.class, "toBoolean", boolean.class, double.class);
 
+    /** JavaScript compliant conversion function from int to boolean */
+    public static final Call TO_BOOLEAN_I = staticCall(JSTYPE_LOOKUP, JSType.class, "toBoolean", boolean.class, int.class);
+
     /** JavaScript compliant conversion function from Object to integer */
     public static final Call TO_INTEGER = staticCall(JSTYPE_LOOKUP, JSType.class, "toInteger", int.class, Object.class);
 
@@ -548,6 +551,17 @@
     }
 
     /**
+     * JavaScript compliant conversion of int to boolean
+     *
+     * @param num an int
+     *
+     * @return a boolean
+     */
+    public static boolean toBoolean(final int num) {
+        return num != 0;
+    }
+
+    /**
      * JavaScript compliant conversion of Object to boolean
      * See ECMA 9.2 ToBoolean
      *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nashorn/script/basic/JDK-8190391.js	Mon Nov 13 15:54:20 2017 +0100
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8190391 : nashorn: "!!" of nonzero even integer var becomes false when returned
+ *
+ * @test
+ * @run
+ */
+
+
+function func(x){
+    return !!x;
+}
+print(func(0));
+print(func(2));
+print(func(3));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nashorn/script/basic/JDK-8190391.js.EXPECTED	Mon Nov 13 15:54:20 2017 +0100
@@ -0,0 +1,3 @@
+false
+true
+true