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

JSObject.getMember is not called if name contains parentheses

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      When a property lookup is performed on an object backed by a JSObject (or AbstractJSObject) implementation, and the property name contains at least a left parenthesis, the corresponding getMember method is not called by the engine.

      In JSObjectLinker, the presence of a left parenthesis seems to be treated as an indication that an exact method with a certain signature should be called on the JSObject instead. However, the main point for implementing a custom JSObject (instead of just exposing a plain Java object) is to control exactly how the object behaves in script code.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      * Extend from AbstractJSObject and override the getMember method.
      * Put an instance of that implementation into a Bindings.
      * Evaluate a script that tries to access a property name with parentheses.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The custom getMember method is called.
      ACTUAL -
      The custom getMember method is not called. Instead, the property access expression simply returns null in the script.

      ---------- BEGIN SOURCE ----------
      import javax.script.ScriptEngine;

      import jdk.nashorn.api.scripting.AbstractJSObject;
      import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

      public class ParenthesisInPropertyName {

          public static class TestJsObject extends AbstractJSObject {

              @Override
              public Object getMember(String name) {
                  System.out.println("Member called: " + name);
                  return null;
              }
          }

          public static void main(String[] args) throws Exception {
              ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
              engine.put("test", new TestJsObject());
              engine.eval("test['foo bar']"); // Member called: foo bar
              engine.eval("test['foo(bar)']"); // Nothing happens
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                hannesw Hannes Wallnoefer
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: