-
Bug
-
Resolution: Fixed
-
P4
-
8, 9
-
b25
-
generic
-
generic
FULL PRODUCT VERSION :
A DESCRIPTION OF THE PROBLEM :
The JavaScript unsigned right shift operator evaluates to a Double when returned to Java land, but the other types of shift and ALL other bitwise operations coerce to Integer. This threw me, because I was casting the result of eval to int, expecting to be able to unbox from Integer, which worked for every other bitwise operation, but not this one.
Numerically, the result is perfectly correct, so the issue is not detectable from inside the script. Perhaps not a bug, but maybe a performance weakness, because in bit manipulation code it's an extra int -> double -> int conversion happening unnecessarily.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
200
50
50
50
ACTUAL -
200
50
50.0
50
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class NashornUnsignedShiftBug {
public static void main(String[] args) throws Exception {
ScriptEngine js = new ScriptEngineManager().getEngineByName("nashorn");
System.out.println(js.eval("100 << 1")); // 200
System.out.println(js.eval("100 >> 1")); // 50
System.out.println(js.eval("100 >>> 1")); // expected 50, but outputs 50.0
System.out.println(js.eval("(100 >>> 1)|0")); // 50
}
}
---------- END SOURCE ----------
A DESCRIPTION OF THE PROBLEM :
The JavaScript unsigned right shift operator evaluates to a Double when returned to Java land, but the other types of shift and ALL other bitwise operations coerce to Integer. This threw me, because I was casting the result of eval to int, expecting to be able to unbox from Integer, which worked for every other bitwise operation, but not this one.
Numerically, the result is perfectly correct, so the issue is not detectable from inside the script. Perhaps not a bug, but maybe a performance weakness, because in bit manipulation code it's an extra int -> double -> int conversion happening unnecessarily.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
200
50
50
50
ACTUAL -
200
50
50.0
50
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class NashornUnsignedShiftBug {
public static void main(String[] args) throws Exception {
ScriptEngine js = new ScriptEngineManager().getEngineByName("nashorn");
System.out.println(js.eval("100 << 1")); // 200
System.out.println(js.eval("100 >> 1")); // 50
System.out.println(js.eval("100 >>> 1")); // expected 50, but outputs 50.0
System.out.println(js.eval("(100 >>> 1)|0")); // 50
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8194668 JSType.toNarrowestNumber never returns Integer
-
- Closed
-