Browse Source

Cannot index array with empty string (#660)

Marko Lahma 6 years ago
parent
commit
6a01501c31
2 changed files with 12 additions and 1 deletions
  1. 7 1
      Jint.Tests/Runtime/ArrayTests.cs
  2. 5 0
      Jint/Native/Array/ArrayInstance.cs

+ 7 - 1
Jint.Tests/Runtime/ArrayTests.cs

@@ -1,5 +1,4 @@
 using System;
 using System;
-using Jint.Runtime;
 using Xunit;
 using Xunit;
 
 
 namespace Jint.Tests.Runtime
 namespace Jint.Tests.Runtime
@@ -46,5 +45,12 @@ namespace Jint.Tests.Runtime
             Assert.Equal("[object Object]", result);
             Assert.Equal("[object Object]", result);
         }
         }
 
 
+        [Fact]
+        public void EmptyStringKey()
+        {
+            var result = _engine.Execute("var x=[];x[\"\"]=8;x[\"\"];").GetCompletionValue().AsNumber();
+
+            Assert.Equal(8, result);
+        }
     }
     }
 }
 }

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

@@ -462,6 +462,11 @@ namespace Jint.Native.Array
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static uint ParseArrayIndex(string p)
         private static uint ParseArrayIndex(string p)
         {
         {
+            if (p.Length == 0)
+            {
+                return uint.MaxValue;
+            }
+
             int d = p[0] - '0';
             int d = p[0] - '0';
 
 
             if (d < 0 || d > 9)
             if (d < 0 || d > 9)