123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using Jint.Native.Array;
- using Jint.Native.Map;
- using Jint.Native.Object;
- using Jint.Native.RegExp;
- using Jint.Native.Set;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- namespace Jint.Native.Iterator
- {
- internal class IteratorInstance : ObjectInstance, IIterator
- {
- private readonly IEnumerator<JsValue> _enumerable;
- public IteratorInstance(Engine engine)
- : this(engine, Enumerable.Empty<JsValue>())
- {
- }
- public IteratorInstance(
- Engine engine,
- IEnumerable<JsValue> enumerable) : base(engine, ObjectClass.Iterator)
- {
- _enumerable = enumerable.GetEnumerator();
- }
- public override object ToObject()
- {
- throw new System.NotImplementedException();
- }
- public override bool Equals(JsValue other)
- {
- return false;
- }
- public virtual bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_enumerable.MoveNext())
- {
- nextItem = new ValueIteratorPosition(_engine, _enumerable.Current);
- return true;
- }
- nextItem = ValueIteratorPosition.Done;
- return false;
- }
- public void Close(CompletionType completion)
- {
- }
- private ObjectInstance CreateIterResultObject(JsValue value, bool done)
- {
- var obj = _engine.Object.Construct(2);
- obj.SetDataProperty("value", value);
- obj.SetDataProperty("done", done);
- return obj;
- }
- private class KeyValueIteratorPosition : ObjectInstance
- {
- internal static readonly ObjectInstance Done = new KeyValueIteratorPosition(null, null, null);
- public KeyValueIteratorPosition(Engine engine, JsValue key, JsValue value) : base(engine)
- {
- var done = ReferenceEquals(null, key) && ReferenceEquals(null, value);
- if (!done)
- {
- var arrayInstance = engine.Array.ConstructFast(2);
- arrayInstance.SetIndexValue(0, key, false);
- arrayInstance.SetIndexValue(1, value, false);
- SetProperty("value", new PropertyDescriptor(arrayInstance, PropertyFlag.AllForbidden));
- }
- SetProperty("done", done ? PropertyDescriptor.AllForbiddenDescriptor.BooleanTrue : PropertyDescriptor.AllForbiddenDescriptor.BooleanFalse);
- }
- }
- private class ValueIteratorPosition : ObjectInstance
- {
- internal static readonly ObjectInstance Done = new KeyValueIteratorPosition(null, null, null);
- public ValueIteratorPosition(Engine engine, JsValue value) : base(engine)
- {
- var done = ReferenceEquals(null, value);
- if (!done)
- {
- SetProperty("value", new PropertyDescriptor(value, PropertyFlag.AllForbidden));
- }
- SetProperty("done", new PropertyDescriptor(done, PropertyFlag.AllForbidden));
- }
- }
- public class MapIterator : IteratorInstance
- {
- private readonly MapInstance _map;
- private int _position;
- public MapIterator(Engine engine, MapInstance map) : base(engine)
- {
- _map = map;
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_position < _map.GetSize())
- {
- var key = _map._map.GetKey(_position);
- var value = _map._map[key];
- _position++;
- nextItem = new KeyValueIteratorPosition(_engine, key, value);
- return true;
- }
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class ArrayLikeIterator : IteratorInstance
- {
- private readonly ArrayOperations _array;
- private uint? _end;
- private uint _position;
- public ArrayLikeIterator(Engine engine, JsValue target) : base(engine)
- {
- if (!(target is ObjectInstance objectInstance))
- {
- ExceptionHelper.ThrowTypeError(engine, "Target must be an object");
- return;
- }
- _array = ArrayOperations.For(objectInstance);
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_end == null)
- {
- _end = _array.GetLength();
- }
- if (_position < _end.Value)
- {
- _array.TryGetValue(_position, out var value);
- nextItem = new KeyValueIteratorPosition(_engine, _position++, value);
- return true;
- }
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class SetIterator : IteratorInstance
- {
- private readonly SetInstance _set;
- private int _position;
- public SetIterator(Engine engine, SetInstance set) : base(engine)
- {
- _set = set;
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_position < _set._set._list.Count)
- {
- var value = _set._set[_position];
- _position++;
- nextItem = new ValueIteratorPosition(_engine, value);
- return true;
- }
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class SetEntryIterator : IteratorInstance
- {
- private readonly SetInstance _set;
- private int _position;
- public SetEntryIterator(Engine engine, SetInstance set) : base(engine)
- {
- _set = set;
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_position < _set._set._list.Count)
- {
- var value = _set._set[_position];
- _position++;
- nextItem = new KeyValueIteratorPosition(_engine, value, value);
- return true;
- }
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class ListIterator : IteratorInstance
- {
- private readonly List<JsValue> _values;
- private int _position;
- private bool _closed;
- public ListIterator(Engine engine, List<JsValue> values) : base(engine)
- {
- _values = values;
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (!_closed && _position < _values.Count)
- {
- var value = _values[_position];
- _position++;
- nextItem = new ValueIteratorPosition(_engine, value);
- return true;
- }
- _closed = true;
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class ArrayLikeKeyIterator : IteratorInstance
- {
- private readonly ArrayOperations _operations;
- private uint _position;
- private bool _closed;
- public ArrayLikeKeyIterator(Engine engine, ObjectInstance objectInstance) : base(engine)
- {
- _operations = ArrayOperations.For(objectInstance);
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- var length = _operations.GetLength();
- if (!_closed && _position < length)
- {
- nextItem = new ValueIteratorPosition(_engine, _position++);
- return true;
- }
- _closed = true;
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- public class ArrayLikeValueIterator : IteratorInstance
- {
- private readonly ArrayOperations _operations;
- private uint _position;
- private bool _closed;
- public ArrayLikeValueIterator(Engine engine, ObjectInstance objectInstance) : base(engine)
- {
- _operations = ArrayOperations.For(objectInstance);
- _position = 0;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- var length = _operations.GetLength();
- if (!_closed && _position < length)
- {
- _operations.TryGetValue(_position++, out var value);
- nextItem = new ValueIteratorPosition(_engine, value);
- return true;
- }
- _closed = true;
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
- internal class ObjectWrapper : IIterator
- {
- private readonly ObjectInstance _target;
- private readonly ICallable _nextMethod;
- public ObjectWrapper(ObjectInstance target)
- {
- _target = target;
- _nextMethod = target.Get(CommonProperties.Next, target) as ICallable
- ?? ExceptionHelper.ThrowTypeError<ICallable>(target.Engine);
- }
- public bool TryIteratorStep(out ObjectInstance result)
- {
- result = IteratorNext();
- var done = result.Get(CommonProperties.Done);
- if (!done.IsUndefined() && TypeConverter.ToBoolean(done))
- {
- return false;
- }
- return true;
- }
- private ObjectInstance IteratorNext()
- {
- var jsValue = _nextMethod.Call(_target, Arguments.Empty);
- return jsValue as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(_target.Engine, "Iterator result " + jsValue + " is not an object");
- }
- public void Close(CompletionType completion)
- {
- if (!_target.TryGetValue(CommonProperties.Return, out var func))
- {
- return;
- }
- var innerResult = Undefined;
- try
- {
- innerResult = ((ICallable) func).Call(_target, Arguments.Empty);
- }
- catch
- {
- if (completion != CompletionType.Throw)
- {
- throw;
- }
- }
- if (completion != CompletionType.Throw && !innerResult.IsObject())
- {
- ExceptionHelper.ThrowTypeError(_target.Engine);
- }
- }
- }
- internal class StringIterator : IteratorInstance
- {
- private readonly TextElementEnumerator _iterator;
- public StringIterator(Engine engine, string str) : base(engine)
- {
- _iterator = StringInfo.GetTextElementEnumerator(str);
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_iterator.MoveNext())
- {
- nextItem = new ValueIteratorPosition(_engine, (string) _iterator.Current);
- return true;
- }
- nextItem = KeyValueIteratorPosition.Done;
- return false;
- }
- }
-
- internal class RegExpStringIterator : IteratorInstance
- {
- private readonly RegExpInstance _iteratingRegExp;
- private readonly string _s;
- private readonly bool _global;
- private readonly bool _unicode;
- private bool _done;
- public RegExpStringIterator(Engine engine, ObjectInstance iteratingRegExp, string iteratedString, bool global, bool unicode) : base(engine)
- {
- if (!(iteratingRegExp is RegExpInstance r))
- {
- ExceptionHelper.ThrowTypeError(engine);
- return;
- }
-
- _iteratingRegExp = r;
- _s = iteratedString;
- _global = global;
- _unicode = unicode;
- }
- public override bool TryIteratorStep(out ObjectInstance nextItem)
- {
- if (_done)
- {
- nextItem = CreateIterResultObject(Undefined, true);
- return false;
- }
-
- var match = RegExpPrototype.RegExpExec(_iteratingRegExp, _s);
- if (match.IsNull())
- {
- _done = true;
- nextItem = CreateIterResultObject(Undefined, true);
- return false;
- }
- if (_global)
- {
- var macthStr = TypeConverter.ToString(match.Get(JsString.NumberZeroString));
- if (macthStr == "")
- {
- var thisIndex = TypeConverter.ToLength(_iteratingRegExp.Get(RegExpInstance.PropertyLastIndex));
- var nextIndex = thisIndex + 1;
- _iteratingRegExp.Set(RegExpInstance.PropertyLastIndex, nextIndex, true);
- }
- }
- else
- {
- _done = true;
- }
- nextItem = CreateIterResultObject(match, false);
- return false;
- }
- }
- }
- }
|