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

Operator == doesn't give correct output within Nashorn for Long

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      C:\Users\ajeetkumarv\Desktop\Nashorn Bug>java -version
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]

      A DESCRIPTION OF THE PROBLEM :
      I've commented the mismatches between outputs of versions in below code.

      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;
      import javax.script.ScriptException;
      import javax.script.Invocable;

      public class NashornTest {

      public static void main(String[] args) throws ScriptException, NoSuchMethodException{

      final String CODE =
      " function testFunction(num1, num2) {"
      // this prints true for java version 1.8.0_66 but false in 1.8.0_121
      // we tried converting them to primitves also using Long.longValue() but results false in 121
      +" print(num1 == num2);" // returns false in version 121 but true in version 66
      +" print(num1.longValue() == num2.longValue());" // returns false in version 121 but true in version 66
      +" }";

      final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
      ENGINE.eval(CODE);
      Invocable invocable = (Invocable) ENGINE;

      invocable.invokeFunction("testFunction", 37170L, 37170L);

      }

      }

      REGRESSION. Last worked in version 8u102

      ADDITIONAL REGRESSION INFORMATION:
      C:\Program Files\Java\jdk1.8.0_66\bin>java -version
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just compile and run below code for java version 1.8_0_66 and for version 1.8_0_121

      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;
      import javax.script.ScriptException;
      import javax.script.Invocable;

      public class NashornTest {

      public static void main(String[] args) throws ScriptException, NoSuchMethodException{

      final String CODE =
      " function testFunction(num1, num2) {"
      // this prints true for java version 1.8.0_66 but false in 1.8.0_121
      // we tried converting them to primitves also using Long.longValue() but results false in 121
      +" print(num1 == num2);" // returns false in version 121 but true in version 66
      +" print(num1.longValue() == num2.longValue());" // returns false in version 121 but true in version 66
      +" }";

      final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
      ENGINE.eval(CODE);
      Invocable invocable = (Invocable) ENGINE;

      invocable.invokeFunction("testFunction", 37170L, 37170L);

      }

      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      From within Nashorn engine:
      I'm expecting when I compare long values num1 == num2 it should be true as in version 1.8_0_66.

      I tested them with converting them to primitive using num1.longValue() == num2.longValue() but in 1.8_0_121 gives false
      ACTUAL -
      I see two long values when compare within Nashorn gave me false in version 121 inspite of having same value. this gave me true in version 66

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No crashes

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      final String CODE =
      " function testFunction(num1, num2) {"
      // this prints true for java version 1.8.0_66 but false in 1.8.0_121
      // we tried converting them to primitves also using Long.longValue() but results false in 121
      +" print(num1 == num2);" // returns false in version 121 but true in version 66
      +" print(num1.longValue() == num2.longValue());" // returns false in version 121 but true in version 66
      +" }";

      final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
      ENGINE.eval(CODE);
      Invocable invocable = (Invocable) ENGINE;

      invocable.invokeFunction("testFunction", 37170L, 37170L);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      As per my observation equals method work.
      But why does num1*1 == num2*1 works, I did this just to see if this converts them to primitives and it works. but num1.longValue() doesn't

      Attachments

        Issue Links

          Activity

            People

              psonal Pallavi Sonal (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: