ArrayInstance.cs 28 KB

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