Skip holes of the array returned by the flatMap callback js_flatMap skipped missing indices of the receiver, but copied every index of the array returned by the callback. For a hole getRawElem returns NOT_FOUND, which was defined into the result and counted, so [1, 2].flatMap(x => [x, , x]) had length 6 with two holes. Skip NOT_FOUND in that loop without moving the target index, like FlattenIntoArray does with its HasProperty check. Fixes #2588
diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeArray.java b/rhino/src/main/java/org/mozilla/javascript/NativeArray.java index f2e99ec..6772e66 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeArray.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeArray.java
@@ -1920,6 +1920,9 @@ long arrLength = getLengthProperty(cx, arr); for (long k = 0; k < arrLength; k++) { Object temp = getRawElem(arr, k); + if (temp == Scriptable.NOT_FOUND) { + continue; + } defineElemOrThrow(cx, result, j++, temp); } } else {
diff --git a/tests/src/test/java/org/mozilla/javascript/tests/es2019/ArrayFlatMapHolesTest.java b/tests/src/test/java/org/mozilla/javascript/tests/es2019/ArrayFlatMapHolesTest.java new file mode 100644 index 0000000..216c6a3 --- /dev/null +++ b/tests/src/test/java/org/mozilla/javascript/tests/es2019/ArrayFlatMapHolesTest.java
@@ -0,0 +1,63 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.tests.es2019; + +import org.junit.jupiter.api.Test; +import org.mozilla.javascript.testutils.Utils; + +/** + * Holes of an array returned by the {@code flatMap} callback are skipped. See + * https://github.com/mozilla/rhino/issues/2588 + */ +public class ArrayFlatMapHolesTest { + + private static final String SHOW = + "function show(a) {\n" + + " var parts = [];\n" + + " for (var i = 0; i < a.length; i++) parts.push(i in a ? String(a[i]) : '<hole>');\n" + + " return '[' + parts.join(', ') + '] ' + a.length;\n" + + "}\n"; + + @Test + public void middleHole() { + Utils.assertWithAllModes_ES6( + "[1, 1, 2, 2] 4", SHOW + "show([1, 2].flatMap(function (x) { return [x, , x]; }))"); + } + + @Test + public void onlyHoles() { + Utils.assertWithAllModes_ES6( + "[] 0", SHOW + "show([1, 2].flatMap(function () { return new Array(3); }))"); + } + + @Test + public void leadingAndTrailingHoles() { + Utils.assertWithAllModes_ES6( + "[1, 2] 2", SHOW + "show([1, 2].flatMap(function (x) { return [, x, ,]; }))"); + } + + @Test + public void deletedElement() { + Utils.assertWithAllModes_ES6( + "[1, 3] 2", + SHOW + + "show([1].flatMap(function () {\n" + + " var r = [1, 2, 3]; delete r[1]; return r;\n" + + "}))"); + } + + @Test + public void explicitUndefinedIsKept() { + Utils.assertWithAllModes_ES6( + "[undefined, 1] 2", + SHOW + "show([1].flatMap(function (x) { return [undefined, x]; }))"); + } + + @Test + public void holesOfReceiverAreSkipped() { + Utils.assertWithAllModes_ES6( + "[1, 1, 2, 2] 4", SHOW + "show([1, , 2].flatMap(function (x) { return [x, x]; }))"); + } +}