-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathJavascriptArray.cpp
13184 lines (11472 loc) · 515 KB
/
JavascriptArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLibraryPch.h"
#include "Types/PathTypeHandler.h"
#include "Types/SpreadArgument.h"
// TODO: Change this generic fatal error to the descriptive one.
#define AssertAndFailFast(x) if (!(x)) { Assert(x); Js::Throw::FatalInternalError(); }
#define AssertMsgAndFailFast(x, m) if (!(x)) { AssertMsg((x), m); Js::Throw::FatalInternalError(); }
using namespace Js;
// Make sure EmptySegment points to read-only memory.
// Can't do this the easy way because SparseArraySegment has a constructor...
static const char EmptySegmentData[sizeof(SparseArraySegmentBase)] = {0};
const SparseArraySegmentBase *JavascriptArray::EmptySegment = (SparseArraySegmentBase *)&EmptySegmentData;
// col0 : allocation bucket
// col1 : No. of missing items to set during initialization depending on bucket.
// col2 : allocation size for elements in given bucket.
// col1 and col2 is calculated at runtime
uint JavascriptNativeFloatArray::allocationBuckets[][AllocationBucketsInfoSize] =
{
{ 3, 0, 0 }, // allocate space for 3 elements for array of length 0,1,2,3
{ 5, 0, 0 }, // allocate space for 5 elements for array of length 4,5
{ 8, 0, 0 }, // allocate space for 8 elements for array of length 6,7,8
};
const Var JavascriptArray::MissingItem = (Var)VarMissingItemPattern;
#if defined(TARGET_64)
const Var JavascriptArray::IntMissingItemVar = (Var)(((uint64)IntMissingItemPattern << 32) | (uint32)IntMissingItemPattern);
uint JavascriptNativeIntArray::allocationBuckets[][AllocationBucketsInfoSize] =
{
// See comments above on how to read this
{2, 0, 0},
{6, 0, 0},
{8, 0, 0},
};
uint JavascriptArray::allocationBuckets[][AllocationBucketsInfoSize] =
{
// See comments above on how to read this
{4, 0, 0},
{6, 0, 0},
{8, 0, 0},
};
#else
const Var JavascriptArray::IntMissingItemVar = (Var)IntMissingItemPattern;
uint JavascriptNativeIntArray::allocationBuckets[][AllocationBucketsInfoSize] =
{
// See comments above on how to read this
{ 3, 0, 0 },
{ 7, 0, 0 },
{ 8, 0, 0 },
};
uint JavascriptArray::allocationBuckets[][AllocationBucketsInfoSize] =
{
// See comments above on how to read this
{ 4, 0, 0 },
{ 8, 0, 0 },
};
#endif
const int32 JavascriptNativeIntArray::MissingItem = IntMissingItemPattern;
const double JavascriptNativeFloatArray::MissingItem = *(double*)&FloatMissingItemPattern;
// Allocate enough space for 4 inline property slots and 16 inline element slots
const size_t JavascriptArray::StackAllocationSize = DetermineAllocationSize<JavascriptArray, 4>(16);
const size_t JavascriptNativeIntArray::StackAllocationSize = DetermineAllocationSize<JavascriptNativeIntArray, 4>(16);
const size_t JavascriptNativeFloatArray::StackAllocationSize = DetermineAllocationSize<JavascriptNativeFloatArray, 4>(16);
SegmentBTree::SegmentBTree()
: segmentCount(0),
segments(nullptr),
keys(nullptr),
children(nullptr)
{
}
uint32 SegmentBTree::GetLazyCrossOverLimit()
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.DisableArrayBTree)
{
return Js::JavascriptArray::InvalidIndex;
}
else if (Js::Configuration::Global.flags.ForceArrayBTree)
{
return ARRAY_CROSSOVER_FOR_VALIDATE;
}
#endif
#ifdef VALIDATE_ARRAY
if (Js::Configuration::Global.flags.ArrayValidate)
{
return ARRAY_CROSSOVER_FOR_VALIDATE;
}
#endif
return SegmentBTree::MinDegree * 3;
}
BOOL SegmentBTree::IsLeaf() const
{
return children == NULL;
}
BOOL SegmentBTree::IsFullNode() const
{
return segmentCount == MaxKeys;
}
void SegmentBTree::InternalFind(SegmentBTree* node, uint32 itemIndex, SparseArraySegmentBase*& prev, SparseArraySegmentBase*& matchOrNext)
{
uint32 i = 0;
for(; i < node->segmentCount; i++)
{
Assert(node->keys[i] == node->segments[i]->left);
if (itemIndex < node->keys[i])
{
break;
}
}
// i indicates the 1st segment in the node past any matching segment.
// the i'th child is the children to the 'left' of the i'th segment.
// If itemIndex matches segment i-1 (note that left is always a match even when length == 0)
bool matches = i > 0 && (itemIndex == node->keys[i-1] || itemIndex < node->keys[i-1] + node->segments[i-1]->length);
if (matches)
{
// Find prev segment
if (node->IsLeaf())
{
if (i > 1)
{
// Previous is either sibling or set in a parent
prev = node->segments[i-2];
}
}
else
{
// prev is the right most leaf in children[i-1] tree
SegmentBTree* child = &node->children[i - 1];
while (!child->IsLeaf())
{
child = &child->children[child->segmentCount];
}
prev = child->segments[child->segmentCount - 1];
}
// Return the matching segment
matchOrNext = node->segments[i-1];
}
else // itemIndex in between segment i-1 and i
{
if (i > 0)
{
// Store in previous in case a match or next is the first segment in a child.
prev = node->segments[i-1];
}
if (node->IsLeaf())
{
matchOrNext = (i == 0 ? node->segments[0] : PointerValue(prev->next));
}
else
{
InternalFind(node->children + i, itemIndex, prev, matchOrNext);
}
}
}
void SegmentBTreeRoot::Find(uint32 itemIndex, SparseArraySegmentBase*& prev, SparseArraySegmentBase*& matchOrNext)
{
prev = matchOrNext = NULL;
InternalFind(this, itemIndex, prev, matchOrNext);
Assert(prev == NULL || (prev->next == matchOrNext));// If prev exists it is immediately before matchOrNext in the list of arraysegments
Assert(prev == NULL || (prev->left < itemIndex && prev->left + prev->length <= itemIndex)); // prev should never be a match (left is a match if length == 0)
Assert(matchOrNext == NULL || (matchOrNext->left >= itemIndex || matchOrNext->left + matchOrNext->length > itemIndex));
}
void SegmentBTreeRoot::Add(Recycler* recycler, SparseArraySegmentBase* newSeg)
{
if (IsFullNode())
{
SegmentBTree * children = AllocatorNewArrayZ(Recycler, recycler, SegmentBTree, MaxDegree);
children[0] = *this;
// Even though the segments point to a GC pointer, the main array should keep a references
// as well. So just make it a leaf allocation
this->segmentCount = 0;
this->segments = AllocatorNewArrayLeafZ(Recycler, recycler, SparseArraySegmentBase*, MaxKeys);
this->keys = AllocatorNewArrayLeafZ(Recycler,recycler,uint32,MaxKeys);
this->children = children;
// This split is the only way the tree gets deeper
SplitChild(recycler, this, 0, &children[0]);
}
InsertNonFullNode(recycler, this, newSeg);
}
void SegmentBTree::SwapSegment(uint32 originalKey, SparseArraySegmentBase* oldSeg, SparseArraySegmentBase* newSeg)
{
// Find old segment
uint32 itemIndex = originalKey;
uint32 i = 0;
for(; i < segmentCount; i++)
{
Assert(keys[i] == segments[i]->left || (oldSeg == newSeg && newSeg == segments[i]));
if (itemIndex < keys[i])
{
break;
}
}
// i is 1 past any match
if (i > 0)
{
if (oldSeg == segments[i-1])
{
segments[i-1] = newSeg;
keys[i-1] = newSeg->left;
return;
}
}
Assert(!IsLeaf());
children[i].SwapSegment(originalKey, oldSeg, newSeg);
}
void SegmentBTree::SplitChild(Recycler* recycler, SegmentBTree* parent, uint32 iChild, SegmentBTree* child)
{
// Split child in two, move it's median key up to parent, and put the result of the split
// on either side of the key moved up into parent
Assert(child != NULL);
Assert(parent != NULL);
Assert(!parent->IsFullNode());
Assert(child->IsFullNode());
SegmentBTree newNode;
newNode.segmentCount = MinKeys;
// Even though the segments point to a GC pointer, the main array should keep a references
// as well. So just make it a leaf allocation
newNode.segments = AllocatorNewArrayLeafZ(Recycler, recycler, SparseArraySegmentBase*, MaxKeys);
newNode.keys = AllocatorNewArrayLeafZ(Recycler,recycler,uint32,MaxKeys);
// Move the keys above the median into the new node
for(uint32 i = 0; i < MinKeys; i++)
{
newNode.segments[i] = child->segments[i+MinDegree];
newNode.keys[i] = child->keys[i+MinDegree];
// Do not leave false positive references around in the b-tree
child->segments[i+MinDegree] = nullptr;
}
// If children exist move those as well.
if (!child->IsLeaf())
{
newNode.children = AllocatorNewArrayZ(Recycler, recycler, SegmentBTree, MaxDegree);
for(uint32 j = 0; j < MinDegree; j++)
{
newNode.children[j] = child->children[j+MinDegree];
// Do not leave false positive references around in the b-tree
child->children[j+MinDegree].segments = nullptr;
child->children[j+MinDegree].children = nullptr;
}
}
child->segmentCount = MinKeys;
// Make room for the new child in parent
for(uint32 j = parent->segmentCount; j > iChild; j--)
{
parent->children[j+1] = parent->children[j];
}
// Copy the contents of the new node into the correct place in the parent's child array
parent->children[iChild+1] = newNode;
// Move the keys to make room for the median key
for(uint32 k = parent->segmentCount; k > iChild; k--)
{
parent->segments[k] = parent->segments[k-1];
parent->keys[k] = parent->keys[k-1];
}
// Move the median key into the proper place in the parent node
parent->segments[iChild] = child->segments[MinKeys];
parent->keys[iChild] = child->keys[MinKeys];
// Do not leave false positive references around in the b-tree
child->segments[MinKeys] = nullptr;
parent->segmentCount++;
}
void SegmentBTree::InsertNonFullNode(Recycler* recycler, SegmentBTree* node, SparseArraySegmentBase* newSeg)
{
Assert(!node->IsFullNode());
AnalysisAssert(node->segmentCount < MaxKeys); // Same as !node->IsFullNode()
Assert(newSeg != NULL);
if (node->IsLeaf())
{
// Move the keys
uint32 i = node->segmentCount - 1;
while( (i != -1) && (newSeg->left < node->keys[i]))
{
node->segments[i+1] = node->segments[i];
node->keys[i+1] = node->keys[i];
i--;
}
if (!node->segments)
{
// Even though the segments point to a GC pointer, the main array should keep a references
// as well. So just make it a leaf allocation
node->segments = AllocatorNewArrayLeafZ(Recycler, recycler, SparseArraySegmentBase*, MaxKeys);
node->keys = AllocatorNewArrayLeafZ(Recycler, recycler, uint32, MaxKeys);
}
node->segments[i + 1] = newSeg;
node->keys[i + 1] = newSeg->left;
node->segmentCount++;
}
else
{
// find the correct child node
uint32 i = node->segmentCount-1;
while((i != -1) && (newSeg->left < node->keys[i]))
{
i--;
}
i++;
// Make room if full
if(node->children[i].IsFullNode())
{
// This split doesn't make the tree any deeper as node already has children.
SplitChild(recycler, node, i, node->children+i);
Assert(node->keys[i] == node->segments[i]->left);
if (newSeg->left > node->keys[i])
{
i++;
}
}
InsertNonFullNode(recycler, node->children+i, newSeg);
}
}
inline void ThrowTypeErrorOnFailureHelper::ThrowTypeErrorOnFailure(BOOL operationSucceeded)
{
if (IsThrowTypeError(operationSucceeded))
{
ThrowTypeErrorOnFailure();
}
}
inline void ThrowTypeErrorOnFailureHelper::ThrowTypeErrorOnFailure()
{
JavascriptError::ThrowTypeError(m_scriptContext, VBSERR_ActionNotSupported, m_functionName);
}
inline BOOL ThrowTypeErrorOnFailureHelper::IsThrowTypeError(BOOL operationSucceeded)
{
return !operationSucceeded;
}
// Make sure EmptySegment points to read-only memory.
// Can't do this the easy way because SparseArraySegment has a constructor...
JavascriptArray::JavascriptArray(DynamicType * type)
: ArrayObject(type, false, 0)
{
Assert(type->GetTypeId() == TypeIds_Array || type->GetTypeId() == TypeIds_NativeIntArray || type->GetTypeId() == TypeIds_NativeFloatArray || ((type->GetTypeId() == TypeIds_ES5Array || type->GetTypeId() == TypeIds_Object) && type->GetPrototype() == GetScriptContext()->GetLibrary()->GetArrayPrototype()));
Assert(EmptySegment->length == 0 && EmptySegment->size == 0 && EmptySegment->next == NULL);
InitArrayFlags(DynamicObjectFlags::InitialArrayValue);
SetHeadAndLastUsedSegment(const_cast<SparseArraySegmentBase *>(EmptySegment));
}
JavascriptArray::JavascriptArray(uint32 length, DynamicType * type)
: ArrayObject(type, false, length)
{
Assert(JavascriptArray::IsNonES5Array(type->GetTypeId()));
Assert(EmptySegment->length == 0 && EmptySegment->size == 0 && EmptySegment->next == NULL);
InitArrayFlags(DynamicObjectFlags::InitialArrayValue);
SetHeadAndLastUsedSegment(const_cast<SparseArraySegmentBase *>(EmptySegment));
}
JavascriptArray::JavascriptArray(uint32 length, uint32 size, DynamicType * type)
: ArrayObject(type, false, length)
{
Assert(type->GetTypeId() == TypeIds_Array);
InitArrayFlags(DynamicObjectFlags::InitialArrayValue);
Recycler* recycler = GetRecycler();
SetHeadAndLastUsedSegment(SparseArraySegment<Var>::AllocateSegment(recycler, 0, 0, size, nullptr));
}
JavascriptArray::JavascriptArray(DynamicType * type, uint32 size)
: ArrayObject(type, false)
{
InitArrayFlags(DynamicObjectFlags::InitialArrayValue);
SetHeadAndLastUsedSegment(DetermineInlineHeadSegmentPointer<JavascriptArray, 0, false>(this));
head->size = size;
head->CheckLengthvsSize();
Var fill = Js::JavascriptArray::MissingItem;
for (uint i = 0; i < size; i++)
{
SparseArraySegment<Var>::From(head)->elements[i] = fill;
}
}
JavascriptNativeIntArray::JavascriptNativeIntArray(uint32 length, uint32 size, DynamicType * type)
: JavascriptNativeArray(type)
{
Assert(type->GetTypeId() == TypeIds_NativeIntArray);
this->length = length;
Recycler* recycler = GetRecycler();
SetHeadAndLastUsedSegment(SparseArraySegment<int32>::AllocateSegment(recycler, 0, 0, size, nullptr));
}
JavascriptNativeIntArray::JavascriptNativeIntArray(DynamicType * type, uint32 size)
: JavascriptNativeArray(type)
{
SetHeadAndLastUsedSegment(DetermineInlineHeadSegmentPointer<JavascriptNativeIntArray, 0, false>(this));
head->size = size;
head->CheckLengthvsSize();
SparseArraySegment<int32>::From(head)->FillSegmentBuffer(0, size);
}
JavascriptNativeFloatArray::JavascriptNativeFloatArray(uint32 length, uint32 size, DynamicType * type)
: JavascriptNativeArray(type)
{
Assert(type->GetTypeId() == TypeIds_NativeFloatArray);
this->length = length;
Recycler* recycler = GetRecycler();
SetHeadAndLastUsedSegment(SparseArraySegment<double>::AllocateSegment(recycler, 0, 0, size, nullptr));
}
JavascriptNativeFloatArray::JavascriptNativeFloatArray(DynamicType * type, uint32 size)
: JavascriptNativeArray(type)
{
SetHeadAndLastUsedSegment(DetermineInlineHeadSegmentPointer<JavascriptNativeFloatArray, 0, false>(this));
head->size = size;
head->CheckLengthvsSize();
SparseArraySegment<double>::From(head)->FillSegmentBuffer(0, size);
}
bool JavascriptArray::IsNonES5Array(Var aValue)
{
TypeId typeId = JavascriptOperators::GetTypeId(aValue);
return JavascriptArray::IsNonES5Array(typeId);
}
bool JavascriptArray::IsNonES5Array(TypeId typeId)
{
return typeId >= TypeIds_ArrayFirst && typeId <= TypeIds_ArrayLast;
}
JavascriptArray* JavascriptArray::TryVarToNonES5Array(Var aValue)
{
return JavascriptArray::IsNonES5Array(aValue) ? UnsafeVarTo<JavascriptArray>(aValue) : nullptr;
}
bool JavascriptArray::IsVarArray(Var aValue)
{
TypeId typeId = JavascriptOperators::GetTypeId(aValue);
return JavascriptArray::IsVarArray(typeId);
}
bool JavascriptArray::IsVarArray(TypeId typeId)
{
return typeId == TypeIds_Array;
}
template<typename T>
bool JavascriptArray::IsMissingItemAt(uint32 index) const
{
SparseArraySegment<T>* headSeg = SparseArraySegment<T>::From(this->head);
return SparseArraySegment<T>::IsMissingItem(&headSeg->elements[index]);
}
bool JavascriptArray::IsMissingItem(uint32 index)
{
if (!(this->head->left <= index && index < (this->head->left+ this->head->length)))
{
return false;
}
bool isIntArray = false, isFloatArray = false;
this->GetArrayTypeAndConvert(&isIntArray, &isFloatArray);
if (isIntArray)
{
return IsMissingItemAt<int32>(index);
}
else if (isFloatArray)
{
return IsMissingItemAt<double>(index);
}
else
{
return IsMissingItemAt<Var>(index);
}
}
// Get JavascriptArray* from a Var, which is either a JavascriptArray* or ESArray*.
JavascriptArray* JavascriptArray::FromAnyArray(Var aValue)
{
AssertOrFailFastMsg(VarIs<JavascriptArray>(aValue), "Ensure var is actually a 'JavascriptArray' or 'ES5Array'");
return static_cast<JavascriptArray *>(VarTo<RecyclableObject>(aValue));
}
JavascriptArray* JavascriptArray::UnsafeFromAnyArray(Var aValue)
{
AssertMsg(VarIs<JavascriptArray>(aValue), "Ensure var is actually a 'JavascriptArray' or 'ES5Array'");
return static_cast<JavascriptArray *>(UnsafeVarTo<RecyclableObject>(aValue));
}
// Check if a Var is a direct-accessible (fast path) JavascriptArray.
bool JavascriptArray::IsDirectAccessArray(Var aValue)
{
return VarIs<RecyclableObject>(aValue) &&
(VirtualTableInfo<JavascriptArray>::HasVirtualTable(aValue) ||
VirtualTableInfo<JavascriptNativeIntArray>::HasVirtualTable(aValue) ||
VirtualTableInfo<JavascriptNativeFloatArray>::HasVirtualTable(aValue));
}
bool JavascriptArray::IsInlineSegment(SparseArraySegmentBase *seg, JavascriptArray *pArr)
{
if (seg == nullptr)
{
return false;
}
SparseArraySegmentBase* inlineHeadSegment = nullptr;
if (VarIs<JavascriptNativeArray>(pArr))
{
if (VarIs<JavascriptNativeFloatArray>(pArr))
{
inlineHeadSegment = DetermineInlineHeadSegmentPointer<JavascriptNativeFloatArray, 0, true>((JavascriptNativeFloatArray*)pArr);
}
else
{
AssertOrFailFast(VarIs<JavascriptNativeIntArray>(pArr));
inlineHeadSegment = DetermineInlineHeadSegmentPointer<JavascriptNativeIntArray, 0, true>((JavascriptNativeIntArray*)pArr);
}
Assert(inlineHeadSegment);
return (seg == inlineHeadSegment);
}
// This will result in false positives. It is used because DetermineInlineHeadSegmentPointer
// does not handle Arrays that change type e.g. from JavascriptNativeIntArray to JavascriptArray
// This conversion in particular is problematic because JavascriptNativeIntArray is larger than JavascriptArray
// so the returned head segment ptr never equals pArr->head. So we will default to using this and deal with
// false positives. It is better than always doing a hard copy.
return pArr->head != nullptr && HasInlineHeadSegment(pArr->head->length);
}
DynamicObjectFlags JavascriptArray::GetFlags() const
{
return GetArrayFlags();
}
DynamicObjectFlags JavascriptArray::GetFlags_Unchecked() const // do not use except in extreme circumstances
{
return GetArrayFlags_Unchecked();
}
void JavascriptArray::SetFlags(const DynamicObjectFlags flags)
{
SetArrayFlags(flags);
}
DynamicType * JavascriptArray::GetInitialType(ScriptContext * scriptContext)
{
return scriptContext->GetLibrary()->GetArrayType();
}
JavascriptArray *JavascriptArray::Jit_GetArrayForArrayOrObjectWithArray(const Var var)
{
bool isObjectWithArray;
return Jit_GetArrayForArrayOrObjectWithArray(var, &isObjectWithArray);
}
JavascriptArray *JavascriptArray::Jit_GetArrayForArrayOrObjectWithArray(const Var var, bool *const isObjectWithArrayRef)
{
Assert(var);
Assert(isObjectWithArrayRef);
*isObjectWithArrayRef = false;
if (!VarIs<RecyclableObject>(var))
{
return nullptr;
}
JavascriptArray *array = nullptr;
INT_PTR vtable = VirtualTableInfoBase::GetVirtualTable(var);
if (!Jit_TryGetArrayForObjectWithArray(var, isObjectWithArrayRef, &vtable, &array))
{
return nullptr;
}
if (vtable != VirtualTableInfo<JavascriptArray>::Address &&
vtable != VirtualTableInfo<CrossSiteObject<JavascriptArray>>::Address &&
vtable != VirtualTableInfo<JavascriptNativeIntArray>::Address &&
vtable != VirtualTableInfo<CrossSiteObject<JavascriptNativeIntArray>>::Address &&
vtable != VirtualTableInfo<JavascriptNativeFloatArray>::Address &&
vtable != VirtualTableInfo<CrossSiteObject<JavascriptNativeFloatArray>>::Address)
{
return nullptr;
}
if (!array)
{
array = VarTo<JavascriptArray>(var);
}
return array;
}
bool JavascriptArray::Jit_TryGetArrayForObjectWithArray(const Var var, bool *const isObjectWithArrayRef, INT_PTR* pVTable, JavascriptArray** pArray)
{
Assert(isObjectWithArrayRef);
Assert(pVTable);
Assert(pArray);
if (*pVTable == VirtualTableInfo<DynamicObject>::Address ||
*pVTable == VirtualTableInfo<CrossSiteObject<DynamicObject>>::Address)
{
ArrayObject* objectArray = VarTo<DynamicObject>(var)->GetObjectArray();
*pArray = (objectArray && VarIs<JavascriptArray>(objectArray)) ? VarTo<JavascriptArray>(objectArray) : nullptr;
if (!(*pArray))
{
return false;
}
*isObjectWithArrayRef = true;
*pVTable = VirtualTableInfoBase::GetVirtualTable(*pArray);
}
return true;
}
JavascriptArray *JavascriptArray::GetArrayForArrayOrObjectWithArray(
const Var var,
bool *const isObjectWithArrayRef,
TypeId *const arrayTypeIdRef)
{
// This is a helper function used by jitted code. The array checks done here match the array checks done by jitted code
// (see Lowerer::GenerateArrayTest) to minimize bailouts.
Assert(var);
Assert(isObjectWithArrayRef);
Assert(arrayTypeIdRef);
*isObjectWithArrayRef = false;
*arrayTypeIdRef = TypeIds_Undefined;
if(!VarIs<RecyclableObject>(var))
{
return nullptr;
}
JavascriptArray *array = nullptr;
INT_PTR vtable = VirtualTableInfoBase::GetVirtualTable(var);
if(vtable == VirtualTableInfo<DynamicObject>::Address)
{
ArrayObject* objectArray = VarTo<DynamicObject>(var)->GetObjectArray();
array = (objectArray && IsNonES5Array(objectArray)) ? VarTo<JavascriptArray>(objectArray) : nullptr;
if(!array)
{
return nullptr;
}
*isObjectWithArrayRef = true;
vtable = VirtualTableInfoBase::GetVirtualTable(array);
}
if(vtable == VirtualTableInfo<JavascriptArray>::Address)
{
*arrayTypeIdRef = TypeIds_Array;
}
else if(vtable == VirtualTableInfo<JavascriptNativeIntArray>::Address)
{
*arrayTypeIdRef = TypeIds_NativeIntArray;
}
else if(vtable == VirtualTableInfo<JavascriptNativeFloatArray>::Address)
{
*arrayTypeIdRef = TypeIds_NativeFloatArray;
}
else
{
return nullptr;
}
if(!array)
{
array = VarTo<JavascriptArray>(var);
}
return array;
}
const SparseArraySegmentBase *JavascriptArray::Jit_GetArrayHeadSegmentForArrayOrObjectWithArray(const Var var)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_GetArrayHeadSegmentForArrayOrObjectWithArray);
JavascriptArray *const array = Jit_GetArrayForArrayOrObjectWithArray(var);
return array ? array->head : nullptr;
JIT_HELPER_END(Array_Jit_GetArrayHeadSegmentForArrayOrObjectWithArray);
}
uint32 JavascriptArray::Jit_GetArrayHeadSegmentLength(const SparseArraySegmentBase *const headSegment)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_GetArrayHeadSegmentLength);
return headSegment ? headSegment->length : 0;
JIT_HELPER_END(Array_Jit_GetArrayHeadSegmentLength);
}
bool JavascriptArray::Jit_OperationInvalidatedArrayHeadSegment(
const SparseArraySegmentBase *const headSegmentBeforeOperation,
const uint32 headSegmentLengthBeforeOperation,
const Var varAfterOperation)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_OperationInvalidatedArrayHeadSegment);
Assert(varAfterOperation);
if(!headSegmentBeforeOperation)
{
return false;
}
const SparseArraySegmentBase *const headSegmentAfterOperation =
Jit_GetArrayHeadSegmentForArrayOrObjectWithArray(varAfterOperation);
return
headSegmentAfterOperation != headSegmentBeforeOperation ||
headSegmentAfterOperation->length != headSegmentLengthBeforeOperation;
JIT_HELPER_END(Array_Jit_OperationInvalidatedArrayHeadSegment);
}
uint32 JavascriptArray::Jit_GetArrayLength(const Var var)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_GetArrayLength);
bool isObjectWithArray;
JavascriptArray *const array = Jit_GetArrayForArrayOrObjectWithArray(var, &isObjectWithArray);
return array && !isObjectWithArray ? array->GetLength() : 0;
JIT_HELPER_END(Array_Jit_GetArrayLength);
}
bool JavascriptArray::Jit_OperationInvalidatedArrayLength(const uint32 lengthBeforeOperation, const Var varAfterOperation)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_OperationInvalidatedArrayLength);
return Jit_GetArrayLength(varAfterOperation) != lengthBeforeOperation;
JIT_HELPER_END(Array_Jit_OperationInvalidatedArrayLength);
}
DynamicObjectFlags JavascriptArray::Jit_GetArrayFlagsForArrayOrObjectWithArray(const Var var)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_GetArrayFlagsForArrayOrObjectWithArray);
JavascriptArray *const array = Jit_GetArrayForArrayOrObjectWithArray(var);
return array && array->UsesObjectArrayOrFlagsAsFlags() ? array->GetFlags() : DynamicObjectFlags::None;
JIT_HELPER_END(Array_Jit_GetArrayFlagsForArrayOrObjectWithArray);
}
bool JavascriptArray::Jit_OperationCreatedFirstMissingValue(
const DynamicObjectFlags flagsBeforeOperation,
const Var varAfterOperation)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Array_Jit_OperationCreatedFirstMissingValue);
Assert(varAfterOperation);
return
!!(flagsBeforeOperation & DynamicObjectFlags::HasNoMissingValues) &&
!(Jit_GetArrayFlagsForArrayOrObjectWithArray(varAfterOperation) & DynamicObjectFlags::HasNoMissingValues);
JIT_HELPER_END(Array_Jit_OperationCreatedFirstMissingValue);
}
bool JavascriptArray::HasNoMissingValues() const
{
return !!(GetFlags() & DynamicObjectFlags::HasNoMissingValues);
}
bool JavascriptArray::HasNoMissingValues_Unchecked() const // do not use except in extreme circumstances
{
return !!(GetFlags_Unchecked() & DynamicObjectFlags::HasNoMissingValues);
}
void JavascriptArray::SetHasNoMissingValues(const bool hasNoMissingValues)
{
SetFlags(
hasNoMissingValues
? GetFlags() | DynamicObjectFlags::HasNoMissingValues
: GetFlags() & ~DynamicObjectFlags::HasNoMissingValues);
}
template<class T>
bool JavascriptArray::IsMissingHeadSegmentItemImpl(const uint32 index) const
{
Assert(index < head->length);
return SparseArraySegment<T>::IsMissingItem(&SparseArraySegment<T>::From(head)->elements[index]);
}
bool JavascriptArray::IsMissingHeadSegmentItem(const uint32 index) const
{
return IsMissingHeadSegmentItemImpl<Var>(index);
}
#if ENABLE_COPYONACCESS_ARRAY
void JavascriptCopyOnAccessNativeIntArray::ConvertCopyOnAccessSegment()
{
Assert(this->GetScriptContext()->GetLibrary()->cacheForCopyOnAccessArraySegments->IsValidIndex(::Math::PointerCastToIntegral<uint32>(this->GetHead())));
SparseArraySegment<int32> *seg = this->GetScriptContext()->GetLibrary()->cacheForCopyOnAccessArraySegments->GetSegmentByIndex(::Math::PointerCastToIntegral<byte>(this->GetHead()));
SparseArraySegment<int32> *newSeg = SparseArraySegment<int32>::AllocateLiteralHeadSegment(this->GetRecycler(), seg->length);
#if ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::CopyOnAccessArrayPhase))
{
Output::Print(_u("Convert copy-on-access array: index(%d) length(%d)\n"), this->GetHead(), seg->length);
Output::Flush();
}
#endif
newSeg->CopySegment(this->GetRecycler(), newSeg, 0, seg, 0, seg->length);
this->SetHeadAndLastUsedSegment(newSeg);
VirtualTableInfo<JavascriptNativeIntArray>::SetVirtualTable(this);
this->type = JavascriptNativeIntArray::GetInitialType(this->GetScriptContext());
ArrayCallSiteInfo *arrayInfo = this->GetArrayCallSiteInfo();
if (arrayInfo && !arrayInfo->isNotCopyOnAccessArray)
{
arrayInfo->isNotCopyOnAccessArray = 1;
}
}
uint32 JavascriptCopyOnAccessNativeIntArray::GetNextIndex(uint32 index) const
{
if (this->length == 0 || (index != Js::JavascriptArray::InvalidIndex && index >= this->length))
{
return Js::JavascriptArray::InvalidIndex;
}
else if (index == Js::JavascriptArray::InvalidIndex)
{
return 0;
}
else
{
return index + 1;
}
}
BOOL JavascriptCopyOnAccessNativeIntArray::DirectGetItemAt(uint32 index, int* outVal)
{
Assert(this->GetScriptContext()->GetLibrary()->cacheForCopyOnAccessArraySegments->IsValidIndex(::Math::PointerCastToIntegral<uint32>(this->GetHead())));
SparseArraySegment<int32> *seg = this->GetScriptContext()->GetLibrary()->cacheForCopyOnAccessArraySegments->GetSegmentByIndex(::Math::PointerCastToIntegral<byte>(this->GetHead()));
if (this->length == 0 || index == Js::JavascriptArray::InvalidIndex || index >= this->length)
{
return FALSE;
}
else
{
*outVal = seg->elements[index];
return TRUE;
}
}
#endif
bool JavascriptNativeIntArray::IsMissingHeadSegmentItem(const uint32 index) const
{
return IsMissingHeadSegmentItemImpl<int32>(index);
}
bool JavascriptNativeFloatArray::IsMissingHeadSegmentItem(const uint32 index) const
{
return IsMissingHeadSegmentItemImpl<double>(index);
}
void JavascriptArray::InternalFillFromPrototype(JavascriptArray *dstArray, uint32 dstIndex, JavascriptArray *srcArray, uint32 start, uint32 end, uint32 count)
{
RecyclableObject* prototype = srcArray->GetPrototype();
while (start + count != end && !JavascriptOperators::IsNull(prototype))
{
ForEachOwnMissingArrayIndexOfObject(srcArray, dstArray, prototype, start, end, dstIndex, [&](uint32 index, Var value) {
uint32 n = dstIndex + (index - start);
dstArray->SetItem(n, value, PropertyOperation_None);
count++;
});
prototype = prototype->GetPrototype();
}
}
/* static */
bool JavascriptArray::HasInlineHeadSegment(uint32 length)
{
return length <= SparseArraySegmentBase::INLINE_CHUNK_SIZE;
}
Var JavascriptArray::OP_NewScArray(uint32 elementCount, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_OP_NewScArray, reentrancylock, scriptContext->GetThreadContext());
// Called only to create array literals: size is known.
return scriptContext->GetLibrary()->CreateArrayLiteral(elementCount);
JIT_HELPER_END(ScrArr_OP_NewScArray);
}
Var JavascriptArray::OP_NewScArrayWithElements(uint32 elementCount, Var *elements, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_OP_NewScArrayWithElements, reentrancylock, scriptContext->GetThreadContext());
// Called only to create array literals: size is known.
JavascriptArray *arr = scriptContext->GetLibrary()->CreateArrayLiteral(elementCount);
SparseArraySegment<Var> *head = SparseArraySegment<Var>::From(arr->head);
Assert(elementCount <= head->length);
CopyArray(head->elements, head->length, elements, elementCount);
#ifdef VALIDATE_ARRAY
arr->ValidateArray();
#endif
return arr;
JIT_HELPER_END(ScrArr_OP_NewScArrayWithElements);
}
Var JavascriptArray::OP_NewScArrayWithMissingValues(uint32 elementCount, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_OP_NewScArrayWithMissingValues, reentrancylock, scriptContext->GetThreadContext());
// Called only to create array literals: size is known.
JavascriptArray *const array = static_cast<JavascriptArray *>(OP_NewScArray(elementCount, scriptContext));
array->SetHasNoMissingValues(false);
SparseArraySegment<Var> *head = SparseArraySegment<Var>::From(array->head);
head->FillSegmentBuffer(0, elementCount);
return array;
JIT_HELPER_END(ScrArr_OP_NewScArrayWithMissingValues);
}
#if ENABLE_PROFILE_INFO
Var JavascriptArray::ProfiledNewScArray(uint32 elementCount, ScriptContext *scriptContext, ArrayCallSiteInfo *arrayInfo, RecyclerWeakReference<FunctionBody> *weakFuncRef)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_ProfiledNewScArray, reentrancylock, scriptContext->GetThreadContext());
if (arrayInfo->IsNativeIntArray())
{
JavascriptNativeIntArray *arr = scriptContext->GetLibrary()->CreateNativeIntArrayLiteral(elementCount);
arr->SetArrayProfileInfo(weakFuncRef, arrayInfo);
return arr;
}
if (arrayInfo->IsNativeFloatArray())
{
JavascriptNativeFloatArray *arr = scriptContext->GetLibrary()->CreateNativeFloatArrayLiteral(elementCount);
arr->SetArrayProfileInfo(weakFuncRef, arrayInfo);
return arr;
}
JavascriptArray *arr = scriptContext->GetLibrary()->CreateArrayLiteral(elementCount);
return arr;
JIT_HELPER_END(ScrArr_ProfiledNewScArray);
}
#endif
Var JavascriptArray::OP_NewScIntArray(AuxArray<int32> *ints, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_OP_NewScIntArray, reentrancylock, scriptContext->GetThreadContext());
JavascriptNativeIntArray *arr = scriptContext->GetLibrary()->CreateNativeIntArrayLiteral(ints->count);
SparseArraySegment<int32> * segment = (SparseArraySegment<int32>*)arr->GetHead();
JavascriptOperators::AddIntsToArraySegment(segment, ints);
return arr;
JIT_HELPER_END(ScrArr_OP_NewScIntArray);
}
#if ENABLE_PROFILE_INFO
Var JavascriptArray::ProfiledNewScIntArray(AuxArray<int32> *ints, ScriptContext* scriptContext, ArrayCallSiteInfo *arrayInfo, RecyclerWeakReference<FunctionBody> *weakFuncRef)
{
JIT_HELPER_NOT_REENTRANT_HEADER(ScrArr_ProfiledNewScIntArray, reentrancylock, scriptContext->GetThreadContext());
// Called only to create array literals: size is known.
uint32 count = ints->count;
if (arrayInfo->IsNativeIntArray())
{
JavascriptNativeIntArray *arr;
#if ENABLE_COPYONACCESS_ARRAY