JavascriptArray.inl 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. //
  9. // Walks all the nodes in this BTree in sorted order.
  10. //
  11. template<typename Func>
  12. void SegmentBTree::Walk(Func& func) const
  13. {
  14. if (!IsLeaf())
  15. {
  16. children[0].Walk(func);
  17. }
  18. for (unsigned int i = 0; i < segmentCount; i++)
  19. {
  20. Assert(keys[i] == segments[i]->left);
  21. func(segments[i]);
  22. if (!IsLeaf())
  23. {
  24. children[i + 1].Walk(func);
  25. }
  26. }
  27. }
  28. template <typename Fn>
  29. SparseArraySegmentBase *
  30. JavascriptArray::ForEachSegment(Fn fn) const
  31. {
  32. return ForEachSegment(this->head, fn);
  33. }
  34. template <typename Fn>
  35. SparseArraySegmentBase *
  36. JavascriptArray::ForEachSegment(SparseArraySegmentBase * segment, Fn fn)
  37. {
  38. DebugOnly(uint32 lastindex = segment? segment->left : 0);
  39. SparseArraySegmentBase * current = segment;
  40. while (current)
  41. {
  42. // Verify that all the segment are sorted
  43. Assert(current->left >= lastindex);
  44. if (fn(current))
  45. {
  46. break;
  47. }
  48. DebugOnly(lastindex = current->left + current->length);
  49. current = current->next;
  50. }
  51. return current;
  52. }
  53. //
  54. // Link prev and current. If prev is NULL, make current the head segment.
  55. //
  56. template<>
  57. inline void JavascriptArray::LinkSegments(SparseArraySegment<int>* prev, SparseArraySegment<int>* current)
  58. {
  59. if (prev && prev->next == nullptr && SparseArraySegmentBase::IsLeafSegment(prev, this->GetScriptContext()->GetRecycler()))
  60. {
  61. prev = this->ReallocNonLeafSegment(prev, current);
  62. }
  63. else
  64. {
  65. LinkSegmentsCommon(prev, current);
  66. }
  67. }
  68. template<>
  69. inline void JavascriptArray::LinkSegments(SparseArraySegment<double>* prev, SparseArraySegment<double>* current)
  70. {
  71. if (prev && prev->next == nullptr && SparseArraySegmentBase::IsLeafSegment(prev, this->GetScriptContext()->GetRecycler()))
  72. {
  73. prev = this->ReallocNonLeafSegment(prev, current);
  74. }
  75. else
  76. {
  77. LinkSegmentsCommon(prev, current);
  78. }
  79. }
  80. template<typename T>
  81. inline void JavascriptArray::LinkSegments(SparseArraySegment<T>* prev, SparseArraySegment<T>* current)
  82. {
  83. LinkSegmentsCommon(prev, current);
  84. }
  85. template<typename T>
  86. inline SparseArraySegment<T>* JavascriptArray::ReallocNonLeafSegment(SparseArraySegment<T> *seg, SparseArraySegmentBase* nextSeg, bool forceNonLeaf)
  87. {
  88. // Find the segment prior to seg.
  89. SparseArraySegmentBase *prior = nullptr;
  90. if (seg != this->head)
  91. {
  92. for (prior = this->head; prior->next != seg; prior = prior->next)
  93. {
  94. Assert(prior->next);
  95. }
  96. }
  97. SparseArraySegment<T> *newSeg = nullptr;
  98. Recycler *recycler = this->GetScriptContext()->GetRecycler();
  99. if (forceNonLeaf)
  100. {
  101. newSeg = SparseArraySegment<T>::template AllocateSegmentImpl<false /*isLeaf*/>(recycler, seg->left, seg->length, nextSeg);
  102. }
  103. else
  104. {
  105. newSeg = SparseArraySegment<T>::AllocateSegment(recycler, seg->left, seg->length, nextSeg);
  106. }
  107. CopyArray(newSeg->elements, seg->length, seg->elements, seg->length);
  108. LinkSegmentsCommon(prior, newSeg);
  109. LinkSegmentsCommon(newSeg, nextSeg);
  110. if (GetLastUsedSegment() == seg)
  111. {
  112. SetLastUsedSegment(newSeg);
  113. }
  114. SegmentBTree * segmentMap = GetSegmentMap();
  115. if (segmentMap)
  116. {
  117. segmentMap->SwapSegment(seg->left, seg, newSeg);
  118. }
  119. return newSeg;
  120. }
  121. /*static*/
  122. template<typename T, uint InlinePropertySlots>
  123. inline SparseArraySegment<typename T::TElement> *JavascriptArray::InitArrayAndHeadSegment(
  124. T *const array,
  125. const uint32 length,
  126. const uint32 size,
  127. const bool wasZeroAllocated)
  128. {
  129. Assert(!array->HasSegmentMap());
  130. SparseArraySegment<typename T::TElement>* head =
  131. DetermineInlineHeadSegmentPointer<T, InlinePropertySlots, false>(array);
  132. if(wasZeroAllocated)
  133. {
  134. if(length != 0)
  135. {
  136. head->length = length;
  137. }
  138. head->size = size;
  139. head->CheckLengthvsSize();
  140. }
  141. else
  142. {
  143. new(head) SparseArraySegment<typename T::TElement>(0, length, size);
  144. }
  145. array->SetHeadAndLastUsedSegment(head);
  146. array->SetHasNoMissingValues();
  147. return head;
  148. }
  149. template<typename unitType, typename className>
  150. inline className * JavascriptArray::New(Recycler * recycler, DynamicType * type)
  151. {
  152. size_t allocationPlusSize;
  153. uint alignedInlineElementSlots;
  154. DetermineAllocationSizeForArrayObjects<className, 0>(
  155. SparseArraySegmentBase::SMALL_CHUNK_SIZE,
  156. &allocationPlusSize,
  157. &alignedInlineElementSlots);
  158. return RecyclerNewPlusZ(recycler, allocationPlusSize, className, type, alignedInlineElementSlots);
  159. }
  160. /*static*/
  161. template<typename unitType, typename className, uint inlineSlots>
  162. className* JavascriptArray::New(uint32 length, DynamicType* arrayType, Recycler* recycler)
  163. {
  164. CompileAssert(static_cast<PropertyIndex>(inlineSlots) == inlineSlots);
  165. Assert(DynamicTypeHandler::RoundUpInlineSlotCapacity(static_cast<PropertyIndex>(inlineSlots)) == inlineSlots);
  166. if(length > SparseArraySegmentBase::HEAD_CHUNK_SIZE)
  167. {
  168. // Use empty segment until we try to store something. Call AllocateHead() at that point.
  169. return RecyclerNew(recycler, className, length, arrayType);
  170. }
  171. size_t allocationPlusSize;
  172. uint alignedInlineElementSlots;
  173. className* array;
  174. DetermineAllocationSizeForArrayObjects<className, inlineSlots>(length, &allocationPlusSize, &alignedInlineElementSlots);
  175. array = RecyclerNewPlusZ(recycler, allocationPlusSize, className, length, arrayType);
  176. SparseArraySegment<unitType> *head =
  177. InitArrayAndHeadSegment<className, inlineSlots>(array, 0, alignedInlineElementSlots, true);
  178. head->FillSegmentBuffer(0, alignedInlineElementSlots);
  179. return array;
  180. }
  181. //
  182. // Allocates the segment inline up to the length of SparseArraySegmentBase::INLINE_CHUNK_SIZE. The downside of having the segment
  183. // inline is that the segment space will never get freed unless the Array is collected.
  184. //
  185. /*static*/
  186. template<typename unitType, typename className, uint inlineSlots>
  187. className* JavascriptArray::NewLiteral(uint32 length, DynamicType* arrayType, Recycler* recycler)
  188. {
  189. CompileAssert(static_cast<PropertyIndex>(inlineSlots) == inlineSlots);
  190. Assert(DynamicTypeHandler::RoundUpInlineSlotCapacity(static_cast<PropertyIndex>(inlineSlots)) == inlineSlots);
  191. className* array;
  192. if(HasInlineHeadSegment(length))
  193. {
  194. size_t allocationPlusSize;
  195. uint alignedInlineElementSlots;
  196. if(!length)
  197. {
  198. DetermineAllocationSize<className, inlineSlots>(
  199. SparseArraySegmentBase::SMALL_CHUNK_SIZE,
  200. &allocationPlusSize,
  201. &alignedInlineElementSlots);
  202. }
  203. else
  204. {
  205. DetermineAllocationSize<className, inlineSlots>(length, &allocationPlusSize, &alignedInlineElementSlots);
  206. }
  207. array = RecyclerNewPlusZ(recycler, allocationPlusSize, className, length, arrayType);
  208. // An new array's head segment length is initialized to zero despite the array length being nonzero because the segment
  209. // doesn't have any values to begin with. An array literal though, is initialized with special op-codes that just store
  210. // the values and don't update the length, so update the length here.
  211. //
  212. // An array literal is also guaranteed to be fully initialized, so even though the head segment currently will have
  213. // missing values (after this update to length), it won't have missing values once the initialization is complete, so
  214. // maintain the state saying "does not have missing values". Furthermore, since the new array literal is not assigned to
  215. // a variable until it is fully initialized, there is no way for script code to use the array while it still has missing
  216. // values.
  217. SparseArraySegment<unitType> *head =
  218. InitArrayAndHeadSegment<className, inlineSlots>(array, length, alignedInlineElementSlots, true);
  219. head->FillSegmentBuffer(length, alignedInlineElementSlots);
  220. Assert(array->HasNoMissingValues());
  221. return array;
  222. }
  223. size_t allocationPlusSize;
  224. DetermineAllocationSize<className, inlineSlots>(0, &allocationPlusSize);
  225. array = RecyclerNewPlusZ(recycler, allocationPlusSize, className, length, arrayType);
  226. SparseArraySegment<unitType> *seg = SparseArraySegment<unitType>::AllocateLiteralHeadSegment(recycler, length);
  227. array->SetHeadAndLastUsedSegment(seg);
  228. array->SetHasNoMissingValues();
  229. // An new array's head segment length is initialized to zero despite the array length being nonzero because the segment
  230. // doesn't have any values to begin with. An array literal though, is initialized with special op-codes that just store
  231. // the values and don't update the length, so update the length here.
  232. //
  233. // An array literal is also guaranteed to be fully initialized, so even though the head segment currently will have
  234. // missing values (after this update to length), it won't have missing values once the initialization is complete, so
  235. // maintain the state saying "does not have missing values". Furthermore, since the new array literal is not assigned to
  236. // a variable until it is fully initialized, there is no way for script code to use the array while it still has missing
  237. // values.
  238. array->head->length = length;
  239. array->head->CheckLengthvsSize();
  240. return array;
  241. }
  242. #if ENABLE_COPYONACCESS_ARRAY
  243. //
  244. // Allocates the segment inline up to the length of SparseArraySegmentBase::INLINE_CHUNK_SIZE. The downside of having the segment
  245. // inline is that the segment space will never get freed unless the Array is collected.
  246. //
  247. /*static*/
  248. template<typename unitType, typename className, uint inlineSlots>
  249. className* JavascriptArray::NewCopyOnAccessLiteral(DynamicType* arrayType, ArrayCallSiteInfo *arrayInfo, FunctionBody *functionBody, const Js::AuxArray<int32> *ints, Recycler* recycler)
  250. {
  251. CompileAssert(static_cast<PropertyIndex>(inlineSlots) == inlineSlots);
  252. Assert(DynamicTypeHandler::RoundUpInlineSlotCapacity(static_cast<PropertyIndex>(inlineSlots)) == inlineSlots);
  253. Assert(arrayInfo->IsNativeIntArray());
  254. className* array = RecyclerNewZ(recycler, JavascriptCopyOnAccessNativeIntArray, ints->count, arrayType);
  255. JavascriptLibrary *lib = functionBody->GetScriptContext()->GetLibrary();
  256. SparseArraySegment<unitType> *seg;
  257. if (JavascriptLibrary::IsCachedCopyOnAccessArrayCallSite(functionBody->GetScriptContext()->GetLibrary() , arrayInfo))
  258. {
  259. seg = lib->cacheForCopyOnAccessArraySegments->GetSegmentByIndex(arrayInfo->copyOnAccessArrayCacheIndex);
  260. }
  261. else
  262. {
  263. seg = SparseArraySegment<unitType>::AllocateLiteralHeadSegment(recycler, ints->count);
  264. }
  265. if (!JavascriptLibrary::IsCachedCopyOnAccessArrayCallSite(lib, arrayInfo))
  266. {
  267. JavascriptOperators::AddIntsToArraySegment(seg, ints);
  268. arrayInfo->copyOnAccessArrayCacheIndex = lib->cacheForCopyOnAccessArraySegments->AddSegment(seg);
  269. }
  270. array->SetHeadAndLastUsedSegment(reinterpret_cast<SparseArraySegmentBase *>(arrayInfo->copyOnAccessArrayCacheIndex)); // storing index in head on purpose: expect AV if treated as other array objects
  271. #if ENABLE_DEBUG_CONFIG_OPTIONS
  272. if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::CopyOnAccessArrayPhase))
  273. {
  274. Output::Print(_u("Create copy-on-access array: func(#%2d) index(%d) length(%d)\n"),
  275. functionBody->GetFunctionNumber(), lib->cacheForCopyOnAccessArraySegments->GetCount(), ints->count);
  276. Output::Flush();
  277. }
  278. #endif
  279. return array;
  280. }
  281. #endif
  282. template<class T, uint InlinePropertySlots>
  283. inline T *JavascriptArray::New(
  284. void *const stackAllocationPointer,
  285. const uint32 length,
  286. DynamicType *const arrayType)
  287. {
  288. Assert(arrayType);
  289. if(stackAllocationPointer)
  290. {
  291. bool isSufficientSpaceForInlinePropertySlots;
  292. const uint availableInlineElementSlots =
  293. DetermineAvailableInlineElementSlots<T, InlinePropertySlots>(
  294. T::StackAllocationSize,
  295. &isSufficientSpaceForInlinePropertySlots);
  296. if(isSufficientSpaceForInlinePropertySlots)
  297. {
  298. T *const array = new(stackAllocationPointer) T(length, arrayType);
  299. if(length <= availableInlineElementSlots)
  300. {
  301. SparseArraySegment<typename T::TElement> *const head =
  302. InitArrayAndHeadSegment<T, InlinePropertySlots>(array, 0, availableInlineElementSlots, false);
  303. head->FillSegmentBuffer(0, availableInlineElementSlots);
  304. }
  305. else
  306. {
  307. // Not enough room to allocate all required element slots inline. Leave the head segment as the empty
  308. // segment and let it be allocated as necessary.
  309. }
  310. Assert(array->HasNoMissingValues());
  311. return array;
  312. }
  313. }
  314. return New<typename T::TElement, T, InlinePropertySlots>(length, arrayType, arrayType->GetRecycler());
  315. }
  316. template<class T, uint InlinePropertySlots>
  317. inline T *JavascriptArray::NewLiteral(
  318. void *const stackAllocationPointer,
  319. const uint32 length,
  320. DynamicType *const arrayType)
  321. {
  322. Assert(arrayType);
  323. if(stackAllocationPointer)
  324. {
  325. bool isSufficientSpaceForInlinePropertySlots;
  326. const uint availableInlineElementSlots =
  327. DetermineAvailableInlineElementSlots<T, InlinePropertySlots>(
  328. T::StackAllocationSize,
  329. &isSufficientSpaceForInlinePropertySlots);
  330. if(isSufficientSpaceForInlinePropertySlots)
  331. {
  332. T *const array = new(stackAllocationPointer) T(length, arrayType);
  333. if(length <= availableInlineElementSlots)
  334. {
  335. SparseArraySegment<typename T::TElement> *const head =
  336. InitArrayAndHeadSegment<T, InlinePropertySlots>(array, length, availableInlineElementSlots, false);
  337. head->FillSegmentBuffer(length, availableInlineElementSlots);
  338. Assert(array->HasNoMissingValues());
  339. return array;
  340. }
  341. // Not enough room to allocate all required element slots inline. Allocate the head segment separately.
  342. SparseArraySegment<typename T::TElement> *const head =
  343. SparseArraySegment<typename T::TElement>::AllocateLiteralHeadSegment(arrayType->GetRecycler(), length);
  344. array->SetHeadAndLastUsedSegment(head);
  345. array->SetHasNoMissingValues();
  346. return array;
  347. }
  348. }
  349. return NewLiteral<typename T::TElement, T, InlinePropertySlots>(length, arrayType, arrayType->GetRecycler());
  350. }
  351. template<typename T>
  352. inline void JavascriptArray::DirectSetItemAt(uint32 itemIndex, T newValue)
  353. {
  354. Assert(itemIndex < InvalidIndex); // Otherwise the code below could overflow and set length = 0
  355. SparseArraySegment<T> *seg = (SparseArraySegment<T>*)this->GetLastUsedSegment();
  356. uint32 offset = itemIndex - seg->left;
  357. if(itemIndex >= seg->left && offset < seg->size)
  358. {
  359. DirectSetItemInLastUsedSegmentAt(offset, newValue);
  360. return;
  361. }
  362. DirectSetItem_Full(itemIndex, newValue);
  363. }
  364. template<typename T>
  365. inline void JavascriptArray::DirectSetItemInLastUsedSegmentAt(const uint32 offset, const T newValue)
  366. {
  367. SparseArraySegment<T> *const seg = (SparseArraySegment<T>*)GetLastUsedSegment();
  368. Assert(seg);
  369. Assert(offset < seg->size);
  370. Assert(!(HasNoMissingValues() &&
  371. offset < seg->length &&
  372. SparseArraySegment<T>::IsMissingItem(&seg->elements[offset]) &&
  373. seg == head));
  374. const bool scanForMissingValues = NeedScanForMissingValuesUponSetItem(seg, offset);
  375. DebugOnly(VerifyNotNeedMarshal(newValue));
  376. seg->elements[offset] = newValue;
  377. if (offset >= seg->length)
  378. {
  379. if(offset > seg->length && seg == head)
  380. {
  381. SetHasNoMissingValues(false);
  382. }
  383. seg->length = offset + 1;
  384. seg->CheckLengthvsSize();
  385. const uint32 itemIndex = seg->left + offset;
  386. if (this->length <= itemIndex)
  387. {
  388. this->length = itemIndex + 1;
  389. }
  390. }
  391. else if(scanForMissingValues)
  392. {
  393. ScanForMissingValues<T>();
  394. }
  395. }
  396. #if ENABLE_PROFILE_INFO
  397. template<typename T>
  398. inline void JavascriptArray::DirectProfiledSetItemInHeadSegmentAt(
  399. const uint32 offset,
  400. const T newValue,
  401. StElemInfo *const stElemInfo)
  402. {
  403. SparseArraySegment<T> *const seg = SparseArraySegment<T>::From(head);
  404. Assert(seg);
  405. Assert(offset < seg->size);
  406. Assert(!(HasNoMissingValues() &&
  407. offset < seg->length &&
  408. SparseArraySegment<T>::IsMissingItem(&seg->elements[offset])));
  409. Assert(stElemInfo);
  410. stElemInfo->filledMissingValue = offset < seg->length && SparseArraySegment<T>::IsMissingItem(&seg->elements[offset]);
  411. const bool scanForMissingValues = NeedScanForMissingValuesUponSetItem(seg, offset);
  412. DebugOnly(VerifyNotNeedMarshal(newValue));
  413. seg->elements[offset] = newValue;
  414. if (offset >= seg->length)
  415. {
  416. if(offset > seg->length)
  417. {
  418. SetHasNoMissingValues(false);
  419. }
  420. seg->length = offset + 1;
  421. seg->CheckLengthvsSize();
  422. const uint32 itemIndex = seg->left + offset;
  423. if (this->length <= itemIndex)
  424. {
  425. this->length = itemIndex + 1;
  426. }
  427. }
  428. else if(scanForMissingValues)
  429. {
  430. ScanForMissingValues<T>();
  431. }
  432. }
  433. #endif
  434. template<typename T>
  435. inline BOOL JavascriptArray::DirectGetItemAt(uint32 index, T* outVal)
  436. {
  437. #ifdef VALIDATE_ARRAY
  438. ValidateArray();
  439. #endif
  440. if (index >= length)
  441. {
  442. return false;
  443. }
  444. #ifdef VALIDATE_ARRAY
  445. T v_btree = NULL;
  446. SparseArraySegmentBase* seg_btree = nullptr;
  447. bool first_pass = true;
  448. #endif
  449. SparseArraySegmentBase* nextSeg;
  450. SegmentBTreeRoot * segmentMap = GetSegmentMap();
  451. if (segmentMap)
  452. {
  453. SparseArraySegmentBase* matchOrNextSeg;
  454. segmentMap->Find(index, nextSeg, matchOrNextSeg);
  455. if (!nextSeg)
  456. {
  457. nextSeg = matchOrNextSeg;
  458. }
  459. }
  460. else
  461. {
  462. #ifdef VALIDATE_ARRAY
  463. SECOND_PASS:
  464. #endif
  465. nextSeg = this->GetBeginLookupSegment(index, false);
  466. }
  467. uint probeCost = 0;
  468. while (nextSeg != nullptr && nextSeg->left <= index)
  469. {
  470. uint32 limit = nextSeg->left + nextSeg->length;
  471. if (index < limit)
  472. {
  473. const T * v = AddressOf(((SparseArraySegment<T>*)nextSeg)->elements[index - nextSeg->left]);
  474. this->SetLastUsedSegment(nextSeg);
  475. #ifdef VALIDATE_ARRAY
  476. Assert(segmentMap == GetSegmentMap());
  477. if (segmentMap && first_pass)
  478. {
  479. v_btree = *v;
  480. seg_btree= nextSeg;
  481. first_pass = false;
  482. goto SECOND_PASS;
  483. }
  484. else if (segmentMap && !first_pass)
  485. {
  486. Assert(seg_btree == nextSeg);
  487. }
  488. #endif
  489. if (SparseArraySegment<T>::IsMissingItem(v))
  490. {
  491. Assert(!(HasNoMissingValues() && nextSeg == head));
  492. return false;
  493. }
  494. *outVal = *v;
  495. return true;
  496. }
  497. nextSeg = nextSeg->next;
  498. Assert(segmentMap == GetSegmentMap());
  499. if (!segmentMap)
  500. {
  501. probeCost++;
  502. if (probeCost > SegmentBTree::GetLazyCrossOverLimit() && this->head != EmptySegment)
  503. {
  504. // Build a SegmentMap
  505. segmentMap = BuildSegmentMap();
  506. // Find the right segment
  507. SparseArraySegmentBase* matchOrNextSeg;
  508. segmentMap->Find(index, nextSeg, matchOrNextSeg);
  509. if (!nextSeg)
  510. {
  511. nextSeg = matchOrNextSeg;
  512. }
  513. }
  514. }
  515. }
  516. #ifdef VALIDATE_ARRAY
  517. Assert(segmentMap == GetSegmentMap());
  518. if (segmentMap && first_pass)
  519. {
  520. v_btree = NULL;
  521. seg_btree= nullptr;
  522. first_pass = false;
  523. goto SECOND_PASS;
  524. }
  525. else if (segmentMap && !first_pass)
  526. {
  527. Assert(v_btree == NULL && seg_btree == nullptr);
  528. }
  529. #endif
  530. return false;
  531. }
  532. template<typename T>
  533. void JavascriptArray::EnsureHead()
  534. {
  535. if (this->head == EmptySegment)
  536. {
  537. this->AllocateHead<T>();
  538. }
  539. }
  540. template<typename T>
  541. void JavascriptArray::AllocateHead()
  542. {
  543. Recycler* recycler = GetRecycler();
  544. uint32 allocLength;
  545. Assert(this->head == EmptySegment);
  546. if (this->length)
  547. {
  548. allocLength = this->length <= MaxInitialDenseLength ? this->length : SparseArraySegmentBase::HEAD_CHUNK_SIZE;
  549. this->head = SparseArraySegment<T>::AllocateSegment(recycler, 0, 0, allocLength, nullptr);
  550. }
  551. else
  552. {
  553. allocLength = SparseArraySegmentBase::HEAD_CHUNK_SIZE;
  554. this->head = SparseArraySegment<T>::AllocateSegment(recycler, 0, 0, allocLength, nullptr);
  555. }
  556. this->SetLastUsedSegment(this->head);
  557. SetHasNoMissingValues();
  558. }
  559. template<typename T>
  560. SparseArraySegment<T>* JavascriptArray::PrepareSegmentForMemOp(uint32 startIndex, uint32 length)
  561. {
  562. uint32 endIndex;
  563. if(UInt32Math::Add(startIndex, length - 1, &endIndex))
  564. {
  565. return nullptr;
  566. }
  567. if (endIndex >= this->length)
  568. {
  569. if (endIndex < JavascriptArray::InvalidIndex)
  570. {
  571. this->length = endIndex + 1;
  572. }
  573. else
  574. {
  575. return nullptr;
  576. }
  577. }
  578. this->EnsureHead<T>();
  579. Recycler* recycler = GetRecycler();
  580. //Find the segment where itemIndex is present or is at the boundary
  581. SparseArraySegment<T>* current = (SparseArraySegment<T>*)this->GetBeginLookupSegment(startIndex, false);
  582. SparseArraySegmentBase* prev = nullptr;
  583. SparseArraySegmentBase* startSeg = nullptr;
  584. SparseArraySegmentBase* endSeg = nullptr;
  585. SparseArraySegmentBase* startPrev = nullptr;
  586. uint32 growby, startOffset, endOffset;
  587. const auto FindStartAndEndSegment = [&]()
  588. {
  589. if (endIndex >= current->left + current->size)
  590. {
  591. current = SparseArraySegment<T>::From(head);
  592. }
  593. else
  594. {
  595. startSeg = endSeg = current;
  596. current = nullptr;
  597. }
  598. while (current != nullptr)
  599. {
  600. startOffset = startIndex - current->left;
  601. endOffset = endIndex - current->left;
  602. if (!startSeg)
  603. {
  604. if (startIndex <= current->left)
  605. {
  606. startPrev = prev;
  607. startSeg = current;
  608. }
  609. else if (startOffset <= current->size)
  610. {
  611. if ((nullptr == current->next) || (startIndex < current->next->left))
  612. {
  613. startPrev = prev;
  614. startSeg = current;
  615. }
  616. }
  617. }
  618. if (!endSeg)
  619. {
  620. if (endIndex <= current->left)
  621. {
  622. endSeg = current;
  623. break;
  624. }
  625. else if (endOffset <= current->size)
  626. {
  627. if ((nullptr == current->next) || (endIndex < current->next->left))
  628. {
  629. endSeg = current;
  630. break;
  631. }
  632. }
  633. }
  634. prev = current;
  635. current = SparseArraySegment<T>::From(current->next);
  636. }
  637. if (!startSeg && !endSeg)
  638. {
  639. startPrev = prev;
  640. }
  641. };
  642. const auto ResizeArrayIfStartIsOutsideArrayLength = [&]()
  643. {
  644. Assert(endSeg == nullptr);
  645. Assert(startIndex >= head->size);
  646. // Reallocate head if it meets a heuristics
  647. if (startPrev == head // prev segment is the head segment
  648. && !head->next // There is only one head segment in the array
  649. && startIndex - head->size <= MergeSegmentsLengthHeuristics // Distance to next index is relatively small
  650. )
  651. {
  652. current = SparseArraySegment<T>::From(head)->GrowByMin(recycler, startIndex + length - head->size);
  653. current->length = endIndex + 1;
  654. current->CheckLengthvsSize();
  655. head = current;
  656. SetHasNoMissingValues(false);
  657. }
  658. else
  659. {
  660. //itemIndex is greater than the (left + size) of last segment in the linked list
  661. current = SparseArraySegment<T>::AllocateSegment(recycler, startIndex, length, (SparseArraySegment<T> *)nullptr);
  662. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  663. current->length = length;
  664. current->CheckLengthvsSize();
  665. if (current == head)
  666. {
  667. Assert(startIndex == 0);
  668. Assert(current->length == length);
  669. SetHasNoMissingValues();
  670. }
  671. }
  672. };
  673. const auto ExtendStartSegmentForMemOp = [&]()
  674. {
  675. startOffset = startIndex - startSeg->left;
  676. if ((startIndex >= startSeg->left) && (startOffset < startSeg->size))
  677. {
  678. // startIndex is within startSeg
  679. if ((startOffset + length) > startSeg->size)
  680. {
  681. // if we don't have enough space in startSeg
  682. growby = length - (startSeg->size - startOffset);
  683. current = ((Js::SparseArraySegment<T>*)startSeg)->GrowByMin(recycler, growby);
  684. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  685. if (current == head)
  686. {
  687. if (startIndex > current->length)
  688. {
  689. // if it's the head segment and memset starts after the segment length, missing value is introduced
  690. SetHasNoMissingValues(false);
  691. }
  692. else if (!HasNoMissingValues())
  693. {
  694. // Have we overwritten all the missing values?
  695. if (!ScanForMissingValues<T>(0, startOffset))
  696. {
  697. SetHasNoMissingValues();
  698. }
  699. }
  700. }
  701. current->length = startOffset + length;
  702. current->CheckLengthvsSize();
  703. }
  704. else
  705. {
  706. // if we have enough space in the startseg
  707. current = (Js::SparseArraySegment<T>*)startSeg;
  708. if (current == head)
  709. {
  710. if (startIndex > current->length)
  711. {
  712. // if it's the head segment and memset starts after the segment length, missing value is introduced
  713. SetHasNoMissingValues(false);
  714. }
  715. else if (!HasNoMissingValues())
  716. {
  717. // Have we overwritten all the missing values?
  718. if (!ScanForMissingValues<T>(0, startOffset))
  719. {
  720. SetHasNoMissingValues();
  721. }
  722. }
  723. }
  724. current->length = current->length > (startOffset + length) ? current->length : (startOffset + length);
  725. current->CheckLengthvsSize();
  726. }
  727. }
  728. else if ((startIndex + 1) <= startSeg->left)
  729. {
  730. if (startIndex + 1 == startSeg->left && startPrev == head)
  731. {
  732. current = SparseArraySegment<T>::From(head)->GrowByMin(recycler, startIndex + length - head->size);
  733. current->length = endIndex + 1;
  734. current->CheckLengthvsSize();
  735. head = current;
  736. }
  737. else
  738. {
  739. // startIndex is in between prev and startIndex
  740. current = SparseArraySegment<T>::AllocateSegment(recycler, startIndex, length, (SparseArraySegment<T> *)nullptr);
  741. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  742. if (current == head)
  743. {
  744. SetHasNoMissingValues();
  745. }
  746. current->length = length;
  747. current->CheckLengthvsSize();
  748. }
  749. }
  750. else
  751. {
  752. Assert(startIndex == startSeg->left + startSeg->size);
  753. current = ((Js::SparseArraySegment<T>*)startSeg)->GrowByMin(recycler, length);
  754. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  755. if (current == head)
  756. {
  757. if (startIndex > current->length)
  758. {
  759. // if it's the head segment and memset starts after the segment length, missing value is introduced
  760. SetHasNoMissingValues(false);
  761. }
  762. }
  763. current->length = startOffset + length;
  764. current->CheckLengthvsSize();
  765. }
  766. startSeg = current;
  767. };
  768. const auto AppendLeftOverItemsFromEndSegment = [&]()
  769. {
  770. if (!endSeg)
  771. {
  772. // end is beyond the length of the array
  773. Assert(endIndex == (current->left + current->length - 1));
  774. current->next = nullptr;
  775. }
  776. else
  777. {
  778. endOffset = endIndex - endSeg->left;
  779. startOffset = startIndex - current->left;
  780. if ((endIndex >= endSeg->left) && (endOffset < endSeg->size))
  781. {
  782. // endIndex is within endSeg
  783. if (endSeg->length - 1 > endOffset)
  784. {
  785. if (startSeg != endSeg)
  786. {
  787. // we have some leftover items on endseg
  788. growby = (endSeg->length - endOffset - 1);
  789. current = current->GrowByMin(recycler, growby);
  790. CopyArray(current->elements + startOffset + length, growby,
  791. ((Js::SparseArraySegment<T>*)endSeg)->elements + endOffset + 1, growby);
  792. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  793. current->length = startOffset + length + growby;
  794. current->CheckLengthvsSize();
  795. }
  796. if (current == head && HasNoMissingValues())
  797. {
  798. if (ScanForMissingValues<T>(startOffset + length, current->length))
  799. {
  800. SetHasNoMissingValues(false);
  801. }
  802. }
  803. }
  804. current->next = endSeg->next;
  805. }
  806. else if ((endIndex + 1) <= endSeg->left)
  807. {
  808. // endIndex is between endSeg and the segment before
  809. if (endIndex + 1 == endSeg->left && current == head)
  810. {
  811. // extend current to hold endSeg
  812. growby = endSeg->length;
  813. current = current->GrowByMin(recycler, growby);
  814. CopyArray(current->elements + endIndex + 1, endSeg->length,
  815. ((Js::SparseArraySegment<T>*)endSeg)->elements, endSeg->length);
  816. LinkSegments((Js::SparseArraySegment<T>*)startPrev, current);
  817. if (HasNoMissingValues())
  818. {
  819. if (ScanForMissingValues<T>(endIndex + 1, endIndex + growby))
  820. {
  821. SetHasNoMissingValues(false);
  822. }
  823. }
  824. current->length = endIndex + growby + 1;
  825. current->CheckLengthvsSize();
  826. current->next = endSeg->next;
  827. }
  828. else
  829. {
  830. current->next = endSeg;
  831. }
  832. }
  833. else
  834. {
  835. //endIndex is at the boundary of endSeg segment at the left + size
  836. Assert(endIndex == endSeg->left + endSeg->size);
  837. current->next = endSeg->next;
  838. }
  839. }
  840. };
  841. FindStartAndEndSegment();
  842. if (startSeg == nullptr)
  843. {
  844. // if start index is greater than array length then we can add a new segment (or extend the last segment based on some heuristics)
  845. ResizeArrayIfStartIsOutsideArrayLength();
  846. }
  847. else
  848. {
  849. // once we found the start segment we extend the start segment until startIndex+length . We don't care about what was there
  850. // as they will be overwritten by the memset/ memcopy. Then we need to append items from the (startIndex+length) to array.length
  851. // from the end segment to the new array
  852. ExtendStartSegmentForMemOp();
  853. AppendLeftOverItemsFromEndSegment();
  854. }
  855. Assert(current);
  856. Assert(current->left <= startIndex);
  857. Assert((startIndex - current->left) < current->size);
  858. return current;
  859. }
  860. template<typename T>
  861. bool JavascriptArray::DirectSetItemAtRangeFromArray(uint32 toStartIndex, uint32 length, JavascriptArray *fromArray, uint32 fromStartIndex)
  862. {
  863. if (length == 0)
  864. {
  865. return true;
  866. }
  867. // Do not do a memcopy from an array that has missing values
  868. if (fromArray == nullptr || fromArray == this || !fromArray->HasNoMissingValues())
  869. {
  870. return false;
  871. }
  872. bool isBtree = false;
  873. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  874. isBtree = Js::Configuration::Global.flags.ForceArrayBTree;
  875. #endif
  876. if (GetSegmentMap() || fromArray->GetSegmentMap() || isBtree)
  877. {
  878. for (uint i = 0; i < length; i++)
  879. {
  880. T val;
  881. if (!fromArray->DirectGetItemAt(fromStartIndex + i, &val))
  882. {
  883. return false;
  884. }
  885. DirectSetItem_Full(toStartIndex + i, val);
  886. }
  887. return true;
  888. }
  889. const auto isSegmentValid = [length](Js::SparseArraySegment<T>* segment, uint32 startIndex) {
  890. uint32 end, segmentEnd;
  891. // Check the segment is int32 enough
  892. return (
  893. segment &&
  894. !UInt32Math::Add(startIndex, length, &end) &&
  895. !UInt32Math::Add(segment->left, segment->length, &segmentEnd) &&
  896. startIndex >= segment->left &&
  897. startIndex < segmentEnd &&
  898. segmentEnd >= end
  899. );
  900. };
  901. //Find the segment where itemIndex is present or is at the boundary
  902. Js::SparseArraySegment<T>* fromSegment = (Js::SparseArraySegment<T>*)fromArray->GetBeginLookupSegment(fromStartIndex, false);
  903. if (!isSegmentValid(fromSegment, fromStartIndex))
  904. {
  905. return false;
  906. }
  907. // Check the from segment first so we don't prepare the toSegment in case it is invalid
  908. SparseArraySegment<T> *toSegment = PrepareSegmentForMemOp<T>(toStartIndex, length);
  909. if (!isSegmentValid(toSegment, toStartIndex))
  910. {
  911. return false;
  912. }
  913. Assert(GetSegmentMap() == nullptr && fromArray->GetSegmentMap() == nullptr);
  914. int memcopySize = length;
  915. int toStartOffset = toStartIndex - toSegment->left;
  916. int fromStartOffset = fromStartIndex - fromSegment->left;
  917. Assert((fromStartOffset + length) <= fromSegment->length);
  918. CopyArray(
  919. toSegment->elements + toStartOffset,
  920. toSegment->size - toStartOffset,
  921. fromSegment->elements + fromStartOffset,
  922. memcopySize
  923. );
  924. fromArray->SetLastUsedSegment(fromSegment);
  925. this->SetLastUsedSegment(toSegment);
  926. #if DBG
  927. if (Js::Configuration::Global.flags.MemOpMissingValueValidate)
  928. {
  929. if (toSegment == head)
  930. {
  931. Assert(ScanForMissingValues<T>(0, this->length) != HasNoMissingValues());
  932. }
  933. }
  934. #endif
  935. return true;
  936. }
  937. template<typename T>
  938. bool JavascriptArray::DirectSetItemAtRange(uint32 startIndex, uint32 length, T newValue)
  939. {
  940. bool isBtree = false;
  941. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  942. isBtree = Js::Configuration::Global.flags.ForceArrayBTree;
  943. #endif
  944. if (GetSegmentMap() || isBtree)
  945. {
  946. for (uint i = startIndex; i < startIndex + length; i++)
  947. {
  948. DirectSetItem_Full<T>(i, newValue);
  949. }
  950. return true;
  951. }
  952. if (startIndex == 0 && head != EmptySegment && length < head->size)
  953. {
  954. CopyValueToSegmentBuferNoCheck(SparseArraySegment<T>::From(head)->elements, length, newValue);
  955. if (length > this->length)
  956. {
  957. this->length = length;
  958. }
  959. if (length > head->length)
  960. {
  961. head->length = length;
  962. head->CheckLengthvsSize();
  963. }
  964. if (!HasNoMissingValues())
  965. {
  966. if (ScanForMissingValues<T>(length, head->length) == false)
  967. {
  968. SetHasNoMissingValues(true);
  969. }
  970. }
  971. this->SetLastUsedSegment(head);
  972. }
  973. else if (startIndex == 0 && length > this->length && (head == EmptySegment || length > head->size))
  974. {
  975. Recycler *recycler = GetRecycler();
  976. this->length = length;
  977. this->EnsureHead<T>();
  978. SparseArraySegmentBase* current = nullptr;
  979. Assert(head->size < length);
  980. current = SparseArraySegment<T>::AllocateSegment(recycler, 0, length, (SparseArraySegment<T> *)nullptr);
  981. this->SetHeadAndLastUsedSegment(current);
  982. Assert(!HasSegmentMap());
  983. SetHasNoMissingValues(true);
  984. CopyValueToSegmentBuferNoCheck(((Js::SparseArraySegment<T>*)current)->elements, length, newValue);
  985. this->SetLastUsedSegment(current);
  986. }
  987. else
  988. {
  989. DirectSetItemAtRangeFull<T>(startIndex, length, newValue);
  990. }
  991. return true;
  992. }
  993. template<typename T>
  994. bool JavascriptArray::DirectSetItemAtRangeFull(uint32 startIndex, uint32 length, T newValue)
  995. {
  996. if (length == 0)
  997. {
  998. return true;
  999. }
  1000. Assert(!GetSegmentMap());
  1001. SparseArraySegment<T> *current = PrepareSegmentForMemOp<T>(startIndex, length);
  1002. if (current == nullptr)
  1003. {
  1004. return false;
  1005. }
  1006. Assert(current->left + current->length >= startIndex + length);
  1007. Field(T)* segmentCopyStart = current->elements + (startIndex - current->left);
  1008. CopyValueToSegmentBuferNoCheck(segmentCopyStart, length, newValue);
  1009. this->SetLastUsedSegment(current);
  1010. #if DBG
  1011. if (Js::Configuration::Global.flags.MemOpMissingValueValidate)
  1012. {
  1013. if (current == head)
  1014. {
  1015. Assert(ScanForMissingValues<T>(0, this->length) != HasNoMissingValues());
  1016. }
  1017. }
  1018. #endif
  1019. return true;
  1020. }
  1021. template<typename T>
  1022. void JavascriptArray::DirectSetItem_Full(uint32 itemIndex, T newValue)
  1023. {
  1024. DebugOnly(VerifyNotNeedMarshal(newValue));
  1025. this->EnsureHead<T>();
  1026. AnalysisAssert(head);
  1027. #ifdef VALIDATE_ARRAY
  1028. ValidateArray();
  1029. #endif
  1030. if (itemIndex >= this->length)
  1031. {
  1032. if (itemIndex != JavascriptArray::InvalidIndex)
  1033. {
  1034. this->length = itemIndex + 1;
  1035. }
  1036. else
  1037. {
  1038. JavascriptError::ThrowRangeError(this->GetScriptContext(), JSERR_ArrayLengthAssignIncorrect);
  1039. }
  1040. }
  1041. Recycler* recycler = GetRecycler();
  1042. //Find the segment where itemIndex is present or is at the boundary
  1043. SparseArraySegment<T>* current = (SparseArraySegment<T>*)this->GetBeginLookupSegment(itemIndex, false);
  1044. // If it doesn't fit in current chunk (watch for overflow), start from beginning as we'll
  1045. // need the prev
  1046. if (current->left + current->size > current->left || itemIndex >= current->left + current->size)
  1047. {
  1048. current = SparseArraySegment<T>::From(head);
  1049. }
  1050. SparseArraySegmentBase* prev = nullptr;
  1051. #ifdef VALIDATE_ARRAY
  1052. SparseArraySegmentBase* current_btree = nullptr;
  1053. SparseArraySegmentBase* prev_btree = nullptr;
  1054. bool first_pass = true;
  1055. #endif
  1056. SegmentBTreeRoot * segmentMap = GetSegmentMap();
  1057. if (segmentMap)
  1058. {
  1059. SparseArraySegmentBase* prevSeg = nullptr;
  1060. SparseArraySegmentBase* currentBase = current;
  1061. segmentMap->Find(itemIndex, prevSeg, currentBase);
  1062. current = (SparseArraySegment<T>*)currentBase;
  1063. Assert(!prevSeg || prevSeg->next == current);
  1064. if (prevSeg)
  1065. {
  1066. bool noExactMatch = !current || itemIndex < current->left;
  1067. Assert(prevSeg->left + prevSeg->size >= prevSeg->left);
  1068. bool extendPrevSeg = itemIndex <= prevSeg->left + prevSeg->size;
  1069. if (noExactMatch && extendPrevSeg)
  1070. {
  1071. current = SparseArraySegment<T>::From(head);
  1072. prev = nullptr;
  1073. if (prevSeg != head)
  1074. {
  1075. // Since we are going to extend prevSeg we need the
  1076. // address of it's left neighbor's next pointer
  1077. currentBase = current;
  1078. segmentMap->Find(prevSeg->left, prevSeg, currentBase);
  1079. current = (SparseArraySegment<T>*)currentBase;
  1080. Assert(prevSeg && prevSeg->next == current);
  1081. prev = prevSeg;
  1082. }
  1083. }
  1084. else
  1085. {
  1086. prev = prevSeg;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. Assert(current == head);
  1092. }
  1093. }
  1094. #ifdef VALIDATE_ARRAY
  1095. SECOND_PASS:
  1096. if (!first_pass)
  1097. {
  1098. current = (SparseArraySegment<T>*)this->GetBeginLookupSegment(itemIndex, false);
  1099. // If it doesn't fit in current chunk (watch for overflow), start from beginning as we'll
  1100. // need the prev
  1101. if (current->left + current->size > current->left || itemIndex >= current->left + current->size)
  1102. {
  1103. current = SparseArraySegment<T>::From(head);
  1104. }
  1105. prev = nullptr;
  1106. }
  1107. #endif
  1108. uint probeCost = 0;
  1109. while(current != nullptr)
  1110. {
  1111. uint32 offset = itemIndex - current->left;
  1112. if (itemIndex < current->left)
  1113. {
  1114. break;
  1115. }
  1116. else if (offset <= current->size)
  1117. {
  1118. if ((nullptr == current->next) || (itemIndex < current->next->left))
  1119. {
  1120. break;
  1121. }
  1122. }
  1123. prev = current;
  1124. current = SparseArraySegment<T>::From(current->next);
  1125. Assert(segmentMap == GetSegmentMap());
  1126. if (!segmentMap)
  1127. {
  1128. probeCost++;
  1129. if (probeCost > SegmentBTree::GetLazyCrossOverLimit())
  1130. {
  1131. // Build a SegmentMap
  1132. segmentMap = BuildSegmentMap();
  1133. SparseArraySegmentBase* prevSeg = nullptr;
  1134. SparseArraySegmentBase* currentBase = current;
  1135. segmentMap->Find(itemIndex, prevSeg, currentBase);
  1136. current = (SparseArraySegment<T>*)currentBase;
  1137. Assert(prevSeg->next == current);
  1138. if (prevSeg)
  1139. {
  1140. bool noExactMatch = !current || itemIndex < current->left;
  1141. Assert(prevSeg->left + prevSeg->size >= prevSeg->left);
  1142. bool extendPrevSeg = itemIndex <= prevSeg->left + prevSeg->size;
  1143. if (noExactMatch && extendPrevSeg)
  1144. {
  1145. current = SparseArraySegment<T>::From(head);
  1146. prev = nullptr;
  1147. if (prevSeg != head)
  1148. {
  1149. // Since we are going to extend prevSeg we need the
  1150. // address of its left neighbor's next pointer
  1151. currentBase = current;
  1152. segmentMap->Find(prevSeg->left, prevSeg, currentBase);
  1153. current = (SparseArraySegment<T>*)currentBase;
  1154. Assert(prevSeg->next == current);
  1155. prev = prevSeg;
  1156. }
  1157. }
  1158. else
  1159. {
  1160. prev = prevSeg;
  1161. }
  1162. }
  1163. else
  1164. {
  1165. Assert(current == head);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. #ifdef VALIDATE_ARRAY
  1171. Assert(segmentMap == GetSegmentMap());
  1172. if (segmentMap && first_pass)
  1173. {
  1174. current_btree = current;
  1175. prev_btree = prev;
  1176. first_pass = false;
  1177. goto SECOND_PASS;
  1178. }
  1179. else if (segmentMap)
  1180. {
  1181. Assert(current_btree == current && prev_btree == prev);
  1182. }
  1183. #endif
  1184. if (current != nullptr)
  1185. {
  1186. uint32 offset = itemIndex - current->left;
  1187. if ((itemIndex >= current->left) && (offset < current->size))
  1188. {
  1189. //itemIndex lies in the segment
  1190. Assert(!(HasNoMissingValues() &&
  1191. offset < current->length &&
  1192. SparseArraySegment<T>::IsMissingItem(&current->elements[offset]) &&
  1193. current == head));
  1194. if(offset > current->length && current == head)
  1195. {
  1196. SetHasNoMissingValues(false);
  1197. }
  1198. const bool scanForMissingValues = NeedScanForMissingValuesUponSetItem(current, offset);
  1199. ((SparseArraySegment<T>*)current)->SetElement(recycler, itemIndex, newValue);
  1200. if(scanForMissingValues)
  1201. {
  1202. ScanForMissingValues<T>();
  1203. }
  1204. }
  1205. else if ((itemIndex + 1) < current->left)
  1206. {
  1207. //itemIndex lies in between current and previous segment
  1208. SparseArraySegment<T>* newSeg = SparseArraySegment<T>::AllocateSegment(recycler, prev, itemIndex);
  1209. newSeg->SetElement(recycler, itemIndex, newValue);
  1210. newSeg->next = current;
  1211. LinkSegments((SparseArraySegment<T>*)prev, newSeg);
  1212. current = newSeg;
  1213. TryAddToSegmentMap(recycler, newSeg);
  1214. Assert(current != head);
  1215. }
  1216. else
  1217. {
  1218. //itemIndex is at boundary of current segment either at the left + size or at left - 1;
  1219. Assert((itemIndex == current->left + current->size) || (itemIndex + 1 == current->left));
  1220. SparseArraySegment<T>* next = SparseArraySegment<T>::From(current->next);
  1221. Assert(segmentMap == GetSegmentMap());
  1222. if (!segmentMap && next != nullptr && (itemIndex + 1) == next->left)
  1223. {
  1224. // Don't merge segments if we are using a segmentMap
  1225. //Special case where we need to merge two segments. itemIndex is on the size boundary
  1226. //of the current segment & left boundary of the next
  1227. const bool currentWasFull = current->length == current->size;
  1228. Assert(itemIndex == current->left + current->size);
  1229. current = SparseArraySegment<T>::CopySegment(recycler, (SparseArraySegment<T>*)current, next->left, next, next->left, next->length);
  1230. current->next = next->next;
  1231. current->SetElement(recycler, itemIndex, newValue);
  1232. LinkSegments((SparseArraySegment<T>*)prev, current);
  1233. if(HasNoMissingValues() && current == head)
  1234. {
  1235. // We just merged the head segment and its next segment and filled the only missing value in-between the
  1236. // two segments. We already know that the previous head segment does not have any missing values. If the
  1237. // previous head segment was full, scan the new head segment starting from the merge point for missing
  1238. // values. If the previous head segment was not full, then merging the segments would have created
  1239. // missing values.
  1240. SetHasNoMissingValues(false);
  1241. if(currentWasFull)
  1242. {
  1243. ScanForMissingValues<T>(offset + 1);
  1244. }
  1245. }
  1246. }
  1247. else
  1248. {
  1249. if(offset > current->length && current == head)
  1250. {
  1251. SetHasNoMissingValues(false);
  1252. }
  1253. const bool currentWasHead = current == head;
  1254. SparseArraySegmentBase* oldSegment = current;
  1255. uint originalKey = oldSegment->left;
  1256. current = current->SetElementGrow(recycler, prev, itemIndex, newValue);
  1257. Assert(segmentMap == GetSegmentMap());
  1258. if (segmentMap)
  1259. {
  1260. segmentMap->SwapSegment(originalKey, oldSegment, current);
  1261. }
  1262. LinkSegments((SparseArraySegment<T>*)prev, current);
  1263. // Scan for missing values when the current segment was grown at the beginning and made the head segment
  1264. if(!currentWasHead && current == head)
  1265. {
  1266. ScanForMissingValues<T>();
  1267. }
  1268. }
  1269. }
  1270. }
  1271. else
  1272. {
  1273. // Reallocate head if need it meets a heuristics
  1274. Assert(itemIndex >= head->size);
  1275. if (prev == head // prev segment is the head segment
  1276. && !head->next // There is only one head segment in the array
  1277. && !segmentMap // There is no segmentMap which makes sure that array is not highly fragmented.
  1278. && itemIndex - head->size <= MergeSegmentsLengthHeuristics // Distance to next index is relatively small
  1279. )
  1280. {
  1281. current = SparseArraySegment<T>::From(head)->GrowByMin(recycler, itemIndex + 1 - head->size);
  1282. current->elements[itemIndex] = newValue;
  1283. current->length = itemIndex + 1;
  1284. current->CheckLengthvsSize();
  1285. head = current;
  1286. SetHasNoMissingValues(false);
  1287. }
  1288. else
  1289. {
  1290. //itemIndex is greater than the (left + size) of last segment in the linked list
  1291. current = SparseArraySegment<T>::AllocateSegment(recycler, itemIndex, 1, (SparseArraySegment<T> *)nullptr);
  1292. current->SetElement(recycler, itemIndex, newValue);
  1293. LinkSegments((SparseArraySegment<T>*)prev, current);
  1294. TryAddToSegmentMap(recycler, current);
  1295. if(current == head)
  1296. {
  1297. Assert(itemIndex == 0);
  1298. Assert(current->length == 1);
  1299. SetHasNoMissingValues();
  1300. }
  1301. }
  1302. }
  1303. this->SetLastUsedSegment(current);
  1304. #ifdef VALIDATE_ARRAY
  1305. ValidateArray();
  1306. #endif
  1307. }
  1308. template<typename T>
  1309. bool JavascriptArray::NeedScanForMissingValuesUponSetItem(SparseArraySegment<T> *const segment, const uint32 offset) const
  1310. {
  1311. Assert(segment);
  1312. // Scan for missing values upon SetItem when a missing value is being filled and the surrounding values are not missing,
  1313. // as this could be the last missing value that is being filled
  1314. return
  1315. offset < segment->length &&
  1316. SparseArraySegment<T>::IsMissingItem(&segment->elements[offset]) &&
  1317. (offset == 0 || !SparseArraySegment<T>::IsMissingItem(&segment->elements[offset - 1])) &&
  1318. (offset == segment->length - 1 || !SparseArraySegment<T>::IsMissingItem(&segment->elements[offset + 1])) &&
  1319. segment == head;
  1320. }
  1321. template<typename T>
  1322. void JavascriptArray::ScanForMissingValues(const uint startIndex)
  1323. {
  1324. Assert(head);
  1325. Assert(!HasNoMissingValues());
  1326. SparseArraySegment<T> *const segment = SparseArraySegment<T>::From(head);
  1327. const uint segmentLength = segment->length;
  1328. const Field(T) * const segmentElements = segment->elements;
  1329. for(uint i = startIndex; i < segmentLength; ++i)
  1330. {
  1331. if(SparseArraySegment<T>::IsMissingItem(&segmentElements[i]))
  1332. {
  1333. return;
  1334. }
  1335. }
  1336. SetHasNoMissingValues();
  1337. }
  1338. template<typename T>
  1339. bool JavascriptArray::ScanForMissingValues(const uint startIndex, const uint endIndex)
  1340. {
  1341. Assert(head);
  1342. //Assert(!HasNoMissingValues());
  1343. SparseArraySegment<T> *const segment = SparseArraySegment<T>::From(head);
  1344. const Field(T) *const segmentElements = segment->elements;
  1345. for (uint i = startIndex; i < endIndex; ++i)
  1346. {
  1347. if (SparseArraySegment<T>::IsMissingItem(&segmentElements[i]))
  1348. {
  1349. return true;
  1350. }
  1351. }
  1352. return false;
  1353. }
  1354. inline void JavascriptArray::DirectSetItemIfNotExist(uint32 index, Var newValue)
  1355. {
  1356. Assert(VirtualTableInfo<JavascriptArray>::HasVirtualTable(this));
  1357. Var oldValue;
  1358. if (!DirectGetItemAt(index, &oldValue))
  1359. {
  1360. DirectSetItemAt(index, newValue);
  1361. }
  1362. }
  1363. //Grow the array head and try to set at the boundary
  1364. template<typename unitType, typename classname>
  1365. inline BOOL JavascriptArray::TryGrowHeadSegmentAndSetItem(uint32 indexInt, unitType iValue)
  1366. {
  1367. SparseArraySegment<unitType> *current = SparseArraySegment<unitType>::From(head);
  1368. if (indexInt == current->length // index is at the boundary of size & length
  1369. && current->size // Make sure its not empty segment.
  1370. && !current->next // There is only head segment.
  1371. && current->length == current->size // Why did we miss the fastpath?
  1372. && !SparseArraySegment<unitType>::IsMissingItem(&iValue)) // value to set is not a missing value.
  1373. {
  1374. current= current->GrowByMin(this->GetRecycler(), indexInt + 1);
  1375. DebugOnly(VerifyNotNeedMarshal(iValue));
  1376. current->elements[indexInt] = iValue;
  1377. current->length = indexInt + 1;
  1378. current->CheckLengthvsSize();
  1379. // There is only a head segment in this condition A segment map is not necessary
  1380. // and most likely invalid at this point. Also we are setting the head and lastUsedSegment
  1381. // to the same segment. Precedent in the rest of the code base dictates the use of
  1382. // SetHeadAndLastUsedSegment which asserts if a segment map exists.
  1383. ClearSegmentMap();
  1384. SetHeadAndLastUsedSegment(current);
  1385. if (this->length <= indexInt)
  1386. {
  1387. this->length = indexInt + 1;
  1388. }
  1389. #ifdef VALIDATE_ARRAY
  1390. ValidateArray();
  1391. #endif
  1392. return true;
  1393. }
  1394. return false;
  1395. }
  1396. //
  1397. // JavascriptArray::IndexTrace specialized on uint32 (small index)
  1398. //
  1399. template<>
  1400. inline Var JavascriptArray::IndexTrace<uint32>::ToNumber(const uint32& index, ScriptContext* scriptContext)
  1401. {
  1402. return JavascriptNumber::ToVar(index, scriptContext);
  1403. }
  1404. template<>
  1405. inline BOOL JavascriptArray::IndexTrace<uint32>::GetItem(JavascriptArray* arr, const uint32& index, Var* outVal)
  1406. {
  1407. return arr->DirectGetItemAt(index, outVal);
  1408. }
  1409. template<>
  1410. inline BOOL JavascriptArray::IndexTrace<uint32>::SetItem(JavascriptArray* arr, const uint32& index, Var newValue)
  1411. {
  1412. return arr->SetItem(index, newValue, PropertyOperation_None);
  1413. }
  1414. template<>
  1415. inline void JavascriptArray::IndexTrace<uint32>::SetItemIfNotExist(JavascriptArray* arr, const uint32& index, Var newValue)
  1416. {
  1417. arr->DirectSetItemIfNotExist(index, newValue);
  1418. }
  1419. template<>
  1420. inline BOOL JavascriptArray::IndexTrace<uint32>::DeleteItem(JavascriptArray* arr, const uint32& index)
  1421. {
  1422. switch (arr->GetTypeId())
  1423. {
  1424. case TypeIds_Array:
  1425. return arr->DirectDeleteItemAt<Var>(index);
  1426. case TypeIds_NativeIntArray:
  1427. return arr->DirectDeleteItemAt<int32>(index);
  1428. case TypeIds_NativeFloatArray:
  1429. return arr->DirectDeleteItemAt<double>(index);
  1430. default:
  1431. Assert(FALSE);
  1432. return FALSE;
  1433. }
  1434. }
  1435. template<>
  1436. inline BOOL JavascriptArray::IndexTrace<uint32>::SetItem(RecyclableObject* obj, const uint32& index, Var newValue, PropertyOperationFlags flags)
  1437. {
  1438. ScriptContext* requestContext = obj->GetScriptContext();
  1439. return JavascriptOperators::SetItem(obj, obj, index, newValue, requestContext, flags);
  1440. }
  1441. template<>
  1442. inline BOOL JavascriptArray::IndexTrace<uint32>::DeleteItem(RecyclableObject* obj, const uint32& index, PropertyOperationFlags flags)
  1443. {
  1444. return JavascriptOperators::DeleteItem(obj, index, flags);
  1445. }
  1446. //
  1447. // JavascriptArray::IndexTrace specialized on BigIndex
  1448. //
  1449. template<>
  1450. inline Var JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::ToNumber(const JavascriptArray::BigIndex& index, ScriptContext* scriptContext)
  1451. {
  1452. return index.ToNumber(scriptContext);
  1453. }
  1454. template<>
  1455. inline BOOL JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::GetItem(JavascriptArray* arr, const JavascriptArray::BigIndex& index, Var* outVal)
  1456. {
  1457. return index.GetItem(arr, outVal);
  1458. }
  1459. template<>
  1460. inline BOOL JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::SetItem(JavascriptArray* arr, const JavascriptArray::BigIndex& index, Var newValue)
  1461. {
  1462. return index.SetItem(arr, newValue);
  1463. }
  1464. template<>
  1465. inline void JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::SetItemIfNotExist(JavascriptArray* arr, const JavascriptArray::BigIndex& index, Var newValue)
  1466. {
  1467. index.SetItemIfNotExist(arr, newValue);
  1468. }
  1469. template<>
  1470. inline BOOL JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::DeleteItem(JavascriptArray* arr, const JavascriptArray::BigIndex& index)
  1471. {
  1472. return index.DeleteItem(arr);
  1473. }
  1474. template<>
  1475. inline BOOL JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::SetItem(RecyclableObject* obj, const JavascriptArray::BigIndex& index, Var newValue, PropertyOperationFlags flags)
  1476. {
  1477. return index.SetItem(obj, newValue, flags);
  1478. }
  1479. template<>
  1480. inline BOOL JavascriptArray::IndexTrace<JavascriptArray::BigIndex>::DeleteItem(RecyclableObject* obj, const JavascriptArray::BigIndex& index, PropertyOperationFlags flags)
  1481. {
  1482. return index.DeleteItem(obj, flags);
  1483. }
  1484. template<class T, uint InlinePropertySlots>
  1485. inline size_t JavascriptArray::DetermineAllocationSize(
  1486. const uint inlineElementSlots,
  1487. size_t *const allocationPlusSizeRef,
  1488. uint *const alignedInlineElementSlotsRef)
  1489. {
  1490. CompileAssert(static_cast<PropertyIndex>(InlinePropertySlots) == InlinePropertySlots);
  1491. Assert(
  1492. DynamicTypeHandler::RoundUpInlineSlotCapacity(static_cast<PropertyIndex>(InlinePropertySlots)) ==
  1493. InlinePropertySlots);
  1494. CompileAssert(
  1495. InlinePropertySlots <=
  1496. (UINT_MAX - (sizeof(T) + sizeof(SparseArraySegment<typename T::TElement>))) / sizeof(Var));
  1497. const uint objectSize =
  1498. sizeof(T) + sizeof(SparseArraySegment<typename T::TElement>) + InlinePropertySlots * sizeof(Var);
  1499. size_t totalSize = UInt32Math::MulAdd<sizeof(typename T::TElement), objectSize>(inlineElementSlots);
  1500. #if defined(_M_X64_OR_ARM64)
  1501. // On x64, the total size won't be anywhere near AllocSizeMath::MaxMemory on x64, so no need to check
  1502. totalSize = HeapInfo::GetAlignedSizeNoCheck(totalSize);
  1503. #else
  1504. totalSize = HeapInfo::GetAlignedSize(totalSize);
  1505. #endif
  1506. if(allocationPlusSizeRef)
  1507. {
  1508. *allocationPlusSizeRef = totalSize - sizeof(T);
  1509. }
  1510. if(alignedInlineElementSlotsRef)
  1511. {
  1512. const size_t alignedInlineElementSlots = (totalSize - objectSize) / sizeof(typename T::TElement);
  1513. *alignedInlineElementSlotsRef = static_cast<uint>(alignedInlineElementSlots);
  1514. Assert(*alignedInlineElementSlotsRef == alignedInlineElementSlots); // ensure no truncation above
  1515. }
  1516. return totalSize;
  1517. }
  1518. template<class ArrayType>
  1519. void JavascriptArray::EnsureCalculationOfAllocationBuckets()
  1520. {
  1521. uint temp;
  1522. for (uint8 i = 0;i < ArrayType::AllocationBucketsCount;i++)
  1523. {
  1524. ArrayType::allocationBuckets[i][AllocationSizeIndex] = (uint)DetermineAllocationSize<ArrayType, 0>(ArrayType::allocationBuckets[i][AllocationBucketIndex], nullptr, &temp);
  1525. ArrayType::allocationBuckets[i][MissingElementsCountIndex] = temp;
  1526. }
  1527. }
  1528. template<class ArrayType, uint InlinePropertySlots>
  1529. inline size_t JavascriptArray::DetermineAllocationSizeForArrayObjects(
  1530. const uint inlineElementSlots,
  1531. size_t *const allocationPlusSizeRef,
  1532. uint *const alignedInlineElementSlotsRef)
  1533. {
  1534. uint8 bucketsCount = ArrayType::AllocationBucketsCount;
  1535. EnsureCalculationOfAllocationBuckets<ArrayType>();
  1536. if (inlineElementSlots >= 0 && inlineElementSlots <= ArrayType::allocationBuckets[bucketsCount - 1][AllocationBucketIndex])
  1537. {
  1538. for (uint8 i = 0;i < bucketsCount;i++)
  1539. {
  1540. uint elementsCountToInitialize = ArrayType::allocationBuckets[i][MissingElementsCountIndex];
  1541. uint allocationSize = ArrayType::allocationBuckets[i][AllocationSizeIndex];
  1542. // Ensure we already have allocation size calculated and within range
  1543. Assert(elementsCountToInitialize > 0 && elementsCountToInitialize <= ArrayType::allocationBuckets[bucketsCount - 1][MissingElementsCountIndex]);
  1544. Assert(allocationSize > 0 && allocationSize <= ArrayType::allocationBuckets[bucketsCount - 1][AllocationSizeIndex]);
  1545. if (inlineElementSlots <= ArrayType::allocationBuckets[i][AllocationBucketIndex])
  1546. {
  1547. if (alignedInlineElementSlotsRef)
  1548. {
  1549. *alignedInlineElementSlotsRef = elementsCountToInitialize;
  1550. }
  1551. if (allocationPlusSizeRef)
  1552. {
  1553. *allocationPlusSizeRef = allocationSize - sizeof(ArrayType);
  1554. }
  1555. return allocationSize;
  1556. }
  1557. }
  1558. }
  1559. return DetermineAllocationSize<ArrayType, InlinePropertySlots>(inlineElementSlots, allocationPlusSizeRef, alignedInlineElementSlotsRef);
  1560. }
  1561. template<class T, uint InlinePropertySlots>
  1562. inline uint JavascriptArray::DetermineAvailableInlineElementSlots(
  1563. const size_t allocationSize,
  1564. bool *const isSufficientSpaceForInlinePropertySlotsRef)
  1565. {
  1566. CompileAssert(static_cast<PropertyIndex>(InlinePropertySlots) == InlinePropertySlots);
  1567. Assert(
  1568. DynamicTypeHandler::RoundUpInlineSlotCapacity(static_cast<PropertyIndex>(InlinePropertySlots)) ==
  1569. InlinePropertySlots);
  1570. Assert(isSufficientSpaceForInlinePropertySlotsRef);
  1571. CompileAssert(
  1572. InlinePropertySlots <=
  1573. (UINT_MAX - (sizeof(T) + sizeof(SparseArraySegment<typename T::TElement>))) / sizeof(Var));
  1574. *isSufficientSpaceForInlinePropertySlotsRef =
  1575. sizeof(T) + InlinePropertySlots * sizeof(Var) + sizeof(SparseArraySegment<typename T::TElement>) <= allocationSize;
  1576. const size_t availableInlineElementSlots =
  1577. (
  1578. allocationSize -
  1579. (sizeof(T) + InlinePropertySlots * sizeof(Var) + sizeof(SparseArraySegment<typename T::TElement>))
  1580. ) / sizeof(typename T::TElement);
  1581. const uint availableInlineElementSlotsUint = static_cast<uint>(availableInlineElementSlots);
  1582. Assert(availableInlineElementSlotsUint == availableInlineElementSlots); // ensure no truncation above
  1583. return availableInlineElementSlotsUint;
  1584. }
  1585. template<class T, uint ConstInlinePropertySlots, bool UseDynamicInlinePropertySlots>
  1586. inline SparseArraySegment<typename T::TElement> *JavascriptArray::DetermineInlineHeadSegmentPointer(T *const array)
  1587. {
  1588. Assert(array);
  1589. Assert(VirtualTableInfo<T>::HasVirtualTable(array) || VirtualTableInfo<CrossSiteObject<T>>::HasVirtualTable(array));
  1590. Assert(!UseDynamicInlinePropertySlots || ConstInlinePropertySlots == 0);
  1591. Assert(
  1592. UseDynamicInlinePropertySlots ||
  1593. ConstInlinePropertySlots == array->GetTypeHandler()->GetInlineSlotCapacity());
  1594. const uint inlinePropertySlots =
  1595. UseDynamicInlinePropertySlots ? array->GetTypeHandler()->GetInlineSlotCapacity() : ConstInlinePropertySlots;
  1596. Assert(inlinePropertySlots == 0 || array->GetTypeHandler()->GetOffsetOfInlineSlots() == sizeof(T));
  1597. return
  1598. reinterpret_cast<SparseArraySegment<typename T::TElement> *>(
  1599. reinterpret_cast<Var *>(array + 1) + inlinePropertySlots);
  1600. }
  1601. //
  1602. // ItemTrace<T> specializations
  1603. //
  1604. template<>
  1605. inline uint32 JavascriptArray::ItemTrace<JavascriptArray>::GetLength(JavascriptArray* obj, ScriptContext* scriptContext)
  1606. {
  1607. return obj->GetLength();
  1608. }
  1609. template<>
  1610. inline BOOL JavascriptArray::ItemTrace<JavascriptArray>::GetItem(JavascriptArray* obj, uint32 index, Var* outVal, ScriptContext* scriptContext)
  1611. {
  1612. Assert(JavascriptArray::IsDirectAccessArray(obj));
  1613. return obj->DirectGetItemAtFull(index, outVal); // Note this does prototype lookup
  1614. }
  1615. template<>
  1616. inline uint32 JavascriptArray::ItemTrace<RecyclableObject>::GetLength(RecyclableObject* obj, ScriptContext* scriptContext)
  1617. {
  1618. return JavascriptConversion::ToUInt32(JavascriptOperators::OP_GetLength(obj, scriptContext), scriptContext);
  1619. }
  1620. template<>
  1621. inline BOOL JavascriptArray::ItemTrace<RecyclableObject>::GetItem(RecyclableObject* obj, uint32 index, Var* outVal, ScriptContext* scriptContext)
  1622. {
  1623. return JavascriptOperators::GetItem(obj, index, outVal, scriptContext);
  1624. }
  1625. } // namespace Js