瀏覽代碼

Fixes Multiple Indexer and DefaultTypeConverter TryConvert (#390)

Alberto Aldegheri 8 年之前
父節點
當前提交
4c13c86bb0
共有 2 個文件被更改,包括 36 次插入2 次删除
  1. 26 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 10 2
      Jint/Runtime/Interop/DefaultTypeConverter.cs

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

@@ -279,6 +279,32 @@ namespace Jint.Tests.Runtime
             Assert.Equal("Donald Duck", dictionary[2]);
         }
 
+        private class DoubleIndexedClass
+        {
+            public int this[int index]
+            {
+                get { return index; }
+            }
+
+            public string this[string index]
+            {
+                get { return index; }
+            }
+        }
+
+        [Fact]
+        public void CanGetIndexUsingBothIntAndStringIndex()
+        {
+            var dictionary = new DoubleIndexedClass();
+
+            _engine.SetValue("dictionary", dictionary);
+
+            RunTest(@"
+                assert(dictionary[1] === 1);
+                assert(dictionary['test'] === 'test');
+            ");
+        }
+
         [Fact]
         public void CanUseGenericMethods()
         {

+ 10 - 2
Jint/Runtime/Interop/DefaultTypeConverter.cs

@@ -222,8 +222,16 @@ namespace Jint.Runtime.Interop
 
             if (canConvert)
             {
-                converted = Convert(value, type, formatProvider);
-                return true;
+                try
+                {
+                    converted = Convert(value, type, formatProvider);
+                    return true;
+                }
+                catch
+                {
+                    converted = null;
+                    return false;
+                }
             }
 
             converted = null;