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

UnwarrantedOptimismException corrupts scope of nested functions with eval

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Variable declared and used in scope of nested function is not accessible when
      eval is used in the function (does not need to be executed)
      and
      a RewriteException is handled as a consequence of UnwarrantedOptimismException.
      Related community threads:
      https://stackoverflow.com/questions/61613216/nashorn-es6-corrupted-function-scope

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. create nashorn engine with es6 language level features
      2. compile and execute script:
      function fn() {
          const object1 = { "name": "Pepa" };
          print(object1.name);
          const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'name');
          print(descriptor1.configurable);
          print(eval("3+1"));
      }
      fn();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Script execution successful without Exception
      Output:
      Pepa
      true
      4
      ACTUAL -
      Exception in thread "main" javax.script.ScriptException: ReferenceError: "descriptor1" is not defined in <eval> at line number 8

      ---------- BEGIN SOURCE ----------
      import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

      import javax.script.Compilable;
      import javax.script.ScriptException;

      public class MainJava {
          public static void main(String[] args) throws ScriptException {
              var brokenScript = String.join("\n",
                      "function fn() {",
                      "const object1 = { 'name': 'Pepa' };",
                      "print(object1.name);",
                      "const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'name');",
                      "print(descriptor1.configurable);",
                      "print(eval('3+1'));",
                      "}",
                      "fn();"
              );
              var factory = new NashornScriptEngineFactory();
              var engine = (Compilable) factory.getScriptEngine("--no-deprecation-warning", "--language=es6", "--log=compiler:finest,fields,recompile:fine");
              var script = engine.compile(brokenScript);
              script.eval();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Rewrite the script to either
      - not use eval in the nested function
      - declare variables as var instead of const
      - avoid UnwarrantedOptimismException (here caused by execution of Object.getOwnPropertyDescriptor)

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: