ArrayOperations.cs 20 KB

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