8190391: nashorn "!!" of nonzero even integer var becomes false when returned

Reviewed-by: sundar, hannesw
This commit is contained in:
Priya Lakshmi Muthuswamy 2017-11-13 15:54:20 +01:00 committed by Hannes Wallnöfer
parent 2245db7522
commit c9944644fd
4 changed files with 55 additions and 1 deletions

View File

@ -138,7 +138,7 @@ class IntType extends BitwiseType {
} 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()) {

View File

@ -89,6 +89,9 @@ public enum JSType {
/** 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);
@ -547,6 +550,17 @@ public enum JSType {
return num != 0 && !Double.isNaN(num);
}
/**
* 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

View File

@ -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));

View File

@ -0,0 +1,3 @@
false
true
true