Browse Source

Allow creating an array instance based on existing PropertyDescriptor array/dictionary (#498)

Allow creating an array instance based on existing PropertyDescriptor array/dictionary, this way, for intense array usages we allow reducing allocations and iterations while adding items to array.
maximburyak 7 years ago
parent
commit
3d593023de
1 changed files with 27 additions and 0 deletions
  1. 27 0
      Jint/Native/Array/ArrayInstance.cs

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

@@ -1,5 +1,6 @@
 using System.Collections;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
 
 
 using Jint.Native.Object;
 using Jint.Native.Object;
@@ -39,6 +40,32 @@ namespace Jint.Native.Array
             }
             }
         }
         }
 
 
+        public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine)
+        {
+            _engine = engine;
+            int length = 0;
+            if (items == null || items.Length == 0)
+            {
+                _dense = System.Array.Empty<PropertyDescriptor>();
+                length = 0;
+            }
+            else
+            {
+                _dense = items;
+                length = items.Length;
+            }            
+            
+            SetOwnProperty(PropertyNameLength, new PropertyDescriptor(length, PropertyFlag.OnlyWritable));
+        }
+
+        public ArrayInstance(Engine engine, Dictionary<uint, PropertyDescriptor> items) : base(engine)
+        {
+            _engine = engine;
+            _sparse = items;
+            var length = items?.Count ?? 0;
+            SetOwnProperty(PropertyNameLength, new PropertyDescriptor(length, PropertyFlag.OnlyWritable));
+        }
+
         public override string Class => "Array";
         public override string Class => "Array";
 
 
         /// Implementation from ObjectInstance official specs as the one
         /// Implementation from ObjectInstance official specs as the one