Skip to content

Commit

Permalink
Add default iterator prototype for IteratorInstance (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Apr 15, 2022
1 parent 650c0cb commit d0eb6aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,26 @@ public void ShouldBeAbleToUseConvertibleStructAsMethodParameter()
Assert.Equal("12345", _engine.Evaluate("test.create(12345)").AsString());
}

[Fact]
public void ShouldGetIteratorForListAndDictionary()
{
const string Script = @"
var it = collection[Symbol.iterator]();
var result = it.next();
var str = """";
while (!result.done) {
str += result.value;
result = it.next();
}
return str;";

_engine.SetValue("collection", new List<string> { "a", "b", "c" });
Assert.Equal("abc", _engine.Evaluate(Script));

_engine.SetValue("collection", new Dictionary<string, object> { {"a", 1 }, { "b", 2 }, { "c", 3 } });
Assert.Equal("a,1b,2c,3", _engine.Evaluate(Script));
}

private class Profile
{
public int AnyProperty => throw new NotSupportedException("NOT SUPPORTED");
Expand Down
3 changes: 2 additions & 1 deletion Jint/Native/Iterator/IteratorInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public IteratorInstance(Engine engine)
IEnumerable<JsValue> enumerable) : base(engine, ObjectClass.Iterator)
{
_enumerable = enumerable.GetEnumerator();
}
_prototype = engine.Realm.Intrinsics.IteratorPrototype;
}

public override object ToObject()
{
Expand Down

0 comments on commit d0eb6aa

Please sign in to comment.