ArrayInstance.cs 28 KB

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