ArrayInstance.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. return (uint) ((JsNumber) _length._value)._value;
  197. }
  198. protected override void AddProperty(JsValue property, PropertyDescriptor descriptor)
  199. {
  200. if (property == CommonProperties.Length)
  201. {
  202. _length = descriptor;
  203. return;
  204. }
  205. base.AddProperty(property, descriptor);
  206. }
  207. protected override bool TryGetProperty(JsValue property, out PropertyDescriptor descriptor)
  208. {
  209. if (property == CommonProperties.Length)
  210. {
  211. descriptor = _length;
  212. return _length != null;
  213. }
  214. return base.TryGetProperty(property, out descriptor);
  215. }
  216. public override List<JsValue> GetOwnPropertyKeys(Types types)
  217. {
  218. var properties = new List<JsValue>(_dense?.Length ?? 0 + 1);
  219. if (_dense != null)
  220. {
  221. var length = System.Math.Min(_dense.Length, GetLength());
  222. for (var i = 0; i < length; i++)
  223. {
  224. if (_dense[i] != null)
  225. {
  226. properties.Add(JsString.Create(i));
  227. }
  228. }
  229. }
  230. else
  231. {
  232. foreach (var entry in _sparse)
  233. {
  234. properties.Add(JsString.Create(entry.Key));
  235. }
  236. }
  237. if (_length != null)
  238. {
  239. properties.Add(CommonProperties.Length);
  240. }
  241. properties.AddRange(base.GetOwnPropertyKeys(types));
  242. return properties;
  243. }
  244. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  245. {
  246. if (_dense != null)
  247. {
  248. var length = System.Math.Min(_dense.Length, GetLength());
  249. for (var i = 0; i < length; i++)
  250. {
  251. if (_dense[i] != null)
  252. {
  253. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(i), _dense[i]);
  254. }
  255. }
  256. }
  257. else
  258. {
  259. foreach (var entry in _sparse)
  260. {
  261. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(entry.Key), entry.Value);
  262. }
  263. }
  264. if (_length != null)
  265. {
  266. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  267. }
  268. foreach (var entry in base.GetOwnProperties())
  269. {
  270. yield return entry;
  271. }
  272. }
  273. public override PropertyDescriptor GetOwnProperty(JsValue property)
  274. {
  275. if (property == CommonProperties.Length)
  276. {
  277. return _length ?? PropertyDescriptor.Undefined;
  278. }
  279. if (IsArrayIndex(property, out var index))
  280. {
  281. if (TryGetDescriptor(index, out var result))
  282. {
  283. return result;
  284. }
  285. return PropertyDescriptor.Undefined;
  286. }
  287. return base.GetOwnProperty(property);
  288. }
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. private PropertyDescriptor GetOwnProperty(uint index)
  291. {
  292. return TryGetDescriptor(index, out var result)
  293. ? result
  294. : PropertyDescriptor.Undefined;
  295. }
  296. internal JsValue Get(uint index)
  297. {
  298. var prop = GetOwnProperty(index);
  299. if (prop == PropertyDescriptor.Undefined)
  300. {
  301. prop = Prototype?.GetProperty(index) ?? PropertyDescriptor.Undefined;
  302. }
  303. return UnwrapJsValue(prop);
  304. }
  305. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  306. private PropertyDescriptor GetProperty(uint index)
  307. {
  308. var prop = GetOwnProperty(index);
  309. if (prop != PropertyDescriptor.Undefined)
  310. {
  311. return prop;
  312. }
  313. return Prototype?.GetProperty(index) ?? PropertyDescriptor.Undefined;
  314. }
  315. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  316. {
  317. if (IsArrayIndex(property, out var index))
  318. {
  319. WriteArrayValue(index, desc);
  320. }
  321. else if (property == CommonProperties.Length)
  322. {
  323. _length = desc;
  324. }
  325. else
  326. {
  327. base.SetOwnProperty(property, desc);
  328. }
  329. }
  330. public override bool HasOwnProperty(JsValue p)
  331. {
  332. if (IsArrayIndex(p, out var index))
  333. {
  334. return index < GetLength()
  335. && (_sparse == null || _sparse.ContainsKey(index))
  336. && (_dense == null || (index < (uint) _dense.Length && _dense[index] != null));
  337. }
  338. if (p == CommonProperties.Length)
  339. {
  340. return _length != null;
  341. }
  342. return base.HasOwnProperty(p);
  343. }
  344. public override void RemoveOwnProperty(JsValue p)
  345. {
  346. if (IsArrayIndex(p, out var index))
  347. {
  348. Delete(index);
  349. }
  350. if (p == CommonProperties.Length)
  351. {
  352. _length = null;
  353. }
  354. base.RemoveOwnProperty(p);
  355. }
  356. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  357. private static bool IsArrayIndex(JsValue p, out uint index)
  358. {
  359. if (p is JsNumber number)
  360. {
  361. var value = number._value;
  362. var intValue = (uint) value;
  363. index = intValue;
  364. return value == intValue && intValue != uint.MaxValue;
  365. }
  366. index = ParseArrayIndex(p.ToString());
  367. return index != uint.MaxValue;
  368. // 15.4 - Use an optimized version of the specification
  369. // return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
  370. }
  371. private static uint ParseArrayIndex(string p)
  372. {
  373. if (p.Length == 0)
  374. {
  375. return uint.MaxValue;
  376. }
  377. int d = p[0] - '0';
  378. if (d < 0 || d > 9)
  379. {
  380. return uint.MaxValue;
  381. }
  382. if (d == 0 && p.Length > 1)
  383. {
  384. // If p is a number that start with '0' and is not '0' then
  385. // its ToString representation can't be the same a p. This is
  386. // not a valid array index. '01' !== ToString(ToUInt32('01'))
  387. // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
  388. return uint.MaxValue;
  389. }
  390. if (p.Length > 1)
  391. {
  392. return StringAsIndex(d, p);
  393. }
  394. return (uint) d;
  395. }
  396. private static uint StringAsIndex(int d, string p)
  397. {
  398. ulong result = (uint) d;
  399. for (int i = 1; i < p.Length; i++)
  400. {
  401. d = p[i] - '0';
  402. if (d < 0 || d > 9)
  403. {
  404. return uint.MaxValue;
  405. }
  406. result = result * 10 + (uint) d;
  407. if (result >= uint.MaxValue)
  408. {
  409. return uint.MaxValue;
  410. }
  411. }
  412. return (uint) result;
  413. }
  414. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  415. internal void SetIndexValue(uint index, JsValue value, bool updateLength)
  416. {
  417. if (updateLength)
  418. {
  419. var length = GetLength();
  420. if (index >= length)
  421. {
  422. SetLength(index + 1);
  423. }
  424. }
  425. WriteArrayValue(index, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  426. }
  427. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  428. internal void SetLength(uint length)
  429. {
  430. _length.Value = length;
  431. }
  432. internal uint GetSmallestIndex()
  433. {
  434. if (_dense != null)
  435. {
  436. return 0;
  437. }
  438. uint smallest = 0;
  439. // only try to help if collection reasonable small
  440. if (_sparse.Count > 0 && _sparse.Count < 100 && !_sparse.ContainsKey(0))
  441. {
  442. smallest = uint.MaxValue;
  443. foreach (var key in _sparse.Keys)
  444. {
  445. smallest = System.Math.Min(key, smallest);
  446. }
  447. }
  448. return smallest;
  449. }
  450. public bool TryGetValue(uint index, out JsValue value)
  451. {
  452. value = Undefined;
  453. if (!TryGetDescriptor(index, out var desc))
  454. {
  455. desc = GetProperty(index);
  456. }
  457. return desc.TryGetValue(this, out value);
  458. }
  459. internal bool DeletePropertyOrThrow(uint index)
  460. {
  461. if (!Delete(index))
  462. {
  463. ExceptionHelper.ThrowTypeError(Engine);
  464. }
  465. return true;
  466. }
  467. internal bool Delete(uint index)
  468. {
  469. var desc = GetOwnProperty(index);
  470. if (desc == PropertyDescriptor.Undefined)
  471. {
  472. return true;
  473. }
  474. if (desc.Configurable)
  475. {
  476. DeleteAt(index);
  477. return true;
  478. }
  479. return false;
  480. }
  481. internal bool DeleteAt(uint index)
  482. {
  483. var temp = _dense;
  484. if (temp != null)
  485. {
  486. if (index < (uint) temp.Length)
  487. {
  488. temp[index] = null;
  489. return true;
  490. }
  491. }
  492. else
  493. {
  494. return _sparse.Remove(index);
  495. }
  496. return false;
  497. }
  498. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  499. private bool TryGetDescriptor(uint index, out PropertyDescriptor descriptor)
  500. {
  501. var temp = _dense;
  502. if (temp != null)
  503. {
  504. descriptor = null;
  505. if (index < (uint) temp.Length)
  506. {
  507. descriptor = temp[index];
  508. }
  509. return descriptor != null;
  510. }
  511. return _sparse.TryGetValue(index, out descriptor);
  512. }
  513. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  514. internal void WriteArrayValue(uint index, PropertyDescriptor desc)
  515. {
  516. // calculate eagerly so we know if we outgrow
  517. var newSize = _dense != null && index >= (uint) _dense.Length
  518. ? System.Math.Max(index, System.Math.Max(_dense.Length, 2)) * 2
  519. : 0;
  520. bool canUseDense = _dense != null
  521. && index < MaxDenseArrayLength
  522. && newSize < MaxDenseArrayLength
  523. && index < _dense.Length + 50; // looks sparse
  524. if (canUseDense)
  525. {
  526. if (index >= (uint) _dense.Length)
  527. {
  528. EnsureCapacity((uint) newSize);
  529. }
  530. _dense[index] = desc;
  531. }
  532. else
  533. {
  534. if (_dense != null)
  535. {
  536. ConvertToSparse();
  537. }
  538. _sparse[index] = desc;
  539. }
  540. }
  541. private void ConvertToSparse()
  542. {
  543. _sparse = new Dictionary<uint, PropertyDescriptor>(_dense.Length <= 1024 ? _dense.Length : 0);
  544. // need to move data
  545. for (uint i = 0; i < (uint) _dense.Length; ++i)
  546. {
  547. if (_dense[i] != null)
  548. {
  549. _sparse[i] = _dense[i];
  550. }
  551. }
  552. _dense = null;
  553. }
  554. internal void EnsureCapacity(uint capacity)
  555. {
  556. if (capacity <= MaxDenseArrayLength && capacity > (uint) _dense.Length)
  557. {
  558. // need to grow
  559. var newArray = new PropertyDescriptor[capacity];
  560. System.Array.Copy(_dense, newArray, _dense.Length);
  561. _dense = newArray;
  562. }
  563. }
  564. public IEnumerator<JsValue> GetEnumerator()
  565. {
  566. var length = GetLength();
  567. for (uint i = 0; i < length; i++)
  568. {
  569. if (TryGetValue(i, out JsValue outValue))
  570. {
  571. yield return outValue;
  572. }
  573. };
  574. }
  575. internal uint Push(JsValue[] arguments)
  576. {
  577. var initialLength = GetLength();
  578. var newLength = initialLength + arguments.Length;
  579. // if we see that we are bringing more than normal growth algorithm handles, ensure capacity eagerly
  580. if (_dense != null
  581. && initialLength != 0
  582. && arguments.Length > initialLength * 2
  583. && newLength <= MaxDenseArrayLength)
  584. {
  585. EnsureCapacity((uint) newLength);
  586. }
  587. var canUseDirectIndexSet = _dense != null && newLength <= _dense.Length;
  588. double n = initialLength;
  589. foreach (var argument in arguments)
  590. {
  591. var desc = new PropertyDescriptor(argument, PropertyFlag.ConfigurableEnumerableWritable);
  592. if (canUseDirectIndexSet)
  593. {
  594. _dense[(uint) n] = desc;
  595. }
  596. else
  597. {
  598. WriteValueSlow(n, desc);
  599. }
  600. n++;
  601. }
  602. // check if we can set length fast without breaking ECMA specification
  603. if (n < uint.MaxValue && CanSetLength())
  604. {
  605. _length.Value = (uint) n;
  606. }
  607. else
  608. {
  609. if (!Set(CommonProperties.Length, newLength, this))
  610. {
  611. ExceptionHelper.ThrowTypeError(_engine);
  612. }
  613. }
  614. return (uint) n;
  615. }
  616. private bool CanSetLength()
  617. {
  618. if (!_length.IsAccessorDescriptor())
  619. {
  620. return _length.Writable;
  621. }
  622. var set = _length.Set;
  623. return !(set is null) && !set.IsUndefined();
  624. }
  625. [MethodImpl(MethodImplOptions.NoInlining)]
  626. private void WriteValueSlow(double n, PropertyDescriptor desc)
  627. {
  628. if (n < uint.MaxValue)
  629. {
  630. WriteArrayValue((uint) n, desc);
  631. }
  632. else
  633. {
  634. DefinePropertyOrThrow((uint) n, desc);
  635. }
  636. }
  637. internal ArrayInstance Map(JsValue[] arguments)
  638. {
  639. var callbackfn = arguments.At(0);
  640. var thisArg = arguments.At(1);
  641. var len = GetLength();
  642. var callable = GetCallable(callbackfn);
  643. var a = Engine.Array.ConstructFast(len);
  644. var args = _engine._jsValueArrayPool.RentArray(3);
  645. args[2] = this;
  646. for (uint k = 0; k < len; k++)
  647. {
  648. if (TryGetValue(k, out var kvalue))
  649. {
  650. args[0] = kvalue;
  651. args[1] = k;
  652. var mappedValue = callable.Call(thisArg, args);
  653. var desc = new PropertyDescriptor(mappedValue, PropertyFlag.ConfigurableEnumerableWritable);
  654. if (a._dense != null && k < (uint) a._dense.Length)
  655. {
  656. a._dense[k] = desc;
  657. }
  658. else
  659. {
  660. a.WriteArrayValue(k, desc);
  661. }
  662. }
  663. }
  664. _engine._jsValueArrayPool.ReturnArray(args);
  665. return a;
  666. }
  667. /// <inheritdoc />
  668. internal override bool FindWithCallback(
  669. JsValue[] arguments,
  670. out uint index,
  671. out JsValue value,
  672. bool visitUnassigned)
  673. {
  674. var thisArg = arguments.At(1);
  675. var callbackfn = arguments.At(0);
  676. var callable = GetCallable(callbackfn);
  677. var len = GetLength();
  678. if (len == 0)
  679. {
  680. index = 0;
  681. value = Undefined;
  682. return false;
  683. }
  684. var args = _engine._jsValueArrayPool.RentArray(3);
  685. args[2] = this;
  686. for (uint k = 0; k < len; k++)
  687. {
  688. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  689. {
  690. args[0] = kvalue;
  691. args[1] = k;
  692. var testResult = callable.Call(thisArg, args);
  693. if (TypeConverter.ToBoolean(testResult))
  694. {
  695. index = k;
  696. value = kvalue;
  697. return true;
  698. }
  699. }
  700. }
  701. _engine._jsValueArrayPool.ReturnArray(args);
  702. index = 0;
  703. value = Undefined;
  704. return false;
  705. }
  706. public override uint Length => GetLength();
  707. public JsValue this[uint index]
  708. {
  709. get
  710. {
  711. TryGetValue(index, out var kValue);
  712. return kValue;
  713. }
  714. }
  715. internal ArrayInstance ToArray(Engine engine)
  716. {
  717. var length = GetLength();
  718. var array = _engine.Array.ConstructFast(length);
  719. for (uint i = 0; i < length; i++)
  720. {
  721. if (TryGetValue(i, out var kValue))
  722. {
  723. array.SetIndexValue(i, kValue, updateLength: false);
  724. }
  725. }
  726. return array;
  727. }
  728. /// <summary>
  729. /// Fast path for concatenating sane-sized arrays, we assume size has been calculated.
  730. /// </summary>
  731. internal void CopyValues(ArrayInstance source, uint sourceStartIndex, uint targetStartIndex, uint length)
  732. {
  733. if (length == 0)
  734. {
  735. return;
  736. }
  737. var dense = _dense;
  738. var sourceDense = source._dense;
  739. if (dense != null && sourceDense != null
  740. && (uint) dense.Length >= targetStartIndex + length
  741. && dense[targetStartIndex] is null)
  742. {
  743. uint j = 0;
  744. for (uint i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
  745. {
  746. var sourcePropertyDescriptor = i < (uint) sourceDense.Length && sourceDense[i] != null
  747. ? sourceDense[i]
  748. : source.GetProperty(i);
  749. dense[targetStartIndex + j] = sourcePropertyDescriptor?._value != null
  750. ? new PropertyDescriptor(sourcePropertyDescriptor._value, PropertyFlag.ConfigurableEnumerableWritable)
  751. : null;
  752. }
  753. }
  754. else
  755. {
  756. // slower version
  757. for (uint k = sourceStartIndex; k < length; k++)
  758. {
  759. if (source.TryGetValue(k, out var subElement))
  760. {
  761. SetIndexValue(targetStartIndex, subElement, updateLength: false);
  762. }
  763. }
  764. }
  765. }
  766. public override string ToString()
  767. {
  768. // debugger can make things hard when evaluates computed values
  769. return "(" + (_length?._value.AsNumber() ?? 0) + ")[]";
  770. }
  771. }
  772. }