8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
authorsundar
Tue, 15 Jul 2014 16:54:01 +0530
changeset 25585 0968d8ab046a
parent 25456 39cfdc2dcaf3
child 25586 cac6440168e2
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings Reviewed-by: jlaskey
nashorn/src/jdk/nashorn/internal/objects/Global.java
nashorn/test/script/basic/JDK-8015969.js
nashorn/test/script/basic/JDK-8050432.js
nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java	Wed Jul 05 19:50:06 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java	Tue Jul 15 16:54:01 2014 +0530
@@ -1763,6 +1763,8 @@
             final int NOT_ENUMERABLE_NOT_CONFIG = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE;
             scontextProperty = addOwnProperty("context", NOT_ENUMERABLE_NOT_CONFIG, null);
             addOwnProperty("engine", NOT_ENUMERABLE_NOT_CONFIG, engine);
+            // default file name
+            addOwnProperty(ScriptEngine.FILENAME, Attribute.NOT_ENUMERABLE, null);
             // __noSuchProperty__ hook for ScriptContext search of missing variables
             final ScriptFunction noSuchProp = ScriptFunctionImpl.makeStrictFunction(NO_SUCH_PROPERTY_NAME, NO_SUCH_PROPERTY);
             addOwnProperty(NO_SUCH_PROPERTY_NAME, Attribute.NOT_ENUMERABLE, noSuchProp);
--- a/nashorn/test/script/basic/JDK-8015969.js	Wed Jul 05 19:50:06 2017 +0200
+++ b/nashorn/test/script/basic/JDK-8015969.js	Tue Jul 15 16:54:01 2014 +0530
@@ -37,24 +37,6 @@
 'use strict';
 
 try {
-    context = 444;
-    print("FAILED!! context write should have thrown error");
-} catch (e) {
-    if (! (e instanceof TypeError)) {
-        print("TypeError expected but got " + e);
-    }
-}
-
-try {
-    engine = "hello";
-    print("FAILED!! engine write should have thrown error");
-} catch (e) {
-    if (! (e instanceof TypeError)) {
-        print("TypeError expected but got " + e);
-    }
-}
-
-try {
     delete context;
     print("FAILED!! context delete should have thrown error");
 } catch (e) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8050432.js	Tue Jul 15 16:54:01 2014 +0530
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2014, 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-8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
+ *
+ * @test
+ * @run
+ */
+
+var ScriptEngine = javax.script.ScriptEngine;
+var m = new javax.script.ScriptEngineManager();
+var engine = m.getEngineByName("nashorn");
+
+engine.put(ScriptEngine.FILENAME, "foo");
+var desc = engine.eval("Object.getOwnPropertyDescriptor(this, '"
+   + ScriptEngine.FILENAME + "')");
+if (desc.enumerable) {
+    fail(ScriptEngine.FILENAME + " is enumerable");
+}
--- a/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Wed Jul 05 19:50:06 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Tue Jul 15 16:54:01 2014 +0530
@@ -607,6 +607,20 @@
         assertEquals(res, "hello");
     }
 
+    // @bug 8050432:javax.script.filename variable should not be enumerable
+    // with nashorn engine's ENGINE_SCOPE bindings
+    @Test
+    public void enumerableGlobalsTest() throws ScriptException {
+        final ScriptEngineManager m = new ScriptEngineManager();
+        final ScriptEngine e = m.getEngineByName("nashorn");
+
+        e.put(ScriptEngine.FILENAME, "test");
+        Object enumerable = e.eval(
+            "Object.getOwnPropertyDescriptor(this, " +
+            " 'javax.script.filename').enumerable");
+        assertEquals(enumerable, Boolean.FALSE);
+    }
+
     private static void checkProperty(final ScriptEngine e, final String name)
         throws ScriptException {
         final String value = System.getProperty(name);