Browse Source

Check if length has been initialized in ArrayInstance.GetLength (#761)

Marko Lahma 5 years ago
parent
commit
11b0173d6a
2 changed files with 15 additions and 0 deletions
  1. 10 0
      Jint.Tests/Runtime/ArrayTests.cs
  2. 5 0
      Jint/Native/Array/ArrayInstance.cs

+ 10 - 0
Jint.Tests/Runtime/ArrayTests.cs

@@ -1,4 +1,5 @@
 using System;
 using System;
+using Jint.Native.Array;
 using Xunit;
 using Xunit;
 
 
 namespace Jint.Tests.Runtime
 namespace Jint.Tests.Runtime
@@ -64,5 +65,14 @@ namespace Jint.Tests.Runtime
             var engine = new Engine();
             var engine = new Engine();
             engine.Execute(code);
             engine.Execute(code);
         }
         }
+
+        [Fact]
+        public void ArrayLengthFromInitialState()
+        {
+            var engine = new Engine();
+            var array = new ArrayInstance(engine, 0);
+            var length = (int) array.Length;
+            Assert.Equal(0, length);
+        }
     }
     }
 }
 }

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

@@ -222,6 +222,11 @@ namespace Jint.Native.Array
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         internal uint GetLength()
         internal uint GetLength()
         {
         {
+            if (_length is null)
+            {
+                return 0;
+            }
+            
             return (uint) ((JsNumber) _length._value)._value;
             return (uint) ((JsNumber) _length._value)._value;
         }
         }