Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array indexing with empty string #660

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Jint.Runtime;
using Xunit;

namespace Jint.Tests.Runtime
Expand Down Expand Up @@ -46,5 +45,12 @@ public void ArrayPrototypeToStringWithObject()
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 changes: 5 additions & 0 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ private static bool IsArrayIndex(string p, out uint index)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ParseArrayIndex(string p)
{
if (p.Length == 0)
{
return uint.MaxValue;
}

int d = p[0] - '0';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to interfere, but why not use char.IsDigit(p[0]) here? I checked the performance of both approaches - they are identical.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we need the actual value, say 5 to return and then when we detect length being more than 1 when start addition to that value.


if (d < 0 || d > 9)
Expand Down