1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef vm_ArrayObject_inl_h
8 #define vm_ArrayObject_inl_h
9 
10 #include "vm/ArrayObject.h"
11 
12 #include "gc/GCTrace.h"
13 #include "vm/String.h"
14 
15 #include "jsgcinlines.h"
16 #include "jsobjinlines.h"
17 
18 #include "vm/TypeInference-inl.h"
19 
20 namespace js {
21 
22 inline void
setLength(ExclusiveContext * cx,uint32_t length)23 ArrayObject::setLength(ExclusiveContext* cx, uint32_t length)
24 {
25     MOZ_ASSERT(lengthIsWritable());
26 
27     if (length > INT32_MAX) {
28         /* Track objects with overflowing lengths in type information. */
29         MarkObjectGroupFlags(cx, this, OBJECT_FLAG_LENGTH_OVERFLOW);
30     }
31 
32     getElementsHeader()->length = length;
33 }
34 
35 /* static */ inline ArrayObject*
createArrayInternal(ExclusiveContext * cx,gc::AllocKind kind,gc::InitialHeap heap,HandleShape shape,HandleObjectGroup group,AutoSetNewObjectMetadata &)36 ArrayObject::createArrayInternal(ExclusiveContext* cx, gc::AllocKind kind, gc::InitialHeap heap,
37                                  HandleShape shape, HandleObjectGroup group,
38                                  AutoSetNewObjectMetadata&)
39 {
40     // Create a new array and initialize everything except for its elements.
41     MOZ_ASSERT(shape && group);
42     MOZ_ASSERT(group->clasp() == shape->getObjectClass());
43     MOZ_ASSERT(group->clasp() == &ArrayObject::class_);
44     MOZ_ASSERT_IF(group->clasp()->finalize, heap == gc::TenuredHeap);
45     MOZ_ASSERT_IF(group->hasUnanalyzedPreliminaryObjects(),
46                   heap == js::gc::TenuredHeap);
47     MOZ_ASSERT(group->clasp()->shouldDelayMetadataCallback());
48 
49     // Arrays can use their fixed slots to store elements, so can't have shapes
50     // which allow named properties to be stored in the fixed slots.
51     MOZ_ASSERT(shape->numFixedSlots() == 0);
52 
53     size_t nDynamicSlots = dynamicSlotsCount(0, shape->slotSpan(), group->clasp());
54     JSObject* obj = Allocate<JSObject>(cx, kind, nDynamicSlots, heap, group->clasp());
55     if (!obj)
56         return nullptr;
57 
58     static_cast<ArrayObject*>(obj)->shape_.init(shape);
59     static_cast<ArrayObject*>(obj)->group_.init(group);
60 
61     cx->compartment()->setObjectPendingMetadata(cx, obj);
62     return &obj->as<ArrayObject>();
63 }
64 
65 /* static */ inline ArrayObject*
finishCreateArray(ArrayObject * obj,HandleShape shape,AutoSetNewObjectMetadata & metadata)66 ArrayObject::finishCreateArray(ArrayObject* obj, HandleShape shape, AutoSetNewObjectMetadata& metadata)
67 {
68     size_t span = shape->slotSpan();
69     if (span)
70         obj->initializeSlotRange(0, span);
71 
72     gc::TraceCreateObject(obj);
73 
74     return obj;
75 }
76 
77 /* static */ inline ArrayObject*
createArray(ExclusiveContext * cx,gc::AllocKind kind,gc::InitialHeap heap,HandleShape shape,HandleObjectGroup group,uint32_t length,AutoSetNewObjectMetadata & metadata)78 ArrayObject::createArray(ExclusiveContext* cx, gc::AllocKind kind, gc::InitialHeap heap,
79                          HandleShape shape, HandleObjectGroup group,
80                          uint32_t length, AutoSetNewObjectMetadata& metadata)
81 {
82     ArrayObject* obj = createArrayInternal(cx, kind, heap, shape, group, metadata);
83     if (!obj)
84         return nullptr;
85 
86     uint32_t capacity = gc::GetGCKindSlots(kind) - ObjectElements::VALUES_PER_HEADER;
87 
88     obj->setFixedElements();
89     new (obj->getElementsHeader()) ObjectElements(capacity, length);
90 
91     return finishCreateArray(obj, shape, metadata);
92 }
93 
94 /* static */ inline ArrayObject*
createCopyOnWriteArray(ExclusiveContext * cx,gc::InitialHeap heap,HandleArrayObject sharedElementsOwner)95 ArrayObject::createCopyOnWriteArray(ExclusiveContext* cx, gc::InitialHeap heap,
96                                     HandleArrayObject sharedElementsOwner)
97 {
98     MOZ_ASSERT(sharedElementsOwner->getElementsHeader()->isCopyOnWrite());
99     MOZ_ASSERT(sharedElementsOwner->getElementsHeader()->ownerObject() == sharedElementsOwner);
100 
101     // Use the smallest allocation kind for the array, as it can't have any
102     // fixed slots (see the assert in createArrayInternal) and will not be using
103     // its fixed elements.
104     gc::AllocKind kind = gc::AllocKind::OBJECT0_BACKGROUND;
105 
106     AutoSetNewObjectMetadata metadata(cx);
107     RootedShape shape(cx, sharedElementsOwner->lastProperty());
108     RootedObjectGroup group(cx, sharedElementsOwner->group());
109     ArrayObject* obj = createArrayInternal(cx, kind, heap, shape, group, metadata);
110     if (!obj)
111         return nullptr;
112 
113     obj->elements_ = sharedElementsOwner->getDenseElementsAllowCopyOnWrite();
114 
115     return finishCreateArray(obj, shape, metadata);
116 }
117 
118 } // namespace js
119 
120 #endif // vm_ArrayObject_inl_h
121