Browse Source

Fixing array index detection

Fixes #228
Sebastien Ros 10 years ago
parent
commit
e48ae5a382
2 changed files with 24 additions and 0 deletions
  1. 14 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 10 0
      Jint/Native/Array/ArrayInstance.cs

+ 14 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -1508,5 +1508,19 @@ namespace Jint.Tests.Runtime
                 }
             ");
         }
+
+        [Fact]
+        public void ArrayIndexShouldBeConvertedToUint32()
+        {
+            // This is missing from ECMA tests suite
+            // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
+
+            RunTest(@"
+                var a = [ 'foo' ];
+                assert(a[0] === 'foo');
+                assert(a['0'] === 'foo');
+                assert(a['00'] === undefined);
+            ");
+        }
     }
 }

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

@@ -306,6 +306,16 @@ namespace Jint.Native.Array
                 return uint.MaxValue;
             }
 
+            if(d == 0 && p.Length > 1)
+            {
+                // If p is a number that start with '0' and is not '0' then
+                // its ToString representation can't be the same a p. This is 
+                // not a valid array index. '01' !== ToString(ToUInt32('01'))
+                // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
+
+                return uint.MaxValue; 
+            }
+
             ulong result = (uint)d;
 
             for (int i = 1; i < p.Length; i++)