ArrayOperations.cs 16 KB

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