ArrayInstance.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Native.Object;
  4. using Jint.Native.Symbol;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Native.Array
  8. {
  9. public class ArrayInstance : ObjectInstance
  10. {
  11. internal PropertyDescriptor _length;
  12. private const int MaxDenseArrayLength = 1024 * 10;
  13. private const ulong MaxArrayLength = 4294967295;
  14. // we have dense and sparse, we usually can start with dense and fall back to sparse when necessary
  15. internal PropertyDescriptor[] _dense;
  16. private Dictionary<uint, PropertyDescriptor> _sparse;
  17. public ArrayInstance(Engine engine, uint capacity = 0) : base(engine, ObjectClass.Array)
  18. {
  19. if (capacity < MaxDenseArrayLength)
  20. {
  21. _dense = capacity > 0 ? new PropertyDescriptor[capacity] : System.Array.Empty<PropertyDescriptor>();
  22. }
  23. else
  24. {
  25. _sparse = new Dictionary<uint, PropertyDescriptor>((int) (capacity <= 1024 ? capacity : 1024));
  26. }
  27. }
  28. /// <summary>
  29. /// Possibility to construct valid array fast, requires that supplied array does not have holes.
  30. /// </summary>
  31. public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, ObjectClass.Array)
  32. {
  33. int length = 0;
  34. if (items == null || items.Length == 0)
  35. {
  36. _dense = System.Array.Empty<PropertyDescriptor>();
  37. length = 0;
  38. }
  39. else
  40. {
  41. _dense = items;
  42. length = items.Length;
  43. }
  44. _length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
  45. }
  46. public ArrayInstance(Engine engine, Dictionary<uint, PropertyDescriptor> items) : base(engine, ObjectClass.Array)
  47. {
  48. _sparse = items;
  49. var length = items?.Count ?? 0;
  50. _length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
  51. }
  52. public override bool IsArrayLike => true;
  53. internal override bool HasOriginalIterator
  54. => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _engine.Realm.Intrinsics.Array.PrototypeObject._originalIteratorFunction);
  55. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  56. {
  57. var oldLenDesc = _length;
  58. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc.Value);
  59. if (property == CommonProperties.Length)
  60. {
  61. var value = desc.Value;
  62. if (ReferenceEquals(value, null))
  63. {
  64. return base.DefineOwnProperty(CommonProperties.Length, desc);
  65. }
  66. var newLenDesc = new PropertyDescriptor(desc);
  67. uint newLen = TypeConverter.ToUint32(value);
  68. if (newLen != TypeConverter.ToNumber(value))
  69. {
  70. ExceptionHelper.ThrowRangeError(_engine.Realm);
  71. }
  72. newLenDesc.Value = newLen;
  73. if (newLen >= oldLen)
  74. {
  75. return base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  76. }
  77. if (!oldLenDesc.Writable)
  78. {
  79. return false;
  80. }
  81. bool newWritable;
  82. if (!newLenDesc.WritableSet || newLenDesc.Writable)
  83. {
  84. newWritable = true;
  85. }
  86. else
  87. {
  88. newWritable = false;
  89. newLenDesc.Writable = true;
  90. }
  91. var succeeded = base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  92. if (!succeeded)
  93. {
  94. return false;
  95. }
  96. var count = _dense?.Length ?? _sparse.Count;
  97. if (count < oldLen - newLen)
  98. {
  99. if (_dense != null)
  100. {
  101. for (uint keyIndex = 0; keyIndex < _dense.Length; ++keyIndex)
  102. {
  103. if (_dense[keyIndex] == null)
  104. {
  105. continue;
  106. }
  107. // is it the index of the array
  108. if (keyIndex >= newLen && keyIndex < oldLen)
  109. {
  110. var deleteSucceeded = Delete(keyIndex);
  111. if (!deleteSucceeded)
  112. {
  113. newLenDesc.Value = keyIndex + 1;
  114. if (!newWritable)
  115. {
  116. newLenDesc.Writable = false;
  117. }
  118. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  119. return false;
  120. }
  121. }
  122. }
  123. }
  124. else
  125. {
  126. // in the case of sparse arrays, treat each concrete element instead of
  127. // iterating over all indexes
  128. var keys = new List<uint>(_sparse.Keys);
  129. var keysCount = keys.Count;
  130. for (var i = 0; i < keysCount; i++)
  131. {
  132. var keyIndex = keys[i];
  133. // is it the index of the array
  134. if (keyIndex >= newLen && keyIndex < oldLen)
  135. {
  136. var deleteSucceeded = Delete(TypeConverter.ToString(keyIndex));
  137. if (!deleteSucceeded)
  138. {
  139. newLenDesc.Value = JsNumber.Create(keyIndex + 1);
  140. if (!newWritable)
  141. {
  142. newLenDesc.Writable = false;
  143. }
  144. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  145. return false;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. else
  152. {
  153. while (newLen < oldLen)
  154. {
  155. // algorithm as per the spec
  156. oldLen--;
  157. var deleteSucceeded = Delete(oldLen);
  158. if (!deleteSucceeded)
  159. {
  160. newLenDesc.Value = oldLen + 1;
  161. if (!newWritable)
  162. {
  163. newLenDesc.Writable = false;
  164. }
  165. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  166. return false;
  167. }
  168. }
  169. }
  170. if (!newWritable)
  171. {
  172. base.DefineOwnProperty(CommonProperties.Length, new PropertyDescriptor(value: null, PropertyFlag.WritableSet));
  173. }
  174. return true;
  175. }
  176. else if (IsArrayIndex(property, out var index))
  177. {
  178. if (index >= oldLen && !oldLenDesc.Writable)
  179. {
  180. return false;
  181. }
  182. var succeeded = base.DefineOwnProperty(property, desc);
  183. if (!succeeded)
  184. {
  185. return false;
  186. }
  187. if (index >= oldLen)
  188. {
  189. oldLenDesc.Value = index + 1;
  190. base.DefineOwnProperty(CommonProperties.Length, oldLenDesc);
  191. }
  192. return true;
  193. }
  194. return base.DefineOwnProperty(property, desc);
  195. }
  196. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  197. internal uint GetLength()
  198. {
  199. if (_length is null)
  200. {
  201. return 0;
  202. }
  203. return (uint) ((JsNumber) _length._value)._value;
  204. }
  205. protected override void AddProperty(JsValue property, PropertyDescriptor descriptor)
  206. {
  207. if (property == CommonProperties.Length)
  208. {
  209. _length = descriptor;
  210. return;
  211. }
  212. base.AddProperty(property, descriptor);
  213. }
  214. protected override bool TryGetProperty(JsValue property, out PropertyDescriptor descriptor)
  215. {
  216. if (property == CommonProperties.Length)
  217. {
  218. descriptor = _length;
  219. return _length != null;
  220. }
  221. return base.TryGetProperty(property, out descriptor);
  222. }
  223. public override List<JsValue> GetOwnPropertyKeys(Types types)
  224. {
  225. var properties = new List<JsValue>(_dense?.Length ?? 0 + 1);
  226. if (_dense != null)
  227. {
  228. var length = System.Math.Min(_dense.Length, GetLength());
  229. for (var i = 0; i < length; i++)
  230. {
  231. if (_dense[i] != null)
  232. {
  233. properties.Add(JsString.Create(i));
  234. }
  235. }
  236. }
  237. else
  238. {
  239. foreach (var entry in _sparse)
  240. {
  241. properties.Add(JsString.Create(entry.Key));
  242. }
  243. }
  244. if (_length != null)
  245. {
  246. properties.Add(CommonProperties.Length);
  247. }
  248. properties.AddRange(base.GetOwnPropertyKeys(types));
  249. return properties;
  250. }
  251. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  252. {
  253. if (_dense != null)
  254. {
  255. var length = System.Math.Min(_dense.Length, GetLength());
  256. for (var i = 0; i < length; i++)
  257. {
  258. if (_dense[i] != null)
  259. {
  260. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(i), _dense[i]);
  261. }
  262. }
  263. }
  264. else
  265. {
  266. foreach (var entry in _sparse)
  267. {
  268. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(entry.Key), entry.Value);
  269. }
  270. }
  271. if (_length != null)
  272. {
  273. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  274. }
  275. foreach (var entry in base.GetOwnProperties())
  276. {
  277. yield return entry;
  278. }
  279. }
  280. public override PropertyDescriptor GetOwnProperty(JsValue property)
  281. {
  282. if (property == CommonProperties.Length)
  283. {
  284. return _length ?? PropertyDescriptor.Undefined;
  285. }
  286. if (IsArrayIndex(property, out var index))
  287. {
  288. if (TryGetDescriptor(index, out var result))
  289. {
  290. return result;
  291. }
  292. return PropertyDescriptor.Undefined;
  293. }
  294. return base.GetOwnProperty(property);
  295. }
  296. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  297. private PropertyDescriptor GetOwnProperty(uint index)
  298. {
  299. return TryGetDescriptor(index, out var result)
  300. ? result
  301. : PropertyDescriptor.Undefined;
  302. }
  303. internal JsValue Get(uint index)
  304. {
  305. var prop = GetOwnProperty(index);
  306. if (prop == PropertyDescriptor.Undefined)
  307. {
  308. prop = Prototype?.GetProperty(JsString.Create(index)) ?? PropertyDescriptor.Undefined;
  309. }
  310. return UnwrapJsValue(prop);
  311. }
  312. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  313. private PropertyDescriptor GetProperty(uint index)
  314. {
  315. var prop = GetOwnProperty(index);
  316. if (prop != PropertyDescriptor.Undefined)
  317. {
  318. return prop;
  319. }
  320. return Prototype?.GetProperty(JsString.Create(index)) ?? PropertyDescriptor.Undefined;
  321. }
  322. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  323. {
  324. if (IsArrayIndex(property, out var index))
  325. {
  326. WriteArrayValue(index, desc);
  327. }
  328. else if (property == CommonProperties.Length)
  329. {
  330. _length = desc;
  331. }
  332. else
  333. {
  334. base.SetOwnProperty(property, desc);
  335. }
  336. }
  337. public override bool HasOwnProperty(JsValue p)
  338. {
  339. if (IsArrayIndex(p, out var index))
  340. {
  341. return index < GetLength()
  342. && (_sparse == null || _sparse.ContainsKey(index))
  343. && (_dense == null || (index < (uint) _dense.Length && _dense[index] != null));
  344. }
  345. if (p == CommonProperties.Length)
  346. {
  347. return _length != null;
  348. }
  349. return base.HasOwnProperty(p);
  350. }
  351. public override void RemoveOwnProperty(JsValue p)
  352. {
  353. if (IsArrayIndex(p, out var index))
  354. {
  355. Delete(index);
  356. }
  357. if (p == CommonProperties.Length)
  358. {
  359. _length = null;
  360. }
  361. base.RemoveOwnProperty(p);
  362. }
  363. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  364. private static bool IsArrayIndex(JsValue p, out uint index)
  365. {
  366. if (p is JsNumber number)
  367. {
  368. var value = number._value;
  369. var intValue = (uint) value;
  370. index = intValue;
  371. return value == intValue && intValue != uint.MaxValue;
  372. }
  373. index = ParseArrayIndex(p.ToString());
  374. return index != uint.MaxValue;
  375. // 15.4 - Use an optimized version of the specification
  376. // return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
  377. }
  378. private static uint ParseArrayIndex(string p)
  379. {
  380. if (p.Length == 0)
  381. {
  382. return uint.MaxValue;
  383. }
  384. int d = p[0] - '0';
  385. if (d < 0 || d > 9)
  386. {
  387. return uint.MaxValue;
  388. }
  389. if (d == 0 && p.Length > 1)
  390. {
  391. // If p is a number that start with '0' and is not '0' then
  392. // its ToString representation can't be the same a p. This is
  393. // not a valid array index. '01' !== ToString(ToUInt32('01'))
  394. // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
  395. return uint.MaxValue;
  396. }
  397. if (p.Length > 1)
  398. {
  399. return StringAsIndex(d, p);
  400. }
  401. return (uint) d;
  402. }
  403. private static uint StringAsIndex(int d, string p)
  404. {
  405. ulong result = (uint) d;
  406. for (int i = 1; i < p.Length; i++)
  407. {
  408. d = p[i] - '0';
  409. if (d < 0 || d > 9)
  410. {
  411. return uint.MaxValue;
  412. }
  413. result = result * 10 + (uint) d;
  414. if (result >= uint.MaxValue)
  415. {
  416. return uint.MaxValue;
  417. }
  418. }
  419. return (uint) result;
  420. }
  421. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  422. internal void SetIndexValue(uint index, JsValue value, bool updateLength)
  423. {
  424. if (updateLength)
  425. {
  426. var length = GetLength();
  427. if (index >= length)
  428. {
  429. SetLength(index + 1);
  430. }
  431. }
  432. WriteArrayValue(index, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  433. }
  434. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  435. internal void SetLength(uint length)
  436. {
  437. _length.Value = length;
  438. }
  439. internal uint GetSmallestIndex()
  440. {
  441. if (_dense != null)
  442. {
  443. return 0;
  444. }
  445. uint smallest = 0;
  446. // only try to help if collection reasonable small
  447. if (_sparse.Count > 0 && _sparse.Count < 100 && !_sparse.ContainsKey(0))
  448. {
  449. smallest = uint.MaxValue;
  450. foreach (var key in _sparse.Keys)
  451. {
  452. smallest = System.Math.Min(key, smallest);
  453. }
  454. }
  455. return smallest;
  456. }
  457. public bool TryGetValue(uint index, out JsValue value)
  458. {
  459. value = Undefined;
  460. if (!TryGetDescriptor(index, out var desc))
  461. {
  462. desc = GetProperty(JsString.Create(index));
  463. }
  464. return desc.TryGetValue(this, out value);
  465. }
  466. internal bool DeletePropertyOrThrow(uint index)
  467. {
  468. if (!Delete(index))
  469. {
  470. ExceptionHelper.ThrowTypeError(_engine.Realm);
  471. }
  472. return true;
  473. }
  474. internal bool Delete(uint index)
  475. {
  476. var desc = GetOwnProperty(index);
  477. if (desc == PropertyDescriptor.Undefined)
  478. {
  479. return true;
  480. }
  481. if (desc.Configurable)
  482. {
  483. DeleteAt(index);
  484. return true;
  485. }
  486. return false;
  487. }
  488. internal bool DeleteAt(uint index)
  489. {
  490. var temp = _dense;
  491. if (temp != null)
  492. {
  493. if (index < (uint) temp.Length)
  494. {
  495. temp[index] = null;
  496. return true;
  497. }
  498. }
  499. else
  500. {
  501. return _sparse.Remove(index);
  502. }
  503. return false;
  504. }
  505. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  506. private bool TryGetDescriptor(uint index, out PropertyDescriptor descriptor)
  507. {
  508. var temp = _dense;
  509. if (temp != null)
  510. {
  511. descriptor = null;
  512. if (index < (uint) temp.Length)
  513. {
  514. descriptor = temp[index];
  515. }
  516. return descriptor != null;
  517. }
  518. return _sparse.TryGetValue(index, out descriptor);
  519. }
  520. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  521. internal void WriteArrayValue(uint index, PropertyDescriptor desc)
  522. {
  523. // calculate eagerly so we know if we outgrow
  524. var newSize = _dense != null && index >= (uint) _dense.Length
  525. ? System.Math.Max(index, System.Math.Max(_dense.Length, 2)) * 2
  526. : 0;
  527. bool canUseDense = _dense != null
  528. && index < MaxDenseArrayLength
  529. && newSize < MaxDenseArrayLength
  530. && index < _dense.Length + 50; // looks sparse
  531. if (canUseDense)
  532. {
  533. if (index >= (uint) _dense.Length)
  534. {
  535. EnsureCapacity((uint) newSize);
  536. }
  537. _dense[index] = desc;
  538. }
  539. else
  540. {
  541. if (_dense != null)
  542. {
  543. ConvertToSparse();
  544. }
  545. _sparse[index] = desc;
  546. }
  547. }
  548. private void ConvertToSparse()
  549. {
  550. _sparse = new Dictionary<uint, PropertyDescriptor>(_dense.Length <= 1024 ? _dense.Length : 0);
  551. // need to move data
  552. for (uint i = 0; i < (uint) _dense.Length; ++i)
  553. {
  554. if (_dense[i] != null)
  555. {
  556. _sparse[i] = _dense[i];
  557. }
  558. }
  559. _dense = null;
  560. }
  561. internal void EnsureCapacity(uint capacity)
  562. {
  563. if (capacity > MaxDenseArrayLength || _dense is null || capacity <= (uint) _dense.Length)
  564. {
  565. return;
  566. }
  567. // need to grow
  568. var newArray = new PropertyDescriptor[capacity];
  569. System.Array.Copy(_dense, newArray, _dense.Length);
  570. _dense = newArray;
  571. }
  572. public IEnumerator<JsValue> GetEnumerator()
  573. {
  574. var length = GetLength();
  575. for (uint i = 0; i < length; i++)
  576. {
  577. if (TryGetValue(i, out JsValue outValue))
  578. {
  579. yield return outValue;
  580. }
  581. };
  582. }
  583. internal uint Push(JsValue[] arguments)
  584. {
  585. var initialLength = GetLength();
  586. var newLength = initialLength + arguments.Length;
  587. // if we see that we are bringing more than normal growth algorithm handles, ensure capacity eagerly
  588. if (_dense != null
  589. && initialLength != 0
  590. && arguments.Length > initialLength * 2
  591. && newLength <= MaxDenseArrayLength)
  592. {
  593. EnsureCapacity((uint) newLength);
  594. }
  595. var canUseDirectIndexSet = _dense != null && newLength <= _dense.Length;
  596. double n = initialLength;
  597. foreach (var argument in arguments)
  598. {
  599. var desc = new PropertyDescriptor(argument, PropertyFlag.ConfigurableEnumerableWritable);
  600. if (canUseDirectIndexSet)
  601. {
  602. _dense[(uint) n] = desc;
  603. }
  604. else
  605. {
  606. WriteValueSlow(n, desc);
  607. }
  608. n++;
  609. }
  610. // check if we can set length fast without breaking ECMA specification
  611. if (n < uint.MaxValue && CanSetLength())
  612. {
  613. _length.Value = (uint) n;
  614. }
  615. else
  616. {
  617. if (!Set(CommonProperties.Length, newLength, this))
  618. {
  619. ExceptionHelper.ThrowTypeError(_engine.Realm);
  620. }
  621. }
  622. return (uint) n;
  623. }
  624. private bool CanSetLength()
  625. {
  626. if (!_length.IsAccessorDescriptor())
  627. {
  628. return _length.Writable;
  629. }
  630. var set = _length.Set;
  631. return !(set is null) && !set.IsUndefined();
  632. }
  633. [MethodImpl(MethodImplOptions.NoInlining)]
  634. private void WriteValueSlow(double n, PropertyDescriptor desc)
  635. {
  636. if (n < uint.MaxValue)
  637. {
  638. WriteArrayValue((uint) n, desc);
  639. }
  640. else
  641. {
  642. DefinePropertyOrThrow((uint) n, desc);
  643. }
  644. }
  645. internal ArrayInstance Map(JsValue[] arguments)
  646. {
  647. var callbackfn = arguments.At(0);
  648. var thisArg = arguments.At(1);
  649. var len = GetLength();
  650. var callable = GetCallable(callbackfn);
  651. var a = Engine.Realm.Intrinsics.Array.ConstructFast(len);
  652. var args = _engine._jsValueArrayPool.RentArray(3);
  653. args[2] = this;
  654. for (uint k = 0; k < len; k++)
  655. {
  656. if (TryGetValue(k, out var kvalue))
  657. {
  658. args[0] = kvalue;
  659. args[1] = k;
  660. var mappedValue = callable.Call(thisArg, args);
  661. var desc = new PropertyDescriptor(mappedValue, PropertyFlag.ConfigurableEnumerableWritable);
  662. if (a._dense != null && k < (uint) a._dense.Length)
  663. {
  664. a._dense[k] = desc;
  665. }
  666. else
  667. {
  668. a.WriteArrayValue(k, desc);
  669. }
  670. }
  671. }
  672. _engine._jsValueArrayPool.ReturnArray(args);
  673. return a;
  674. }
  675. /// <inheritdoc />
  676. internal override bool FindWithCallback(
  677. JsValue[] arguments,
  678. out uint index,
  679. out JsValue value,
  680. bool visitUnassigned)
  681. {
  682. var thisArg = arguments.At(1);
  683. var callbackfn = arguments.At(0);
  684. var callable = GetCallable(callbackfn);
  685. var len = GetLength();
  686. if (len == 0)
  687. {
  688. index = 0;
  689. value = Undefined;
  690. return false;
  691. }
  692. var args = _engine._jsValueArrayPool.RentArray(3);
  693. args[2] = this;
  694. for (uint k = 0; k < len; k++)
  695. {
  696. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  697. {
  698. args[0] = kvalue;
  699. args[1] = k;
  700. var testResult = callable.Call(thisArg, args);
  701. if (TypeConverter.ToBoolean(testResult))
  702. {
  703. index = k;
  704. value = kvalue;
  705. return true;
  706. }
  707. }
  708. }
  709. _engine._jsValueArrayPool.ReturnArray(args);
  710. index = 0;
  711. value = Undefined;
  712. return false;
  713. }
  714. public override uint Length => GetLength();
  715. internal override bool IsIntegerIndexedArray => true;
  716. public JsValue this[uint index]
  717. {
  718. get
  719. {
  720. TryGetValue(index, out var kValue);
  721. return kValue;
  722. }
  723. }
  724. internal ArrayInstance ToArray(Engine engine)
  725. {
  726. var length = GetLength();
  727. var array = _engine.Realm.Intrinsics.Array.ConstructFast(length);
  728. for (uint i = 0; i < length; i++)
  729. {
  730. if (TryGetValue(i, out var kValue))
  731. {
  732. array.SetIndexValue(i, kValue, updateLength: false);
  733. }
  734. }
  735. return array;
  736. }
  737. /// <summary>
  738. /// Fast path for concatenating sane-sized arrays, we assume size has been calculated.
  739. /// </summary>
  740. internal void CopyValues(ArrayInstance source, uint sourceStartIndex, uint targetStartIndex, uint length)
  741. {
  742. if (length == 0)
  743. {
  744. return;
  745. }
  746. var dense = _dense;
  747. var sourceDense = source._dense;
  748. if (dense != null && sourceDense != null
  749. && (uint) dense.Length >= targetStartIndex + length
  750. && dense[targetStartIndex] is null)
  751. {
  752. uint j = 0;
  753. for (uint i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
  754. {
  755. var sourcePropertyDescriptor = i < (uint) sourceDense.Length && sourceDense[i] != null
  756. ? sourceDense[i]
  757. : source.GetProperty(i);
  758. dense[targetStartIndex + j] = sourcePropertyDescriptor?._value is not null
  759. ? new PropertyDescriptor(sourcePropertyDescriptor._value, PropertyFlag.ConfigurableEnumerableWritable)
  760. : null;
  761. }
  762. }
  763. else
  764. {
  765. // slower version
  766. for (uint k = sourceStartIndex; k < length; k++)
  767. {
  768. if (source.TryGetValue(k, out var subElement))
  769. {
  770. SetIndexValue(targetStartIndex, subElement, updateLength: false);
  771. }
  772. }
  773. }
  774. }
  775. public override string ToString()
  776. {
  777. // debugger can make things hard when evaluates computed values
  778. return "(" + (_length?._value.AsNumber() ?? 0) + ")[]";
  779. }
  780. }
  781. }