ArrayInstance.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. using System.Collections;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Native.Array
  9. {
  10. public class ArrayInstance : ObjectInstance, IEnumerable<JsValue>
  11. {
  12. internal PropertyDescriptor? _length;
  13. private const int MaxDenseArrayLength = 10_000_000;
  14. // we have dense and sparse, we usually can start with dense and fall back to sparse when necessary
  15. // when we have plain JsValues, _denseValues is used - if any operation occurs which requires setting more property flags
  16. // we convert to _sparse and _denseValues is set to null - it will be a slow array
  17. internal JsValue?[]? _dense;
  18. private Dictionary<uint, PropertyDescriptor?>? _sparse;
  19. private ObjectChangeFlags _objectChangeFlags;
  20. private protected ArrayInstance(Engine engine, InternalTypes type) : base(engine, type: type)
  21. {
  22. _dense = System.Array.Empty<JsValue?>();
  23. }
  24. private protected ArrayInstance(Engine engine, uint capacity = 0, uint length = 0) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  25. {
  26. InitializePrototypeAndValidateCapacity(engine, capacity);
  27. if (capacity < MaxDenseArrayLength)
  28. {
  29. _dense = capacity > 0 ? new JsValue?[capacity] : System.Array.Empty<JsValue?>();
  30. }
  31. else
  32. {
  33. _sparse = new Dictionary<uint, PropertyDescriptor?>(1024);
  34. }
  35. _length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
  36. }
  37. private protected ArrayInstance(Engine engine, JsValue[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  38. {
  39. InitializePrototypeAndValidateCapacity(engine, capacity: 0);
  40. _dense = items;
  41. _length = new PropertyDescriptor(items.Length, PropertyFlag.OnlyWritable);
  42. }
  43. private void InitializePrototypeAndValidateCapacity(Engine engine, uint capacity)
  44. {
  45. _prototype = engine.Realm.Intrinsics.Array.PrototypeObject;
  46. if (capacity > 0 && capacity > engine.Options.Constraints.MaxArraySize)
  47. {
  48. ThrowMaximumArraySizeReachedException(engine, capacity);
  49. }
  50. }
  51. public sealed override bool IsArrayLike => true;
  52. public sealed override bool IsArray() => true;
  53. internal sealed override bool HasOriginalIterator
  54. => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _engine.Realm.Intrinsics.Array.PrototypeObject._originalIteratorFunction);
  55. /// <summary>
  56. /// Checks whether there have been changes to object prototype chain which could render fast access patterns impossible.
  57. /// </summary>
  58. internal bool CanUseFastAccess
  59. {
  60. get
  61. {
  62. if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != 0)
  63. {
  64. // could be a mutating property for example, length might change, not safe anymore
  65. return false;
  66. }
  67. if (_prototype is not ArrayPrototype arrayPrototype
  68. || !ReferenceEquals(_prototype, _engine.Realm.Intrinsics.Array.PrototypeObject))
  69. {
  70. // somebody has switched prototype
  71. return false;
  72. }
  73. if ((arrayPrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) != 0)
  74. {
  75. // maybe somebody moved integer property to prototype? not safe anymore
  76. return false;
  77. }
  78. if (arrayPrototype.Prototype is not ObjectPrototype arrayPrototypePrototype
  79. || !ReferenceEquals(arrayPrototypePrototype, _engine.Realm.Intrinsics.Array.PrototypeObject.Prototype))
  80. {
  81. return false;
  82. }
  83. return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == 0;
  84. }
  85. }
  86. public sealed override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  87. {
  88. if (property == CommonProperties.Length)
  89. {
  90. return DefineLength(desc);
  91. }
  92. var isArrayIndex = IsArrayIndex(property, out var index);
  93. TrackChanges(property, desc, isArrayIndex);
  94. if (isArrayIndex)
  95. {
  96. ConvertToSparse();
  97. return DefineOwnProperty(index, desc);
  98. }
  99. return base.DefineOwnProperty(property, desc);
  100. }
  101. private bool DefineLength(PropertyDescriptor desc)
  102. {
  103. var value = desc.Value;
  104. if (ReferenceEquals(value, null))
  105. {
  106. return base.DefineOwnProperty(CommonProperties.Length, desc);
  107. }
  108. var newLenDesc = new PropertyDescriptor(desc);
  109. uint newLen = TypeConverter.ToUint32(value);
  110. if (newLen != TypeConverter.ToNumber(value))
  111. {
  112. ExceptionHelper.ThrowRangeError(_engine.Realm);
  113. }
  114. var oldLenDesc = _length;
  115. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc!.Value);
  116. newLenDesc.Value = newLen;
  117. if (newLen >= oldLen)
  118. {
  119. return base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  120. }
  121. if (!oldLenDesc.Writable)
  122. {
  123. return false;
  124. }
  125. bool newWritable;
  126. if (!newLenDesc.WritableSet || newLenDesc.Writable)
  127. {
  128. newWritable = true;
  129. }
  130. else
  131. {
  132. newWritable = false;
  133. newLenDesc.Writable = true;
  134. }
  135. var succeeded = base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  136. if (!succeeded)
  137. {
  138. return false;
  139. }
  140. var count = _dense?.Length ?? _sparse!.Count;
  141. if (count < oldLen - newLen)
  142. {
  143. if (_dense != null)
  144. {
  145. for (uint keyIndex = 0; keyIndex < _dense.Length; ++keyIndex)
  146. {
  147. if (_dense[keyIndex] == null)
  148. {
  149. continue;
  150. }
  151. // is it the index of the array
  152. if (keyIndex >= newLen && keyIndex < oldLen)
  153. {
  154. var deleteSucceeded = Delete(keyIndex);
  155. if (!deleteSucceeded)
  156. {
  157. newLenDesc.Value = keyIndex + 1;
  158. if (!newWritable)
  159. {
  160. newLenDesc.Writable = false;
  161. }
  162. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  163. return false;
  164. }
  165. }
  166. }
  167. }
  168. else
  169. {
  170. // in the case of sparse arrays, treat each concrete element instead of
  171. // iterating over all indexes
  172. var keys = new List<uint>(_sparse!.Keys);
  173. var keysCount = keys.Count;
  174. for (var i = 0; i < keysCount; i++)
  175. {
  176. var keyIndex = keys[i];
  177. // is it the index of the array
  178. if (keyIndex >= newLen && keyIndex < oldLen)
  179. {
  180. var deleteSucceeded = Delete(TypeConverter.ToString(keyIndex));
  181. if (!deleteSucceeded)
  182. {
  183. newLenDesc.Value = JsNumber.Create(keyIndex + 1);
  184. if (!newWritable)
  185. {
  186. newLenDesc.Writable = false;
  187. }
  188. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  189. return false;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. else
  196. {
  197. while (newLen < oldLen)
  198. {
  199. // algorithm as per the spec
  200. oldLen--;
  201. var deleteSucceeded = Delete(oldLen);
  202. if (!deleteSucceeded)
  203. {
  204. newLenDesc.Value = oldLen + 1;
  205. if (!newWritable)
  206. {
  207. newLenDesc.Writable = false;
  208. }
  209. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  210. return false;
  211. }
  212. }
  213. }
  214. if (!newWritable)
  215. {
  216. base.DefineOwnProperty(CommonProperties.Length, new PropertyDescriptor(value: null, PropertyFlag.WritableSet));
  217. }
  218. return true;
  219. }
  220. private bool DefineOwnProperty(uint index, PropertyDescriptor desc)
  221. {
  222. var oldLenDesc = _length;
  223. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc!.Value);
  224. if (index >= oldLen && !oldLenDesc.Writable)
  225. {
  226. return false;
  227. }
  228. var succeeded = base.DefineOwnProperty(index, desc);
  229. if (!succeeded)
  230. {
  231. return false;
  232. }
  233. if (index >= oldLen)
  234. {
  235. oldLenDesc.Value = index + 1;
  236. base.DefineOwnProperty(CommonProperties.Length, oldLenDesc);
  237. }
  238. return true;
  239. }
  240. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  241. internal uint GetLength()
  242. {
  243. if (_length is null)
  244. {
  245. return 0;
  246. }
  247. return (uint) ((JsNumber) _length._value!)._value;
  248. }
  249. protected sealed override void AddProperty(JsValue property, PropertyDescriptor descriptor)
  250. {
  251. if (property == CommonProperties.Length)
  252. {
  253. _length = descriptor;
  254. return;
  255. }
  256. base.AddProperty(property, descriptor);
  257. }
  258. protected sealed override bool TryGetProperty(JsValue property, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  259. {
  260. if (property == CommonProperties.Length)
  261. {
  262. descriptor = _length;
  263. return _length != null;
  264. }
  265. return base.TryGetProperty(property, out descriptor);
  266. }
  267. public sealed override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
  268. {
  269. if ((types & Types.String) == 0)
  270. {
  271. return base.GetOwnPropertyKeys(types);
  272. }
  273. var temp = _dense;
  274. var properties = new List<JsValue>(temp?.Length ?? 0 + 1);
  275. if (temp != null)
  276. {
  277. var length = System.Math.Min(temp.Length, GetLength());
  278. for (var i = 0; i < length; i++)
  279. {
  280. if (temp[i] != null)
  281. {
  282. properties.Add(JsString.Create(i));
  283. }
  284. }
  285. }
  286. else
  287. {
  288. foreach (var entry in _sparse!)
  289. {
  290. properties.Add(JsString.Create(entry.Key));
  291. }
  292. }
  293. if (_length != null)
  294. {
  295. properties.Add(CommonProperties.Length);
  296. }
  297. properties.AddRange(base.GetOwnPropertyKeys(types));
  298. return properties;
  299. }
  300. /// <summary>
  301. /// Returns key and value pairs for actual array entries, excludes parent and optionally length.
  302. /// </summary>
  303. /// <param name="includeLength">Whether to return length and it's value.</param>
  304. public IEnumerable<KeyValuePair<string, JsValue>> GetEntries(bool includeLength = false)
  305. {
  306. foreach (var (index, value) in this.Enumerate())
  307. {
  308. yield return new KeyValuePair<string, JsValue>(TypeConverter.ToString(index), value);
  309. }
  310. if (includeLength && _length != null)
  311. {
  312. yield return new KeyValuePair<string, JsValue>(CommonProperties.Length._value, _length.Value);
  313. }
  314. }
  315. public sealed override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  316. {
  317. var temp = _dense;
  318. if (temp != null)
  319. {
  320. var length = System.Math.Min(temp.Length, GetLength());
  321. for (uint i = 0; i < length; i++)
  322. {
  323. var value = temp[i];
  324. if (value != null)
  325. {
  326. if (_sparse is null || !_sparse.TryGetValue(i, out var descriptor) || descriptor is null)
  327. {
  328. _sparse ??= new Dictionary<uint, PropertyDescriptor?>();
  329. _sparse[i] = descriptor = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  330. }
  331. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(i), descriptor);
  332. }
  333. }
  334. }
  335. else if (_sparse != null)
  336. {
  337. foreach (var entry in _sparse)
  338. {
  339. var value = entry.Value;
  340. if (value is not null)
  341. {
  342. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(entry.Key), value);
  343. }
  344. }
  345. }
  346. if (_length != null)
  347. {
  348. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  349. }
  350. foreach (var entry in base.GetOwnProperties())
  351. {
  352. yield return entry;
  353. }
  354. }
  355. public sealed override PropertyDescriptor GetOwnProperty(JsValue property)
  356. {
  357. if (property == CommonProperties.Length)
  358. {
  359. return _length ?? PropertyDescriptor.Undefined;
  360. }
  361. if (IsArrayIndex(property, out var index))
  362. {
  363. if (TryGetDescriptor(index, createIfMissing: true, out var result))
  364. {
  365. return result;
  366. }
  367. return PropertyDescriptor.Undefined;
  368. }
  369. return base.GetOwnProperty(property);
  370. }
  371. internal JsValue Get(uint index)
  372. {
  373. if (!TryGetValue(index, out var value))
  374. {
  375. value = UnwrapJsValue(Prototype?.GetProperty(JsString.Create(index)) ?? PropertyDescriptor.Undefined);
  376. }
  377. return value;
  378. }
  379. public sealed override JsValue Get(JsValue property, JsValue receiver)
  380. {
  381. if (IsSafeSelfTarget(receiver) && IsArrayIndex(property, out var index) && TryGetValue(index, out var value))
  382. {
  383. return value;
  384. }
  385. if (property == CommonProperties.Length)
  386. {
  387. var length = _length?._value;
  388. if (length is not null)
  389. {
  390. return length;
  391. }
  392. }
  393. return base.Get(property, receiver);
  394. }
  395. public sealed override bool Set(JsValue property, JsValue value, JsValue receiver)
  396. {
  397. var isSafeSelfTarget = IsSafeSelfTarget(receiver);
  398. if (isSafeSelfTarget && CanUseFastAccess)
  399. {
  400. if (IsArrayIndex(property, out var index))
  401. {
  402. SetIndexValue(index, value, updateLength: true);
  403. return true;
  404. }
  405. if (property == CommonProperties.Length
  406. && _length is { Writable: true }
  407. && value is JsNumber jsNumber
  408. && jsNumber.IsInteger()
  409. && jsNumber._value <= MaxDenseArrayLength
  410. && jsNumber._value >= GetLength())
  411. {
  412. // we don't need explicit resize
  413. _length.Value = jsNumber;
  414. return true;
  415. }
  416. }
  417. // slow path
  418. return base.Set(property, value, receiver);
  419. }
  420. private bool IsSafeSelfTarget(JsValue receiver) => ReferenceEquals(receiver, this) && Extensible;
  421. public sealed override bool HasProperty(JsValue property)
  422. {
  423. if (IsArrayIndex(property, out var index) && GetValue(index, unwrapFromNonDataDescriptor: false) is not null)
  424. {
  425. return true;
  426. }
  427. return base.HasProperty(property);
  428. }
  429. internal bool HasProperty(ulong index)
  430. {
  431. if (index < uint.MaxValue)
  432. {
  433. var temp = _dense;
  434. if (temp != null)
  435. {
  436. if (index < (uint) temp.Length && temp[index] is not null)
  437. {
  438. return true;
  439. }
  440. }
  441. else if (_sparse!.ContainsKey((uint) index))
  442. {
  443. return true;
  444. }
  445. }
  446. return base.HasProperty(index);
  447. }
  448. protected internal sealed override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  449. {
  450. var isArrayIndex = IsArrayIndex(property, out var index);
  451. TrackChanges(property, desc, isArrayIndex);
  452. if (isArrayIndex)
  453. {
  454. WriteArrayValue(index, desc);
  455. }
  456. else if (property == CommonProperties.Length)
  457. {
  458. _length = desc;
  459. }
  460. else
  461. {
  462. base.SetOwnProperty(property, desc);
  463. }
  464. }
  465. private void TrackChanges(JsValue property, PropertyDescriptor desc, bool isArrayIndex)
  466. {
  467. EnsureInitialized();
  468. if (isArrayIndex)
  469. {
  470. if (!desc.IsDefaultArrayValueDescriptor() && desc.Flags != PropertyFlag.None)
  471. {
  472. _objectChangeFlags |= ObjectChangeFlags.NonDefaultDataDescriptorUsage;
  473. }
  474. if (GetType() != typeof(JsArray))
  475. {
  476. _objectChangeFlags |= ObjectChangeFlags.ArrayIndex;
  477. }
  478. }
  479. else
  480. {
  481. _objectChangeFlags |= property.IsSymbol() ? ObjectChangeFlags.Symbol : ObjectChangeFlags.Property;
  482. }
  483. }
  484. public sealed override void RemoveOwnProperty(JsValue p)
  485. {
  486. if (IsArrayIndex(p, out var index))
  487. {
  488. Delete(index);
  489. }
  490. if (p == CommonProperties.Length)
  491. {
  492. _length = null;
  493. }
  494. base.RemoveOwnProperty(p);
  495. }
  496. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  497. internal static bool IsArrayIndex(JsValue p, out uint index)
  498. {
  499. if (p is JsNumber number)
  500. {
  501. var value = number._value;
  502. var intValue = (uint) value;
  503. index = intValue;
  504. return value == intValue && intValue != uint.MaxValue;
  505. }
  506. index = ParseArrayIndex(p.ToString());
  507. return index != uint.MaxValue;
  508. // 15.4 - Use an optimized version of the specification
  509. // return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
  510. }
  511. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  512. internal static uint ParseArrayIndex(string p)
  513. {
  514. if (p.Length == 0 || p.Length > 1 && !IsInRange(p[0], '1', '9') || !uint.TryParse(p, out var d))
  515. {
  516. return uint.MaxValue;
  517. }
  518. return d;
  519. }
  520. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  521. private static bool IsInRange(char c, char min, char max) => c - (uint) min <= max - (uint) min;
  522. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  523. internal void SetIndexValue(uint index, JsValue? value, bool updateLength)
  524. {
  525. if (updateLength)
  526. {
  527. EnsureCorrectLength(index);
  528. }
  529. WriteArrayValue(index, value);
  530. }
  531. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  532. private void EnsureCorrectLength(uint index)
  533. {
  534. var length = GetLength();
  535. if (index >= length)
  536. {
  537. SetLength(index + 1);
  538. }
  539. }
  540. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  541. internal void SetLength(ulong length)
  542. {
  543. var number = JsNumber.Create(length);
  544. if (Extensible && _length!._flags == PropertyFlag.OnlyWritable)
  545. {
  546. _length!.Value = number;
  547. }
  548. else
  549. {
  550. // slow path
  551. Set(CommonProperties.Length, number, true);
  552. }
  553. }
  554. internal uint GetSmallestIndex()
  555. {
  556. if (_dense != null)
  557. {
  558. return 0;
  559. }
  560. uint smallest = 0;
  561. // only try to help if collection reasonable small
  562. if (_sparse!.Count > 0 && _sparse.Count < 100 && !_sparse.ContainsKey(0))
  563. {
  564. smallest = uint.MaxValue;
  565. foreach (var key in _sparse.Keys)
  566. {
  567. smallest = System.Math.Min(key, smallest);
  568. }
  569. }
  570. return smallest;
  571. }
  572. internal bool DeletePropertyOrThrow(uint index)
  573. {
  574. if (!Delete(index))
  575. {
  576. ExceptionHelper.ThrowTypeError(_engine.Realm);
  577. }
  578. return true;
  579. }
  580. internal bool Delete(uint index)
  581. {
  582. // check fast path
  583. var temp = _dense;
  584. if (temp != null)
  585. {
  586. if (index < (uint) temp.Length)
  587. {
  588. if (!TryGetDescriptor(index, createIfMissing: false, out var descriptor) || descriptor.Configurable)
  589. {
  590. temp[index] = null;
  591. return true;
  592. }
  593. }
  594. }
  595. if (!TryGetDescriptor(index, createIfMissing: false, out var desc))
  596. {
  597. return true;
  598. }
  599. if (desc.Configurable)
  600. {
  601. DeleteAt(index);
  602. return true;
  603. }
  604. return false;
  605. }
  606. internal bool DeleteAt(uint index)
  607. {
  608. var temp = _dense;
  609. if (temp != null)
  610. {
  611. if (index < (uint) temp.Length)
  612. {
  613. temp[index] = null;
  614. return true;
  615. }
  616. }
  617. else
  618. {
  619. return _sparse!.Remove(index);
  620. }
  621. return false;
  622. }
  623. private bool TryGetDescriptor(uint index, bool createIfMissing, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  624. {
  625. descriptor = null;
  626. var temp = _dense;
  627. if (temp != null)
  628. {
  629. if (index < (uint) temp.Length)
  630. {
  631. var value = temp[index];
  632. if (value != null)
  633. {
  634. if (_sparse is null || !_sparse.TryGetValue(index, out descriptor) || descriptor is null)
  635. {
  636. if (!createIfMissing)
  637. {
  638. return false;
  639. }
  640. _sparse ??= new Dictionary<uint, PropertyDescriptor?>();
  641. _sparse[index] = descriptor = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  642. }
  643. descriptor.Value = value;
  644. return true;
  645. }
  646. }
  647. return false;
  648. }
  649. _sparse?.TryGetValue(index, out descriptor);
  650. return descriptor is not null;
  651. }
  652. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  653. internal bool TryGetValue(uint index, out JsValue value)
  654. {
  655. value = GetValue(index, unwrapFromNonDataDescriptor: true)!;
  656. if (value is not null)
  657. {
  658. return true;
  659. }
  660. return TryGetValueUnlikely(index, out value);
  661. }
  662. [MethodImpl(MethodImplOptions.NoInlining)]
  663. private bool TryGetValueUnlikely(uint index, out JsValue value)
  664. {
  665. if (!CanUseFastAccess)
  666. {
  667. // slow path must be checked for prototype
  668. var prototype = Prototype;
  669. JsValue key = index;
  670. while (prototype is not null)
  671. {
  672. var desc = prototype.GetOwnProperty(key);
  673. if (desc != PropertyDescriptor.Undefined)
  674. {
  675. value = UnwrapJsValue(desc);
  676. return true;
  677. }
  678. prototype = prototype.Prototype;
  679. }
  680. }
  681. value = Undefined;
  682. return false;
  683. }
  684. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  685. private JsValue? GetValue(uint index, bool unwrapFromNonDataDescriptor)
  686. {
  687. var temp = _dense;
  688. if (temp != null)
  689. {
  690. if (index < (uint) temp.Length)
  691. {
  692. return temp[index];
  693. }
  694. return null;
  695. }
  696. return GetValueUnlikely(index, unwrapFromNonDataDescriptor);
  697. }
  698. [MethodImpl(MethodImplOptions.NoInlining)]
  699. private JsValue? GetValueUnlikely(uint index, bool unwrapFromNonDataDescriptor)
  700. {
  701. JsValue? value = null;
  702. if (_sparse!.TryGetValue(index, out var descriptor) && descriptor != null)
  703. {
  704. value = descriptor.IsDataDescriptor() || unwrapFromNonDataDescriptor
  705. ? UnwrapJsValue(descriptor)
  706. : null;
  707. }
  708. return value;
  709. }
  710. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  711. private void WriteArrayValue(uint index, PropertyDescriptor descriptor)
  712. {
  713. var temp = _dense;
  714. if (temp != null && descriptor.IsDefaultArrayValueDescriptor())
  715. {
  716. if (index < (uint) temp.Length)
  717. {
  718. temp[index] = descriptor.Value;
  719. }
  720. else
  721. {
  722. WriteArrayValueUnlikely(index, descriptor.Value);
  723. }
  724. }
  725. else
  726. {
  727. WriteArrayValueUnlikely(index, descriptor);
  728. }
  729. }
  730. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  731. private void WriteArrayValue(uint index, JsValue? value)
  732. {
  733. var temp = _dense;
  734. if (temp != null)
  735. {
  736. if (index < (uint) temp.Length)
  737. {
  738. temp[index] = value;
  739. return;
  740. }
  741. }
  742. WriteArrayValueUnlikely(index, value);
  743. }
  744. [MethodImpl(MethodImplOptions.NoInlining)]
  745. private void WriteArrayValueUnlikely(uint index, JsValue? value)
  746. {
  747. // calculate eagerly so we know if we outgrow
  748. var dense = _dense;
  749. var newSize = dense != null && index >= (uint) dense.Length
  750. ? System.Math.Max(index, System.Math.Max(dense.Length, 2)) * 2
  751. : 0;
  752. var canUseDense = dense != null
  753. && index < MaxDenseArrayLength
  754. && newSize < MaxDenseArrayLength
  755. && index < dense.Length + 50; // looks sparse
  756. if (canUseDense)
  757. {
  758. EnsureCapacity((uint) newSize);
  759. _dense![index] = value;
  760. }
  761. else
  762. {
  763. ConvertToSparse();
  764. _sparse![index] = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  765. }
  766. }
  767. private void WriteArrayValueUnlikely(uint index, PropertyDescriptor? value)
  768. {
  769. if (_sparse == null)
  770. {
  771. ConvertToSparse();
  772. }
  773. _sparse![index] = value;
  774. }
  775. private void ConvertToSparse()
  776. {
  777. // need to move data
  778. var temp = _dense;
  779. if (temp is null)
  780. {
  781. return;
  782. }
  783. _sparse ??= new Dictionary<uint, PropertyDescriptor?>();
  784. for (uint i = 0; i < (uint) temp.Length; ++i)
  785. {
  786. var value = temp[i];
  787. if (value != null)
  788. {
  789. _sparse.TryGetValue(i, out var descriptor);
  790. descriptor ??= new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  791. descriptor.Value = value;
  792. _sparse[i] = descriptor;
  793. }
  794. else
  795. {
  796. _sparse.Remove(i);
  797. }
  798. }
  799. _dense = null;
  800. }
  801. internal void EnsureCapacity(uint capacity, bool force = false)
  802. {
  803. var dense = _dense;
  804. if (dense is null)
  805. {
  806. return;
  807. }
  808. if (!force && (capacity > MaxDenseArrayLength || capacity <= (uint) dense.Length))
  809. {
  810. return;
  811. }
  812. if (capacity > _engine.Options.Constraints.MaxArraySize)
  813. {
  814. ThrowMaximumArraySizeReachedException(_engine, capacity);
  815. }
  816. // need to grow
  817. var newArray = new JsValue[capacity];
  818. System.Array.Copy(dense, newArray, dense.Length);
  819. _dense = newArray;
  820. }
  821. public JsValue[] ToArray()
  822. {
  823. var length = GetLength();
  824. var array = new JsValue[length];
  825. for (uint i = 0; i < length; i++)
  826. {
  827. TryGetValue(i, out var outValue);
  828. array[i] = outValue;
  829. }
  830. return array;
  831. }
  832. IEnumerator IEnumerable.GetEnumerator()
  833. {
  834. return GetEnumerator();
  835. }
  836. public IEnumerator<JsValue> GetEnumerator()
  837. {
  838. foreach (var (_, value) in this.Enumerate())
  839. {
  840. yield return value;
  841. }
  842. }
  843. private readonly record struct IndexedEntry(int Index, JsValue Value);
  844. private IEnumerable<IndexedEntry> Enumerate()
  845. {
  846. if (!CanUseFastAccess)
  847. {
  848. // slow path where prototype is also checked
  849. var length = GetLength();
  850. for (uint i = 0; i < length; i++)
  851. {
  852. TryGetValue(i, out var outValue);
  853. yield return new IndexedEntry((int) i, outValue);
  854. }
  855. yield break;
  856. }
  857. var temp = _dense;
  858. if (temp != null)
  859. {
  860. var length = System.Math.Min(temp.Length, GetLength());
  861. for (var i = 0; i < length; i++)
  862. {
  863. var value = temp[i];
  864. if (value != null)
  865. {
  866. yield return new IndexedEntry(i, value);
  867. }
  868. }
  869. }
  870. else
  871. {
  872. foreach (var entry in _sparse!)
  873. {
  874. var descriptor = entry.Value;
  875. if (descriptor is not null)
  876. {
  877. yield return new IndexedEntry((int) entry.Key, descriptor.Value);
  878. }
  879. }
  880. }
  881. }
  882. /// <summary>
  883. /// Pushes the value to the end of the array instance.
  884. /// </summary>
  885. public void Push(JsValue value)
  886. {
  887. var initialLength = GetLength();
  888. var newLength = initialLength + 1;
  889. var temp = _dense;
  890. var canUseDirectIndexSet = temp != null && newLength <= temp.Length;
  891. double n = initialLength;
  892. if (canUseDirectIndexSet)
  893. {
  894. temp![(uint) n] = value;
  895. }
  896. else
  897. {
  898. WriteValueSlow(n, value);
  899. }
  900. // check if we can set length fast without breaking ECMA specification
  901. if (n < uint.MaxValue && CanSetLength())
  902. {
  903. _length!.Value = newLength;
  904. }
  905. else
  906. {
  907. if (!Set(CommonProperties.Length, newLength))
  908. {
  909. ExceptionHelper.ThrowTypeError(_engine.Realm);
  910. }
  911. }
  912. }
  913. /// <summary>
  914. /// Pushes the given values to the end of the array.
  915. /// </summary>
  916. public uint Push(JsValue[] values)
  917. {
  918. var initialLength = GetLength();
  919. var newLength = initialLength + values.Length;
  920. // if we see that we are bringing more than normal growth algorithm handles, ensure capacity eagerly
  921. if (_dense != null
  922. && initialLength != 0
  923. && values.Length > initialLength * 2
  924. && newLength <= MaxDenseArrayLength)
  925. {
  926. EnsureCapacity((uint) newLength);
  927. }
  928. var temp = _dense;
  929. ulong n = initialLength;
  930. foreach (var argument in values)
  931. {
  932. if (n < ArrayOperations.MaxArrayLength)
  933. {
  934. WriteArrayValue((uint) n, argument);
  935. }
  936. else
  937. {
  938. DefineOwnProperty(n, new PropertyDescriptor(argument, PropertyFlag.ConfigurableEnumerableWritable));
  939. }
  940. n++;
  941. }
  942. // check if we can set length fast without breaking ECMA specification
  943. if (n < ArrayOperations.MaxArrayLength && CanSetLength())
  944. {
  945. _length!.Value = n;
  946. }
  947. else
  948. {
  949. if (!Set(CommonProperties.Length, newLength))
  950. {
  951. ExceptionHelper.ThrowTypeError(_engine.Realm);
  952. }
  953. }
  954. return (uint) n;
  955. }
  956. private bool CanSetLength()
  957. {
  958. if (!_length!.IsAccessorDescriptor())
  959. {
  960. return _length.Writable;
  961. }
  962. var set = _length.Set;
  963. return set is not null && !set.IsUndefined();
  964. }
  965. [MethodImpl(MethodImplOptions.NoInlining)]
  966. private void WriteValueSlow(double n, JsValue value)
  967. {
  968. if (n < ArrayOperations.MaxArrayLength)
  969. {
  970. WriteArrayValue((uint) n, value);
  971. }
  972. else
  973. {
  974. DefinePropertyOrThrow((uint) n, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  975. }
  976. }
  977. internal JsArray Map(JsValue[] arguments)
  978. {
  979. var callbackfn = arguments.At(0);
  980. var thisArg = arguments.At(1);
  981. var len = GetLength();
  982. var callable = GetCallable(callbackfn);
  983. var a = Engine.Realm.Intrinsics.Array.ArrayCreate(len);
  984. var args = _engine._jsValueArrayPool.RentArray(3);
  985. args[2] = this;
  986. for (uint k = 0; k < len; k++)
  987. {
  988. if (TryGetValue(k, out var kvalue))
  989. {
  990. args[0] = kvalue;
  991. args[1] = k;
  992. var mappedValue = callable.Call(thisArg, args);
  993. if (a._dense != null && k < (uint) a._dense.Length)
  994. {
  995. a._dense[k] = mappedValue;
  996. }
  997. else
  998. {
  999. a.WriteArrayValue(k, mappedValue);
  1000. }
  1001. }
  1002. }
  1003. _engine._jsValueArrayPool.ReturnArray(args);
  1004. return a;
  1005. }
  1006. /// <inheritdoc />
  1007. internal sealed override bool FindWithCallback(
  1008. JsValue[] arguments,
  1009. out ulong index,
  1010. out JsValue value,
  1011. bool visitUnassigned,
  1012. bool fromEnd = false)
  1013. {
  1014. var thisArg = arguments.At(1);
  1015. var callbackfn = arguments.At(0);
  1016. var callable = GetCallable(callbackfn);
  1017. var len = GetLength();
  1018. if (len == 0)
  1019. {
  1020. index = 0;
  1021. value = Undefined;
  1022. return false;
  1023. }
  1024. var args = _engine._jsValueArrayPool.RentArray(3);
  1025. args[2] = this;
  1026. if (!fromEnd)
  1027. {
  1028. for (uint k = 0; k < len; k++)
  1029. {
  1030. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  1031. {
  1032. kvalue ??= Undefined;
  1033. args[0] = kvalue;
  1034. args[1] = k;
  1035. var testResult = callable.Call(thisArg, args);
  1036. if (TypeConverter.ToBoolean(testResult))
  1037. {
  1038. index = k;
  1039. value = kvalue;
  1040. return true;
  1041. }
  1042. }
  1043. }
  1044. }
  1045. else
  1046. {
  1047. for (long k = len - 1; k >= 0; k--)
  1048. {
  1049. var idx = (uint) k;
  1050. if (TryGetValue(idx, out var kvalue) || visitUnassigned)
  1051. {
  1052. kvalue ??= Undefined;
  1053. args[0] = kvalue;
  1054. args[1] = idx;
  1055. var testResult = callable.Call(thisArg, args);
  1056. if (TypeConverter.ToBoolean(testResult))
  1057. {
  1058. index = idx;
  1059. value = kvalue;
  1060. return true;
  1061. }
  1062. }
  1063. }
  1064. }
  1065. _engine._jsValueArrayPool.ReturnArray(args);
  1066. index = 0;
  1067. value = Undefined;
  1068. return false;
  1069. }
  1070. public sealed override uint Length => GetLength();
  1071. internal sealed override bool IsIntegerIndexedArray => true;
  1072. public JsValue this[uint index]
  1073. {
  1074. get
  1075. {
  1076. TryGetValue(index, out var kValue);
  1077. return kValue;
  1078. }
  1079. set
  1080. {
  1081. SetIndexValue(index, value, updateLength: true);
  1082. }
  1083. }
  1084. public JsValue this[int index]
  1085. {
  1086. get
  1087. {
  1088. JsValue? kValue;
  1089. if (index >= 0)
  1090. {
  1091. TryGetValue((uint) index, out kValue);
  1092. }
  1093. else
  1094. {
  1095. // slow path
  1096. TryGetValue(JsNumber.Create(index), out kValue);
  1097. }
  1098. return kValue;
  1099. }
  1100. set
  1101. {
  1102. if (index >= 0)
  1103. {
  1104. SetIndexValue((uint) index, value, updateLength: true);
  1105. }
  1106. else
  1107. {
  1108. Set(index, value);
  1109. }
  1110. }
  1111. }
  1112. /// <summary>
  1113. /// Fast path for concatenating sane-sized arrays, we assume size has been calculated.
  1114. /// </summary>
  1115. internal void CopyValues(JsArray source, uint sourceStartIndex, uint targetStartIndex, uint length)
  1116. {
  1117. if (length == 0)
  1118. {
  1119. return;
  1120. }
  1121. var sourceDense = source._dense;
  1122. if (sourceDense is not null)
  1123. {
  1124. EnsureCapacity((uint) (targetStartIndex + sourceDense.LongLength));
  1125. }
  1126. var dense = _dense;
  1127. if (dense != null
  1128. && sourceDense != null
  1129. && (uint) dense.Length >= targetStartIndex + length
  1130. && dense[targetStartIndex] is null)
  1131. {
  1132. uint j = 0;
  1133. for (var i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
  1134. {
  1135. JsValue? sourceValue;
  1136. if (i < (uint) sourceDense.Length && sourceDense[i] != null)
  1137. {
  1138. sourceValue = sourceDense[i];
  1139. }
  1140. else
  1141. {
  1142. if (!source.TryGetValue(i, out var temp))
  1143. {
  1144. sourceValue = source.Prototype?.Get(JsString.Create(i));
  1145. }
  1146. else
  1147. {
  1148. sourceValue = temp;
  1149. }
  1150. }
  1151. dense[targetStartIndex + j] = sourceValue;
  1152. }
  1153. }
  1154. else
  1155. {
  1156. // slower version
  1157. for (uint k = sourceStartIndex; k < length; k++)
  1158. {
  1159. if (source.TryGetValue(k, out var subElement))
  1160. {
  1161. SetIndexValue(targetStartIndex, subElement, updateLength: false);
  1162. }
  1163. targetStartIndex++;
  1164. }
  1165. }
  1166. }
  1167. public sealed override string ToString()
  1168. {
  1169. // debugger can make things hard when evaluates computed values
  1170. return "(" + (_length?._value!.AsNumber() ?? 0) + ")[]";
  1171. }
  1172. private static void ThrowMaximumArraySizeReachedException(Engine engine, uint capacity)
  1173. {
  1174. ExceptionHelper.ThrowMemoryLimitExceededException(
  1175. $"The array size {capacity} is larger than maximum allowed ({engine.Options.Constraints.MaxArraySize})"
  1176. );
  1177. }
  1178. }
  1179. internal static class ArrayPropertyDescriptorExtensions
  1180. {
  1181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1182. internal static bool IsDefaultArrayValueDescriptor(this PropertyDescriptor propertyDescriptor)
  1183. => propertyDescriptor.Flags == PropertyFlag.ConfigurableEnumerableWritable && propertyDescriptor.IsDataDescriptor();
  1184. }
  1185. }