aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorattila <none@none>2013-07-16 17:03:30 +0200
committerattila <none@none>2013-07-16 17:03:30 +0200
commit1769f7504373cc0364d318086c9901f6fe7a001c (patch)
tree6df66fceacd1f9bcfa8424d861c22386e1e0319b
parentb02ba273b8aef9268dff4a660d1f0ccd804b4cac (diff)
downloadnashorn-1769f7504373cc0364d318086c9901f6fe7a001c.tar.gz
8015356: array concatenation should skip empty elements
Reviewed-by: jlaskey, sundar
-rw-r--r--src/jdk/nashorn/internal/objects/NativeArray.java49
-rw-r--r--test/script/basic/JDK-8015356.js44
-rw-r--r--test/script/basic/JDK-8015356.js.EXPECTED13
3 files changed, 84 insertions, 22 deletions
diff --git a/src/jdk/nashorn/internal/objects/NativeArray.java b/src/jdk/nashorn/internal/objects/NativeArray.java
index ca02cf63..c1b64c78 100644
--- a/src/jdk/nashorn/internal/objects/NativeArray.java
+++ b/src/jdk/nashorn/internal/objects/NativeArray.java
@@ -552,35 +552,40 @@ public final class NativeArray extends ScriptObject {
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object concat(final Object self, final Object... args) {
final ArrayList<Object> list = new ArrayList<>();
- final Object selfToObject = Global.toObject(self);
+ concatToList(list, Global.toObject(self));
- if (isArray(selfToObject)) {
- final Iterator<Object> iter = arrayLikeIterator(selfToObject, true);
- while (iter.hasNext()) {
- list.add(iter.next());
- }
- } else {
- // single element, add it
- list.add(selfToObject);
+ for (final Object obj : args) {
+ concatToList(list, obj);
}
- for (final Object obj : args) {
- if (isArray(obj) || obj instanceof Iterable || (obj != null && obj.getClass().isArray())) {
- final Iterator<Object> iter = arrayLikeIterator(obj, true);
- if (iter.hasNext()) {
- while (iter.hasNext()) {
- list.add(iter.next());
+ return new NativeArray(list.toArray());
+ }
+
+ private static void concatToList(final ArrayList<Object> list, final Object obj) {
+ final boolean isScriptArray = isArray(obj);
+ final boolean isScriptObject = isScriptArray || obj instanceof ScriptObject;
+ if (isScriptArray || obj instanceof Iterable || (obj != null && obj.getClass().isArray())) {
+ final Iterator<Object> iter = arrayLikeIterator(obj, true);
+ if (iter.hasNext()) {
+ for(int i = 0; iter.hasNext(); ++i) {
+ final Object value = iter.next();
+ if(value == ScriptRuntime.UNDEFINED && isScriptObject && !((ScriptObject)obj).has(i)) {
+ // TODO: eventually rewrite arrayLikeIterator to use a three-state enum for handling
+ // UNDEFINED instead of an "includeUndefined" boolean with states SKIP, INCLUDE,
+ // RETURN_EMPTY. Until then, this is how we'll make sure that empty elements don't make it
+ // into the concatenated array.
+ list.add(ScriptRuntime.EMPTY);
+ } else {
+ list.add(value);
}
- } else if (!isArray(obj)) {
- list.add(obj); // add empty object, but not an empty array
}
- } else {
- // single element, add it
- list.add(obj);
+ } else if (!isScriptArray) {
+ list.add(obj); // add empty object, but not an empty array
}
+ } else {
+ // single element, add it
+ list.add(obj);
}
-
- return new NativeArray(list.toArray());
}
/**
diff --git a/test/script/basic/JDK-8015356.js b/test/script/basic/JDK-8015356.js
new file mode 100644
index 00000000..edc77c0c
--- /dev/null
+++ b/test/script/basic/JDK-8015356.js
@@ -0,0 +1,44 @@
+/*
+ * 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-8015355: Array concatenation should ignore empty array elements.
+ *
+ * @test
+ * @run
+ */
+
+print("***")
+print([].concat([,]).hasOwnProperty("0"))
+
+print("***")
+var x = [].concat([,'a',,'b',,'c'])
+for(var i in x) {
+ print(i + ": " + x[i])
+}
+
+print("***")
+x = x.concat(['d',,'e',,'f',,])
+for(var i in x) {
+ print(i + ": " + x[i])
+}
diff --git a/test/script/basic/JDK-8015356.js.EXPECTED b/test/script/basic/JDK-8015356.js.EXPECTED
new file mode 100644
index 00000000..3e9e80d1
--- /dev/null
+++ b/test/script/basic/JDK-8015356.js.EXPECTED
@@ -0,0 +1,13 @@
+***
+false
+***
+1: a
+3: b
+5: c
+***
+1: a
+3: b
+5: c
+6: d
+8: e
+10: f