ArrayInstance.cs 28 KB

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