changeset 324:c70f60578385

8015830: Javascript mapping of ScriptEngine bindings does not expose keys Reviewed-by: jlaskey, lagergren
author sundar
date Tue, 04 Jun 2013 22:31:48 +0530
parents 78113cda23bf
children 62b096f7bac3
files src/jdk/nashorn/api/scripting/ScriptObjectMirror.java src/jdk/nashorn/internal/runtime/ScriptObject.java src/jdk/nashorn/internal/runtime/ScriptRuntime.java test/script/basic/JDK-8015830.js test/script/basic/JDK-8015830.js.EXPECTED
diffstat 5 files changed, 103 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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<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 @@
         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 @@
         });
     }
 
+    /**
+     * 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>() {
--- 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)
--- 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));
         }
--- /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);
+}
--- /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 () { }