8191301: JavaImporter fails to resolve imported elements within functions, that contain too many statements
Reviewed-by: hannesw, attila
Contributed-by: priya.lakshmi.muthuswamy@oracle.com
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java Wed Dec 13 14:49:48 2017 -0800
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java Thu Dec 14 02:26:45 2017 +0100
@@ -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 Thu Dec 14 02:26:45 2017 +0100
@@ -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 Thu Dec 14 02:26:45 2017 +0100
@@ -0,0 +1,7 @@
+a
+a
+a
+a
+a
+a
+a