Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport fix ArrayInstance.CopyValues to handle holes correctly #1901

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Jint.Tests/Jint.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
</ItemGroup>

<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Xunit" />
</ItemGroup>

Expand Down
25 changes: 25 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,31 @@ public void Concat()
Assert.Equal(7, engine.Evaluate("get.length"));
}

[Fact]
public void ConcatHandlesHolesCorrectly()
{
const string Code = """
function colors(specifier) {
var n = specifier.length / 6 | 0, colors = new Array(n), i = 0;
while (i < n) colors[i] = "#" + specifier.slice(i * 6, ++i * 6);
return colors;
}

new Array(3).concat("d8b365f5f5f55ab4ac","a6611adfc27d80cdc1018571").map(colors);
""";

var engine = new Engine();

var a = engine.Evaluate(Code).AsArray();

a.Length.Should().Be(5);
a[0].Should().Be(JsValue.Undefined);
a[1].Should().Be(JsValue.Undefined);
a[2].Should().Be(JsValue.Undefined);
a[3].Should().BeOfType<JsArray>().Which.Should().ContainInOrder("#d8b365", "#f5f5f5", "#5ab4ac");
a[4].Should().BeOfType<JsArray>().Which.Should().ContainInOrder("#a6611a", "#dfc27d", "#80cdc1", "#018571");
}

[Fact]
public void Shift()
{
Expand Down
1 change: 0 additions & 1 deletion Jint.Tests/Runtime/TypeDescriptorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using FluentAssertions;
using Jint.Runtime.Interop;

namespace Jint.Tests.Runtime;
Expand Down
11 changes: 2 additions & 9 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,20 +1327,13 @@ internal void CopyValues(JsArray source, uint sourceStartIndex, uint targetStart
for (var i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
{
JsValue? sourceValue;
if (i < (uint) sourceDense.Length && sourceDense[i] is not null)
if (i < (uint) sourceDense.Length)
{
sourceValue = sourceDense[i];
}
else
{
if (!source.TryGetValue(i, out var temp))
{
sourceValue = source.Prototype?.Get(JsString.Create(i));
}
else
{
sourceValue = temp;
}
source.TryGetValue(i, out sourceValue);
}

dense[targetStartIndex + j] = sourceValue;
Expand Down