Skip to content

Commit

Permalink
Fix ArrayInstance.CopyValues to handle holes correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jun 26, 2024
1 parent 62b0243 commit 6608c5a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
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

0 comments on commit 6608c5a

Please sign in to comment.