Skip to content

Commit

Permalink
fix StringDictionarySlim enumeration after first item delete (sebasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored and ruixiao-yu committed Feb 13, 2020
1 parent 834115c commit dbbafb1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit dbbafb1

Please sign in to comment.