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

Nashorn, large numbers will be automatically converted to scientific notation

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      os: macOS Monterey 12.2.1
      java: 1.8.0_251

      A DESCRIPTION OF THE PROBLEM :
      When Nashorn parses a piece of JavaScript code, if there is a large number greater than 10 digits in the script, it will be parsed using the "double" type, which may result in a scientific notation format, which could cause errors in executing the JavaScript code


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      {
           {
              var param = "KYJ50A-01";
              switch (param) {
              case "KYJ50A-01": {
                       {
                          r = 2200001549;
                      }
                      break;
                  }

              default: {
                       {
                          0;
                      }
                  }

              }
          }
      }

      ACTUAL -
      {
           {
              var param = "KYJ50A-01";
              switch (param) {
              case "KYJ50A-01": {
                       {
                          r = 2.200001549E9;
                      }
                      break;
                  }

              default: {
                       {
                          0;
                      }
                  }

              }
          }
      }


      ---------- BEGIN SOURCE ----------
      import jdk.nashorn.internal.ir.Block;
      import jdk.nashorn.internal.ir.FunctionNode;
      import jdk.nashorn.internal.parser.Parser;
      import jdk.nashorn.internal.runtime.Context;
      import jdk.nashorn.internal.runtime.ScriptEnvironment;
      import jdk.nashorn.internal.runtime.Source;
      import jdk.nashorn.internal.runtime.options.Options;

      import java.io.IOException;
      import java.io.PrintWriter;

      public class NashornNumber {

          public static void main(String[] args) throws IOException {
              String s = "{ var param = \"KYJ50A-01\";\n" +
                      " switch (param) {\n" +
                      " case \"KYJ50A-01\":\n" +
                      " {\n" +
                      " r = 2200001549;\n" +
                      " }\n" +
                      " break;\n" +
                      " default: \n" +
                      " {\n" +
                      " 0;\n" +
                      " }\n" +
                      " }\n" +
                      "}";

              Parser parser = new Parser(new ScriptEnvironment(new Options(""),
                      new PrintWriter(System.out, true),
                      new PrintWriter(System.err, true)),
                      Source.sourceFor("source", s), new Context.ThrowErrorManager());
              FunctionNode functionNode = parser.parse();
              Block block = functionNode.getBody();
              PrintVisitor printVisitor = new PrintVisitor();
              block.accept(printVisitor);
              System.out.println(printVisitor);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Due to the inability to directly modify the JDK Nashorn code, the current solution is to use byte code technology to perform AOP manual intervention in the jdk.nashorn.internal.parser.Lexer#valueOf method, so as to return a value of type BigDecimal.

      FREQUENCY : always


            attila Attila Szegedi
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: