Skip to content

Commit

Permalink
Improve ParseArrayIndex when property is length (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 10, 2023
1 parent cbe3b6f commit bd171d0
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,31 +597,20 @@ internal static bool IsArrayIndex(JsValue p, out uint index)
// return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint ParseArrayIndex(string p)
{
if (p.Length == 0)
{
return uint.MaxValue;
}

if (p.Length > 1 && p[0] == '0')
{
// 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;
}

if (!uint.TryParse(p, out var d))
if (p.Length == 0 || p.Length > 1 && !IsInRange(p[0], '1', '9') || !uint.TryParse(p, out var d))
{
return uint.MaxValue;
}

return d;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsInRange(char c, char min, char max) => c - (uint) min <= max - (uint) min;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetIndexValue(uint index, JsValue? value, bool updateLength)
{
Expand Down

0 comments on commit bd171d0

Please sign in to comment.