-
Notifications
You must be signed in to change notification settings - Fork 54
/
Shape.cpp
2308 lines (1926 loc) · 64.9 KB
/
Shape.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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* JS symbol tables. */
#include "vm/Shape-inl.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/PodOperations.h"
#include "gc/FreeOp.h"
#include "gc/HashUtil.h"
#include "gc/Policy.h"
#include "gc/PublicIterators.h"
#include "js/HashTable.h"
#include "js/UniquePtr.h"
#include "util/Text.h"
#include "vm/JSAtom.h"
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/Caches-inl.h"
#include "vm/JSContext-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/Realm-inl.h"
using namespace js;
using mozilla::CeilingLog2Size;
using mozilla::PodZero;
using JS::AutoCheckCannotGC;
Shape* const ShapeTable::Entry::SHAPE_REMOVED =
(Shape*)ShapeTable::Entry::SHAPE_COLLISION;
bool ShapeIC::init(JSContext* cx) {
size_ = MAX_SIZE;
entries_.reset(cx->pod_calloc<Entry>(size_));
return (!entries_) ? false : true;
}
bool ShapeTable::init(JSContext* cx, Shape* lastProp) {
uint32_t sizeLog2 = CeilingLog2Size(entryCount_);
uint32_t size = JS_BIT(sizeLog2);
if (entryCount_ >= size - (size >> 2)) {
sizeLog2++;
}
if (sizeLog2 < MIN_SIZE_LOG2) {
sizeLog2 = MIN_SIZE_LOG2;
}
size = JS_BIT(sizeLog2);
entries_.reset(cx->pod_calloc<Entry>(size));
if (!entries_) {
return false;
}
MOZ_ASSERT(sizeLog2 <= HASH_BITS);
hashShift_ = HASH_BITS - sizeLog2;
for (Shape::Range<NoGC> r(lastProp); !r.empty(); r.popFront()) {
Shape& shape = r.front();
Entry& entry = searchUnchecked<MaybeAdding::Adding>(shape.propid());
/*
* Beware duplicate args and arg vs. var conflicts: the youngest shape
* (nearest to lastProp) must win. See bug 600067.
*/
if (!entry.shape()) {
entry.setPreservingCollision(&shape);
}
}
MOZ_ASSERT(capacity() == size);
MOZ_ASSERT(size >= MIN_SIZE);
MOZ_ASSERT(!needsToGrow());
return true;
}
void Shape::removeFromDictionary(NativeObject* obj) {
MOZ_ASSERT(inDictionary());
MOZ_ASSERT(obj->inDictionaryMode());
MOZ_ASSERT(listp);
MOZ_ASSERT(obj->shape()->inDictionary());
MOZ_ASSERT(obj->shape()->listp == obj->shapePtr());
if (parent) {
parent->listp = listp;
}
*listp = parent;
listp = nullptr;
obj->shape()->clearCachedBigEnoughForShapeTable();
}
void Shape::insertIntoDictionary(GCPtrShape* dictp) {
// Don't assert inDictionaryMode() here because we may be called from
// NativeObject::toDictionaryMode via Shape::initDictionaryShape.
MOZ_ASSERT(inDictionary());
MOZ_ASSERT(!listp);
MOZ_ASSERT_IF(*dictp, (*dictp)->inDictionary());
MOZ_ASSERT_IF(*dictp, (*dictp)->listp == dictp);
MOZ_ASSERT_IF(*dictp, zone() == (*dictp)->zone());
setParent(dictp->get());
if (parent) {
parent->listp = &parent;
}
listp = (GCPtrShape*)dictp;
*dictp = this;
}
bool Shape::makeOwnBaseShape(JSContext* cx) {
MOZ_ASSERT(!base()->isOwned());
MOZ_ASSERT(cx->zone() == zone());
BaseShape* nbase = Allocate<BaseShape, NoGC>(cx);
if (!nbase) {
return false;
}
new (nbase) BaseShape(StackBaseShape(this));
nbase->setOwned(base()->toUnowned());
this->base_ = nbase;
return true;
}
void Shape::handoffTableTo(Shape* shape) {
MOZ_ASSERT(inDictionary() && shape->inDictionary());
if (this == shape) {
return;
}
MOZ_ASSERT(base()->isOwned() && !shape->base()->isOwned());
BaseShape* nbase = base();
MOZ_ASSERT_IF(!shape->isEmptyShape() && shape->isDataProperty(),
nbase->slotSpan() > shape->slot());
this->base_ = nbase->baseUnowned();
nbase->adoptUnowned(shape->base()->toUnowned());
shape->base_ = nbase;
}
/* static */
bool Shape::hashify(JSContext* cx, Shape* shape) {
MOZ_ASSERT(!shape->hasTable());
if (!shape->ensureOwnBaseShape(cx)) {
return false;
}
UniquePtr<ShapeTable> table =
cx->make_unique<ShapeTable>(shape->entryCount());
if (!table) {
return false;
}
if (!table->init(cx, shape)) {
return false;
}
shape->base()->setTable(table.release());
return true;
}
/* static */
bool Shape::cachify(JSContext* cx, Shape* shape) {
MOZ_ASSERT(!shape->hasTable() && !shape->hasIC());
if (!shape->ensureOwnBaseShape(cx)) {
return false;
}
UniquePtr<ShapeIC> ic = cx->make_unique<ShapeIC>();
if (!ic) {
return false;
}
if (!ic->init(cx)) {
return false;
}
shape->base()->setIC(ic.release());
return true;
}
bool ShapeTable::change(JSContext* cx, int log2Delta) {
MOZ_ASSERT(entries_);
MOZ_ASSERT(-1 <= log2Delta && log2Delta <= 1);
/*
* Grow, shrink, or compress by changing this->entries_.
*/
uint32_t oldLog2 = HASH_BITS - hashShift_;
uint32_t newLog2 = oldLog2 + log2Delta;
uint32_t oldSize = JS_BIT(oldLog2);
uint32_t newSize = JS_BIT(newLog2);
Entry* newTable = cx->maybe_pod_calloc<Entry>(newSize);
if (!newTable) {
return false;
}
/* Now that we have newTable allocated, update members. */
MOZ_ASSERT(newLog2 <= HASH_BITS);
hashShift_ = HASH_BITS - newLog2;
removedCount_ = 0;
Entry* oldTable = entries_.release();
entries_.reset(newTable);
/* Copy only live entries, leaving removed and free ones behind. */
AutoCheckCannotGC nogc;
for (Entry* oldEntry = oldTable; oldSize != 0; oldEntry++) {
if (Shape* shape = oldEntry->shape()) {
Entry& entry = search<MaybeAdding::Adding>(shape->propid(), nogc);
MOZ_ASSERT(entry.isFree());
entry.setShape(shape);
}
oldSize--;
}
MOZ_ASSERT(capacity() == newSize);
/* Finally, free the old entries storage. */
js_free(oldTable);
return true;
}
bool ShapeTable::grow(JSContext* cx) {
MOZ_ASSERT(needsToGrow());
uint32_t size = capacity();
int delta = removedCount_ < (size >> 2);
MOZ_ASSERT(entryCount_ + removedCount_ <= size - 1);
if (!change(cx, delta)) {
if (entryCount_ + removedCount_ == size - 1) {
ReportOutOfMemory(cx);
return false;
}
}
return true;
}
void ShapeCachePtr::trace(JSTracer* trc) {
if (isIC()) {
getICPointer()->trace(trc);
} else if (isTable()) {
getTablePointer()->trace(trc);
}
}
void ShapeIC::trace(JSTracer* trc) {
for (size_t i = 0; i < entryCount(); i++) {
Entry& entry = entries_[i];
if (entry.shape_) {
TraceManuallyBarrieredEdge(trc, &entry.shape_, "ShapeIC shape");
}
}
}
void ShapeTable::trace(JSTracer* trc) {
for (size_t i = 0; i < capacity(); i++) {
Entry& entry = getEntry(i);
Shape* shape = entry.shape();
if (shape) {
TraceManuallyBarrieredEdge(trc, &shape, "ShapeTable shape");
if (shape != entry.shape()) {
entry.setPreservingCollision(shape);
}
}
}
}
#ifdef JSGC_HASH_TABLE_CHECKS
void ShapeCachePtr::checkAfterMovingGC() {
if (isIC()) {
getICPointer()->checkAfterMovingGC();
} else if (isTable()) {
getTablePointer()->checkAfterMovingGC();
}
}
void ShapeIC::checkAfterMovingGC() {
for (size_t i = 0; i < entryCount(); i++) {
Entry& entry = entries_[i];
Shape* shape = entry.shape_;
if (shape) {
CheckGCThingAfterMovingGC(shape);
}
}
}
void ShapeTable::checkAfterMovingGC() {
for (size_t i = 0; i < capacity(); i++) {
Entry& entry = getEntry(i);
Shape* shape = entry.shape();
if (shape) {
CheckGCThingAfterMovingGC(shape);
}
}
}
#endif
/* static */
Shape* Shape::replaceLastProperty(JSContext* cx, StackBaseShape& base,
TaggedProto proto, HandleShape shape) {
MOZ_ASSERT(!shape->inDictionary());
if (!shape->parent) {
/* Treat as resetting the initial property of the shape hierarchy. */
gc::AllocKind kind = gc::GetGCObjectKind(shape->numFixedSlots());
return EmptyShape::getInitialShape(
cx, base.clasp, proto, kind, base.flags & BaseShape::OBJECT_FLAG_MASK);
}
UnownedBaseShape* nbase = BaseShape::getUnowned(cx, base);
if (!nbase) {
return nullptr;
}
Rooted<StackShape> child(cx, StackShape(shape));
child.setBase(nbase);
return cx->zone()->propertyTree().getChild(cx, shape->parent, child);
}
/*
* Get or create a property-tree or dictionary child property of |parent|,
* which must be lastProperty() if inDictionaryMode(), else parent must be
* one of lastProperty() or lastProperty()->parent.
*/
/* static */ MOZ_ALWAYS_INLINE Shape* NativeObject::getChildDataProperty(
JSContext* cx, HandleNativeObject obj, HandleShape parent,
MutableHandle<StackShape> child) {
MOZ_ASSERT(child.isDataProperty());
if (child.hasMissingSlot()) {
uint32_t slot;
if (obj->inDictionaryMode()) {
if (!allocDictionarySlot(cx, obj, &slot)) {
return nullptr;
}
} else {
slot = obj->slotSpan();
MOZ_ASSERT(slot >= JSSLOT_FREE(obj->getClass()));
// Objects with many properties are converted to dictionary
// mode, so we can't overflow SHAPE_MAXIMUM_SLOT here.
MOZ_ASSERT(slot <
JSSLOT_FREE(obj->getClass()) + PropertyTree::MAX_HEIGHT);
MOZ_ASSERT(slot < SHAPE_MAXIMUM_SLOT);
}
child.setSlot(slot);
} else {
/*
* Slots can only be allocated out of order on objects in
* dictionary mode. Otherwise the child's slot must be after the
* parent's slot (if it has one), because slot number determines
* slot span for objects with that shape. Usually child slot
* *immediately* follows parent slot, but there may be a slot gap
* when the object uses some -- but not all -- of its reserved
* slots to store properties.
*/
MOZ_ASSERT(obj->inDictionaryMode() || parent->hasMissingSlot() ||
child.slot() == parent->maybeSlot() + 1 ||
(parent->maybeSlot() + 1 < JSSLOT_FREE(obj->getClass()) &&
child.slot() == JSSLOT_FREE(obj->getClass())));
}
if (obj->inDictionaryMode()) {
MOZ_ASSERT(parent == obj->lastProperty());
Shape* shape = Allocate<Shape>(cx);
if (!shape) {
return nullptr;
}
if (child.slot() >= obj->lastProperty()->base()->slotSpan()) {
if (!obj->setSlotSpan(cx, child.slot() + 1)) {
new (shape) Shape(obj->lastProperty()->base()->unowned(), 0);
return nullptr;
}
}
shape->initDictionaryShape(child, obj->numFixedSlots(), obj->shapePtr());
return shape;
}
Shape* shape = cx->zone()->propertyTree().inlinedGetChild(cx, parent, child);
if (!shape) {
return nullptr;
}
MOZ_ASSERT(shape->parent == parent);
MOZ_ASSERT_IF(parent != obj->lastProperty(),
parent == obj->lastProperty()->parent);
if (!obj->setLastProperty(cx, shape)) {
return nullptr;
}
return shape;
}
/* static */ MOZ_ALWAYS_INLINE Shape* NativeObject::getChildAccessorProperty(
JSContext* cx, HandleNativeObject obj, HandleShape parent,
MutableHandle<StackShape> child) {
MOZ_ASSERT(!child.isDataProperty());
// Accessor properties have no slot, but slot_ will reflect that of parent.
child.setSlot(parent->maybeSlot());
if (obj->inDictionaryMode()) {
MOZ_ASSERT(parent == obj->lastProperty());
Shape* shape = Allocate<AccessorShape>(cx);
if (!shape) {
return nullptr;
}
shape->initDictionaryShape(child, obj->numFixedSlots(), obj->shapePtr());
return shape;
}
Shape* shape = cx->zone()->propertyTree().inlinedGetChild(cx, parent, child);
if (!shape) {
return nullptr;
}
MOZ_ASSERT(shape->parent == parent);
MOZ_ASSERT_IF(parent != obj->lastProperty(),
parent == obj->lastProperty()->parent);
if (!obj->setLastProperty(cx, shape)) {
return nullptr;
}
return shape;
}
/* static */
bool js::NativeObject::toDictionaryMode(JSContext* cx, HandleNativeObject obj) {
MOZ_ASSERT(!obj->inDictionaryMode());
MOZ_ASSERT(cx->isInsideCurrentCompartment(obj));
uint32_t span = obj->slotSpan();
// Clone the shapes into a new dictionary list. Don't update the last
// property of this object until done, otherwise a GC triggered while
// creating the dictionary will get the wrong slot span for this object.
RootedShape root(cx);
RootedShape dictionaryShape(cx);
RootedShape shape(cx, obj->lastProperty());
while (shape) {
MOZ_ASSERT(!shape->inDictionary());
Shape* dprop = shape->isAccessorShape() ? Allocate<AccessorShape>(cx)
: Allocate<Shape>(cx);
if (!dprop) {
ReportOutOfMemory(cx);
return false;
}
GCPtrShape* listp = dictionaryShape ? &dictionaryShape->parent : nullptr;
StackShape child(shape);
dprop->initDictionaryShape(child, obj->numFixedSlots(), listp);
if (!dictionaryShape) {
root = dprop;
}
MOZ_ASSERT(!dprop->hasTable());
dictionaryShape = dprop;
shape = shape->previous();
}
if (!Shape::hashify(cx, root)) {
ReportOutOfMemory(cx);
return false;
}
if (IsInsideNursery(obj) &&
!cx->nursery().queueDictionaryModeObjectToSweep(obj)) {
ReportOutOfMemory(cx);
return false;
}
MOZ_ASSERT(root->listp == nullptr);
root->listp = obj->shapePtr();
obj->setShape(root);
MOZ_ASSERT(obj->inDictionaryMode());
root->base()->setSlotSpan(span);
return true;
}
static bool ShouldConvertToDictionary(NativeObject* obj) {
/*
* Use a lower limit if this object is likely a hashmap (SETELEM was used
* to set properties).
*/
if (obj->hadElementsAccess()) {
return obj->lastProperty()->entryCount() >=
PropertyTree::MAX_HEIGHT_WITH_ELEMENTS_ACCESS;
}
return obj->lastProperty()->entryCount() >= PropertyTree::MAX_HEIGHT;
}
static MOZ_ALWAYS_INLINE UnownedBaseShape* GetBaseShapeForNewShape(
JSContext* cx, HandleShape last, HandleId id) {
uint32_t index;
bool indexed = IdIsIndex(id, &index);
bool interestingSymbol =
JSID_IS_SYMBOL(id) && JSID_TO_SYMBOL(id)->isInterestingSymbol();
if (MOZ_LIKELY(!indexed && !interestingSymbol)) {
return last->base()->unowned();
}
StackBaseShape base(last->base());
if (indexed) {
base.flags |= BaseShape::INDEXED;
} else if (interestingSymbol) {
base.flags |= BaseShape::HAS_INTERESTING_SYMBOL;
}
return BaseShape::getUnowned(cx, base);
}
namespace js {
class MOZ_RAII AutoCheckShapeConsistency {
#ifdef DEBUG
HandleNativeObject obj_;
#endif
public:
explicit AutoCheckShapeConsistency(HandleNativeObject obj)
#ifdef DEBUG
: obj_(obj)
#endif
{
}
#ifdef DEBUG
~AutoCheckShapeConsistency() { obj_->checkShapeConsistency(); }
#endif
};
} // namespace js
/* static */ MOZ_ALWAYS_INLINE bool
NativeObject::maybeConvertToOrGrowDictionaryForAdd(
JSContext* cx, HandleNativeObject obj, HandleId id, ShapeTable** table,
ShapeTable::Entry** entry, const AutoKeepShapeCaches& keep) {
MOZ_ASSERT(!!*table == !!*entry);
// The code below deals with either converting obj to dictionary mode or
// growing an object that's already in dictionary mode.
if (!obj->inDictionaryMode()) {
if (!ShouldConvertToDictionary(obj)) {
return true;
}
if (!toDictionaryMode(cx, obj)) {
return false;
}
*table = obj->lastProperty()->maybeTable(keep);
} else {
if (!(*table)->needsToGrow()) {
return true;
}
if (!(*table)->grow(cx)) {
return false;
}
}
*entry = &(*table)->search<MaybeAdding::Adding>(id, keep);
MOZ_ASSERT(!(*entry)->shape());
return true;
}
MOZ_ALWAYS_INLINE void Shape::updateDictionaryTable(
ShapeTable* table, ShapeTable::Entry* entry,
const AutoKeepShapeCaches& keep) {
MOZ_ASSERT(table);
MOZ_ASSERT(entry);
MOZ_ASSERT(inDictionary());
// Store this Shape in the table entry.
entry->setPreservingCollision(this);
table->incEntryCount();
// Pass the table along to the new last property, namely *this.
MOZ_ASSERT(parent->maybeTable(keep) == table);
parent->handoffTableTo(this);
}
static void AssertValidPropertyOp(NativeObject* obj, GetterOp getter,
SetterOp setter, unsigned attrs) {
// We only support PropertyOp accessors on ArrayObject and ArgumentsObject
// and we don't want to add more of these properties (bug 1404885).
#ifdef DEBUG
if ((getter && !(attrs & JSPROP_GETTER)) ||
(setter && !(attrs & JSPROP_SETTER))) {
MOZ_ASSERT(obj->is<ArrayObject>() || obj->is<ArgumentsObject>());
}
#endif
}
/* static */
Shape* NativeObject::addAccessorPropertyInternal(
JSContext* cx, HandleNativeObject obj, HandleId id, GetterOp getter,
SetterOp setter, unsigned attrs, ShapeTable* table,
ShapeTable::Entry* entry, const AutoKeepShapeCaches& keep) {
AutoCheckShapeConsistency check(obj);
AutoRooterGetterSetter gsRoot(cx, attrs, &getter, &setter);
AssertValidPropertyOp(obj, getter, setter, attrs);
if (!maybeConvertToOrGrowDictionaryForAdd(cx, obj, id, &table, &entry,
keep)) {
return nullptr;
}
// Find or create a property tree node labeled by our arguments.
RootedShape shape(cx);
{
RootedShape last(cx, obj->lastProperty());
Rooted<UnownedBaseShape*> nbase(cx, GetBaseShapeForNewShape(cx, last, id));
if (!nbase) {
return nullptr;
}
Rooted<StackShape> child(cx,
StackShape(nbase, id, SHAPE_INVALID_SLOT, attrs));
child.updateGetterSetter(getter, setter);
shape = getChildAccessorProperty(cx, obj, last, &child);
if (!shape) {
return nullptr;
}
}
MOZ_ASSERT(shape == obj->lastProperty());
if (table) {
shape->updateDictionaryTable(table, entry, keep);
}
return shape;
}
/* static */
Shape* NativeObject::addDataPropertyInternal(JSContext* cx,
HandleNativeObject obj,
HandleId id, uint32_t slot,
unsigned attrs, ShapeTable* table,
ShapeTable::Entry* entry,
const AutoKeepShapeCaches& keep) {
AutoCheckShapeConsistency check(obj);
// The slot, if any, must be a reserved slot.
MOZ_ASSERT(slot == SHAPE_INVALID_SLOT ||
slot < JSCLASS_RESERVED_SLOTS(obj->getClass()));
if (!maybeConvertToOrGrowDictionaryForAdd(cx, obj, id, &table, &entry,
keep)) {
return nullptr;
}
// Find or create a property tree node labeled by our arguments.
RootedShape shape(cx);
{
RootedShape last(cx, obj->lastProperty());
Rooted<UnownedBaseShape*> nbase(cx, GetBaseShapeForNewShape(cx, last, id));
if (!nbase) {
return nullptr;
}
Rooted<StackShape> child(cx, StackShape(nbase, id, slot, attrs));
shape = getChildDataProperty(cx, obj, last, &child);
if (!shape) {
return nullptr;
}
}
MOZ_ASSERT(shape == obj->lastProperty());
if (table) {
shape->updateDictionaryTable(table, entry, keep);
}
return shape;
}
static MOZ_ALWAYS_INLINE Shape* PropertyTreeReadBarrier(Shape* parent,
Shape* shape) {
JS::Zone* zone = shape->zone();
if (zone->needsIncrementalBarrier()) {
// We need a read barrier for the shape tree, since these are weak
// pointers.
Shape* tmp = shape;
TraceManuallyBarrieredEdge(zone->barrierTracer(), &tmp, "read barrier");
MOZ_ASSERT(tmp == shape);
return shape;
}
if (MOZ_LIKELY(!zone->isGCSweepingOrCompacting() ||
!IsAboutToBeFinalizedUnbarriered(&shape))) {
if (shape->isMarkedGray()) {
UnmarkGrayShapeRecursively(shape);
}
return shape;
}
// The shape we've found is unreachable and due to be finalized, so
// remove our weak reference to it and don't use it.
MOZ_ASSERT(parent->isMarkedAny());
parent->removeChild(shape);
return nullptr;
}
/* static */
Shape* NativeObject::addEnumerableDataProperty(JSContext* cx,
HandleNativeObject obj,
HandleId id) {
// Like addProperty(Internal), but optimized for the common case of adding a
// new enumerable data property.
AutoCheckShapeConsistency check(obj);
// Fast path for non-dictionary shapes with a single kid.
do {
AutoCheckCannotGC nogc;
Shape* lastProperty = obj->lastProperty();
if (lastProperty->inDictionary()) {
break;
}
KidsPointer* kidp = &lastProperty->kids;
if (!kidp->isShape()) {
break;
}
Shape* kid = kidp->toShape();
MOZ_ASSERT(!kid->inDictionary());
if (kid->propidRaw() != id || kid->isAccessorShape() ||
kid->attributes() != JSPROP_ENUMERATE ||
kid->base()->unowned() != lastProperty->base()->unowned()) {
break;
}
MOZ_ASSERT(kid->isDataProperty());
kid = PropertyTreeReadBarrier(lastProperty, kid);
if (!kid) {
break;
}
if (!obj->setLastProperty(cx, kid)) {
return nullptr;
}
return kid;
} while (0);
AutoKeepShapeCaches keep(cx);
ShapeTable* table = nullptr;
ShapeTable::Entry* entry = nullptr;
if (!obj->inDictionaryMode()) {
if (MOZ_UNLIKELY(ShouldConvertToDictionary(obj))) {
if (!toDictionaryMode(cx, obj)) {
return nullptr;
}
table = obj->lastProperty()->maybeTable(keep);
entry = &table->search<MaybeAdding::Adding>(id, keep);
}
} else {
table = obj->lastProperty()->ensureTableForDictionary(cx, keep);
if (!table) {
return nullptr;
}
if (table->needsToGrow()) {
if (!table->grow(cx)) {
return nullptr;
}
}
entry = &table->search<MaybeAdding::Adding>(id, keep);
MOZ_ASSERT(!entry->shape());
}
MOZ_ASSERT(!!table == !!entry);
/* Find or create a property tree node labeled by our arguments. */
RootedShape last(cx, obj->lastProperty());
UnownedBaseShape* nbase = GetBaseShapeForNewShape(cx, last, id);
if (!nbase) {
return nullptr;
}
Shape* shape;
if (obj->inDictionaryMode()) {
uint32_t slot;
if (!allocDictionarySlot(cx, obj, &slot)) {
return nullptr;
}
Rooted<StackShape> child(cx, StackShape(nbase, id, slot, JSPROP_ENUMERATE));
MOZ_ASSERT(last == obj->lastProperty());
shape = Allocate<Shape>(cx);
if (!shape) {
return nullptr;
}
if (slot >= obj->lastProperty()->base()->slotSpan()) {
if (MOZ_UNLIKELY(!obj->setSlotSpan(cx, slot + 1))) {
new (shape) Shape(obj->lastProperty()->base()->unowned(), 0);
return nullptr;
}
}
shape->initDictionaryShape(child, obj->numFixedSlots(), obj->shapePtr());
} else {
uint32_t slot = obj->slotSpan();
MOZ_ASSERT(slot >= JSSLOT_FREE(obj->getClass()));
// Objects with many properties are converted to dictionary
// mode, so we can't overflow SHAPE_MAXIMUM_SLOT here.
MOZ_ASSERT(slot < JSSLOT_FREE(obj->getClass()) + PropertyTree::MAX_HEIGHT);
MOZ_ASSERT(slot < SHAPE_MAXIMUM_SLOT);
Rooted<StackShape> child(cx, StackShape(nbase, id, slot, JSPROP_ENUMERATE));
shape = cx->zone()->propertyTree().inlinedGetChild(cx, last, child);
if (!shape) {
return nullptr;
}
if (!obj->setLastProperty(cx, shape)) {
return nullptr;
}
}
MOZ_ASSERT(shape == obj->lastProperty());
if (table) {
shape->updateDictionaryTable(table, entry, keep);
}
return shape;
}
Shape* js::ReshapeForAllocKind(JSContext* cx, Shape* shape, TaggedProto proto,
gc::AllocKind allocKind) {
// Compute the number of fixed slots with the new allocation kind.
size_t nfixed = gc::GetGCKindSlots(allocKind, shape->getObjectClass());
// Get all the ids in the shape, in order.
js::AutoIdVector ids(cx);
{
for (unsigned i = 0; i < shape->slotSpan(); i++) {
if (!ids.append(JSID_VOID)) {
return nullptr;
}
}
Shape* nshape = shape;
while (!nshape->isEmptyShape()) {
ids[nshape->slot()].set(nshape->propid());
nshape = nshape->previous();
}
}
// Construct the new shape, without updating type information.
RootedId id(cx);
RootedShape newShape(
cx, EmptyShape::getInitialShape(cx, shape->getObjectClass(), proto,
nfixed, shape->getObjectFlags()));
if (!newShape) {
return nullptr;
}
for (unsigned i = 0; i < ids.length(); i++) {
id = ids[i];
UnownedBaseShape* nbase = GetBaseShapeForNewShape(cx, newShape, id);
if (!nbase) {
return nullptr;
}
Rooted<StackShape> child(cx, StackShape(nbase, id, i, JSPROP_ENUMERATE));
newShape = cx->zone()->propertyTree().getChild(cx, newShape, child);
if (!newShape) {
return nullptr;
}
}
return newShape;
}
/*
* Assert some invariants that should hold when changing properties. It's the
* responsibility of the callers to ensure these hold.
*/
static void AssertCanChangeAttrs(Shape* shape, unsigned attrs) {
#ifdef DEBUG
if (shape->configurable()) {
return;
}
/* A permanent property must stay permanent. */
MOZ_ASSERT(attrs & JSPROP_PERMANENT);
/* Reject attempts to remove a slot from the permanent data property. */
MOZ_ASSERT_IF(shape->isDataProperty(),
!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
#endif
}
static void AssertValidArrayIndex(NativeObject* obj, jsid id) {
#ifdef DEBUG
if (obj->is<ArrayObject>()) {
ArrayObject* arr = &obj->as<ArrayObject>();
uint32_t index;
if (IdIsIndex(id, &index)) {
MOZ_ASSERT(index < arr->length() || arr->lengthIsWritable());
}
}
#endif
}
/* static */
bool NativeObject::maybeToDictionaryModeForPut(JSContext* cx,
HandleNativeObject obj,
MutableHandleShape shape) {
// Overwriting a non-last property requires switching to dictionary mode.
// The shape tree is shared immutable, and we can't removeProperty and then
// addAccessorPropertyInternal because a failure under add would lose data.
if (shape == obj->lastProperty() || obj->inDictionaryMode()) {
return true;
}
if (!toDictionaryMode(cx, obj)) {
return false;
}
AutoCheckCannotGC nogc;
ShapeTable* table = obj->lastProperty()->maybeTable(nogc);
MOZ_ASSERT(table);
shape.set(
table->search<MaybeAdding::NotAdding>(shape->propid(), nogc).shape());
return true;
}
/* static */
Shape* NativeObject::putDataProperty(JSContext* cx, HandleNativeObject obj,
HandleId id, unsigned attrs) {
MOZ_ASSERT(!JSID_IS_VOID(id));
AutoCheckShapeConsistency check(obj);
AssertValidArrayIndex(obj, id);
// Search for id in order to claim its entry if table has been allocated.
AutoKeepShapeCaches keep(cx);
RootedShape shape(cx);
{
ShapeTable* table;
ShapeTable::Entry* entry;
if (!Shape::search<MaybeAdding::Adding>(cx, obj->lastProperty(), id, keep,
shape.address(), &table, &entry)) {
return nullptr;
}
if (!shape) {
MOZ_ASSERT(
obj->isExtensible() ||
(JSID_IS_INT(id) && obj->containsDenseElement(JSID_TO_INT(id))),
"Can't add new property to non-extensible object");
return addDataPropertyInternal(cx, obj, id, SHAPE_INVALID_SLOT, attrs,
table, entry, keep);
}
// Property exists: search must have returned a valid entry.
MOZ_ASSERT_IF(entry, !entry->isRemoved());
}
AssertCanChangeAttrs(shape, attrs);
// If the caller wants to allocate a slot, but doesn't care which slot,
// copy the existing shape's slot into slot so we can match shape, if all
// other members match.