changeset 48326:e1eb12343774

Merge
author jjg
date Wed, 13 Dec 2017 17:29:44 -0800
parents e5cdedd37b78 cc738fba6893
children d2a837cf9ff1
files
diffstat 3 files changed, 81 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java	Wed Dec 13 17:27:43 2017 -0800
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java	Wed Dec 13 17:29:44 2017 -0800
@@ -43,6 +43,7 @@
 import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.ScriptRuntime;
 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
+import jdk.nashorn.internal.runtime.WithObject;
 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
 
 /**
@@ -106,10 +107,35 @@
      */
     @Function(attributes = Attribute.NOT_ENUMERABLE)
     public static Object __noSuchProperty__(final Object self, final Object name) {
-        if (! (self instanceof NativeJavaImporter)) {
-            throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self));
+        final NativeJavaImporter javaImporter = getJavaImporter(self);
+        if (javaImporter != null) {
+            return javaImporter.createProperty(JSType.toString(name));
         }
-        return ((NativeJavaImporter)self).createProperty(JSType.toString(name));
+        throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self));
+    }
+
+    private static NativeJavaImporter getJavaImporter(Object self) {
+        final NativeJavaImporter expression;
+        if (self instanceof NativeJavaImporter) {
+            expression = (NativeJavaImporter)self;
+        } else if (self instanceof ScriptObject) {
+            expression = getJavaImporterInScope((ScriptObject)self);
+        } else {
+            expression = null;
+        }
+        return expression;
+    }
+
+    private static NativeJavaImporter getJavaImporterInScope(ScriptObject self) {
+        for (ScriptObject obj = self; obj != null; obj = obj.getProto()) {
+            if (obj instanceof WithObject) {
+                final ScriptObject expression = ((WithObject)obj).getExpression();
+                if (expression instanceof NativeJavaImporter) {
+                    return (NativeJavaImporter)expression;
+                }
+            }
+        }
+        return null;
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nashorn/script/basic/JDK-8191301.js	Wed Dec 13 17:29:44 2017 -0800
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8191301 : JavaImporter fails to resolve imported elements within functions, that contain too many statements
+ *
+ * @test
+ * @run
+ * @option -Dnashorn.compiler.splitter.threshold=150
+ * @fork
+ */
+
+var imports = new JavaImporter(java.lang);
+with (imports) {
+    function func() {
+        System.out.println('a');
+        System.out.println('a');
+        System.out.println('a');
+        System.out.println('a');
+        System.out.println('a');
+        System.out.println('a');
+        System.out.println('a');
+    };
+    func();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nashorn/script/basic/JDK-8191301.js.EXPECTED	Wed Dec 13 17:29:44 2017 -0800
@@ -0,0 +1,7 @@
+a
+a
+a
+a
+a
+a
+a