# HG changeset patch # User hannesw # Date 1486551461 -3600 # Node ID 1e6fad0e8950b8114c48475157979d9dd7646f71 # Parent 3c6fbdf6e785aaf18d35ce9c6684369952fd22ec 8171539: Better script accessibility for JavaScript Reviewed-by: jlaskey, sundar diff -r 3c6fbdf6e785 -r 1e6fad0e8950 src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java --- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java Thu Jun 22 18:42:48 2017 +0000 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java Wed Feb 08 11:57:41 2017 +0100 @@ -90,9 +90,9 @@ @ScriptClass("Global") public final class Global extends Scope { // This special value is used to flag a lazily initialized global property. - // This also serves as placeholder value used in place of a location property - // (__FILE__, __DIR__, __LINE__) private static final Object LAZY_SENTINEL = new Object(); + // This serves as placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__) + private static final Object LOCATION_PLACEHOLDER = new Object(); private static final String PACKAGE_PREFIX = "jdk.nashorn.internal.objects."; @@ -916,7 +916,7 @@ public volatile Object org; /** - * Getter for the Nashorn extension: Java access - global.javaImporter. + * Getter for the Nashorn extension: Java access - global.JavaImporter. * * @param self self reference * @return the value of the JavaImporter property @@ -931,7 +931,7 @@ } /** - * Setter for the Nashorn extension: Java access - global.javaImporter. + * Setter for the Nashorn extension: Java access - global.JavaImporter. * * @param self self reference * @param value value of the JavaImporter property @@ -975,15 +975,15 @@ /** Nashorn extension: current script's file name */ @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) - public static final Object __FILE__ = LAZY_SENTINEL; + public static final Object __FILE__ = LOCATION_PLACEHOLDER; /** Nashorn extension: current script's directory */ @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) - public static final Object __DIR__ = LAZY_SENTINEL; + public static final Object __DIR__ = LOCATION_PLACEHOLDER; /** Nashorn extension: current source line number being executed */ @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT) - public static final Object __LINE__ = LAZY_SENTINEL; + public static final Object __LINE__ = LOCATION_PLACEHOLDER; private volatile NativeDate DEFAULT_DATE; @@ -2093,6 +2093,9 @@ } private synchronized ScriptFunction getBuiltinJavaImporter() { + if (getContext().getEnv()._no_java) { + throw new IllegalStateException(); + } if (this.builtinJavaImporter == null) { this.builtinJavaImporter = initConstructor("JavaImporter", ScriptFunction.class); } @@ -2100,6 +2103,9 @@ } private synchronized ScriptObject getBuiltinJavaApi() { + if (getContext().getEnv()._no_java) { + throw new IllegalStateException(); + } if (this.builtinJavaApi == null) { this.builtinJavaApi = initConstructor("Java", ScriptObject.class); this.builtInJavaExtend = (ScriptFunction)builtinJavaApi.get("extend"); @@ -2325,7 +2331,7 @@ * @return true if the value is a placeholder, false otherwise. */ public static boolean isLocationPropertyPlaceholder(final Object placeholder) { - return placeholder == LAZY_SENTINEL; + return placeholder == LOCATION_PLACEHOLDER; } /** @@ -2628,6 +2634,17 @@ this.javaApi = LAZY_SENTINEL; this.javaImporter = LAZY_SENTINEL; initJavaAccess(); + } else { + // delete nasgen-created global properties related to java access + this.delete("Java", false); + this.delete("JavaImporter", false); + this.delete("Packages", false); + this.delete("com", false); + this.delete("edu", false); + this.delete("java", false); + this.delete("javafx", false); + this.delete("javax", false); + this.delete("org", false); } if (! env._no_typed_arrays) { diff -r 3c6fbdf6e785 -r 1e6fad0e8950 test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java --- a/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java Thu Jun 22 18:42:48 2017 +0000 +++ b/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java Wed Feb 08 11:57:41 2017 +0100 @@ -25,6 +25,7 @@ package jdk.nashorn.internal.runtime.test; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.io.File; import javax.script.ScriptEngine; @@ -77,6 +78,65 @@ } catch (final ScriptException e) { //emtpy } + try { + engine.eval("Java"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("JavaImporter"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("Packages"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("com"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("edu"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("java"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("javafx"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("javax"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + engine.eval("org"); + fail("TypeError should have been thrown"); + } catch (final ScriptException e) { + //emtpy + } + try { + assertEquals(engine.eval("Java = this[\"__LINE__\"]; Java === this[\"__LINE__\"]"), Boolean.TRUE); + } catch (final ScriptException e) { + fail("Unexpected exception", e); + } } @Test