Debian Codegraph @

js/src/vm/ArrayObject.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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_h
8 #define vm_ArrayObject_h
9
10 #include "vm/JSContext.h"
11 #include "vm/NativeObject.h"
12
13 namespace js {
14
15 class AutoSetNewObjectMetadata;
16
17 class ArrayObject : public NativeObject {
18 public:
19 // Array(x) eagerly allocates dense elements if x <= this value.
20 // This number was chosen so that the elements, the elements header,
21 // and the MediumBuffer header all fit within MaxMediumAllocSize.
22 static const uint32_t EagerAllocationMaxLength =
23 (1 << 16) - ObjectElements::VALUES_PER_HEADER - 1;
24
25 static const JSClass class_;
26
27 bool lengthIsWritable() const {
28 return !getElementsHeader()->hasNonwritableArrayLength();
29 }
30
31 uint32_t length() const { return getElementsHeader()->length; }
32
33 void setNonWritableLength(JSContext* cx) {
34 shrinkCapacityToInitializedLength(cx);
35 assertInt32LengthFuse(cx);
36 getElementsHeader()->setNonwritableArrayLength();
37 }
38
39 void setLengthToInitializedLength() {
40 MOZ_ASSERT(lengthIsWritable());
41 MOZ_ASSERT_IF(length() != getElementsHeader()->length,
42 !denseElementsAreFrozen());
43 getElementsHeader()->length = getDenseInitializedLength();
44 static_assert(MAX_DENSE_ELEMENTS_COUNT <= INT32_MAX,
45 "No need to check HasSeenArrayExceedsInt32LengthFuse");
46 }
47
48 void setLength(JSContext* cx, uint32_t length) {
49 MOZ_ASSERT(lengthIsWritable());
50 MOZ_ASSERT_IF(length != getElementsHeader()->length,
51 !denseElementsAreFrozen());
52 assertInt32LengthFuse(cx);
53 NativeObject::elementsSizeMustNotOverflow();
54 if (MOZ_UNLIKELY(length > INT32_MAX)) {
55 cx->runtime()->hasSeenArrayExceedsInt32LengthFuse.ref().popFuse(cx);
56 }
57 getElementsHeader()->length = length;
58 }
59
60 void assertInt32LengthFuse(JSContext* cx) {
61 MOZ_ASSERT_IF(
62 length() > INT32_MAX,
63 !cx->runtime()->hasSeenArrayExceedsInt32LengthFuse.ref().intact());
64 }
65
66 // Try to add a new dense element to this array. The array must be extensible.
67 //
68 // Returns DenseElementResult::Incomplete if `index >= length`, if the array
69 // has sparse elements, if we're adding a sparse element, or if the array
70 // already contains a dense element at this index.
71 inline DenseElementResult addDenseElementNoLengthChange(JSContext* cx,
72 uint32_t index,
73 const Value& val);
74
75 // Make an array object with the specified initial state.
76 static MOZ_ALWAYS_INLINE ArrayObject* create(
77 JSContext* cx, gc::AllocKind kind, gc::Heap heap,
78 Handle<SharedShape*> shape, uint32_t length, uint32_t slotSpan,
79 AutoSetNewObjectMetadata& metadata, gc::AllocSite* site = nullptr);
80 };
81
82 } // namespace js
83
84 #endif // vm_ArrayObject_h