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/StringType.h"
14 
15 #include "vm/JSObject-inl.h"
16 #include "vm/TypeInference-inl.h"
17 
18 namespace js {
19 
setLength(JSContext * cx,uint32_t length)20 inline void ArrayObject::setLength(JSContext* cx, uint32_t length) {
21   MOZ_ASSERT(lengthIsWritable());
22   MOZ_ASSERT_IF(length != getElementsHeader()->length,
23                 !denseElementsAreFrozen());
24 
25   if (length > INT32_MAX) {
26     /* Track objects with overflowing lengths in type information. */
27     MarkObjectGroupFlags(cx, this, OBJECT_FLAG_LENGTH_OVERFLOW);
28   }
29 
30   getElementsHeader()->length = length;
31 }
32 
createArrayInternal(JSContext * cx,gc::AllocKind kind,gc::InitialHeap heap,HandleShape shape,HandleObjectGroup group,AutoSetNewObjectMetadata &)33 /* static */ inline ArrayObject* ArrayObject::createArrayInternal(
34     JSContext* cx, gc::AllocKind kind, gc::InitialHeap heap, HandleShape shape,
35     HandleObjectGroup group, AutoSetNewObjectMetadata&) {
36   const js::Class* clasp = group->clasp();
37   MOZ_ASSERT(shape && group);
38   MOZ_ASSERT(clasp == shape->getObjectClass());
39   MOZ_ASSERT(clasp == &ArrayObject::class_);
40   MOZ_ASSERT_IF(clasp->hasFinalize(), heap == gc::TenuredHeap);
41   MOZ_ASSERT_IF(group->hasUnanalyzedPreliminaryObjects(),
42                 heap == js::gc::TenuredHeap);
43 
44   // Arrays can use their fixed slots to store elements, so can't have shapes
45   // which allow named properties to be stored in the fixed slots.
46   MOZ_ASSERT(shape->numFixedSlots() == 0);
47 
48   size_t nDynamicSlots = dynamicSlotsCount(0, shape->slotSpan(), clasp);
49   JSObject* obj = js::Allocate<JSObject>(cx, kind, nDynamicSlots, heap, clasp);
50   if (!obj) return nullptr;
51 
52   ArrayObject* aobj = static_cast<ArrayObject*>(obj);
53   aobj->initGroup(group);
54   aobj->initShape(shape);
55   // NOTE: Dynamic slots are created internally by Allocate<JSObject>.
56   if (!nDynamicSlots) aobj->initSlots(nullptr);
57 
58   MOZ_ASSERT(clasp->shouldDelayMetadataBuilder());
59   cx->compartment()->setObjectPendingMetadata(cx, aobj);
60 
61   return aobj;
62 }
63 
finishCreateArray(ArrayObject * obj,HandleShape shape,AutoSetNewObjectMetadata & metadata)64 /* static */ inline ArrayObject* ArrayObject::finishCreateArray(
65     ArrayObject* obj, HandleShape shape, AutoSetNewObjectMetadata& metadata) {
66   size_t span = shape->slotSpan();
67   if (span) obj->initializeSlotRange(0, span);
68 
69   gc::TraceCreateObject(obj);
70 
71   return obj;
72 }
73 
createArray(JSContext * cx,gc::AllocKind kind,gc::InitialHeap heap,HandleShape shape,HandleObjectGroup group,uint32_t length,AutoSetNewObjectMetadata & metadata)74 /* static */ inline ArrayObject* ArrayObject::createArray(
75     JSContext* cx, gc::AllocKind kind, gc::InitialHeap heap, HandleShape shape,
76     HandleObjectGroup group, uint32_t length,
77     AutoSetNewObjectMetadata& metadata) {
78   ArrayObject* obj =
79       createArrayInternal(cx, kind, heap, shape, group, metadata);
80   if (!obj) return nullptr;
81 
82   uint32_t capacity =
83       gc::GetGCKindSlots(kind) - ObjectElements::VALUES_PER_HEADER;
84 
85   obj->setFixedElements();
86   new (obj->getElementsHeader()) ObjectElements(capacity, length);
87 
88   return finishCreateArray(obj, shape, metadata);
89 }
90 
createCopyOnWriteArray(JSContext * cx,gc::InitialHeap heap,HandleArrayObject sharedElementsOwner)91 /* static */ inline ArrayObject* ArrayObject::createCopyOnWriteArray(
92     JSContext* cx, gc::InitialHeap heap,
93     HandleArrayObject sharedElementsOwner) {
94   MOZ_ASSERT(sharedElementsOwner->getElementsHeader()->isCopyOnWrite());
95   MOZ_ASSERT(sharedElementsOwner->getElementsHeader()->ownerObject() ==
96              sharedElementsOwner);
97 
98   // Use the smallest allocation kind for the array, as it can't have any
99   // fixed slots (see the assert in createArrayInternal) and will not be using
100   // its fixed elements.
101   gc::AllocKind kind = gc::AllocKind::OBJECT0_BACKGROUND;
102 
103   AutoSetNewObjectMetadata metadata(cx);
104   RootedShape shape(cx, sharedElementsOwner->lastProperty());
105   RootedObjectGroup group(cx, sharedElementsOwner->group());
106   ArrayObject* obj =
107       createArrayInternal(cx, kind, heap, shape, group, metadata);
108   if (!obj) return nullptr;
109 
110   obj->elements_ = sharedElementsOwner->getDenseElementsAllowCopyOnWrite();
111 
112   return finishCreateArray(obj, shape, metadata);
113 }
114 
115 }  // namespace js
116 
117 #endif  // vm_ArrayObject_inl_h
118