2
0
Эх сурвалжийг харах

Backport fix ArrayInstance.CopyValues to handle holes correctly (#1901)

Marko Lahma 1 жил өмнө
parent
commit
0b3b46eb3f

+ 1 - 0
Jint.Tests/Jint.Tests.csproj

@@ -35,6 +35,7 @@
   </ItemGroup>
 
   <ItemGroup>
+    <Using Include="FluentAssertions" />
     <Using Include="Xunit" />
   </ItemGroup>
 

+ 25 - 0
Jint.Tests/Runtime/ArrayTests.cs

@@ -302,6 +302,31 @@ public class ArrayTests
         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()
     {

+ 0 - 1
Jint.Tests/Runtime/TypeDescriptorTests.cs

@@ -1,5 +1,4 @@
 using System.Collections;
-using FluentAssertions;
 using Jint.Runtime.Interop;
 
 namespace Jint.Tests.Runtime;

+ 2 - 9
Jint/Native/Array/ArrayInstance.cs

@@ -1327,20 +1327,13 @@ namespace Jint.Native.Array
                 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;