-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Harmonize iterator prototype intrinsics to follow spec
- Loading branch information
Showing
16 changed files
with
364 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using Jint.Native.Iterator; | ||
using Jint.Native.Object; | ||
using Jint.Native.TypedArray; | ||
using Jint.Runtime; | ||
|
||
namespace Jint.Native.Array | ||
{ | ||
internal class ArrayIteratorPrototype : IteratorPrototype | ||
{ | ||
internal ArrayIteratorPrototype( | ||
Engine engine, | ||
Realm realm, | ||
ObjectPrototype objectPrototype) : base(engine, realm, "Array Iterator", objectPrototype) | ||
{ | ||
} | ||
|
||
internal IteratorInstance Construct(ObjectInstance array, Func<Intrinsics, Prototype> prototypeSelector) | ||
{ | ||
var instance = new ArrayLikeIterator(Engine, array, ArrayIteratorType.KeyAndValue) | ||
{ | ||
_prototype = prototypeSelector(_realm.Intrinsics) | ||
}; | ||
|
||
return instance; | ||
} | ||
|
||
internal IteratorInstance Construct(ObjectInstance array, ArrayIteratorType kind) | ||
{ | ||
var instance = new ArrayLikeIterator(Engine, array, kind) | ||
{ | ||
_prototype = this | ||
}; | ||
|
||
return instance; | ||
} | ||
|
||
private sealed class ArrayLikeIterator : IteratorInstance | ||
{ | ||
private readonly ArrayIteratorType _kind; | ||
private readonly TypedArrayInstance _typedArray; | ||
private readonly ArrayOperations _operations; | ||
private uint _position; | ||
private bool _closed; | ||
|
||
public ArrayLikeIterator(Engine engine, ObjectInstance objectInstance, ArrayIteratorType kind) : base(engine) | ||
{ | ||
_kind = kind; | ||
_typedArray = objectInstance as TypedArrayInstance; | ||
if (_typedArray is null) | ||
{ | ||
_operations = ArrayOperations.For(objectInstance); | ||
} | ||
|
||
_position = 0; | ||
} | ||
|
||
public override bool TryIteratorStep(out ObjectInstance nextItem) | ||
{ | ||
uint len; | ||
if (_typedArray is not null) | ||
{ | ||
_typedArray._viewedArrayBuffer.AssertNotDetached(); | ||
len = _typedArray.Length; | ||
} | ||
else | ||
{ | ||
len = _operations.GetLength(); | ||
} | ||
|
||
if (!_closed && _position < len) | ||
{ | ||
JsValue value; | ||
if (_typedArray is not null) | ||
{ | ||
nextItem = _kind switch | ||
{ | ||
ArrayIteratorType.Key => new ValueIteratorPosition(_engine, _position), | ||
ArrayIteratorType.Value => new ValueIteratorPosition(_engine, _typedArray[(int) _position]), | ||
_ => new KeyValueIteratorPosition(_engine, _position, _typedArray[(int) _position]) | ||
}; | ||
} | ||
else | ||
{ | ||
_operations.TryGetValue(_position, out value); | ||
if (_kind == ArrayIteratorType.Key) | ||
{ | ||
nextItem = new ValueIteratorPosition(_engine, _position); | ||
} | ||
else if (_kind == ArrayIteratorType.Value) | ||
{ | ||
nextItem = new ValueIteratorPosition(_engine, value); | ||
} | ||
else | ||
{ | ||
nextItem = new KeyValueIteratorPosition(_engine, _position, value); | ||
} | ||
} | ||
|
||
_position++; | ||
return true; | ||
} | ||
|
||
_closed = true; | ||
nextItem = KeyValueIteratorPosition.Done; | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.