Browse Source

Added Enumerator for Array Instance (#485)

* fixed bug #436
Pranay Kumar 7 years ago
parent
commit
12e5def2f9
2 changed files with 42 additions and 2 deletions
  1. 23 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 19 2
      Jint/Native/Array/ArrayInstance.cs

+ 23 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection;
 using Jint.Native;
 using Jint.Native;
+using Jint.Native.Array;
 using Jint.Native.Object;
 using Jint.Native.Object;
 using Jint.Tests.Runtime.Converters;
 using Jint.Tests.Runtime.Converters;
 using Jint.Tests.Runtime.Domain;
 using Jint.Tests.Runtime.Domain;
@@ -653,6 +654,28 @@ namespace Jint.Tests.Runtime
             Assert.Equal("bar.com", ((object[])parts)[1]);
             Assert.Equal("bar.com", ((object[])parts)[1]);
         }
         }
 
 
+        [Fact]
+        public void ShouldLoopWithNativeEnumerator()
+        {
+            JsValue adder(JsValue argValue)
+            {
+                ArrayInstance args = argValue.AsArray();
+                double sum = 0;
+                foreach (var item in args)
+                {
+                    if (item.IsNumber())
+                    {
+                        sum += item.AsNumber();
+                    }
+                }
+                return sum;
+            }
+            var result = _engine.SetValue("getSum", new Func<JsValue, JsValue>(adder))
+                .Execute("getSum([1,2,3]);");
+
+            Assert.True(result.GetCompletionValue() == 6);
+        }
+
         [Fact]
         [Fact]
         public void ShouldConvertBooleanInstanceToBool()
         public void ShouldConvertBooleanInstanceToBool()
         {
         {

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

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
 
 
 using Jint.Native.Object;
 using Jint.Native.Object;
@@ -10,7 +11,7 @@ using TypeConverter = Jint.Runtime.TypeConverter;
 
 
 namespace Jint.Native.Array
 namespace Jint.Native.Array
 {
 {
-    public class ArrayInstance : ObjectInstance
+    public class ArrayInstance : ObjectInstance, IEnumerable<JsValue>
     {
     {
         private readonly Engine _engine;
         private readonly Engine _engine;
 
 
@@ -596,5 +597,21 @@ namespace Jint.Native.Array
                 _dense = newArray;
                 _dense = newArray;
             }
             }
         }
         }
+
+        public IEnumerator<JsValue> GetEnumerator()
+        {
+            for (uint i = 0; i < GetLength(); i++)
+            {
+                if (TryGetValue(i, out JsValue outValue))
+                {
+                    yield return outValue;
+                }
+            };
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            return GetEnumerator();
+        }
     }
     }
 }
 }