123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- using System.Collections;
- using System.Collections.Generic;
- using Jint.Native.Number;
- using Jint.Native.Object;
- using Jint.Native.TypedArray;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- namespace Jint.Native.Array
- {
- internal abstract class ArrayOperations : IEnumerable<JsValue>
- {
- protected internal const ulong MaxArrayLength = 4294967295;
- protected internal const ulong MaxArrayLikeLength = NumberConstructor.MaxSafeInteger;
- public static ArrayOperations For(ObjectInstance instance)
- {
- if (instance is ArrayInstance arrayInstance)
- {
- return new ArrayInstanceOperations(arrayInstance);
- }
- if (instance is TypedArrayInstance typedArrayInstance)
- {
- return new TypedArrayInstanceOperations(typedArrayInstance);
- }
- return new ObjectInstanceOperations(instance);
- }
- public static ArrayOperations For(Realm realm, JsValue thisObj)
- {
- var instance = TypeConverter.ToObject(realm, thisObj);
- return For(instance);
- }
- public abstract ObjectInstance Target { get; }
- public abstract ulong GetSmallestIndex(ulong length);
- public abstract uint GetLength();
- public abstract ulong GetLongLength();
- public abstract void SetLength(ulong length);
- public abstract void EnsureCapacity(ulong capacity);
- public abstract JsValue Get(ulong index);
- public virtual JsValue[] GetAll(Types elementTypes)
- {
- var n = (int) GetLength();
- var jsValues = new JsValue[n];
- for (uint i = 0; i < (uint) jsValues.Length; i++)
- {
- var jsValue = Get(i);
- if ((jsValue.Type & elementTypes) == 0)
- {
- ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
- }
- jsValues[i] = jsValue;
- }
- return jsValues;
- }
- public abstract bool TryGetValue(ulong index, out JsValue value);
- public bool HasProperty(ulong index) => Target.HasProperty(index);
- public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);
- public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);
- public abstract void DeletePropertyOrThrow(ulong index);
- internal ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);
- IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);
- IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);
- internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
- {
- private readonly ArrayOperations _obj;
- private ulong _current;
- private bool _initialized;
- private readonly uint _length;
- public ArrayLikeIterator(ArrayOperations obj)
- {
- _obj = obj;
- _length = obj.GetLength();
- Reset();
- }
- public JsValue Current
- {
- get
- {
- if (!_initialized)
- {
- return null;
- }
- else
- {
- return _obj.TryGetValue(_current, out var temp) ? temp : null;
- }
- }
- }
- object IEnumerator.Current => Current;
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- if (!_initialized)
- {
- _initialized = true;
- }
- else
- {
- _current++;
- }
- return _current < _length;
- }
- public void Reset()
- {
- _initialized = false;
- _current = 0;
- }
- }
- private sealed class ObjectInstanceOperations : ArrayOperations<ObjectInstance>
- {
- public ObjectInstanceOperations(ObjectInstance target) : base(target)
- {
- }
- private double GetIntegerLength()
- {
- var descValue = _target.Get(CommonProperties.Length, _target);
- if (!ReferenceEquals(descValue, null))
- {
- return TypeConverter.ToInteger(descValue);
- }
- return 0;
- }
- public override ulong GetSmallestIndex(ulong length)
- {
- // there are some evil tests that iterate a lot with unshift..
- if (_target.Properties == null)
- {
- return 0;
- }
- var min = length;
- foreach (var entry in _target.Properties)
- {
- if (ulong.TryParse(entry.Key.ToString(), out var index))
- {
- min = System.Math.Min(index, min);
- }
- }
- if (_target.Prototype?.Properties != null)
- {
- foreach (var entry in _target.Prototype.Properties)
- {
- if (ulong.TryParse(entry.Key.ToString(), out var index))
- {
- min = System.Math.Min(index, min);
- }
- }
- }
- return min;
- }
- public override uint GetLength()
- {
- var integerLength = GetIntegerLength();
- return (uint) (integerLength >= 0 ? integerLength : 0);
- }
- public override ulong GetLongLength()
- {
- var integerLength = GetIntegerLength();
- if (integerLength <= 0)
- {
- return 0;
- }
- return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
- }
- public override void SetLength(ulong length)
- => _target.Set(CommonProperties.Length, length, true);
- public override void EnsureCapacity(ulong capacity)
- {
- }
- public override JsValue Get(ulong index)
- => _target.Get(JsString.Create(index), _target);
- public override bool TryGetValue(ulong index, out JsValue value)
- {
- var propertyName = JsString.Create(index);
- var property = _target.GetProperty(propertyName);
- var kPresent = property != PropertyDescriptor.Undefined;
- value = kPresent ? _target.UnwrapJsValue(property) : JsValue.Undefined;
- return kPresent;
- }
- public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
- => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
- public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
- => _target.Set(JsString.Create(index), value, throwOnError);
- public override void DeletePropertyOrThrow(ulong index)
- => _target.DeletePropertyOrThrow(JsString.Create(index));
- }
- private sealed class ArrayInstanceOperations : ArrayOperations<ArrayInstance>
- {
- public ArrayInstanceOperations(ArrayInstance target) : base(target)
- {
- }
- public override ulong GetSmallestIndex(ulong length)
- => _target.GetSmallestIndex();
- public override uint GetLength()
- => (uint) ((JsNumber) _target._length._value)._value;
- public override ulong GetLongLength()
- => (ulong) ((JsNumber) _target._length._value)._value;
- public override void SetLength(ulong length)
- => _target.Set(CommonProperties.Length, length, true);
- public override void EnsureCapacity(ulong capacity)
- => _target.EnsureCapacity((uint) capacity);
- public override bool TryGetValue(ulong index, out JsValue value)
- // array max size is uint
- => _target.TryGetValue((uint) index, out value);
- public override JsValue Get(ulong index) => _target.Get((uint) index);
- public override JsValue[] GetAll(Types elementTypes)
- {
- var n = _target.GetLength();
- if (_target._dense == null || _target._dense.Length < n)
- {
- return base.GetAll(elementTypes);
- }
- // optimized
- var jsValues = new JsValue[n];
- for (uint i = 0; i < (uint) jsValues.Length; i++)
- {
- var prop = _target._dense[i] ?? PropertyDescriptor.Undefined;
- if (prop == PropertyDescriptor.Undefined)
- {
- prop = _target.Prototype?.GetProperty(i) ?? PropertyDescriptor.Undefined;
- }
- var jsValue = _target.UnwrapJsValue(prop);
- if ((jsValue.Type & elementTypes) == 0)
- {
- ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
- }
- jsValues[i] = jsValue;
- }
- return jsValues;
- }
- public override void DeletePropertyOrThrow(ulong index)
- => _target.DeletePropertyOrThrow((uint) index);
- public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
- => _target.SetIndexValue((uint) index, value, updateLength: false);
- public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
- => _target.SetIndexValue((uint) index, value, updateLength);
- }
- private sealed class TypedArrayInstanceOperations : ArrayOperations
- {
- private readonly TypedArrayInstance _target;
- public TypedArrayInstanceOperations(TypedArrayInstance target)
- {
- _target = target;
- }
- public override ObjectInstance Target => _target;
- public override ulong GetSmallestIndex(ulong length) => 0;
- public override uint GetLength()
- {
- if (!_target.IsConcatSpreadable)
- {
- return _target.Length;
- }
- var descValue = _target.Get(CommonProperties.Length, _target);
- if (!ReferenceEquals(descValue, null))
- {
- return (uint) TypeConverter.ToInteger(descValue);
- }
- return 0;
- }
- public override ulong GetLongLength() => GetLength();
- public override void SetLength(ulong length)
- {
- }
- public override void EnsureCapacity(ulong capacity)
- {
- }
- public override JsValue Get(ulong index) => _target[(int) index];
- public override bool TryGetValue(ulong index, out JsValue value)
- {
- if (index < _target.Length)
- {
- value = _target[(int) index];
- return true;
- }
- value = JsValue.Undefined;
- return false;
- }
- public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
- => _target.CreateDataPropertyOrThrow(index, value);
- public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
- => _target[(int) index] = value;
- public override void DeletePropertyOrThrow(ulong index)
- => _target.DeletePropertyOrThrow(index);
- }
- }
- /// <summary>
- /// Adapter to use optimized array operations when possible.
- /// Gaps the difference between ArgumentsInstance and ArrayInstance.
- /// </summary>
- internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
- {
- protected readonly T _target;
- protected ArrayOperations(T target)
- {
- _target = target;
- }
- public override ObjectInstance Target => _target;
- }
- }
|