ArrayOperations.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System.Collections;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Native.TypedArray;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Native.Array
  8. {
  9. internal abstract class ArrayOperations : IEnumerable<JsValue>
  10. {
  11. protected internal const ulong MaxArrayLength = 4294967295;
  12. protected internal const ulong MaxArrayLikeLength = NumberConstructor.MaxSafeInteger;
  13. public static ArrayOperations For(ObjectInstance instance)
  14. {
  15. if (instance is ArrayInstance { CanUseFastAccess: true } arrayInstance)
  16. {
  17. return new ArrayInstanceOperations(arrayInstance);
  18. }
  19. if (instance is TypedArrayInstance typedArrayInstance)
  20. {
  21. return new TypedArrayInstanceOperations(typedArrayInstance);
  22. }
  23. return new ObjectInstanceOperations(instance);
  24. }
  25. public static ArrayOperations For(Realm realm, JsValue thisObj)
  26. {
  27. var instance = TypeConverter.ToObject(realm, thisObj);
  28. return For(instance);
  29. }
  30. public abstract ObjectInstance Target { get; }
  31. public abstract ulong GetSmallestIndex(ulong length);
  32. public abstract uint GetLength();
  33. public abstract ulong GetLongLength();
  34. public abstract void SetLength(ulong length);
  35. public abstract void EnsureCapacity(ulong capacity);
  36. public abstract JsValue Get(ulong index);
  37. public virtual JsValue[] GetAll(Types elementTypes)
  38. {
  39. var n = (int) GetLength();
  40. var jsValues = new JsValue[n];
  41. for (uint i = 0; i < (uint) jsValues.Length; i++)
  42. {
  43. var jsValue = Get(i);
  44. if ((jsValue.Type & elementTypes) == 0)
  45. {
  46. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  47. }
  48. jsValues[i] = jsValue;
  49. }
  50. return jsValues;
  51. }
  52. public abstract bool TryGetValue(ulong index, out JsValue value);
  53. public bool HasProperty(ulong index) => Target.HasProperty(index);
  54. public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);
  55. public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);
  56. public abstract void DeletePropertyOrThrow(ulong index);
  57. internal ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);
  58. IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);
  59. IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);
  60. internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
  61. {
  62. private readonly ArrayOperations _obj;
  63. private ulong _current;
  64. private bool _initialized;
  65. private readonly uint _length;
  66. public ArrayLikeIterator(ArrayOperations obj)
  67. {
  68. _obj = obj;
  69. _length = obj.GetLength();
  70. Reset();
  71. }
  72. public JsValue Current
  73. {
  74. get
  75. {
  76. if (!_initialized)
  77. {
  78. return JsValue.Undefined;
  79. }
  80. else
  81. {
  82. return _obj.TryGetValue(_current, out var temp) ? temp : JsValue.Undefined;
  83. }
  84. }
  85. }
  86. object? IEnumerator.Current => Current;
  87. public void Dispose()
  88. {
  89. }
  90. public bool MoveNext()
  91. {
  92. if (!_initialized)
  93. {
  94. _initialized = true;
  95. }
  96. else
  97. {
  98. _current++;
  99. }
  100. return _current < _length;
  101. }
  102. public void Reset()
  103. {
  104. _initialized = false;
  105. _current = 0;
  106. }
  107. }
  108. private sealed class ObjectInstanceOperations : ArrayOperations<ObjectInstance>
  109. {
  110. public ObjectInstanceOperations(ObjectInstance target) : base(target)
  111. {
  112. }
  113. private double GetIntegerLength()
  114. {
  115. var descValue = _target.Get(CommonProperties.Length, _target);
  116. if (!ReferenceEquals(descValue, null))
  117. {
  118. return TypeConverter.ToInteger(descValue);
  119. }
  120. return 0;
  121. }
  122. public override ulong GetSmallestIndex(ulong length)
  123. {
  124. return _target.GetSmallestIndex(length);
  125. }
  126. public override uint GetLength()
  127. {
  128. var integerLength = GetIntegerLength();
  129. return (uint) (integerLength >= 0 ? integerLength : 0);
  130. }
  131. public override ulong GetLongLength()
  132. {
  133. var integerLength = GetIntegerLength();
  134. if (integerLength <= 0)
  135. {
  136. return 0;
  137. }
  138. return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
  139. }
  140. public override void SetLength(ulong length)
  141. => _target.Set(CommonProperties.Length, length, true);
  142. public override void EnsureCapacity(ulong capacity)
  143. {
  144. }
  145. public override JsValue Get(ulong index)
  146. => _target.Get(JsString.Create(index), _target);
  147. public override bool TryGetValue(ulong index, out JsValue value)
  148. {
  149. var propertyName = JsString.Create(index);
  150. var property = _target.GetProperty(propertyName);
  151. var kPresent = property != PropertyDescriptor.Undefined;
  152. value = kPresent ? _target.UnwrapJsValue(property) : JsValue.Undefined;
  153. return kPresent;
  154. }
  155. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  156. => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
  157. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  158. => _target.Set(JsString.Create(index), value, throwOnError);
  159. public override void DeletePropertyOrThrow(ulong index)
  160. => _target.DeletePropertyOrThrow(JsString.Create(index));
  161. }
  162. private sealed class ArrayInstanceOperations : ArrayOperations<ArrayInstance>
  163. {
  164. public ArrayInstanceOperations(ArrayInstance target) : base(target)
  165. {
  166. }
  167. public override ulong GetSmallestIndex(ulong length)
  168. => _target.GetSmallestIndex();
  169. public override uint GetLength()
  170. => (uint) ((JsNumber) _target._length!._value!)._value;
  171. public override ulong GetLongLength()
  172. => (ulong) ((JsNumber) _target._length!._value!)._value;
  173. public override void SetLength(ulong length)
  174. => _target.Set(CommonProperties.Length, length, true);
  175. public override void EnsureCapacity(ulong capacity)
  176. => _target.EnsureCapacity((uint) capacity);
  177. public override bool TryGetValue(ulong index, out JsValue value)
  178. // array max size is uint
  179. => _target.TryGetValue((uint) index, out value);
  180. public override JsValue Get(ulong index) => _target.Get((uint) index);
  181. public override JsValue[] GetAll(Types elementTypes)
  182. {
  183. var n = _target.GetLength();
  184. if (_target._dense == null || _target._dense.Length < n)
  185. {
  186. return base.GetAll(elementTypes);
  187. }
  188. // optimized
  189. var jsValues = new JsValue[n];
  190. for (uint i = 0; i < (uint) jsValues.Length; i++)
  191. {
  192. var prop = _target._dense[i] ?? PropertyDescriptor.Undefined;
  193. if (prop == PropertyDescriptor.Undefined)
  194. {
  195. prop = _target.Prototype?.GetProperty(i) ?? PropertyDescriptor.Undefined;
  196. }
  197. var jsValue = _target.UnwrapJsValue(prop);
  198. if ((jsValue.Type & elementTypes) == 0)
  199. {
  200. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  201. }
  202. jsValues[i] = jsValue;
  203. }
  204. return jsValues;
  205. }
  206. public override void DeletePropertyOrThrow(ulong index)
  207. => _target.DeletePropertyOrThrow((uint) index);
  208. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  209. => _target.SetIndexValue((uint) index, value, updateLength: false);
  210. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  211. => _target.SetIndexValue((uint) index, value, updateLength);
  212. }
  213. private sealed class TypedArrayInstanceOperations : ArrayOperations
  214. {
  215. private readonly TypedArrayInstance _target;
  216. public TypedArrayInstanceOperations(TypedArrayInstance target)
  217. {
  218. _target = target;
  219. }
  220. public override ObjectInstance Target => _target;
  221. public override ulong GetSmallestIndex(ulong length) => 0;
  222. public override uint GetLength()
  223. {
  224. if (!_target.IsConcatSpreadable)
  225. {
  226. return _target.Length;
  227. }
  228. var descValue = _target.Get(CommonProperties.Length, _target);
  229. if (!ReferenceEquals(descValue, null))
  230. {
  231. return (uint) TypeConverter.ToInteger(descValue);
  232. }
  233. return 0;
  234. }
  235. public override ulong GetLongLength() => GetLength();
  236. public override void SetLength(ulong length)
  237. {
  238. }
  239. public override void EnsureCapacity(ulong capacity)
  240. {
  241. }
  242. public override JsValue Get(ulong index) => _target[(int) index];
  243. public override bool TryGetValue(ulong index, out JsValue value)
  244. {
  245. if (index < _target.Length)
  246. {
  247. value = _target[(int) index];
  248. return true;
  249. }
  250. value = JsValue.Undefined;
  251. return false;
  252. }
  253. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  254. => _target.CreateDataPropertyOrThrow(index, value);
  255. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  256. => _target[(int) index] = value;
  257. public override void DeletePropertyOrThrow(ulong index)
  258. => _target.DeletePropertyOrThrow(index);
  259. }
  260. }
  261. /// <summary>
  262. /// Adapter to use optimized array operations when possible.
  263. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  264. /// </summary>
  265. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  266. {
  267. protected readonly T _target;
  268. protected ArrayOperations(T target)
  269. {
  270. _target = target;
  271. }
  272. public override ObjectInstance Target => _target;
  273. }
  274. }