소스 검색

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

Marko Lahma 1 년 전
부모
커밋
0b3b46eb3f
4개의 변경된 파일28개의 추가작업 그리고 10개의 파일을 삭제
  1. 1 0
      Jint.Tests/Jint.Tests.csproj
  2. 25 0
      Jint.Tests/Runtime/ArrayTests.cs
  3. 0 1
      Jint.Tests/Runtime/TypeDescriptorTests.cs
  4. 2 9
      Jint/Native/Array/ArrayInstance.cs

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

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

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

@@ -302,6 +302,31 @@ public class ArrayTests
         Assert.Equal(7, engine.Evaluate("get.length"));
         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]
     [Fact]
     public void Shift()
     public void Shift()
     {
     {

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

@@ -1,5 +1,4 @@
 using System.Collections;
 using System.Collections;
-using FluentAssertions;
 using Jint.Runtime.Interop;
 using Jint.Runtime.Interop;
 
 
 namespace Jint.Tests.Runtime;
 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++)
                 for (var i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
                 {
                 {
                     JsValue? sourceValue;
                     JsValue? sourceValue;
-                    if (i < (uint) sourceDense.Length && sourceDense[i] is not null)
+                    if (i < (uint) sourceDense.Length)
                     {
                     {
                         sourceValue = sourceDense[i];
                         sourceValue = sourceDense[i];
                     }
                     }
                     else
                     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;
                     dense[targetStartIndex + j] = sourceValue;