ArrayInstance.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. using System.Collections;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Native.Array
  9. {
  10. public class ArrayInstance : ObjectInstance, IEnumerable<JsValue>
  11. {
  12. internal PropertyDescriptor? _length;
  13. private const int MaxDenseArrayLength = 10_000_000;
  14. // we have dense and sparse, we usually can start with dense and fall back to sparse when necessary
  15. // entries are lazy and can be either of type PropertyDescriptor or plain JsValue while there is no need for extra info
  16. internal object?[]? _dense;
  17. private Dictionary<uint, object?>? _sparse;
  18. private ObjectChangeFlags _objectChangeFlags;
  19. private bool _isObjectArray = true;
  20. private protected ArrayInstance(Engine engine, InternalTypes type) : base(engine, type: type)
  21. {
  22. _dense = System.Array.Empty<object?>();
  23. }
  24. private protected ArrayInstance(Engine engine, uint capacity = 0, uint length = 0) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  25. {
  26. _prototype = engine.Realm.Intrinsics.Array.PrototypeObject;
  27. if (capacity > engine.Options.Constraints.MaxArraySize)
  28. {
  29. ThrowMaximumArraySizeReachedException(engine, capacity);
  30. }
  31. if (capacity < MaxDenseArrayLength)
  32. {
  33. _dense = capacity > 0 ? new object?[capacity] : System.Array.Empty<object?>();
  34. }
  35. else
  36. {
  37. _sparse = new Dictionary<uint, object?>(1024);
  38. }
  39. _length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
  40. }
  41. private protected ArrayInstance(Engine engine, JsValue[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  42. {
  43. Initialize(engine, items);
  44. }
  45. private protected ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  46. {
  47. Initialize(engine, items);
  48. }
  49. private protected ArrayInstance(Engine engine, object[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
  50. {
  51. Initialize(engine, items);
  52. }
  53. private void Initialize<T>(Engine engine, T[] items) where T : class
  54. {
  55. if (items.Length > engine.Options.Constraints.MaxArraySize)
  56. {
  57. ThrowMaximumArraySizeReachedException(engine, (uint) items.Length);
  58. }
  59. _prototype = engine.Realm.Intrinsics.Array.PrototypeObject;
  60. _isObjectArray = typeof(T) == typeof(object);
  61. int length;
  62. if (items == null || items.Length == 0)
  63. {
  64. _dense = System.Array.Empty<object>();
  65. length = 0;
  66. }
  67. else
  68. {
  69. _dense = items;
  70. length = items.Length;
  71. }
  72. _length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
  73. }
  74. public sealed override bool IsArrayLike => true;
  75. public sealed override bool IsArray() => true;
  76. internal sealed override bool HasOriginalIterator
  77. => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _engine.Realm.Intrinsics.Array.PrototypeObject._originalIteratorFunction);
  78. /// <summary>
  79. /// Checks whether there have been changes to object prototype chain which could render fast access patterns impossible.
  80. /// </summary>
  81. internal bool CanUseFastAccess
  82. {
  83. get
  84. {
  85. if ((_objectChangeFlags & ObjectChangeFlags.NonDefaultDataDescriptorUsage) != 0)
  86. {
  87. // could be a mutating property for example, length might change, not safe anymore
  88. return false;
  89. }
  90. if (_prototype is not ArrayPrototype arrayPrototype
  91. || !ReferenceEquals(_prototype, _engine.Realm.Intrinsics.Array.PrototypeObject))
  92. {
  93. // somebody has switched prototype
  94. return false;
  95. }
  96. if ((arrayPrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) != 0)
  97. {
  98. // maybe somebody moved integer property to prototype? not safe anymore
  99. return false;
  100. }
  101. if (arrayPrototype.Prototype is not ObjectPrototype arrayPrototypePrototype
  102. || !ReferenceEquals(arrayPrototypePrototype, _engine.Realm.Intrinsics.Array.PrototypeObject.Prototype))
  103. {
  104. return false;
  105. }
  106. return (arrayPrototypePrototype._objectChangeFlags & ObjectChangeFlags.ArrayIndex) == 0;
  107. }
  108. }
  109. public sealed override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  110. {
  111. var isArrayIndex = IsArrayIndex(property, out var index);
  112. TrackChanges(property, desc, isArrayIndex);
  113. if (isArrayIndex)
  114. {
  115. return DefineOwnProperty(index, desc);
  116. }
  117. if (property == CommonProperties.Length)
  118. {
  119. var value = desc.Value;
  120. if (ReferenceEquals(value, null))
  121. {
  122. return base.DefineOwnProperty(CommonProperties.Length, desc);
  123. }
  124. var newLenDesc = new PropertyDescriptor(desc);
  125. uint newLen = TypeConverter.ToUint32(value);
  126. if (newLen != TypeConverter.ToNumber(value))
  127. {
  128. ExceptionHelper.ThrowRangeError(_engine.Realm);
  129. }
  130. var oldLenDesc = _length;
  131. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc!.Value);
  132. newLenDesc.Value = newLen;
  133. if (newLen >= oldLen)
  134. {
  135. return base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  136. }
  137. if (!oldLenDesc.Writable)
  138. {
  139. return false;
  140. }
  141. bool newWritable;
  142. if (!newLenDesc.WritableSet || newLenDesc.Writable)
  143. {
  144. newWritable = true;
  145. }
  146. else
  147. {
  148. newWritable = false;
  149. newLenDesc.Writable = true;
  150. }
  151. var succeeded = base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  152. if (!succeeded)
  153. {
  154. return false;
  155. }
  156. var count = _dense?.Length ?? _sparse!.Count;
  157. if (count < oldLen - newLen)
  158. {
  159. if (_dense != null)
  160. {
  161. for (uint keyIndex = 0; keyIndex < _dense.Length; ++keyIndex)
  162. {
  163. if (_dense[keyIndex] == null)
  164. {
  165. continue;
  166. }
  167. // is it the index of the array
  168. if (keyIndex >= newLen && keyIndex < oldLen)
  169. {
  170. var deleteSucceeded = Delete(keyIndex);
  171. if (!deleteSucceeded)
  172. {
  173. newLenDesc.Value = keyIndex + 1;
  174. if (!newWritable)
  175. {
  176. newLenDesc.Writable = false;
  177. }
  178. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  179. return false;
  180. }
  181. }
  182. }
  183. }
  184. else
  185. {
  186. // in the case of sparse arrays, treat each concrete element instead of
  187. // iterating over all indexes
  188. var keys = new List<uint>(_sparse!.Keys);
  189. var keysCount = keys.Count;
  190. for (var i = 0; i < keysCount; i++)
  191. {
  192. var keyIndex = keys[i];
  193. // is it the index of the array
  194. if (keyIndex >= newLen && keyIndex < oldLen)
  195. {
  196. var deleteSucceeded = Delete(TypeConverter.ToString(keyIndex));
  197. if (!deleteSucceeded)
  198. {
  199. newLenDesc.Value = JsNumber.Create(keyIndex + 1);
  200. if (!newWritable)
  201. {
  202. newLenDesc.Writable = false;
  203. }
  204. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  205. return false;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. else
  212. {
  213. while (newLen < oldLen)
  214. {
  215. // algorithm as per the spec
  216. oldLen--;
  217. var deleteSucceeded = Delete(oldLen);
  218. if (!deleteSucceeded)
  219. {
  220. newLenDesc.Value = oldLen + 1;
  221. if (!newWritable)
  222. {
  223. newLenDesc.Writable = false;
  224. }
  225. base.DefineOwnProperty(CommonProperties.Length, newLenDesc);
  226. return false;
  227. }
  228. }
  229. }
  230. if (!newWritable)
  231. {
  232. base.DefineOwnProperty(CommonProperties.Length, new PropertyDescriptor(value: null, PropertyFlag.WritableSet));
  233. }
  234. return true;
  235. }
  236. return base.DefineOwnProperty(property, desc);
  237. }
  238. private bool DefineOwnProperty(uint index, PropertyDescriptor desc)
  239. {
  240. var oldLenDesc = _length;
  241. var oldLen = (uint) TypeConverter.ToNumber(oldLenDesc!.Value);
  242. if (index >= oldLen && !oldLenDesc.Writable)
  243. {
  244. return false;
  245. }
  246. var succeeded = base.DefineOwnProperty(index, desc);
  247. if (!succeeded)
  248. {
  249. return false;
  250. }
  251. if (index >= oldLen)
  252. {
  253. oldLenDesc.Value = index + 1;
  254. base.DefineOwnProperty(CommonProperties.Length, oldLenDesc);
  255. }
  256. return true;
  257. }
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. internal uint GetLength()
  260. {
  261. if (_length is null)
  262. {
  263. return 0;
  264. }
  265. return (uint) ((JsNumber) _length._value!)._value;
  266. }
  267. protected sealed override void AddProperty(JsValue property, PropertyDescriptor descriptor)
  268. {
  269. if (property == CommonProperties.Length)
  270. {
  271. _length = descriptor;
  272. return;
  273. }
  274. base.AddProperty(property, descriptor);
  275. }
  276. protected sealed override bool TryGetProperty(JsValue property, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  277. {
  278. if (property == CommonProperties.Length)
  279. {
  280. descriptor = _length;
  281. return _length != null;
  282. }
  283. return base.TryGetProperty(property, out descriptor);
  284. }
  285. public sealed override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
  286. {
  287. if ((types & Types.String) == 0)
  288. {
  289. return base.GetOwnPropertyKeys(types);
  290. }
  291. var temp = _dense;
  292. var properties = new List<JsValue>(temp?.Length ?? 0 + 1);
  293. if (temp != null)
  294. {
  295. var length = System.Math.Min(temp.Length, GetLength());
  296. for (var i = 0; i < length; i++)
  297. {
  298. if (temp[i] != null)
  299. {
  300. properties.Add(JsString.Create(i));
  301. }
  302. }
  303. }
  304. else
  305. {
  306. foreach (var entry in _sparse!)
  307. {
  308. properties.Add(JsString.Create(entry.Key));
  309. }
  310. }
  311. if (_length != null)
  312. {
  313. properties.Add(CommonProperties.Length);
  314. }
  315. properties.AddRange(base.GetOwnPropertyKeys(types));
  316. return properties;
  317. }
  318. /// <summary>
  319. /// Returns key and value pairs for actual array entries, excludes parent and optionally length.
  320. /// </summary>
  321. /// <param name="includeLength">Whether to return length and it's value.</param>
  322. public IEnumerable<KeyValuePair<string, JsValue>> GetEntries(bool includeLength = false)
  323. {
  324. foreach (var (index, value) in this.Enumerate())
  325. {
  326. yield return new KeyValuePair<string, JsValue>(TypeConverter.ToString(index), value);
  327. }
  328. if (includeLength && _length != null)
  329. {
  330. yield return new KeyValuePair<string, JsValue>(CommonProperties.Length._value, _length.Value);
  331. }
  332. }
  333. public sealed override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  334. {
  335. var temp = _dense;
  336. if (temp != null)
  337. {
  338. var length = System.Math.Min(temp.Length, GetLength());
  339. for (var i = 0; i < length; i++)
  340. {
  341. var value = temp[i];
  342. if (value != null)
  343. {
  344. if (value is not PropertyDescriptor descriptor)
  345. {
  346. temp[i] = descriptor = new PropertyDescriptor((JsValue) value, PropertyFlag.ConfigurableEnumerableWritable);
  347. }
  348. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(i), descriptor);
  349. }
  350. }
  351. }
  352. else
  353. {
  354. foreach (var entry in _sparse!)
  355. {
  356. var value = entry.Value;
  357. if (value is not null)
  358. {
  359. if (value is not PropertyDescriptor descriptor)
  360. {
  361. _sparse[entry.Key] = descriptor = new PropertyDescriptor((JsValue) value, PropertyFlag.ConfigurableEnumerableWritable);
  362. }
  363. yield return new KeyValuePair<JsValue, PropertyDescriptor>(TypeConverter.ToString(entry.Key), descriptor);
  364. }
  365. }
  366. }
  367. if (_length != null)
  368. {
  369. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  370. }
  371. foreach (var entry in base.GetOwnProperties())
  372. {
  373. yield return entry;
  374. }
  375. }
  376. public sealed override PropertyDescriptor GetOwnProperty(JsValue property)
  377. {
  378. if (property == CommonProperties.Length)
  379. {
  380. return _length ?? PropertyDescriptor.Undefined;
  381. }
  382. if (IsArrayIndex(property, out var index))
  383. {
  384. if (TryGetDescriptor(index, out var result))
  385. {
  386. return result;
  387. }
  388. return PropertyDescriptor.Undefined;
  389. }
  390. return base.GetOwnProperty(property);
  391. }
  392. internal JsValue Get(uint index)
  393. {
  394. if (!TryGetValue(index, out var value))
  395. {
  396. value = UnwrapJsValue(Prototype?.GetProperty(JsString.Create(index)) ?? PropertyDescriptor.Undefined);
  397. }
  398. return value;
  399. }
  400. public sealed override JsValue Get(JsValue property, JsValue receiver)
  401. {
  402. if (IsSafeSelfTarget(receiver) && IsArrayIndex(property, out var index) && TryGetValue(index, out var value))
  403. {
  404. return value;
  405. }
  406. if (property == CommonProperties.Length)
  407. {
  408. var length = _length?._value;
  409. if (length is not null)
  410. {
  411. return length;
  412. }
  413. }
  414. return base.Get(property, receiver);
  415. }
  416. public sealed override bool Set(JsValue property, JsValue value, JsValue receiver)
  417. {
  418. var isSafeSelfTarget = IsSafeSelfTarget(receiver);
  419. if (isSafeSelfTarget && IsArrayIndex(property, out var index))
  420. {
  421. var temp = _dense;
  422. if (temp is not null && CanUseFastAccess)
  423. {
  424. var current = index < temp.Length ? temp[index] : null;
  425. if (current is not PropertyDescriptor p || p.IsDefaultArrayValueDescriptor())
  426. {
  427. SetIndexValue(index, value, true);
  428. return true;
  429. }
  430. }
  431. // slower and more allocating
  432. if (TryGetDescriptor(index, out var descriptor))
  433. {
  434. if (descriptor.IsDefaultArrayValueDescriptor())
  435. {
  436. // fast path with direct write without allocations
  437. descriptor.Value = value;
  438. return true;
  439. }
  440. }
  441. else if (CanUseFastAccess)
  442. {
  443. // we know it's to be written to own array backing field as new value
  444. SetIndexValue(index, value, true);
  445. return true;
  446. }
  447. }
  448. // slow path
  449. return base.Set(property, value, receiver);
  450. }
  451. private bool IsSafeSelfTarget(JsValue receiver) => ReferenceEquals(receiver, this) && Extensible;
  452. public sealed override bool HasProperty(JsValue property)
  453. {
  454. if (IsArrayIndex(property, out var index) && GetValue(index, unwrapFromNonDataDescriptor: false) is not null)
  455. {
  456. return true;
  457. }
  458. return base.HasProperty(property);
  459. }
  460. protected internal sealed override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  461. {
  462. var isArrayIndex = IsArrayIndex(property, out var index);
  463. TrackChanges(property, desc, isArrayIndex);
  464. if (isArrayIndex)
  465. {
  466. WriteArrayValue(index, desc);
  467. }
  468. else if (property == CommonProperties.Length)
  469. {
  470. _length = desc;
  471. }
  472. else
  473. {
  474. base.SetOwnProperty(property, desc);
  475. }
  476. }
  477. private void TrackChanges(JsValue property, PropertyDescriptor desc, bool isArrayIndex)
  478. {
  479. EnsureInitialized();
  480. if (isArrayIndex)
  481. {
  482. if (!desc.IsDefaultArrayValueDescriptor())
  483. {
  484. _objectChangeFlags |= ObjectChangeFlags.NonDefaultDataDescriptorUsage;
  485. }
  486. _objectChangeFlags |= ObjectChangeFlags.ArrayIndex;
  487. }
  488. else
  489. {
  490. _objectChangeFlags |= property.IsSymbol() ? ObjectChangeFlags.Symbol : ObjectChangeFlags.Property;
  491. }
  492. }
  493. public sealed override void RemoveOwnProperty(JsValue p)
  494. {
  495. if (IsArrayIndex(p, out var index))
  496. {
  497. Delete(index);
  498. }
  499. if (p == CommonProperties.Length)
  500. {
  501. _length = null;
  502. }
  503. base.RemoveOwnProperty(p);
  504. }
  505. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  506. internal static bool IsArrayIndex(JsValue p, out uint index)
  507. {
  508. if (p is JsNumber number)
  509. {
  510. var value = number._value;
  511. var intValue = (uint) value;
  512. index = intValue;
  513. return value == intValue && intValue != uint.MaxValue;
  514. }
  515. index = ParseArrayIndex(p.ToString());
  516. return index != uint.MaxValue;
  517. // 15.4 - Use an optimized version of the specification
  518. // return TypeConverter.ToString(index) == TypeConverter.ToString(p) && index != uint.MaxValue;
  519. }
  520. internal static uint ParseArrayIndex(string p)
  521. {
  522. if (p.Length == 0)
  523. {
  524. return uint.MaxValue;
  525. }
  526. if (p.Length > 1 && p[0] == '0')
  527. {
  528. // If p is a number that start with '0' and is not '0' then
  529. // its ToString representation can't be the same a p. This is
  530. // not a valid array index. '01' !== ToString(ToUInt32('01'))
  531. // http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
  532. return uint.MaxValue;
  533. }
  534. if (!uint.TryParse(p, out var d))
  535. {
  536. return uint.MaxValue;
  537. }
  538. return d;
  539. }
  540. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  541. internal void SetIndexValue(uint index, JsValue? value, bool updateLength)
  542. {
  543. if (updateLength)
  544. {
  545. EnsureCorrectLength(index);
  546. }
  547. WriteArrayValue(index, value);
  548. }
  549. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  550. private void EnsureCorrectLength(uint index)
  551. {
  552. var length = GetLength();
  553. if (index >= length)
  554. {
  555. SetLength(index + 1);
  556. }
  557. }
  558. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  559. internal void SetLength(ulong length)
  560. {
  561. var number = JsNumber.Create(length);
  562. if (Extensible && _length!._flags == PropertyFlag.OnlyWritable)
  563. {
  564. _length!.Value = number;
  565. }
  566. else
  567. {
  568. // slow path
  569. Set(CommonProperties.Length, number, true);
  570. }
  571. }
  572. internal uint GetSmallestIndex()
  573. {
  574. if (_dense != null)
  575. {
  576. return 0;
  577. }
  578. uint smallest = 0;
  579. // only try to help if collection reasonable small
  580. if (_sparse!.Count > 0 && _sparse.Count < 100 && !_sparse.ContainsKey(0))
  581. {
  582. smallest = uint.MaxValue;
  583. foreach (var key in _sparse.Keys)
  584. {
  585. smallest = System.Math.Min(key, smallest);
  586. }
  587. }
  588. return smallest;
  589. }
  590. internal bool DeletePropertyOrThrow(uint index)
  591. {
  592. if (!Delete(index))
  593. {
  594. ExceptionHelper.ThrowTypeError(_engine.Realm);
  595. }
  596. return true;
  597. }
  598. internal bool Delete(uint index)
  599. {
  600. // check fast path
  601. var temp = _dense;
  602. if (temp != null)
  603. {
  604. if (index < (uint) temp.Length)
  605. {
  606. var value = temp[index];
  607. if (value is JsValue || value is PropertyDescriptor { Configurable: true })
  608. {
  609. temp[index] = null;
  610. return true;
  611. }
  612. }
  613. }
  614. if (!TryGetDescriptor(index, out var desc))
  615. {
  616. return true;
  617. }
  618. if (desc.Configurable)
  619. {
  620. DeleteAt(index);
  621. return true;
  622. }
  623. return false;
  624. }
  625. internal bool DeleteAt(uint index)
  626. {
  627. var temp = _dense;
  628. if (temp != null)
  629. {
  630. if (index < (uint) temp.Length)
  631. {
  632. temp[index] = null;
  633. return true;
  634. }
  635. }
  636. else
  637. {
  638. return _sparse!.Remove(index);
  639. }
  640. return false;
  641. }
  642. private bool TryGetDescriptor(uint index, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  643. {
  644. descriptor = null;
  645. var temp = _dense;
  646. if (temp != null)
  647. {
  648. if (index < (uint) temp.Length)
  649. {
  650. var value = temp[index];
  651. if (value is JsValue jsValue)
  652. {
  653. if (EnsureCompatibleDense(typeof(PropertyDescriptor)))
  654. {
  655. temp = _dense!;
  656. }
  657. temp[index] = descriptor = new PropertyDescriptor(jsValue, PropertyFlag.ConfigurableEnumerableWritable);
  658. }
  659. else if (value is PropertyDescriptor propertyDescriptor)
  660. {
  661. descriptor = propertyDescriptor;
  662. }
  663. }
  664. return descriptor != null;
  665. }
  666. if (_sparse!.TryGetValue(index, out var sparseValue))
  667. {
  668. if (sparseValue is JsValue jsValue)
  669. {
  670. _sparse[index] = descriptor = new PropertyDescriptor(jsValue, PropertyFlag.ConfigurableEnumerableWritable);
  671. }
  672. else if (sparseValue is PropertyDescriptor propertyDescriptor)
  673. {
  674. descriptor = propertyDescriptor;
  675. }
  676. }
  677. return descriptor is not null;
  678. }
  679. internal bool TryGetValue(uint index, out JsValue value)
  680. {
  681. value = GetValue(index, unwrapFromNonDataDescriptor: true)!;
  682. if (value is not null)
  683. {
  684. return true;
  685. }
  686. if (!CanUseFastAccess)
  687. {
  688. // slow path must be checked for prototype
  689. var prototype = Prototype;
  690. JsValue key = index;
  691. while (prototype is not null)
  692. {
  693. var desc = prototype.GetOwnProperty(key);
  694. if (desc != PropertyDescriptor.Undefined)
  695. {
  696. value = UnwrapJsValue(desc);
  697. return true;
  698. }
  699. prototype = prototype.Prototype;
  700. }
  701. }
  702. value = Undefined;
  703. return false;
  704. }
  705. private JsValue? GetValue(uint index, bool unwrapFromNonDataDescriptor)
  706. {
  707. object? value = null;
  708. var temp = _dense;
  709. if (temp != null)
  710. {
  711. if (index < (uint) temp.Length)
  712. {
  713. value = temp[index];
  714. }
  715. }
  716. else
  717. {
  718. _sparse!.TryGetValue(index, out value);
  719. }
  720. if (value is JsValue jsValue)
  721. {
  722. return jsValue;
  723. }
  724. if (value is PropertyDescriptor propertyDescriptor)
  725. {
  726. return propertyDescriptor.IsDataDescriptor() || unwrapFromNonDataDescriptor ? UnwrapJsValue(propertyDescriptor) : null;
  727. }
  728. return null;
  729. }
  730. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  731. private void WriteArrayValue(uint index, object? value)
  732. {
  733. var dense = _dense;
  734. if (dense != null && index < (uint) dense.Length)
  735. {
  736. if (value is not null && !_isObjectArray && EnsureCompatibleDense(value is JsValue ? typeof(JsValue) : typeof(PropertyDescriptor)))
  737. {
  738. dense = _dense;
  739. }
  740. dense![index] = value;
  741. }
  742. else
  743. {
  744. WriteArrayValueUnlikely(index, value);
  745. }
  746. }
  747. private void WriteArrayValueUnlikely(uint index, object? value)
  748. {
  749. // calculate eagerly so we know if we outgrow
  750. var dense = _dense;
  751. var newSize = dense != null && index >= (uint) dense.Length
  752. ? System.Math.Max(index, System.Math.Max(dense.Length, 2)) * 2
  753. : 0;
  754. var canUseDense = dense != null
  755. && index < MaxDenseArrayLength
  756. && newSize < MaxDenseArrayLength
  757. && index < dense.Length + 50; // looks sparse
  758. if (canUseDense)
  759. {
  760. EnsureCapacity((uint) newSize);
  761. _dense![index] = value;
  762. }
  763. else
  764. {
  765. if (dense != null)
  766. {
  767. ConvertToSparse();
  768. }
  769. _sparse![index] = value;
  770. }
  771. }
  772. /// <summary>
  773. /// Converts to object array when needed. Returns true if conversion was made.
  774. /// </summary>
  775. private bool EnsureCompatibleDense(Type expectedElementType)
  776. {
  777. if (!_isObjectArray)
  778. {
  779. return CheckConversionUnlikely(expectedElementType);
  780. }
  781. return false;
  782. }
  783. private bool CheckConversionUnlikely(Type expectedElementType)
  784. {
  785. var currentElementType = _dense!.GetType().GetElementType();
  786. if (currentElementType != typeof(object) && !expectedElementType.IsAssignableFrom(currentElementType))
  787. {
  788. // triggers conversion for array
  789. EnsureCapacity((uint) _dense.Length, force: true);
  790. return true;
  791. }
  792. return false;
  793. }
  794. private void ConvertToSparse()
  795. {
  796. _sparse = new Dictionary<uint, object?>(_dense!.Length <= 1024 ? _dense.Length : 0);
  797. // need to move data
  798. for (uint i = 0; i < (uint) _dense.Length; ++i)
  799. {
  800. if (_dense[i] != null)
  801. {
  802. _sparse[i] = _dense[i];
  803. }
  804. }
  805. _dense = null;
  806. }
  807. internal void EnsureCapacity(uint capacity, bool force = false)
  808. {
  809. if (!force && (capacity > MaxDenseArrayLength || _dense is null || capacity <= (uint) _dense.Length))
  810. {
  811. return;
  812. }
  813. if (capacity > _engine.Options.Constraints.MaxArraySize)
  814. {
  815. ThrowMaximumArraySizeReachedException(_engine, capacity);
  816. }
  817. // need to grow
  818. var newArray = new object[capacity];
  819. System.Array.Copy(_dense, newArray, _dense!.Length);
  820. _dense = newArray;
  821. _isObjectArray = true;
  822. }
  823. public JsValue[] ToArray()
  824. {
  825. var length = GetLength();
  826. var array = new JsValue[length];
  827. for (uint i = 0; i < length; i++)
  828. {
  829. TryGetValue(i, out var outValue);
  830. array[i] = outValue;
  831. }
  832. return array;
  833. }
  834. IEnumerator IEnumerable.GetEnumerator()
  835. {
  836. return GetEnumerator();
  837. }
  838. public IEnumerator<JsValue> GetEnumerator()
  839. {
  840. foreach (var (_, value) in this.Enumerate())
  841. {
  842. yield return value;
  843. }
  844. }
  845. private readonly record struct IndexedEntry(int Index, JsValue Value);
  846. private IEnumerable<IndexedEntry> Enumerate()
  847. {
  848. if (!CanUseFastAccess)
  849. {
  850. // slow path where prototype is also checked
  851. var length = GetLength();
  852. for (uint i = 0; i < length; i++)
  853. {
  854. TryGetValue(i, out var outValue);
  855. yield return new IndexedEntry((int) i, outValue);
  856. }
  857. yield break;
  858. }
  859. var temp = _dense;
  860. if (temp != null)
  861. {
  862. var length = System.Math.Min(temp.Length, GetLength());
  863. for (var i = 0; i < length; i++)
  864. {
  865. var value = temp[i];
  866. if (value != null)
  867. {
  868. if (value is not PropertyDescriptor descriptor)
  869. {
  870. yield return new IndexedEntry(i, (JsValue) value);
  871. }
  872. else
  873. {
  874. yield return new IndexedEntry(i, descriptor.Value);
  875. }
  876. }
  877. }
  878. }
  879. else
  880. {
  881. foreach (var entry in _sparse!)
  882. {
  883. var value = entry.Value;
  884. if (value is not null)
  885. {
  886. if (value is not PropertyDescriptor descriptor)
  887. {
  888. yield return new IndexedEntry((int) entry.Key, (JsValue) value);
  889. }
  890. else
  891. {
  892. yield return new IndexedEntry((int) entry.Key, descriptor.Value);
  893. }
  894. }
  895. }
  896. }
  897. }
  898. /// <summary>
  899. /// Pushes the value to the end of the array instance.
  900. /// </summary>
  901. public void Push(JsValue value)
  902. {
  903. var initialLength = GetLength();
  904. var newLength = initialLength + 1;
  905. var temp = _dense;
  906. var canUseDirectIndexSet = temp != null && newLength <= temp.Length;
  907. double n = initialLength;
  908. if (canUseDirectIndexSet)
  909. {
  910. temp![(uint) n] = value;
  911. }
  912. else
  913. {
  914. WriteValueSlow(n, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  915. }
  916. // check if we can set length fast without breaking ECMA specification
  917. if (n < uint.MaxValue && CanSetLength())
  918. {
  919. _length!.Value = newLength;
  920. }
  921. else
  922. {
  923. if (!Set(CommonProperties.Length, newLength))
  924. {
  925. ExceptionHelper.ThrowTypeError(_engine.Realm);
  926. }
  927. }
  928. }
  929. /// <summary>
  930. /// Pushes the given values to the end of the array.
  931. /// </summary>
  932. public uint Push(JsValue[] values)
  933. {
  934. var initialLength = GetLength();
  935. var newLength = initialLength + values.Length;
  936. // if we see that we are bringing more than normal growth algorithm handles, ensure capacity eagerly
  937. if (_dense != null
  938. && initialLength != 0
  939. && values.Length > initialLength * 2
  940. && newLength <= MaxDenseArrayLength)
  941. {
  942. EnsureCapacity((uint) newLength);
  943. }
  944. var temp = _dense;
  945. ulong n = initialLength;
  946. foreach (var argument in values)
  947. {
  948. if (n < ArrayOperations.MaxArrayLength)
  949. {
  950. WriteArrayValue((uint) n, argument);
  951. }
  952. else
  953. {
  954. DefineOwnProperty(n, new PropertyDescriptor(argument, PropertyFlag.ConfigurableEnumerableWritable));
  955. }
  956. n++;
  957. }
  958. // check if we can set length fast without breaking ECMA specification
  959. if (n < ArrayOperations.MaxArrayLength && CanSetLength())
  960. {
  961. _length!.Value = n;
  962. }
  963. else
  964. {
  965. if (!Set(CommonProperties.Length, newLength))
  966. {
  967. ExceptionHelper.ThrowTypeError(_engine.Realm);
  968. }
  969. }
  970. return (uint) n;
  971. }
  972. private bool CanSetLength()
  973. {
  974. if (!_length!.IsAccessorDescriptor())
  975. {
  976. return _length.Writable;
  977. }
  978. var set = _length.Set;
  979. return set is not null && !set.IsUndefined();
  980. }
  981. [MethodImpl(MethodImplOptions.NoInlining)]
  982. private void WriteValueSlow(double n, PropertyDescriptor desc)
  983. {
  984. if (n < uint.MaxValue)
  985. {
  986. WriteArrayValue((uint) n, desc);
  987. }
  988. else
  989. {
  990. DefinePropertyOrThrow((uint) n, desc);
  991. }
  992. }
  993. internal JsArray Map(JsValue[] arguments)
  994. {
  995. var callbackfn = arguments.At(0);
  996. var thisArg = arguments.At(1);
  997. var len = GetLength();
  998. var callable = GetCallable(callbackfn);
  999. var a = Engine.Realm.Intrinsics.Array.ArrayCreate(len);
  1000. var args = _engine._jsValueArrayPool.RentArray(3);
  1001. args[2] = this;
  1002. for (uint k = 0; k < len; k++)
  1003. {
  1004. if (TryGetValue(k, out var kvalue))
  1005. {
  1006. args[0] = kvalue;
  1007. args[1] = k;
  1008. var mappedValue = callable.Call(thisArg, args);
  1009. if (a._dense != null && k < (uint) a._dense.Length)
  1010. {
  1011. a._dense[k] = mappedValue;
  1012. }
  1013. else
  1014. {
  1015. a.WriteArrayValue(k, mappedValue);
  1016. }
  1017. }
  1018. }
  1019. _engine._jsValueArrayPool.ReturnArray(args);
  1020. return a;
  1021. }
  1022. /// <inheritdoc />
  1023. internal sealed override bool FindWithCallback(
  1024. JsValue[] arguments,
  1025. out uint index,
  1026. out JsValue value,
  1027. bool visitUnassigned,
  1028. bool fromEnd = false)
  1029. {
  1030. var thisArg = arguments.At(1);
  1031. var callbackfn = arguments.At(0);
  1032. var callable = GetCallable(callbackfn);
  1033. var len = GetLength();
  1034. if (len == 0)
  1035. {
  1036. index = 0;
  1037. value = Undefined;
  1038. return false;
  1039. }
  1040. var args = _engine._jsValueArrayPool.RentArray(3);
  1041. args[2] = this;
  1042. if (!fromEnd)
  1043. {
  1044. for (uint k = 0; k < len; k++)
  1045. {
  1046. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  1047. {
  1048. kvalue ??= Undefined;
  1049. args[0] = kvalue;
  1050. args[1] = k;
  1051. var testResult = callable.Call(thisArg, args);
  1052. if (TypeConverter.ToBoolean(testResult))
  1053. {
  1054. index = k;
  1055. value = kvalue;
  1056. return true;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. else
  1062. {
  1063. for (long k = len - 1; k >= 0; k--)
  1064. {
  1065. var idx = (uint) k;
  1066. if (TryGetValue(idx, out var kvalue) || visitUnassigned)
  1067. {
  1068. kvalue ??= Undefined;
  1069. args[0] = kvalue;
  1070. args[1] = idx;
  1071. var testResult = callable.Call(thisArg, args);
  1072. if (TypeConverter.ToBoolean(testResult))
  1073. {
  1074. index = idx;
  1075. value = kvalue;
  1076. return true;
  1077. }
  1078. }
  1079. }
  1080. }
  1081. _engine._jsValueArrayPool.ReturnArray(args);
  1082. index = 0;
  1083. value = Undefined;
  1084. return false;
  1085. }
  1086. public sealed override uint Length => GetLength();
  1087. internal sealed override bool IsIntegerIndexedArray => true;
  1088. public JsValue this[uint index]
  1089. {
  1090. get
  1091. {
  1092. TryGetValue(index, out var kValue);
  1093. return kValue;
  1094. }
  1095. set
  1096. {
  1097. SetIndexValue(index, value, updateLength: true);
  1098. }
  1099. }
  1100. public JsValue this[int index]
  1101. {
  1102. get
  1103. {
  1104. JsValue? kValue;
  1105. if (index >= 0)
  1106. {
  1107. TryGetValue((uint) index, out kValue);
  1108. }
  1109. else
  1110. {
  1111. // slow path
  1112. TryGetValue(JsNumber.Create(index), out kValue);
  1113. }
  1114. return kValue;
  1115. }
  1116. set
  1117. {
  1118. if (index >= 0)
  1119. {
  1120. SetIndexValue((uint) index, value, updateLength: true);
  1121. }
  1122. else
  1123. {
  1124. Set(index, value);
  1125. }
  1126. }
  1127. }
  1128. /// <summary>
  1129. /// Fast path for concatenating sane-sized arrays, we assume size has been calculated.
  1130. /// </summary>
  1131. internal void CopyValues(JsArray source, uint sourceStartIndex, uint targetStartIndex, uint length)
  1132. {
  1133. if (length == 0)
  1134. {
  1135. return;
  1136. }
  1137. var sourceDense = source._dense;
  1138. if (sourceDense is not null)
  1139. {
  1140. EnsureCapacity((uint) (targetStartIndex + sourceDense.LongLength));
  1141. }
  1142. var dense = _dense;
  1143. if (dense != null && sourceDense != null
  1144. && (uint) dense.Length >= targetStartIndex + length
  1145. && dense[targetStartIndex] is null)
  1146. {
  1147. uint j = 0;
  1148. for (var i = sourceStartIndex; i < sourceStartIndex + length; ++i, j++)
  1149. {
  1150. object? sourceValue;
  1151. if (i < (uint) sourceDense.Length && sourceDense[i] != null)
  1152. {
  1153. sourceValue = sourceDense[i];
  1154. if (sourceValue is PropertyDescriptor propertyDescriptor)
  1155. {
  1156. sourceValue = UnwrapJsValue(propertyDescriptor);
  1157. }
  1158. }
  1159. else
  1160. {
  1161. if (!source.TryGetValue(i, out var temp))
  1162. {
  1163. sourceValue = source.Prototype?.Get(JsString.Create(i));
  1164. }
  1165. else
  1166. {
  1167. sourceValue = temp;
  1168. }
  1169. }
  1170. dense[targetStartIndex + j] = sourceValue;
  1171. }
  1172. }
  1173. else
  1174. {
  1175. // slower version
  1176. for (uint k = sourceStartIndex; k < length; k++)
  1177. {
  1178. if (source.TryGetValue(k, out var subElement))
  1179. {
  1180. SetIndexValue(targetStartIndex, subElement, updateLength: false);
  1181. }
  1182. targetStartIndex++;
  1183. }
  1184. }
  1185. }
  1186. public sealed override string ToString()
  1187. {
  1188. // debugger can make things hard when evaluates computed values
  1189. return "(" + (_length?._value!.AsNumber() ?? 0) + ")[]";
  1190. }
  1191. private static void ThrowMaximumArraySizeReachedException(Engine engine, uint capacity)
  1192. {
  1193. ExceptionHelper.ThrowMemoryLimitExceededException(
  1194. $"The array size {capacity} is larger than maximum allowed ({engine.Options.Constraints.MaxArraySize})"
  1195. );
  1196. }
  1197. }
  1198. internal static class ArrayPropertyDescriptorExtensions
  1199. {
  1200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1201. internal static bool IsDefaultArrayValueDescriptor(this PropertyDescriptor propertyDescriptor)
  1202. => propertyDescriptor.Flags == PropertyFlag.ConfigurableEnumerableWritable && propertyDescriptor.IsDataDescriptor();
  1203. }
  1204. }