8058422: Users should be able to overwrite "context" and "engine" variables
authorsundar
Mon, 15 Sep 2014 15:18:13 +0530
changeset 26647 a6acc63c2a31
parent 26646 332e9901f0ed
child 26648 9a64e15eff37
8058422: Users should be able to overwrite "context" and "engine" variables Reviewed-by: lagergren, attila
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java
nashorn/test/script/basic/JDK-8058422.js
nashorn/test/src/jdk/nashorn/api/scripting/ScopeTest.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Fri Sep 12 15:01:48 2014 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Mon Sep 15 15:18:13 2014 +0530
@@ -439,8 +439,8 @@
 
     // current ScriptContext to use - can be null.
     private ScriptContext scontext;
-    // associated Property object for "context" property.
-    private jdk.nashorn.internal.runtime.Property scontextProperty;
+    // current ScriptEngine associated - can be null.
+    private ScriptEngine engine;
 
     /**
      * Set the current script context
@@ -448,7 +448,6 @@
      */
     public void setScriptContext(final ScriptContext scontext) {
         this.scontext = scontext;
-        scontextProperty.setValue(this, this, scontext, false);
     }
 
     // global constants for this global - they can be replaced with MethodHandle.constant until invalidated
@@ -581,6 +580,7 @@
             return;
         }
 
+        this.engine = engine;
         init(engine);
     }
 
@@ -917,6 +917,13 @@
             }
         }
 
+        switch (nameStr) {
+            case "context":
+                return sctxt;
+            case "engine":
+                return global.engine;
+        }
+
         if (self == UNDEFINED) {
             // scope access and so throw ReferenceError
             throw referenceError(global, "not.defined", nameStr);
@@ -1789,9 +1796,6 @@
         }
 
         if (engine != null) {
-            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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8058422.js	Mon Sep 15 15:18:13 2014 +0530
@@ -0,0 +1,55 @@
+/*
+ * 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-8058422: Users should be able to overwrite "context" and "engine" variables
+ *
+ * @test
+ * @run
+ */
+
+var m = new javax.script.ScriptEngineManager();
+var e = m.getEngineByName("nashorn");
+e.put("foo", "hello");
+var obj = e.eval("context.getAttribute('foo')");
+if (obj != "hello") {
+    fail("Expected 'obj' to be 'hello'");
+}
+
+e.put("context", "bar");
+if (e.eval("context") != "bar") {
+    fail("Expected 'context' to be 'bar'");
+}
+
+if (e.eval("foo") != "hello") {
+    fail("Expected 'foo' to be 'hello'");
+}
+
+if (e.eval("engine") != e) {
+    fail("'engine' is not evaluaed to current engine");
+}
+
+e.put("engine", "foobar");
+if (e.eval("engine") != "foobar") {
+    fail("'engine' is not evalued to 'foobar'");
+}
--- a/nashorn/test/src/jdk/nashorn/api/scripting/ScopeTest.java	Fri Sep 12 15:01:48 2014 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/ScopeTest.java	Mon Sep 15 15:18:13 2014 +0530
@@ -582,6 +582,60 @@
         assertEquals(e.eval("x", newCtxt), 2);
     }
 
+    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
+    @Test
+    public static void contextOverwriteTest() throws ScriptException {
+        final ScriptEngineManager m = new ScriptEngineManager();
+        final ScriptEngine e = m.getEngineByName("nashorn");
+        final Bindings b = new SimpleBindings();
+        b.put("context", "hello");
+        b.put("foo", 32);
+        final ScriptContext newCtxt = new SimpleScriptContext();
+        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
+        e.setContext(newCtxt);
+        assertEquals(e.eval("context"), "hello");
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+    }
+
+    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
+    @Test
+    public static void contextOverwriteInScriptTest() throws ScriptException {
+        final ScriptEngineManager m = new ScriptEngineManager();
+        final ScriptEngine e = m.getEngineByName("nashorn");
+        e.put("foo", 32);
+
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+        assertEquals(e.eval("context = 'bar'"), "bar");
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+    }
+
+    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
+    @Test
+    public static void engineOverwriteTest() throws ScriptException {
+        final ScriptEngineManager m = new ScriptEngineManager();
+        final ScriptEngine e = m.getEngineByName("nashorn");
+        final Bindings b = new SimpleBindings();
+        b.put("engine", "hello");
+        b.put("foo", 32);
+        final ScriptContext newCtxt = new SimpleScriptContext();
+        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
+        e.setContext(newCtxt);
+        assertEquals(e.eval("engine"), "hello");
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+    }
+
+    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
+    @Test
+    public static void engineOverwriteInScriptTest() throws ScriptException {
+        final ScriptEngineManager m = new ScriptEngineManager();
+        final ScriptEngine e = m.getEngineByName("nashorn");
+        e.put("foo", 32);
+
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+        assertEquals(e.eval("engine = 'bar'"), "bar");
+        assertEquals(((Number)e.eval("foo")).intValue(), 32);
+    }
+
     // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook
     @Test
     public static void testMegamorphicGetInGlobal() throws Exception {