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/NativeObject.h" 11 12 namespace js { 13 14 class AutoSetNewObjectMetadata; 15 16 class ArrayObject : public NativeObject { 17 public: 18 // Array(x) eagerly allocates dense elements if x <= this value. Without 19 // the subtraction the max would roll over to the next power-of-two (4096) 20 // due to the way that growElements() and goodAllocated() work. 21 static const uint32_t EagerAllocationMaxLength = 22 2048 - ObjectElements::VALUES_PER_HEADER; 23 24 static const JSClass class_; 25 lengthIsWritable()26 bool lengthIsWritable() const { 27 return !getElementsHeader()->hasNonwritableArrayLength(); 28 } 29 length()30 uint32_t length() const { return getElementsHeader()->length; } 31 setNonWritableLength(JSContext * cx)32 void setNonWritableLength(JSContext* cx) { 33 shrinkCapacityToInitializedLength(cx); 34 getElementsHeader()->setNonwritableArrayLength(); 35 } 36 37 inline void setLength(JSContext* cx, uint32_t length); 38 39 // Variant of setLength for use on arrays where the length cannot overflow 40 // int32_t. setLengthInt32(uint32_t length)41 void setLengthInt32(uint32_t length) { 42 MOZ_ASSERT(lengthIsWritable()); 43 MOZ_ASSERT_IF(length != getElementsHeader()->length, 44 !denseElementsAreFrozen()); 45 MOZ_ASSERT(length <= INT32_MAX); 46 getElementsHeader()->length = length; 47 } 48 49 // Make an array object with the specified initial state. 50 static inline ArrayObject* createArray(JSContext* cx, gc::AllocKind kind, 51 gc::InitialHeap heap, 52 HandleShape shape, 53 HandleObjectGroup group, 54 uint32_t length, 55 AutoSetNewObjectMetadata& metadata); 56 57 // Make a copy-on-write array object which shares the elements of an 58 // existing object. 59 static inline ArrayObject* createCopyOnWriteArray( 60 JSContext* cx, gc::InitialHeap heap, 61 HandleArrayObject sharedElementsOwner); 62 63 private: 64 // Helper for the above methods. 65 static inline ArrayObject* createArrayInternal( 66 JSContext* cx, gc::AllocKind kind, gc::InitialHeap heap, 67 HandleShape shape, HandleObjectGroup group, AutoSetNewObjectMetadata&); 68 69 static inline ArrayObject* finishCreateArray( 70 ArrayObject* obj, HandleShape shape, AutoSetNewObjectMetadata& metadata); 71 }; 72 73 } // namespace js 74 75 #endif // vm_ArrayObject_h 76