# HG changeset patch # User hannesw # Date 1513848363 -3600 # Node ID fcb5b835bf329911cc2044ac96c4f6fd6655b774 # Parent 26b47ea4c77dc46158abcbbecdbb7ceedc7e18e4 8193491: JavaImporter fails to resolve method elements within functions, that contain too many statements Reviewed-by: hannesw, sundar, jlaskey Contributed-by: priya.lakshmi.muthuswamy@oracle.com diff -r 26b47ea4c77d -r fcb5b835bf32 src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java --- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java Wed Dec 20 13:28:23 2017 -0800 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJavaImporter.java Thu Dec 21 10:26:03 2017 +0100 @@ -25,26 +25,14 @@ package jdk.nashorn.internal.objects; -import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; -import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid; - -import jdk.dynalink.CallSiteDescriptor; import jdk.dynalink.beans.StaticClass; -import jdk.dynalink.linker.GuardedInvocation; -import jdk.dynalink.linker.LinkRequest; -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.FindProperty; 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; -import jdk.nashorn.internal.runtime.WithObject; -import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; /** * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names. @@ -98,88 +86,19 @@ return new NativeJavaImporter(args); } - /** - * "No such property" handler. - * - * @param self self reference - * @param name property name - * @return value of the missing property - */ - @Function(attributes = Attribute.NOT_ENUMERABLE) - public static Object __noSuchProperty__(final Object self, final Object name) { - final NativeJavaImporter javaImporter = getJavaImporter(self); - if (javaImporter != null) { - return javaImporter.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; - } + @Override + protected FindProperty findProperty(final Object key, final boolean deep, final boolean isScope, final ScriptObject start) { + final FindProperty find = super.findProperty(key, deep, isScope, start); + if (find == null && key instanceof String) { + final String name = (String) key; + final Object value = createProperty(name); + if(value != null) { + // We must avoid calling findProperty recursively, so we pass null as first argument + setObject(null, 0, key, value); + return super.findProperty(key, deep, isScope, start); } } - return null; - } - - /** - * "No such method call" handler - * - * @param self self reference - * @param args arguments to method - * @return never returns always throw TypeError - */ - @Function(attributes = Attribute.NOT_ENUMERABLE) - public static Object __noSuchMethod__(final Object self, final Object... args) { - 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); - } - - @Override - public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) { - return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request); - } - - @Override - protected Object invokeNoSuchProperty(final Object key, final boolean isScope, final int programPoint) { - if (!(key instanceof String)) { - return super.invokeNoSuchProperty(key, isScope, programPoint); - } - final Object retval = createProperty((String) key); - if (isValid(programPoint)) { - throw new UnwarrantedOptimismException(retval, programPoint); - } - return retval; - } - - private boolean createAndSetProperty(final CallSiteDescriptor desc) { - final String name = NashornCallSiteDescriptor.getOperand(desc); - final Object value = createProperty(name); - if(value != null) { - set(name, value, 0); - return true; - } - return false; + return find; } private Object createProperty(final String name) { diff -r 26b47ea4c77d -r fcb5b835bf32 test/nashorn/script/basic/JDK-8011555.js.EXPECTED --- a/test/nashorn/script/basic/JDK-8011555.js.EXPECTED Wed Dec 20 13:28:23 2017 -0800 +++ b/test/nashorn/script/basic/JDK-8011555.js.EXPECTED Thu Dec 21 10:26:03 2017 +0100 @@ -1,1 +1,1 @@ -TypeError: function __noSuchMethod__() { [native code] } is not a constructor function +ReferenceError: "X" is not defined diff -r 26b47ea4c77d -r fcb5b835bf32 test/nashorn/script/basic/JDK-8193491.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/nashorn/script/basic/JDK-8193491.js Thu Dec 21 10:26:03 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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-8193491 : JavaImporter fails to resolve method elements within functions, that contain too many statements + * + * @test + * @run + * @option -Dnashorn.compiler.splitter.threshold=200 + * @fork + */ + +var imports = new JavaImporter(java.lang, java.util, java.io); +with (imports) { + function func() { + var m = new HashMap(); + var f = new File("."); + var i = new Integer(2); + System.out.println(i); + System.out.println('a'); + }; + func(); +} diff -r 26b47ea4c77d -r fcb5b835bf32 test/nashorn/script/basic/JDK-8193491.js.EXPECTED --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/nashorn/script/basic/JDK-8193491.js.EXPECTED Thu Dec 21 10:26:03 2017 +0100 @@ -0,0 +1,2 @@ +2 +a diff -r 26b47ea4c77d -r fcb5b835bf32 test/nashorn/script/nosecurity/JDK-8165198.js --- a/test/nashorn/script/nosecurity/JDK-8165198.js Wed Dec 20 13:28:23 2017 -0800 +++ b/test/nashorn/script/nosecurity/JDK-8165198.js Thu Dec 21 10:26:03 2017 +0100 @@ -30,8 +30,11 @@ var NashornScriptEngineFactory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory"); var e = new NashornScriptEngineFactory().getScriptEngine("-ot=false"); -var output = e.eval("with(new JavaImporter(java.util)){x}"); -print(output); +try { + e.eval("with(new JavaImporter(java.util)){x}"); +} catch (e) { + print(e); +} e.eval("with(new JavaImporter(java.util)){x=1}"); var output2 = e.eval("with(new JavaImporter(java.util)){x}"); print(output2); diff -r 26b47ea4c77d -r fcb5b835bf32 test/nashorn/script/nosecurity/JDK-8165198.js.EXPECTED --- a/test/nashorn/script/nosecurity/JDK-8165198.js.EXPECTED Wed Dec 20 13:28:23 2017 -0800 +++ b/test/nashorn/script/nosecurity/JDK-8165198.js.EXPECTED Thu Dec 21 10:26:03 2017 +0100 @@ -1,2 +1,2 @@ -null +javax.script.ScriptException: ReferenceError: "x" is not defined in at line number 1 1