提交 6e3abded 编写于 作者: S sundar

8015830: Javascript mapping of ScriptEngine bindings does not expose keys

Reviewed-by: jlaskey, lagergren
上级 5e99fd36
......@@ -31,7 +31,7 @@ import java.util.AbstractMap;
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 @@ import jdk.nashorn.internal.runtime.ScriptRuntime;
* 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 @@ final class ScriptObjectMirror extends JSObject implements Bindings {
return inGlobal(new Callable<Set<Map.Entry<String, Object>>>() {
@Override public Set<Map.Entry<String, Object>> call() {
final Iterator<String> iter = sobj.propertyIterator();
final Set<Map.Entry<String, Object>> entries = new HashSet<>();
final Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>();
while (iter.hasNext()) {
final String key = iter.next();
......@@ -253,7 +253,7 @@ final class ScriptObjectMirror extends JSObject implements Bindings {
return inGlobal(new Callable<Set<String>>() {
@Override public Set<String> call() {
final Iterator<String> iter = sobj.propertyIterator();
final Set<String> keySet = new HashSet<>();
final Set<String> keySet = new LinkedHashSet<>();
while (iter.hasNext()) {
keySet.add(iter.next());
......@@ -302,6 +302,21 @@ final class ScriptObjectMirror extends JSObject implements Bindings {
});
}
/**
* 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<Boolean>() {
@Override public Boolean call() {
return sobj.delete(unwrap(key, global));
}
});
}
@Override
public int size() {
return inGlobal(new Callable<Integer>() {
......
......@@ -1511,6 +1511,17 @@ public abstract class ScriptObject extends PropertyListenerManager implements Pr
return oldValue;
}
/**
* 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
......
......@@ -40,6 +40,7 @@ import java.util.Locale;
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 @@ public final class ScriptRuntime {
};
}
if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).keySet().iterator();
}
return Collections.emptyIterator();
}
......@@ -280,6 +285,10 @@ public final class ScriptRuntime {
};
}
if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).values().iterator();
}
if (obj instanceof Iterable) {
return ((Iterable<?>)obj).iterator();
}
......@@ -591,6 +600,10 @@ public final class ScriptRuntime {
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));
}
......
/*
* 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);
}
x = 100
doit = function () { }
100
function () { }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册