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 StringDictionarySlim enumeration after first item delete #677

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
22 changes: 22 additions & 0 deletions Jint.Tests/Runtime/ObjectInstanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Linq;
using Jint.Native;
using Jint.Native.Object;
using Xunit;

namespace Jint.Tests.Runtime
{
public class ObjectInstanceTests
{
[Fact]
public void RemovingFirstPropertyFromObjectInstancePropertiesBucketAndEnumerating()
{
var engine = new Engine();
var instance = new ObjectInstance(engine);
instance.FastAddProperty("bare", JsValue.Null, true, true, true);
instance.FastAddProperty("scope", JsValue.Null, true, true, true);
instance.RemoveOwnProperty("bare");
var propertyNames = instance.GetOwnProperties().Select(x => x.Key).ToList();
Assert.Equal(new [] { "scope" }, propertyNames);
}
}
}
11 changes: 10 additions & 1 deletion Jint/Collections/StringDictionarySlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,30 @@ public struct Enumerator : IEnumerator<KeyValuePair<string, TValue>>
{
private readonly StringDictionarySlim<TValue> _dictionary;
private int _index;
private int _count;
private KeyValuePair<string, TValue> _current;

internal Enumerator(StringDictionarySlim<TValue> dictionary)
{
_dictionary = dictionary;
_index = 0;
_count = _dictionary._count;
_current = default;
}

public bool MoveNext()
{
if (_index == _dictionary._count)
if (_count == 0)
{
_current = default;
return false;
}

_count--;

while (_dictionary._entries[_index].next < -1)
_index++;

_current = new KeyValuePair<string, TValue>(
_dictionary._entries[_index].key,
_dictionary._entries[_index++].value);
Expand All @@ -270,6 +277,8 @@ public bool MoveNext()
void IEnumerator.Reset()
{
_index = 0;
_count = _dictionary._count;
_current = default;
}

public void Dispose() { }
Expand Down