Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8187362

Nashorn unsigned right shift operator unexpectedly returns floating-point

    XMLWordPrintable

Details

    • b25
    • generic
    • generic

    Description

      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 ----------

      Attachments

        Issue Links

          Activity

            People

              pmuthuswamy Priya Lakshmi Muthuswamy (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: