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

EnsureCapacity can throw if called against sparse mode array #746

Merged
merged 1 commit into from
Jun 8, 2020
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
12 changes: 12 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ public void EmptyStringKey()

Assert.Equal(8, result);
}

[Fact]
public void LargeArraySize()
{
const string code = @"
let arr = [];
for (let i = 0; i < 10000; i++) arr.push(i);
Copy link
Owner

Choose a reason for hiding this comment

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

Showing off with these let keywords now ;)

for (let i=0;i<10000;i++) arr.splice(0, 1);
";
var engine = new Engine();
engine.Execute(code);
}
}
}
12 changes: 7 additions & 5 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,15 @@ private void ConvertToSparse()

internal void EnsureCapacity(uint capacity)
{
if (capacity <= MaxDenseArrayLength && capacity > (uint) _dense.Length)
if (capacity > MaxDenseArrayLength || _dense is null || capacity <= (uint) _dense.Length)
Copy link
Owner

Choose a reason for hiding this comment

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

I understand that the capacity doubles?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Capacity is decided by the caller, we can grow as asked if it hasn't flipped to be dictionary (sparse) already. Usually logic asks for double the size but for example splice knows what to expect and thus was signaling specific size when algorithm is at specific point. Same like using constructor parameter, can signal known size.

{
// need to grow
var newArray = new PropertyDescriptor[capacity];
System.Array.Copy(_dense, newArray, _dense.Length);
_dense = newArray;
return;
}

// need to grow
var newArray = new PropertyDescriptor[capacity];
System.Array.Copy(_dense, newArray, _dense.Length);
_dense = newArray;
}

public IEnumerator<JsValue> GetEnumerator()
Expand Down