src/jdk/nashorn/internal/runtime/ScriptObject.java

Print this page

        

@@ -1322,11 +1322,23 @@
      * return an array of own property keys associated with the object.
      *
      * @param all True if to include non-enumerable keys.
      * @return Array of keys.
      */
-    public String[] getOwnKeys(final boolean all) {
+    public final String[] getOwnKeys(final boolean all) {
+        return getOwnKeys(all, null);
+    }
+
+    /**
+     * return an array of own property keys associated with the object.
+     *
+     * @param all True if to include non-enumerable keys.
+     * @param nonEnumerable set of non-enumerable properties seen already.Used
+       to filter out shadowed, but enumerable properties from proto children.
+     * @return Array of keys.
+     */
+    protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
         final List<Object> keys    = new ArrayList<>();
         final PropertyMap  selfMap = this.getMap();
 
         final ArrayData array  = getArray();
         final long length      = array.length();

@@ -1336,12 +1348,25 @@
                 keys.add(JSType.toString(i));
             }
         }
 
         for (final Property property : selfMap.getProperties()) {
-            if (all || property.isEnumerable()) {
-                keys.add(property.getKey());
+            final boolean enumerable = property.isEnumerable();
+            final String key = property.getKey();
+            if (all) {
+                keys.add(key);
+            } else if (enumerable) {
+                // either we don't have non-enumerable filter set or filter set
+                // does not contain the current property.
+                if (nonEnumerable == null || !nonEnumerable.contains(key)) {
+                    keys.add(key);
+                }
+            } else {
+                // store this non-enumerable property for later proto walk
+                if (nonEnumerable != null) {
+                    nonEnumerable.add(key);
+                }
             }
         }
 
         return keys.toArray(new String[keys.size()]);
     }

@@ -2396,12 +2421,13 @@
         }
 
         @Override
         protected void init() {
             final Set<String> keys = new LinkedHashSet<>();
+            final Set<String> nonEnumerable = new HashSet<>();
             for (ScriptObject self = object; self != null; self = self.getProto()) {
-                keys.addAll(Arrays.asList(self.getOwnKeys(false)));
+                keys.addAll(Arrays.asList(self.getOwnKeys(false, nonEnumerable)));
             }
             this.values = keys.toArray(new String[keys.size()]);
         }
     }
 

@@ -2411,12 +2437,13 @@
         }
 
         @Override
         protected void init() {
             final ArrayList<Object> valueList = new ArrayList<>();
+            final Set<String> nonEnumerable = new HashSet<>();
             for (ScriptObject self = object; self != null; self = self.getProto()) {
-                for (final String key : self.getOwnKeys(false)) {
+                for (final String key : self.getOwnKeys(false, nonEnumerable)) {
                     valueList.add(self.get(key));
                 }
             }
             this.values = valueList.toArray(new Object[valueList.size()]);
         }