ArrayOperations.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. using System.Collections;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Native.String;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.Array;
  8. internal abstract class ArrayOperations : IEnumerable<JsValue>
  9. {
  10. protected internal const ulong MaxArrayLength = 4294967295;
  11. protected internal const ulong MaxArrayLikeLength = NumberConstructor.MaxSafeInteger;
  12. public static ArrayOperations For(Realm realm, JsValue value, bool forWrite)
  13. {
  14. if (!forWrite)
  15. {
  16. if (value.IsString())
  17. {
  18. return new JsStringOperations(realm, (JsString) value);
  19. }
  20. if (value is StringInstance stringInstance)
  21. {
  22. return new JsStringOperations(realm, stringInstance);
  23. }
  24. if (value is JsArray { CanUseFastAccess: true } array && array.Length <= (array._dense?.Length ?? -1))
  25. {
  26. return new ArrayReadOperations(array);
  27. }
  28. }
  29. return For(TypeConverter.ToObject(realm, value), forWrite);
  30. }
  31. public static ArrayOperations For(ObjectInstance instance, bool forWrite)
  32. {
  33. if (instance is JsArray { CanUseFastAccess: true } arrayInstance)
  34. {
  35. return new JsArrayOperations(arrayInstance);
  36. }
  37. if (instance is JsTypedArray typedArrayInstance)
  38. {
  39. return new JsTypedArrayOperations(typedArrayInstance);
  40. }
  41. if (instance is ArrayLikeWrapper arrayWrapper)
  42. {
  43. return new ArrayLikeOperations(arrayWrapper);
  44. }
  45. if (instance is ObjectWrapper wrapper)
  46. {
  47. var descriptor = wrapper._typeDescriptor;
  48. if (descriptor.IsArrayLike && wrapper.Target is ICollection)
  49. {
  50. return new IndexWrappedOperations(wrapper);
  51. }
  52. }
  53. return new ObjectOperations(instance);
  54. }
  55. public abstract ObjectInstance Target { get; }
  56. public abstract ulong GetSmallestIndex(ulong length);
  57. public abstract uint GetLength();
  58. public abstract ulong GetLongLength();
  59. public abstract void SetLength(ulong length);
  60. public abstract void EnsureCapacity(ulong capacity);
  61. public abstract JsValue Get(ulong index);
  62. public virtual JsValue[] GetAll(
  63. Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object,
  64. bool skipHoles = false)
  65. {
  66. uint writeIndex = 0;
  67. var n = (int) GetLength();
  68. var jsValues = new JsValue[n];
  69. for (uint i = 0; i < (uint) jsValues.Length; i++)
  70. {
  71. var jsValue = skipHoles && !HasProperty(i) ? JsValue.Undefined : Get(i);
  72. if ((jsValue.Type & elementTypes) == Types.Empty)
  73. {
  74. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  75. }
  76. jsValues[writeIndex++] = jsValue;
  77. }
  78. return jsValues;
  79. }
  80. public abstract bool TryGetValue(ulong index, out JsValue value);
  81. public abstract bool HasProperty(ulong index);
  82. public abstract void CreateDataPropertyOrThrow(ulong index, JsValue value);
  83. public abstract void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true);
  84. public abstract void DeletePropertyOrThrow(ulong index);
  85. public ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);
  86. IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);
  87. IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);
  88. internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
  89. {
  90. private readonly ArrayOperations _obj;
  91. private ulong _current;
  92. private bool _initialized;
  93. private readonly uint _length;
  94. public ArrayLikeIterator(ArrayOperations obj)
  95. {
  96. _obj = obj;
  97. _length = obj.GetLength();
  98. Reset();
  99. }
  100. public JsValue Current
  101. {
  102. get
  103. {
  104. return !_initialized
  105. ? JsValue.Undefined
  106. : _obj.TryGetValue(_current, out var temp)
  107. ? temp
  108. : JsValue.Undefined;
  109. }
  110. }
  111. object? IEnumerator.Current => Current;
  112. public void Dispose()
  113. {
  114. }
  115. public bool MoveNext()
  116. {
  117. if (!_initialized)
  118. {
  119. _initialized = true;
  120. }
  121. else
  122. {
  123. _current++;
  124. }
  125. return _current < _length;
  126. }
  127. public void Reset()
  128. {
  129. _initialized = false;
  130. _current = 0;
  131. }
  132. }
  133. private sealed class ObjectOperations : ArrayOperations<ObjectInstance>
  134. {
  135. public ObjectOperations(ObjectInstance target) : base(target)
  136. {
  137. }
  138. private double GetIntegerLength()
  139. {
  140. var descValue = _target.Get(CommonProperties.Length);
  141. if (descValue is not null)
  142. {
  143. return TypeConverter.ToInteger(descValue);
  144. }
  145. return 0;
  146. }
  147. public override ulong GetSmallestIndex(ulong length)
  148. {
  149. return _target.GetSmallestIndex(length);
  150. }
  151. public override uint GetLength()
  152. {
  153. var integerLength = GetIntegerLength();
  154. return (uint) (integerLength >= 0 ? integerLength : 0);
  155. }
  156. public override ulong GetLongLength()
  157. {
  158. var integerLength = GetIntegerLength();
  159. if (integerLength <= 0)
  160. {
  161. return 0;
  162. }
  163. return (ulong) System.Math.Min(integerLength, MaxArrayLikeLength);
  164. }
  165. public override void SetLength(ulong length)
  166. => _target.Set(CommonProperties.Length, length, true);
  167. public override void EnsureCapacity(ulong capacity)
  168. {
  169. }
  170. public override JsValue Get(ulong index)
  171. => _target.Get(JsString.Create(index));
  172. public override bool TryGetValue(ulong index, out JsValue value)
  173. {
  174. var propertyName = JsString.Create(index);
  175. var kPresent = _target.HasProperty(propertyName);
  176. value = kPresent ? _target.Get(propertyName) : JsValue.Undefined;
  177. return kPresent;
  178. }
  179. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  180. => _target.CreateDataPropertyOrThrow(JsString.Create(index), value);
  181. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  182. => _target.Set(JsString.Create(index), value, throwOnError);
  183. public override void DeletePropertyOrThrow(ulong index)
  184. => _target.DeletePropertyOrThrow(JsString.Create(index));
  185. public override bool HasProperty(ulong index) => Target.HasProperty(index);
  186. }
  187. private sealed class JsArrayOperations : ArrayOperations<JsArray>
  188. {
  189. public JsArrayOperations(JsArray target) : base(target)
  190. {
  191. }
  192. public override ulong GetSmallestIndex(ulong length)
  193. => _target.GetSmallestIndex();
  194. public override uint GetLength()
  195. => (uint) ((JsNumber) _target._length!._value!)._value;
  196. public override ulong GetLongLength()
  197. => (ulong) ((JsNumber) _target._length!._value!)._value;
  198. public override void SetLength(ulong length)
  199. => _target.SetLength(length);
  200. public override void EnsureCapacity(ulong capacity)
  201. => _target.EnsureCapacity((uint) capacity);
  202. public override bool TryGetValue(ulong index, out JsValue value)
  203. // array max size is uint
  204. => _target.TryGetValue((uint) index, out value);
  205. public override JsValue Get(ulong index) => _target.Get((uint) index);
  206. public override JsValue[] GetAll(Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object, bool skipHoles = false)
  207. {
  208. var n = _target.GetLength();
  209. if (_target._dense == null || _target._dense.Length < n)
  210. {
  211. return base.GetAll(elementTypes);
  212. }
  213. // optimized
  214. uint writeIndex = 0;
  215. var jsValues = new JsValue[n];
  216. for (uint i = 0; i < (uint) jsValues.Length; i++)
  217. {
  218. var value = _target._dense[i];
  219. if (value is null)
  220. {
  221. value = _target.Prototype?.Get(i) ?? JsValue.Undefined;
  222. }
  223. if ((value.Type & elementTypes) == Types.Empty)
  224. {
  225. ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
  226. }
  227. jsValues[writeIndex++] = (JsValue?) value ?? JsValue.Undefined;
  228. }
  229. return jsValues;
  230. }
  231. public override void DeletePropertyOrThrow(ulong index)
  232. => _target.DeletePropertyOrThrow((uint) index);
  233. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  234. => _target.SetIndexValue((uint) index, value, updateLength: false);
  235. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  236. => _target.SetIndexValue((uint) index, value, updateLength);
  237. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  238. }
  239. private sealed class JsTypedArrayOperations : ArrayOperations
  240. {
  241. private readonly JsTypedArray _target;
  242. public JsTypedArrayOperations(JsTypedArray target)
  243. {
  244. _target = target;
  245. }
  246. public override ObjectInstance Target => _target;
  247. public override ulong GetSmallestIndex(ulong length) => 0;
  248. public override uint GetLength()
  249. {
  250. if (!_target.IsConcatSpreadable)
  251. {
  252. return _target.GetLength();
  253. }
  254. var descValue = _target.Get(CommonProperties.Length);
  255. if (descValue is not null)
  256. {
  257. return (uint) TypeConverter.ToInteger(descValue);
  258. }
  259. return 0;
  260. }
  261. public override ulong GetLongLength() => GetLength();
  262. public override void SetLength(ulong length)
  263. {
  264. }
  265. public override void EnsureCapacity(ulong capacity)
  266. {
  267. }
  268. public override JsValue Get(ulong index) => _target[(int) index];
  269. public override bool TryGetValue(ulong index, out JsValue value)
  270. {
  271. if (_target.IsValidIntegerIndex(index))
  272. {
  273. value = _target[(int) index];
  274. return true;
  275. }
  276. value = JsValue.Undefined;
  277. return false;
  278. }
  279. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  280. => _target.CreateDataPropertyOrThrow(index, value);
  281. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  282. => _target[(int) index] = value;
  283. public override void DeletePropertyOrThrow(ulong index)
  284. => _target.DeletePropertyOrThrow(index);
  285. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  286. }
  287. private sealed class JsStringOperations : ArrayOperations
  288. {
  289. private readonly Realm _realm;
  290. private readonly JsString _target;
  291. private ObjectInstance? _wrappedTarget;
  292. public JsStringOperations(Realm realm, JsString target)
  293. {
  294. _realm = realm;
  295. _target = target;
  296. }
  297. public JsStringOperations(Realm realm, StringInstance stringInstance) : this(realm, stringInstance.StringData)
  298. {
  299. _wrappedTarget = stringInstance;
  300. }
  301. public override ObjectInstance Target => _wrappedTarget ??= _realm.Intrinsics.String.Construct(_target);
  302. public override ulong GetSmallestIndex(ulong length) => 0;
  303. public override uint GetLength() => (uint) _target.Length;
  304. public override ulong GetLongLength() => GetLength();
  305. public override void SetLength(ulong length) => throw new NotSupportedException();
  306. public override void EnsureCapacity(ulong capacity)
  307. {
  308. }
  309. public override JsValue Get(ulong index) => index < (ulong) _target.Length ? _target[(int) index] : JsValue.Undefined;
  310. public override bool TryGetValue(ulong index, out JsValue value)
  311. {
  312. if (index < (ulong) _target.Length)
  313. {
  314. value = _target[(int) index];
  315. return true;
  316. }
  317. value = JsValue.Undefined;
  318. return false;
  319. }
  320. public override bool HasProperty(ulong index) => index < (ulong) _target.Length;
  321. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  322. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  323. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  324. }
  325. private sealed class ArrayReadOperations : ArrayOperations
  326. {
  327. private readonly JsArray _target;
  328. private readonly JsValue?[] _data;
  329. private readonly uint _length;
  330. public ArrayReadOperations(JsArray target)
  331. {
  332. _target = target;
  333. _data = target._dense ?? System.Array.Empty<JsValue>();
  334. _length = target.Length;
  335. }
  336. public override ObjectInstance Target => _target;
  337. public override ulong GetSmallestIndex(ulong length) => 0;
  338. public override uint GetLength() => _length;
  339. public override ulong GetLongLength() => _length;
  340. public override void SetLength(ulong length) => throw new NotSupportedException();
  341. public override void EnsureCapacity(ulong capacity)
  342. {
  343. }
  344. public override JsValue Get(ulong index) => (index < (ulong) _data.Length ? _data[(int) index] : JsValue.Undefined) ?? JsValue.Undefined;
  345. public override bool TryGetValue(ulong index, out JsValue value)
  346. {
  347. if (index < _length)
  348. {
  349. value = _data[(int) index]!;
  350. return value is not null;
  351. }
  352. value = JsValue.Undefined;
  353. return false;
  354. }
  355. public override bool HasProperty(ulong index) => index < _length && _data[index] is not null;
  356. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  357. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  358. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  359. }
  360. private sealed class IndexWrappedOperations : ArrayOperations
  361. {
  362. private readonly ObjectWrapper _target;
  363. private readonly ICollection _collection;
  364. private readonly IList _list;
  365. public IndexWrappedOperations(ObjectWrapper wrapper)
  366. {
  367. _target = wrapper;
  368. _collection = (ICollection) wrapper.Target;
  369. _list = (IList) wrapper.Target;
  370. }
  371. public override ObjectInstance Target => _target;
  372. public override ulong GetSmallestIndex(ulong length) => 0;
  373. public override uint GetLength() => (uint) _collection.Count;
  374. public override ulong GetLongLength() => GetLength();
  375. public override void SetLength(ulong length)
  376. {
  377. if (_list == null)
  378. {
  379. throw new NotSupportedException();
  380. }
  381. while (_list.Count > (int) length)
  382. {
  383. // shrink list to fit
  384. _list.RemoveAt(_list.Count - 1);
  385. }
  386. while (_list.Count < (int) length)
  387. {
  388. // expand list to fit
  389. _list.Add(null);
  390. }
  391. }
  392. public override void EnsureCapacity(ulong capacity)
  393. {
  394. }
  395. public override JsValue Get(ulong index) => index < (ulong) _collection.Count ? ReadValue((int) index) : JsValue.Undefined;
  396. public override bool TryGetValue(ulong index, out JsValue value)
  397. {
  398. if (index < (ulong) _collection.Count)
  399. {
  400. value = ReadValue((int) index);
  401. return true;
  402. }
  403. value = JsValue.Undefined;
  404. return false;
  405. }
  406. private JsValue ReadValue(int index)
  407. {
  408. if (_list is not null)
  409. {
  410. return (uint) index < _list.Count ? JsValue.FromObject(_target.Engine, _list[index]) : JsValue.Undefined;
  411. }
  412. // via reflection is slow, but better than nothing
  413. return JsValue.FromObject(_target.Engine, _target._typeDescriptor.IntegerIndexerProperty!.GetValue(Target, [index]));
  414. }
  415. public override bool HasProperty(ulong index) => index < (ulong) _collection.Count;
  416. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  417. => _target.CreateDataPropertyOrThrow(index, value);
  418. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  419. {
  420. if (updateLength && _list != null && index >= (ulong) _list.Count)
  421. {
  422. SetLength(index + 1);
  423. }
  424. _target[(int) index] = value;
  425. }
  426. public override void DeletePropertyOrThrow(ulong index)
  427. => _target.DeletePropertyOrThrow(index);
  428. }
  429. private sealed class ArrayLikeOperations : ArrayOperations
  430. {
  431. private readonly ArrayLikeWrapper _target;
  432. public ArrayLikeOperations(ArrayLikeWrapper wrapper)
  433. {
  434. _target = wrapper;
  435. }
  436. public override ObjectInstance Target => _target;
  437. public override ulong GetSmallestIndex(ulong length) => 0;
  438. public override uint GetLength() => (uint) _target.Length;
  439. public override ulong GetLongLength() => GetLength();
  440. public override void SetLength(ulong length)
  441. {
  442. while (_target.Length > (int) length)
  443. {
  444. // shrink list to fit
  445. _target.RemoveAt(_target.Length - 1);
  446. }
  447. while (_target.Length < (int) length)
  448. {
  449. // expand list to fit
  450. _target.AddDefault();
  451. }
  452. }
  453. public override void EnsureCapacity(ulong capacity)
  454. {
  455. _target.EnsureCapacity((int)capacity);
  456. }
  457. public override JsValue Get(ulong index) => index < (ulong) _target.Length ? ReadValue((int) index) : JsValue.Undefined;
  458. public override bool TryGetValue(ulong index, out JsValue value)
  459. {
  460. if (index < (ulong) _target.Length)
  461. {
  462. value = ReadValue((int) index);
  463. return true;
  464. }
  465. value = JsValue.Undefined;
  466. return false;
  467. }
  468. private JsValue ReadValue(int index)
  469. {
  470. return (uint) index < _target.Length ? JsValue.FromObject(_target.Engine, _target.GetAt(index)) : JsValue.Undefined;
  471. }
  472. public override bool HasProperty(ulong index) => index < (ulong) _target.Length;
  473. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  474. => _target.CreateDataPropertyOrThrow(index, value);
  475. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  476. {
  477. _target.SetAt((int)index, value);
  478. }
  479. public override void DeletePropertyOrThrow(ulong index)
  480. => _target.DeletePropertyOrThrow(index);
  481. }
  482. }
  483. /// <summary>
  484. /// Adapter to use optimized array operations when possible.
  485. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  486. /// </summary>
  487. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  488. {
  489. protected readonly T _target;
  490. protected ArrayOperations(T target)
  491. {
  492. _target = target;
  493. }
  494. public override ObjectInstance Target => _target;
  495. }