2
0

ArrayInstance.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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.ArrayExt.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.ArrayExt.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. 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 bool Set(in Key propertyName, JsValue value, JsValue receiver)
  56. {
  57. if (!CanPut(propertyName))
  58. {
  59. return false;
  60. }
  61. var ownDesc = GetOwnProperty(propertyName);
  62. if (ownDesc.IsDataDescriptor())
  63. {
  64. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  65. return DefineOwnProperty(propertyName, valueDesc);
  66. }
  67. // property is an accessor or inherited
  68. var desc = GetProperty(propertyName);
  69. if (desc.IsAccessorDescriptor())
  70. {
  71. var setter = desc.Set.TryCast<ICallable>();
  72. setter.Call(receiver, new[] {value});
  73. }
  74. else
  75. {
  76. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  77. return DefineOwnProperty(propertyName, newDesc);
  78. }
  79. return true;
  80. }
  81. public override bool DefineOwnProperty(in Key propertyName, PropertyDescriptor desc)
  82. {
  83. var oldLenDesc = _length;
  84. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc.Value);
  85. if (propertyName == KnownKeys.Length)
  86. {
  87. var value = desc.Value;
  88. if (ReferenceEquals(value, null))
  89. {
  90. return base.DefineOwnProperty("length", desc);
  91. }
  92. var newLenDesc = new PropertyDescriptor(desc);
  93. uint newLen = TypeConverter.ToUint32(value);
  94. if (newLen != TypeConverter.ToNumber(value))
  95. {
  96. ExceptionHelper.ThrowRangeError(_engine);
  97. }
  98. newLenDesc.Value = newLen;
  99. if (newLen >= oldLen)
  100. {
  101. return base.DefineOwnProperty("length", newLenDesc);
  102. }
  103. if (!oldLenDesc.Writable)
  104. {
  105. return false;
  106. }
  107. bool newWritable;
  108. if (!newLenDesc.WritableSet || newLenDesc.Writable)
  109. {
  110. newWritable = true;
  111. }
  112. else
  113. {
  114. newWritable = false;
  115. newLenDesc.Writable = true;
  116. }
  117. var succeeded = base.DefineOwnProperty("length", newLenDesc);
  118. if (!succeeded)
  119. {
  120. return false;
  121. }
  122. var count = _dense?.Length ?? _sparse.Count;
  123. if (count < oldLen - newLen)
  124. {
  125. if (_dense != null)
  126. {
  127. for (uint keyIndex = 0; keyIndex < _dense.Length; ++keyIndex)
  128. {
  129. if (_dense[keyIndex] == null)
  130. {
  131. continue;
  132. }
  133. // is it the index of the array
  134. if (keyIndex >= newLen && keyIndex < oldLen)
  135. {
  136. var deleteSucceeded = DeleteAt(keyIndex);
  137. if (!deleteSucceeded)
  138. {
  139. newLenDesc.Value = keyIndex + 1;
  140. if (!newWritable)
  141. {
  142. newLenDesc.Writable = false;
  143. }
  144. base.DefineOwnProperty("length", newLenDesc);
  145. return false;
  146. }
  147. }
  148. }
  149. }
  150. else
  151. {
  152. // in the case of sparse arrays, treat each concrete element instead of
  153. // iterating over all indexes
  154. var keys = new List<uint>(_sparse.Keys);
  155. var keysCount = keys.Count;
  156. for (var i = 0; i < keysCount; i++)
  157. {
  158. var keyIndex = keys[i];
  159. // is it the index of the array
  160. if (keyIndex >= newLen && keyIndex < oldLen)
  161. {
  162. var deleteSucceeded = Delete(TypeConverter.ToString(keyIndex));
  163. if (!deleteSucceeded)
  164. {
  165. newLenDesc.Value = JsNumber.Create(keyIndex + 1);
  166. if (!newWritable)
  167. {
  168. newLenDesc.Writable = false;
  169. }
  170. base.DefineOwnProperty("length", newLenDesc);
  171. return false;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. else
  178. {
  179. while (newLen < oldLen)
  180. {
  181. // algorithm as per the spec
  182. oldLen--;
  183. var deleteSucceeded = Delete(TypeConverter.ToString(oldLen));
  184. if (!deleteSucceeded)
  185. {
  186. newLenDesc.Value = oldLen + 1;
  187. if (!newWritable)
  188. {
  189. newLenDesc.Writable = false;
  190. }
  191. base.DefineOwnProperty("length", newLenDesc);
  192. return false;
  193. }
  194. }
  195. }
  196. if (!newWritable)
  197. {
  198. base.DefineOwnProperty("length", new PropertyDescriptor(value: null, PropertyFlag.WritableSet));
  199. }
  200. return true;
  201. }
  202. else if (IsArrayIndex(propertyName, out var index))
  203. {
  204. if (index >= oldLen && !oldLenDesc.Writable)
  205. {
  206. return false;
  207. }
  208. var succeeded = base.DefineOwnProperty(propertyName, desc);
  209. if (!succeeded)
  210. {
  211. return false;
  212. }
  213. if (index >= oldLen)
  214. {
  215. oldLenDesc.Value = index + 1;
  216. base.DefineOwnProperty("length", oldLenDesc);
  217. }
  218. return true;
  219. }
  220. return base.DefineOwnProperty(propertyName, desc);
  221. }
  222. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  223. public uint GetLength()
  224. {
  225. return (uint) ((JsNumber) _length._value)._value;
  226. }
  227. protected override void AddProperty(in Key propertyName, PropertyDescriptor descriptor)
  228. {
  229. if (propertyName == KnownKeys.Length)
  230. {
  231. _length = descriptor;
  232. return;
  233. }
  234. base.AddProperty(propertyName, descriptor);
  235. }
  236. protected override bool TryGetProperty(in Key propertyName, out PropertyDescriptor descriptor)
  237. {
  238. if (propertyName == KnownKeys.Length)
  239. {
  240. descriptor = _length;
  241. return _length != null;
  242. }
  243. return base.TryGetProperty(propertyName, out descriptor);
  244. }
  245. internal override List<JsValue> GetOwnPropertyKeys(Types types)
  246. {
  247. var properties = new List<JsValue>(_dense?.Length ?? 0 + 1);
  248. if (_dense != null)
  249. {
  250. var length = System.Math.Min(_dense.Length, GetLength());
  251. for (var i = 0; i < length; i++)
  252. {
  253. if (_dense[i] != null)
  254. {
  255. properties.Add(TypeConverter.ToString(i));
  256. }
  257. }
  258. }
  259. else
  260. {
  261. foreach (var entry in _sparse)
  262. {
  263. properties.Add(TypeConverter.ToString(entry.Key));
  264. }
  265. }
  266. if (_length != null)
  267. {
  268. properties.Add(KnownKeys.Length);
  269. }
  270. properties.AddRange(base.GetOwnPropertyKeys(types));
  271. return properties;
  272. }
  273. public override IEnumerable<KeyValuePair<Key, PropertyDescriptor>> GetOwnProperties()
  274. {
  275. if (_dense != null)
  276. {
  277. var length = System.Math.Min(_dense.Length, GetLength());
  278. for (var i = 0; i < length; i++)
  279. {
  280. if (_dense[i] != null)
  281. {
  282. yield return new KeyValuePair<Key, PropertyDescriptor>(TypeConverter.ToString(i), _dense[i]);
  283. }
  284. }
  285. }
  286. else
  287. {
  288. foreach (var entry in _sparse)
  289. {
  290. yield return new KeyValuePair<Key, PropertyDescriptor>(TypeConverter.ToString(entry.Key), entry.Value);
  291. }
  292. }
  293. if (_length != null)
  294. {
  295. yield return new KeyValuePair<Key, PropertyDescriptor>(KnownKeys.Length, _length);
  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(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(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. internal 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. if (!TryGetDescriptor(index, out var desc))
  477. {
  478. desc = GetProperty(index);
  479. }
  480. return desc.TryGetValue(this, out value);
  481. }
  482. internal bool DeleteAt(uint index)
  483. {
  484. if (_dense != null)
  485. {
  486. if (index < (uint) _dense.Length)
  487. {
  488. _dense[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(KnownKeys.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 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.ToString());
  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. }