ArrayOperations.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using System.Collections;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Native.String;
  5. using Jint.Native.TypedArray;
  6. using Jint.Runtime;
  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(Realm realm, JsValue value, bool forWrite)
  14. {
  15. if (!forWrite)
  16. {
  17. if (value.IsString())
  18. {
  19. return new JsStringOperations(realm, (JsString) value);
  20. }
  21. if (value is StringInstance stringInstance)
  22. {
  23. return new JsStringOperations(realm, stringInstance);
  24. }
  25. if (value is JsArray { CanUseFastAccess: true } array && array.Length <= (array._dense?.Length ?? -1))
  26. {
  27. return new ArrayReadOperations(array);
  28. }
  29. }
  30. return For(TypeConverter.ToObject(realm, value), forWrite);
  31. }
  32. public static ArrayOperations For(ObjectInstance instance, bool forWrite)
  33. {
  34. if (instance is JsArray { CanUseFastAccess: true } arrayInstance)
  35. {
  36. return new JsArrayOperations(arrayInstance);
  37. }
  38. if (instance is JsTypedArray typedArrayInstance)
  39. {
  40. return new JsTypedArrayOperations(typedArrayInstance);
  41. }
  42. return new ObjectOperations(instance);
  43. }
  44. public abstract ObjectInstance Target { get; }
  45. public abstract ulong GetSmallestIndex(ulong length);
  46. public abstract uint GetLength();
  47. public abstract ulong GetLongLength();
  48. public abstract void SetLength(ulong length);
  49. public abstract void EnsureCapacity(ulong capacity);
  50. public abstract JsValue Get(ulong index);
  51. public virtual JsValue[] GetAll(
  52. Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object,
  53. bool skipHoles = false)
  54. {
  55. uint writeIndex = 0;
  56. var n = (int) GetLength();
  57. var jsValues = new JsValue[n];
  58. for (uint i = 0; i < (uint) jsValues.Length; i++)
  59. {
  60. var jsValue = skipHoles && !HasProperty(i) ? JsValue.Undefined : Get(i);
  61. if ((jsValue.Type & elementTypes) == Types.Empty)
  62. {
  63. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  64. }
  65. jsValues[writeIndex++] = jsValue;
  66. }
  67. return jsValues;
  68. }
  69. public abstract bool TryGetValue(ulong index, out JsValue value);
  70. public abstract bool HasProperty(ulong index);
  71. public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);
  72. public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);
  73. public abstract void DeletePropertyOrThrow(ulong index);
  74. public ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);
  75. IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);
  76. IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);
  77. internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
  78. {
  79. private readonly ArrayOperations _obj;
  80. private ulong _current;
  81. private bool _initialized;
  82. private readonly uint _length;
  83. public ArrayLikeIterator(ArrayOperations obj)
  84. {
  85. _obj = obj;
  86. _length = obj.GetLength();
  87. Reset();
  88. }
  89. public JsValue Current
  90. {
  91. get
  92. {
  93. return !_initialized
  94. ? JsValue.Undefined
  95. : _obj.TryGetValue(_current, out var temp) ? temp : JsValue.Undefined;
  96. }
  97. }
  98. object? IEnumerator.Current => Current;
  99. public void Dispose()
  100. {
  101. }
  102. public bool MoveNext()
  103. {
  104. if (!_initialized)
  105. {
  106. _initialized = true;
  107. }
  108. else
  109. {
  110. _current++;
  111. }
  112. return _current < _length;
  113. }
  114. public void Reset()
  115. {
  116. _initialized = false;
  117. _current = 0;
  118. }
  119. }
  120. private sealed class ObjectOperations : ArrayOperations<ObjectInstance>
  121. {
  122. public ObjectOperations(ObjectInstance target) : base(target)
  123. {
  124. }
  125. private double GetIntegerLength()
  126. {
  127. var descValue = _target.Get(CommonProperties.Length);
  128. if (!ReferenceEquals(descValue, null))
  129. {
  130. return TypeConverter.ToInteger(descValue);
  131. }
  132. return 0;
  133. }
  134. public override ulong GetSmallestIndex(ulong length)
  135. {
  136. return _target.GetSmallestIndex(length);
  137. }
  138. public override uint GetLength()
  139. {
  140. var integerLength = GetIntegerLength();
  141. return (uint) (integerLength >= 0 ? integerLength : 0);
  142. }
  143. public override ulong GetLongLength()
  144. {
  145. var integerLength = GetIntegerLength();
  146. if (integerLength <= 0)
  147. {
  148. return 0;
  149. }
  150. return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
  151. }
  152. public override void SetLength(ulong length)
  153. => _target.Set(CommonProperties.Length, length, true);
  154. public override void EnsureCapacity(ulong capacity)
  155. {
  156. }
  157. public override JsValue Get(ulong index)
  158. => _target.Get(JsString.Create(index));
  159. public override bool TryGetValue(ulong index, out JsValue value)
  160. {
  161. var propertyName = JsString.Create(index);
  162. var kPresent = _target.HasProperty(propertyName);
  163. value = kPresent ? _target.Get(propertyName) : JsValue.Undefined;
  164. return kPresent;
  165. }
  166. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  167. => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
  168. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  169. => _target.Set(JsString.Create(index), value, throwOnError);
  170. public override void DeletePropertyOrThrow(ulong index)
  171. => _target.DeletePropertyOrThrow(JsString.Create(index));
  172. public override bool HasProperty(ulong index) => Target.HasProperty(index);
  173. }
  174. private sealed class JsArrayOperations : ArrayOperations<JsArray>
  175. {
  176. public JsArrayOperations(JsArray target) : base(target)
  177. {
  178. }
  179. public override ulong GetSmallestIndex(ulong length)
  180. => _target.GetSmallestIndex();
  181. public override uint GetLength()
  182. => (uint) ((JsNumber) _target._length!._value!)._value;
  183. public override ulong GetLongLength()
  184. => (ulong) ((JsNumber) _target._length!._value!)._value;
  185. public override void SetLength(ulong length)
  186. => _target.SetLength(length);
  187. public override void EnsureCapacity(ulong capacity)
  188. => _target.EnsureCapacity((uint) capacity);
  189. public override bool TryGetValue(ulong index, out JsValue value)
  190. // array max size is uint
  191. => _target.TryGetValue((uint) index, out value);
  192. public override JsValue Get(ulong index) => _target.Get((uint) index);
  193. public override JsValue[] GetAll(Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object, bool skipHoles = false)
  194. {
  195. var n = _target.GetLength();
  196. if (_target._dense == null || _target._dense.Length < n)
  197. {
  198. return base.GetAll(elementTypes);
  199. }
  200. // optimized
  201. uint writeIndex = 0;
  202. var jsValues = new JsValue[n];
  203. for (uint i = 0; i < (uint) jsValues.Length; i++)
  204. {
  205. var value = _target._dense[i];
  206. if (value is null)
  207. {
  208. value = _target.Prototype?.Get(i) ?? JsValue.Undefined;
  209. }
  210. if ((value.Type & elementTypes) == Types.Empty)
  211. {
  212. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  213. }
  214. jsValues[writeIndex++] = (JsValue?) value ?? JsValue.Undefined;
  215. }
  216. return jsValues;
  217. }
  218. public override void DeletePropertyOrThrow(ulong index)
  219. => _target.DeletePropertyOrThrow((uint) index);
  220. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  221. => _target.SetIndexValue((uint) index, value, updateLength: false);
  222. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  223. => _target.SetIndexValue((uint) index, value, updateLength);
  224. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  225. }
  226. private sealed class JsTypedArrayOperations : ArrayOperations
  227. {
  228. private readonly JsTypedArray _target;
  229. public JsTypedArrayOperations(JsTypedArray target)
  230. {
  231. _target = target;
  232. }
  233. public override ObjectInstance Target => _target;
  234. public override ulong GetSmallestIndex(ulong length) => 0;
  235. public override uint GetLength()
  236. {
  237. if (!_target.IsConcatSpreadable)
  238. {
  239. return _target.GetLength();
  240. }
  241. var descValue = _target.Get(CommonProperties.Length);
  242. if (!ReferenceEquals(descValue, null))
  243. {
  244. return (uint) TypeConverter.ToInteger(descValue);
  245. }
  246. return 0;
  247. }
  248. public override ulong GetLongLength() => GetLength();
  249. public override void SetLength(ulong length)
  250. {
  251. }
  252. public override void EnsureCapacity(ulong capacity)
  253. {
  254. }
  255. public override JsValue Get(ulong index) => _target[(int) index];
  256. public override bool TryGetValue(ulong index, out JsValue value)
  257. {
  258. if (index < _target.GetLength())
  259. {
  260. value = _target[(int) index];
  261. return true;
  262. }
  263. value = JsValue.Undefined;
  264. return false;
  265. }
  266. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  267. => _target.CreateDataPropertyOrThrow(index, value);
  268. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  269. => _target[(int) index] = value;
  270. public override void DeletePropertyOrThrow(ulong index)
  271. => _target.DeletePropertyOrThrow(index);
  272. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  273. }
  274. private sealed class JsStringOperations : ArrayOperations
  275. {
  276. private readonly Realm _realm;
  277. private readonly JsString _target;
  278. private ObjectInstance? _wrappedTarget;
  279. public JsStringOperations(Realm realm, JsString target)
  280. {
  281. _realm = realm;
  282. _target = target;
  283. }
  284. public JsStringOperations(Realm realm, StringInstance stringInstance) : this(realm, stringInstance.StringData)
  285. {
  286. _wrappedTarget = stringInstance;
  287. }
  288. public override ObjectInstance Target => _wrappedTarget ??= _realm.Intrinsics.String.Construct(_target);
  289. public override ulong GetSmallestIndex(ulong length) => 0;
  290. public override uint GetLength() => (uint) _target.Length;
  291. public override ulong GetLongLength() => GetLength();
  292. public override void SetLength(ulong length) => throw new NotSupportedException();
  293. public override void EnsureCapacity(ulong capacity)
  294. {
  295. }
  296. public override JsValue Get(ulong index) => index < (ulong) _target.Length ? _target[(int) index] : JsValue.Undefined;
  297. public override bool TryGetValue(ulong index, out JsValue value)
  298. {
  299. if (index < (ulong) _target.Length)
  300. {
  301. value = _target[(int) index];
  302. return true;
  303. }
  304. value = JsValue.Undefined;
  305. return false;
  306. }
  307. public override bool HasProperty(ulong index) => index < (ulong) _target.Length;
  308. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  309. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  310. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  311. }
  312. private sealed class ArrayReadOperations : ArrayOperations
  313. {
  314. private readonly JsArray _target;
  315. private readonly JsValue?[] _data;
  316. private readonly uint _length;
  317. public ArrayReadOperations(JsArray target)
  318. {
  319. _target = target;
  320. _data = target._dense ?? System.Array.Empty<JsValue>();
  321. _length = target.Length;
  322. }
  323. public override ObjectInstance Target => _target;
  324. public override ulong GetSmallestIndex(ulong length) => 0;
  325. public override uint GetLength() => _length;
  326. public override ulong GetLongLength() => _length;
  327. public override void SetLength(ulong length) => throw new NotSupportedException();
  328. public override void EnsureCapacity(ulong capacity)
  329. {
  330. }
  331. public override JsValue Get(ulong index) => (index < (ulong) _data.Length ? _data[(int) index] : JsValue.Undefined) ?? JsValue.Undefined;
  332. public override bool TryGetValue(ulong index, out JsValue value)
  333. {
  334. if (index < _length)
  335. {
  336. value = _data[(int) index]!;
  337. return value is not null;
  338. }
  339. value = JsValue.Undefined;
  340. return false;
  341. }
  342. public override bool HasProperty(ulong index) => index < _length && _data[index] is not null;
  343. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  344. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  345. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  346. }
  347. }
  348. /// <summary>
  349. /// Adapter to use optimized array operations when possible.
  350. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  351. /// </summary>
  352. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  353. {
  354. protected readonly T _target;
  355. protected ArrayOperations(T target)
  356. {
  357. _target = target;
  358. }
  359. public override ObjectInstance Target => _target;
  360. }
  361. }