ArrayOperations.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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.BigInt | 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. Throw.TypeErrorNoEngine("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(
  207. Types elementTypes = Types.Empty | Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Number | Types.Symbol | Types.BigInt | Types.Object,
  208. bool skipHoles = false)
  209. {
  210. var n = _target.GetLength();
  211. if (_target._dense == null || _target._dense.Length < n)
  212. {
  213. return base.GetAll(elementTypes, skipHoles);
  214. }
  215. // optimized
  216. uint writeIndex = 0;
  217. var jsValues = new JsValue[n];
  218. for (uint i = 0; i < (uint) jsValues.Length; i++)
  219. {
  220. var value = _target._dense[i];
  221. if (value is null)
  222. {
  223. value = _target.Prototype?.Get(i) ?? JsValue.Undefined;
  224. }
  225. if ((value.Type & elementTypes) == Types.Empty)
  226. {
  227. Throw.TypeErrorNoEngine("invalid type");
  228. }
  229. jsValues[writeIndex++] = (JsValue?) value ?? JsValue.Undefined;
  230. }
  231. return jsValues;
  232. }
  233. public override void DeletePropertyOrThrow(ulong index)
  234. => _target.DeletePropertyOrThrow((uint) index);
  235. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  236. => _target.SetIndexValue((uint) index, value, updateLength: false);
  237. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  238. => _target.SetIndexValue((uint) index, value, updateLength);
  239. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  240. }
  241. private sealed class JsTypedArrayOperations : ArrayOperations
  242. {
  243. private readonly JsTypedArray _target;
  244. public JsTypedArrayOperations(JsTypedArray target)
  245. {
  246. _target = target;
  247. }
  248. public override ObjectInstance Target => _target;
  249. public override ulong GetSmallestIndex(ulong length) => 0;
  250. public override uint GetLength()
  251. {
  252. if (!_target.IsConcatSpreadable)
  253. {
  254. return _target.GetLength();
  255. }
  256. var descValue = _target.Get(CommonProperties.Length);
  257. if (descValue is not null)
  258. {
  259. return (uint) TypeConverter.ToInteger(descValue);
  260. }
  261. return 0;
  262. }
  263. public override ulong GetLongLength() => GetLength();
  264. public override void SetLength(ulong length)
  265. {
  266. }
  267. public override void EnsureCapacity(ulong capacity)
  268. {
  269. }
  270. public override JsValue Get(ulong index) => _target[(int) index];
  271. public override bool TryGetValue(ulong index, out JsValue value)
  272. {
  273. if (_target.IsValidIntegerIndex(index))
  274. {
  275. value = _target[(int) index];
  276. return true;
  277. }
  278. value = JsValue.Undefined;
  279. return false;
  280. }
  281. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  282. => _target.CreateDataPropertyOrThrow(index, value);
  283. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  284. => _target[(int) index] = value;
  285. public override void DeletePropertyOrThrow(ulong index)
  286. => _target.DeletePropertyOrThrow(index);
  287. public override bool HasProperty(ulong index) => _target.HasProperty(index);
  288. }
  289. private sealed class JsStringOperations : ArrayOperations
  290. {
  291. private readonly Realm _realm;
  292. private readonly JsString _target;
  293. private ObjectInstance? _wrappedTarget;
  294. public JsStringOperations(Realm realm, JsString target)
  295. {
  296. _realm = realm;
  297. _target = target;
  298. }
  299. public JsStringOperations(Realm realm, StringInstance stringInstance) : this(realm, stringInstance.StringData)
  300. {
  301. _wrappedTarget = stringInstance;
  302. }
  303. public override ObjectInstance Target => _wrappedTarget ??= _realm.Intrinsics.String.Construct(_target);
  304. public override ulong GetSmallestIndex(ulong length) => 0;
  305. public override uint GetLength() => (uint) _target.Length;
  306. public override ulong GetLongLength() => GetLength();
  307. public override void SetLength(ulong length) => throw new NotSupportedException();
  308. public override void EnsureCapacity(ulong capacity)
  309. {
  310. }
  311. public override JsValue Get(ulong index) => index < (ulong) _target.Length ? _target[(int) index] : JsValue.Undefined;
  312. public override bool TryGetValue(ulong index, out JsValue value)
  313. {
  314. if (index < (ulong) _target.Length)
  315. {
  316. value = _target[(int) index];
  317. return true;
  318. }
  319. value = JsValue.Undefined;
  320. return false;
  321. }
  322. public override bool HasProperty(ulong index) => index < (ulong) _target.Length;
  323. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  324. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  325. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  326. }
  327. private sealed class ArrayReadOperations : ArrayOperations
  328. {
  329. private readonly JsArray _target;
  330. private readonly JsValue?[] _data;
  331. private readonly uint _length;
  332. public ArrayReadOperations(JsArray target)
  333. {
  334. _target = target;
  335. _data = target._dense ?? [];
  336. _length = target.Length;
  337. }
  338. public override ObjectInstance Target => _target;
  339. public override ulong GetSmallestIndex(ulong length) => 0;
  340. public override uint GetLength() => _length;
  341. public override ulong GetLongLength() => _length;
  342. public override void SetLength(ulong length) => throw new NotSupportedException();
  343. public override void EnsureCapacity(ulong capacity)
  344. {
  345. }
  346. public override JsValue Get(ulong index) => (index < (ulong) _data.Length ? _data[(int) index] : JsValue.Undefined) ?? JsValue.Undefined;
  347. public override bool TryGetValue(ulong index, out JsValue value)
  348. {
  349. if (index < _length)
  350. {
  351. value = _data[(int) index]!;
  352. return value is not null;
  353. }
  354. value = JsValue.Undefined;
  355. return false;
  356. }
  357. public override bool HasProperty(ulong index) => index < _length && _data[index] is not null;
  358. public override void CreateDataPropertyOrThrow(ulong index, JsValue value) => throw new NotSupportedException();
  359. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) => throw new NotSupportedException();
  360. public override void DeletePropertyOrThrow(ulong index) => throw new NotSupportedException();
  361. }
  362. private sealed class IndexWrappedOperations : ArrayOperations
  363. {
  364. private readonly ObjectWrapper _target;
  365. private readonly ICollection _collection;
  366. private readonly IList _list;
  367. public IndexWrappedOperations(ObjectWrapper wrapper)
  368. {
  369. _target = wrapper;
  370. _collection = (ICollection) wrapper.Target;
  371. _list = (IList) wrapper.Target;
  372. }
  373. public override ObjectInstance Target => _target;
  374. public override ulong GetSmallestIndex(ulong length) => 0;
  375. public override uint GetLength() => (uint) _collection.Count;
  376. public override ulong GetLongLength() => GetLength();
  377. public override void SetLength(ulong length)
  378. {
  379. if (_list == null)
  380. {
  381. throw new NotSupportedException();
  382. }
  383. while (_list.Count > (int) length)
  384. {
  385. // shrink list to fit
  386. _list.RemoveAt(_list.Count - 1);
  387. }
  388. while (_list.Count < (int) length)
  389. {
  390. // expand list to fit
  391. _list.Add(null);
  392. }
  393. }
  394. public override void EnsureCapacity(ulong capacity)
  395. {
  396. }
  397. public override JsValue Get(ulong index) => index < (ulong) _collection.Count ? ReadValue((int) index) : JsValue.Undefined;
  398. public override bool TryGetValue(ulong index, out JsValue value)
  399. {
  400. if (index < (ulong) _collection.Count)
  401. {
  402. value = ReadValue((int) index);
  403. return true;
  404. }
  405. value = JsValue.Undefined;
  406. return false;
  407. }
  408. private JsValue ReadValue(int index)
  409. {
  410. if (_list is not null)
  411. {
  412. return (uint) index < _list.Count ? JsValue.FromObject(_target.Engine, _list[index]) : JsValue.Undefined;
  413. }
  414. // via reflection is slow, but better than nothing
  415. return JsValue.FromObject(_target.Engine, _target._typeDescriptor.IntegerIndexerProperty!.GetValue(Target, [index]));
  416. }
  417. public override bool HasProperty(ulong index) => index < (ulong) _collection.Count;
  418. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  419. => _target.CreateDataPropertyOrThrow(index, value);
  420. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  421. {
  422. if (updateLength && _list != null && index >= (ulong) _list.Count)
  423. {
  424. SetLength(index + 1);
  425. }
  426. _target[(int) index] = value;
  427. }
  428. public override void DeletePropertyOrThrow(ulong index)
  429. => _target.DeletePropertyOrThrow(index);
  430. }
  431. private sealed class ArrayLikeOperations : ArrayOperations
  432. {
  433. private readonly ArrayLikeWrapper _target;
  434. public ArrayLikeOperations(ArrayLikeWrapper wrapper)
  435. {
  436. _target = wrapper;
  437. }
  438. public override ObjectInstance Target => _target;
  439. public override ulong GetSmallestIndex(ulong length) => 0;
  440. public override uint GetLength() => (uint) _target.Length;
  441. public override ulong GetLongLength() => GetLength();
  442. public override void SetLength(ulong length)
  443. {
  444. while (_target.Length > (int) length)
  445. {
  446. // shrink list to fit
  447. _target.RemoveAt(_target.Length - 1);
  448. }
  449. while (_target.Length < (int) length)
  450. {
  451. // expand list to fit
  452. _target.AddDefault();
  453. }
  454. }
  455. public override void EnsureCapacity(ulong capacity)
  456. {
  457. _target.EnsureCapacity((int) capacity);
  458. }
  459. public override JsValue Get(ulong index) => index < (ulong) _target.Length ? ReadValue((int) index) : JsValue.Undefined;
  460. public override bool TryGetValue(ulong index, out JsValue value)
  461. {
  462. if (index < (ulong) _target.Length)
  463. {
  464. value = ReadValue((int) index);
  465. return true;
  466. }
  467. value = JsValue.Undefined;
  468. return false;
  469. }
  470. private JsValue ReadValue(int index)
  471. {
  472. return (uint) index < _target.Length ? JsValue.FromObject(_target.Engine, _target.GetAt(index)) : JsValue.Undefined;
  473. }
  474. public override bool HasProperty(ulong index) => index < (ulong) _target.Length;
  475. public override void CreateDataPropertyOrThrow(ulong index, JsValue value)
  476. => _target.CreateDataPropertyOrThrow(index, value);
  477. public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true)
  478. {
  479. _target.SetAt((int) index, value);
  480. }
  481. public override void DeletePropertyOrThrow(ulong index)
  482. => _target.DeletePropertyOrThrow(index);
  483. }
  484. }
  485. /// <summary>
  486. /// Adapter to use optimized array operations when possible.
  487. /// Gaps the difference between ArgumentsInstance and ArrayInstance.
  488. /// </summary>
  489. internal abstract class ArrayOperations<T> : ArrayOperations where T : ObjectInstance
  490. {
  491. protected readonly T _target;
  492. protected ArrayOperations(T target)
  493. {
  494. _target = target;
  495. }
  496. public override ObjectInstance Target => _target;
  497. }