Преглед на файлове

Fix ArrayInstance.GetOwnProperties with typed dense array (#1420)

fixes https://github.com/sebastienros/jint/issues/1419
Marko Lahma преди 2 години
родител
ревизия
4d84b91d3e
променени са 2 файла, в които са добавени 20 реда и са изтрити 0 реда
  1. 12 0
      Jint.Tests/Runtime/ArrayTests.cs
  2. 8 0
      Jint/Native/Array/ArrayInstance.cs

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

@@ -304,4 +304,16 @@ return get + '' === ""length,0,1,2,3"";";
         var engine = new Engine();
         Assert.True(engine.Evaluate(Script).AsBoolean());
     }
+
+    [Fact]
+    public void ShouldBeAbleToInitFromArray()
+    {
+        var engine = new Engine();
+        var propertyDescriptors = new JsArray(engine, new JsValue[] { 1 }).GetOwnProperties().ToArray();
+        Assert.Equal(2, propertyDescriptors.Length);
+        Assert.Equal("0", propertyDescriptors[0].Key);
+        Assert.Equal(1, propertyDescriptors[0].Value.Value);
+        Assert.Equal("length", propertyDescriptors[1].Key);
+        Assert.Equal(1, propertyDescriptors[1].Value.Value);
+    }
 }

+ 8 - 0
Jint/Native/Array/ArrayInstance.cs

@@ -404,6 +404,10 @@ namespace Jint.Native.Array
                     {
                         if (value is not PropertyDescriptor descriptor)
                         {
+                            if (EnsureCompatibleDense(typeof(PropertyDescriptor)))
+                            {
+                                temp = _dense!;
+                            }
                             temp[i] = descriptor = new PropertyDescriptor((JsValue) value, PropertyFlag.ConfigurableEnumerableWritable);
                         }
                         yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(i), descriptor);
@@ -419,6 +423,10 @@ namespace Jint.Native.Array
                     {
                         if (value is not PropertyDescriptor descriptor)
                         {
+                            if (EnsureCompatibleDense(typeof(PropertyDescriptor)))
+                            {
+                                temp = _dense!;
+                            }
                             _sparse[entry.Key] = descriptor = new PropertyDescriptor((JsValue) value, PropertyFlag.ConfigurableEnumerableWritable);
                         }
                         yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(entry.Key), descriptor);