8058422: Users should be able to overwrite "context" and "engine" variables

Mon, 15 Sep 2014 15:18:13 +0530

author
sundar
date
Mon, 15 Sep 2014 15:18:13 +0530
changeset 1013
3ce674906b2a
parent 1012
bac02d5a397f
child 1014
21cd010d3a0a

8058422: Users should be able to overwrite "context" and "engine" variables
Reviewed-by: lagergren, attila

src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8058422.js file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/api/scripting/ScopeTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Fri Sep 12 16:07:44 2014 +0200
     1.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Mon Sep 15 15:18:13 2014 +0530
     1.3 @@ -439,8 +439,8 @@
     1.4  
     1.5      // current ScriptContext to use - can be null.
     1.6      private ScriptContext scontext;
     1.7 -    // associated Property object for "context" property.
     1.8 -    private jdk.nashorn.internal.runtime.Property scontextProperty;
     1.9 +    // current ScriptEngine associated - can be null.
    1.10 +    private ScriptEngine engine;
    1.11  
    1.12      /**
    1.13       * Set the current script context
    1.14 @@ -448,7 +448,6 @@
    1.15       */
    1.16      public void setScriptContext(final ScriptContext scontext) {
    1.17          this.scontext = scontext;
    1.18 -        scontextProperty.setValue(this, this, scontext, false);
    1.19      }
    1.20  
    1.21      // global constants for this global - they can be replaced with MethodHandle.constant until invalidated
    1.22 @@ -581,6 +580,7 @@
    1.23              return;
    1.24          }
    1.25  
    1.26 +        this.engine = engine;
    1.27          init(engine);
    1.28      }
    1.29  
    1.30 @@ -917,6 +917,13 @@
    1.31              }
    1.32          }
    1.33  
    1.34 +        switch (nameStr) {
    1.35 +            case "context":
    1.36 +                return sctxt;
    1.37 +            case "engine":
    1.38 +                return global.engine;
    1.39 +        }
    1.40 +
    1.41          if (self == UNDEFINED) {
    1.42              // scope access and so throw ReferenceError
    1.43              throw referenceError(global, "not.defined", nameStr);
    1.44 @@ -1789,9 +1796,6 @@
    1.45          }
    1.46  
    1.47          if (engine != null) {
    1.48 -            final int NOT_ENUMERABLE_NOT_CONFIG = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE;
    1.49 -            scontextProperty = addOwnProperty("context", NOT_ENUMERABLE_NOT_CONFIG, null);
    1.50 -            addOwnProperty("engine", NOT_ENUMERABLE_NOT_CONFIG, engine);
    1.51              // default file name
    1.52              addOwnProperty(ScriptEngine.FILENAME, Attribute.NOT_ENUMERABLE, null);
    1.53              // __noSuchProperty__ hook for ScriptContext search of missing variables
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK-8058422.js	Mon Sep 15 15:18:13 2014 +0530
     2.3 @@ -0,0 +1,55 @@
     2.4 +/*
     2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + * 
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + * 
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + * 
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + * 
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * JDK-8058422: Users should be able to overwrite "context" and "engine" variables
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +var m = new javax.script.ScriptEngineManager();
    2.35 +var e = m.getEngineByName("nashorn");
    2.36 +e.put("foo", "hello");
    2.37 +var obj = e.eval("context.getAttribute('foo')");
    2.38 +if (obj != "hello") {
    2.39 +    fail("Expected 'obj' to be 'hello'");
    2.40 +}
    2.41 +
    2.42 +e.put("context", "bar");
    2.43 +if (e.eval("context") != "bar") {
    2.44 +    fail("Expected 'context' to be 'bar'");
    2.45 +}
    2.46 +
    2.47 +if (e.eval("foo") != "hello") {
    2.48 +    fail("Expected 'foo' to be 'hello'");
    2.49 +}
    2.50 +
    2.51 +if (e.eval("engine") != e) {
    2.52 +    fail("'engine' is not evaluaed to current engine");
    2.53 +}
    2.54 +
    2.55 +e.put("engine", "foobar");
    2.56 +if (e.eval("engine") != "foobar") {
    2.57 +    fail("'engine' is not evalued to 'foobar'");
    2.58 +}
     3.1 --- a/test/src/jdk/nashorn/api/scripting/ScopeTest.java	Fri Sep 12 16:07:44 2014 +0200
     3.2 +++ b/test/src/jdk/nashorn/api/scripting/ScopeTest.java	Mon Sep 15 15:18:13 2014 +0530
     3.3 @@ -582,6 +582,60 @@
     3.4          assertEquals(e.eval("x", newCtxt), 2);
     3.5      }
     3.6  
     3.7 +    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
     3.8 +    @Test
     3.9 +    public static void contextOverwriteTest() throws ScriptException {
    3.10 +        final ScriptEngineManager m = new ScriptEngineManager();
    3.11 +        final ScriptEngine e = m.getEngineByName("nashorn");
    3.12 +        final Bindings b = new SimpleBindings();
    3.13 +        b.put("context", "hello");
    3.14 +        b.put("foo", 32);
    3.15 +        final ScriptContext newCtxt = new SimpleScriptContext();
    3.16 +        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    3.17 +        e.setContext(newCtxt);
    3.18 +        assertEquals(e.eval("context"), "hello");
    3.19 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.20 +    }
    3.21 +
    3.22 +    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
    3.23 +    @Test
    3.24 +    public static void contextOverwriteInScriptTest() throws ScriptException {
    3.25 +        final ScriptEngineManager m = new ScriptEngineManager();
    3.26 +        final ScriptEngine e = m.getEngineByName("nashorn");
    3.27 +        e.put("foo", 32);
    3.28 +
    3.29 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.30 +        assertEquals(e.eval("context = 'bar'"), "bar");
    3.31 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.32 +    }
    3.33 +
    3.34 +    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
    3.35 +    @Test
    3.36 +    public static void engineOverwriteTest() throws ScriptException {
    3.37 +        final ScriptEngineManager m = new ScriptEngineManager();
    3.38 +        final ScriptEngine e = m.getEngineByName("nashorn");
    3.39 +        final Bindings b = new SimpleBindings();
    3.40 +        b.put("engine", "hello");
    3.41 +        b.put("foo", 32);
    3.42 +        final ScriptContext newCtxt = new SimpleScriptContext();
    3.43 +        newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    3.44 +        e.setContext(newCtxt);
    3.45 +        assertEquals(e.eval("engine"), "hello");
    3.46 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.47 +    }
    3.48 +
    3.49 +    // @bug 8058422: Users should be able to overwrite "context" and "engine" variables
    3.50 +    @Test
    3.51 +    public static void engineOverwriteInScriptTest() throws ScriptException {
    3.52 +        final ScriptEngineManager m = new ScriptEngineManager();
    3.53 +        final ScriptEngine e = m.getEngineByName("nashorn");
    3.54 +        e.put("foo", 32);
    3.55 +
    3.56 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.57 +        assertEquals(e.eval("engine = 'bar'"), "bar");
    3.58 +        assertEquals(((Number)e.eval("foo")).intValue(), 32);
    3.59 +    }
    3.60 +
    3.61      // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook
    3.62      @Test
    3.63      public static void testMegamorphicGetInGlobal() throws Exception {

mercurial