Ver código fonte

Merge pull request #58 from flts/feature/index

Index support improvements
Sébastien Ros 11 anos atrás
pai
commit
de2987e720

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

@@ -165,6 +165,42 @@ namespace Jint.Tests.Runtime
 
             Assert.Equal("Mickey Mouse", dictionary[1]);
             Assert.Equal("Donald Duck", dictionary[2]);
+        }
+
+        [Fact]
+        public void CanUseIndexOnCollection()
+        {
+            var collection = new System.Collections.ObjectModel.Collection<string>();
+            collection.Add("Mickey Mouse");
+            collection.Add("Goofy");
+
+            _engine.SetValue("dictionary", collection);
+
+            RunTest(@"
+                dictionary[1] = 'Donald Duck';
+                assert(dictionary[1] === 'Donald Duck');
+            ");
+
+            Assert.Equal("Mickey Mouse", collection[0]);
+            Assert.Equal("Donald Duck", collection[1]);
+        }
+
+        [Fact]
+        public void CanUseIndexOnList()
+        {
+            var arrayList = new System.Collections.ArrayList(2);
+            arrayList.Add("Mickey Mouse");
+            arrayList.Add("Goofy");
+
+            _engine.SetValue("dictionary", arrayList);
+
+            RunTest(@"
+                dictionary[1] = 'Donald Duck';
+                assert(dictionary[1] === 'Donald Duck');
+            ");
+
+            Assert.Equal("Mickey Mouse", arrayList[0]);
+            Assert.Equal("Donald Duck", arrayList[1]);
         }
 
         [Fact]

+ 2 - 2
Jint/Runtime/Interop/ObjectWrapper .cs

@@ -90,8 +90,8 @@ namespace Jint.Runtime.Interop
                 return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methods), false, true, false);
             }
 
-            // if no methods are found check if target is an IDictionary
-            if (typeof(IDictionary).IsAssignableFrom(type))
+            // if no methods are found check if target implemented indexing
+            if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
             {
                 return new IndexDescriptor(Engine, propertyName, Target);
             }