src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java

Print this page

        

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package jdk.nashorn.internal.objects;
 
+import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
 
 import jdk.internal.dynalink.CallSiteDescriptor;
 import jdk.internal.dynalink.beans.StaticClass;
 import jdk.internal.dynalink.linker.GuardedInvocation;

@@ -34,13 +35,15 @@
 import jdk.nashorn.internal.objects.annotations.Attribute;
 import jdk.nashorn.internal.objects.annotations.Constructor;
 import jdk.nashorn.internal.objects.annotations.Function;
 import jdk.nashorn.internal.objects.annotations.ScriptClass;
 import jdk.nashorn.internal.runtime.Context;
+import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.NativeJavaPackage;
 import jdk.nashorn.internal.runtime.PropertyMap;
 import jdk.nashorn.internal.runtime.ScriptObject;
+import jdk.nashorn.internal.runtime.ScriptRuntime;
 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
 
 /**
  * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names.
  * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript

@@ -92,37 +95,34 @@
     public static NativeJavaImporter constructor(final boolean isNew, final Object self, final Object... args) {
         return new NativeJavaImporter(args);
     }
 
     /**
-     * "No such property" call placeholder.
-     *
-     * This can never be called as we override {@link ScriptObject#noSuchProperty}. We do declare it here as it's a signal
-     * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a {@code noSuchProperty} on this object.
+     * "No such property" handler.
      *
      * @param self self reference
      * @param name property name
-     * @return never returns
+     * @return value of the missing property
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object __noSuchProperty__(final Object self, final Object name) {
-        throw new AssertionError("__noSuchProperty__ placeholder called");
+        if (! (self instanceof NativeJavaImporter)) {
+            throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self));
+        }
+        return ((NativeJavaImporter)self).createProperty(JSType.toString(name));
     }
 
     /**
-     * "No such method call" placeholder
-     *
-     * This can never be called as we override {@link ScriptObject#noSuchMethod}. We do declare it here as it's a signal
-     * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a noSuchProperty on this object.
+     * "No such method call" handler
      *
      * @param self self reference
      * @param args arguments to method
-     * @return never returns
+     * @return never returns always throw TypeError
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object __noSuchMethod__(final Object self, final Object... args) {
-        throw new AssertionError("__noSuchMethod__ placeholder called");
+       throw typeError("not.a.function", ScriptRuntime.safeToString(args[0]));
     }
 
     @Override
     public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
         return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchProperty(desc, request);