ArrayOperations.cs 22 KB

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