# HG changeset patch # User sundar # Date 1370365308 -19800 # Node ID c70f60578385b946eae85b5c2a7c988c2c52cf34 # Parent 78113cda23bf4f6f6e193ce50f6ae1fcaca7dabc 8015830: Javascript mapping of ScriptEngine bindings does not expose keys Reviewed-by: jlaskey, lagergren diff -r 78113cda23bf -r c70f60578385 src/jdk/nashorn/api/scripting/ScriptObjectMirror.java --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Tue Jun 04 17:33:14 2013 +0530 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Tue Jun 04 22:31:48 2013 +0530 @@ -31,7 +31,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -48,7 +48,7 @@ * access ScriptObject via the javax.script.Bindings interface or * netscape.javascript.JSObject interface. */ -final class ScriptObjectMirror extends JSObject implements Bindings { +public final class ScriptObjectMirror extends JSObject implements Bindings { private final ScriptObject sobj; private final ScriptObject global; @@ -217,7 +217,7 @@ return inGlobal(new Callable>>() { @Override public Set> call() { final Iterator iter = sobj.propertyIterator(); - final Set> entries = new HashSet<>(); + final Set> entries = new LinkedHashSet<>(); while (iter.hasNext()) { final String key = iter.next(); @@ -253,7 +253,7 @@ return inGlobal(new Callable>() { @Override public Set call() { final Iterator iter = sobj.propertyIterator(); - final Set keySet = new HashSet<>(); + final Set keySet = new LinkedHashSet<>(); while (iter.hasNext()) { keySet.add(iter.next()); @@ -302,6 +302,21 @@ }); } + /** + * Delete a property from this object. + * + * @param key the property to be deleted + * + * @return if the delete was successful or not + */ + public boolean delete(final Object key) { + return inGlobal(new Callable() { + @Override public Boolean call() { + return sobj.delete(unwrap(key, global)); + } + }); + } + @Override public int size() { return inGlobal(new Callable() { diff -r 78113cda23bf -r c70f60578385 src/jdk/nashorn/internal/runtime/ScriptObject.java --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java Tue Jun 04 17:33:14 2013 +0530 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java Tue Jun 04 22:31:48 2013 +0530 @@ -1512,6 +1512,17 @@ } /** + * Delete a property from the ScriptObject. + * (to help ScriptObjectMirror implementation) + * + * @param key the key of the property + * @return if the delete was successful or not + */ + public boolean delete(final Object key) { + return delete(key, getContext()._strict); + } + + /** * Return the size of the ScriptObject - i.e. the number of properties * it contains * (java.util.Map-like method to help ScriptObjectMirror implementation) diff -r 78113cda23bf -r c70f60578385 src/jdk/nashorn/internal/runtime/ScriptRuntime.java --- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Tue Jun 04 17:33:14 2013 +0530 +++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Tue Jun 04 22:31:48 2013 +0530 @@ -40,6 +40,7 @@ import java.util.NoSuchElementException; import java.util.Objects; import jdk.internal.dynalink.beans.StaticClass; +import jdk.nashorn.api.scripting.ScriptObjectMirror; import jdk.nashorn.internal.codegen.CompilerConstants.Call; import jdk.nashorn.internal.ir.debug.JSONWriter; import jdk.nashorn.internal.parser.Lexer; @@ -240,6 +241,10 @@ }; } + if (obj instanceof ScriptObjectMirror) { + return ((ScriptObjectMirror)obj).keySet().iterator(); + } + return Collections.emptyIterator(); } @@ -280,6 +285,10 @@ }; } + if (obj instanceof ScriptObjectMirror) { + return ((ScriptObjectMirror)obj).values().iterator(); + } + if (obj instanceof Iterable) { return ((Iterable)obj).iterator(); } @@ -591,6 +600,10 @@ throw typeError("cant.delete.property", safeToString(property), "null"); } + if (obj instanceof ScriptObjectMirror) { + return ((ScriptObjectMirror)obj).delete(property); + } + if (JSType.isPrimitive(obj)) { return ((ScriptObject) JSType.toScriptObject(obj)).delete(property, Boolean.TRUE.equals(strict)); } diff -r 78113cda23bf -r c70f60578385 test/script/basic/JDK-8015830.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/JDK-8015830.js Tue Jun 04 22:31:48 2013 +0530 @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2010, 2013, 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-8015830: Javascript mapping of ScriptEngine bindings does not expose keys + * + * @test + * @run + */ + +var m = new javax.script.ScriptEngineManager(); +var engine = m.getEngineByName("nashorn"); + +engine.eval("x = 100; doit = function () { }"); + +var global = engine.getBindings(javax.script.ScriptContext.ENGINE_SCOPE); + +for(k in global){ + print(k + " = " + global[k]); +} + +for each (k in global) { + print(k); +} + +for(k in global) { + delete global[k]; +} + +for(k in global){ + print(k + " = " + global[k]); +} + +for each(k in global) { + print(k); +} diff -r 78113cda23bf -r c70f60578385 test/script/basic/JDK-8015830.js.EXPECTED --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/JDK-8015830.js.EXPECTED Tue Jun 04 22:31:48 2013 +0530 @@ -0,0 +1,4 @@ +x = 100 +doit = function () { } +100 +function () { }