1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "src/elements-kind.h"
9 #include "src/heap/heap.h"
10 #include "src/isolate.h"
11 #include "src/objects.h"
16 // Abstract base class for handles that can operate on objects with differing
18 class ElementsAccessor {
20 explicit ElementsAccessor(const char* name) : name_(name) { }
21 virtual ~ElementsAccessor() { }
23 const char* name() const { return name_; }
25 // Checks the elements of an object for consistency, asserting when a problem
27 virtual void Validate(Handle<JSObject> obj) = 0;
29 // Returns true if a holder contains an element with the specified index
30 // without iterating up the prototype chain. The caller can optionally pass
31 // in the backing store to use for the check, which must be compatible with
32 // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the
33 // holder->elements() is used as the backing store.
34 virtual bool HasElement(Handle<JSObject> holder, uint32_t index,
35 Handle<FixedArrayBase> backing_store) = 0;
37 inline bool HasElement(Handle<JSObject> holder, uint32_t index) {
38 return HasElement(holder, index, handle(holder->elements()));
41 // Returns true if the backing store is compact in the given range
42 virtual bool IsPacked(Handle<JSObject> holder,
43 Handle<FixedArrayBase> backing_store, uint32_t start,
46 virtual Handle<Object> Get(Handle<FixedArrayBase> backing_store,
49 // Modifies the length data property as specified for JSArrays and resizes the
50 // underlying backing store accordingly. The method honors the semantics of
51 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
52 // have non-deletable elements can only be shrunk to the size of highest
53 // element that is non-deletable.
54 virtual void SetLength(Handle<JSArray> holder, uint32_t new_length) = 0;
56 // Deletes an element in an object.
57 virtual void Delete(Handle<JSObject> holder, uint32_t entry) = 0;
59 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
60 // of elements from source after source_start to the destination array.
61 static const int kCopyToEnd = -1;
62 // If kCopyToEndAndInitializeToHole is specified as the copy_size to
63 // CopyElements, it copies all of elements from source after source_start to
64 // destination array, padding any remaining uninitialized elements in the
65 // destination array with the hole.
66 static const int kCopyToEndAndInitializeToHole = -2;
68 // Copy elements from one backing store to another. Typically, callers specify
69 // the source JSObject or JSArray in source_holder. If the holder's backing
70 // store is available, it can be passed in source and source_holder is
72 virtual void CopyElements(
73 Handle<FixedArrayBase> source,
74 uint32_t source_start,
75 ElementsKind source_kind,
76 Handle<FixedArrayBase> destination,
77 uint32_t destination_start,
80 // NOTE: this method violates the handlified function signature convention:
81 // raw pointer parameter |source_holder| in the function that allocates.
82 // This is done intentionally to avoid ArrayConcat() builtin performance
84 virtual void CopyElements(
85 JSObject* source_holder,
86 uint32_t source_start,
87 ElementsKind source_kind,
88 Handle<FixedArrayBase> destination,
89 uint32_t destination_start,
92 inline void CopyElements(
93 Handle<JSObject> from_holder,
94 Handle<FixedArrayBase> to,
95 ElementsKind from_kind) {
97 *from_holder, 0, from_kind, to, 0, kCopyToEndAndInitializeToHole);
100 virtual void GrowCapacityAndConvert(Handle<JSObject> object,
101 uint32_t capacity) = 0;
103 virtual void AddElementsToKeyAccumulator(Handle<JSObject> receiver,
104 KeyAccumulator* accumulator,
105 FixedArray::KeyFilter filter) = 0;
107 // Returns a shared ElementsAccessor for the specified ElementsKind.
108 static ElementsAccessor* ForKind(ElementsKind elements_kind) {
109 DCHECK(static_cast<int>(elements_kind) < kElementsKindCount);
110 return elements_accessors_[elements_kind];
113 static ElementsAccessor* ForArray(Handle<FixedArrayBase> array);
115 static void InitializeOncePerProcess();
116 static void TearDown();
118 virtual void Set(FixedArrayBase* backing_store, uint32_t entry,
121 virtual void Reconfigure(Handle<JSObject> object,
122 Handle<FixedArrayBase> backing_store, uint32_t entry,
123 Handle<Object> value,
124 PropertyAttributes attributes) = 0;
126 virtual void Add(Handle<JSObject> object, uint32_t index,
127 Handle<Object> value, PropertyAttributes attributes,
128 uint32_t new_capacity) = 0;
130 static Handle<JSArray> Concat(Isolate* isolate, Arguments* args,
131 uint32_t concat_size);
133 virtual uint32_t Push(Handle<JSArray> receiver,
134 Handle<FixedArrayBase> backing_store, Arguments* args,
135 uint32_t push_size) = 0;
137 virtual uint32_t Unshift(Handle<JSArray> receiver,
138 Handle<FixedArrayBase> backing_store,
139 Arguments* args, uint32_t unshift_size) = 0;
141 virtual Handle<JSArray> Slice(Handle<JSObject> receiver,
142 Handle<FixedArrayBase> backing_store,
143 uint32_t start, uint32_t end) = 0;
145 virtual Handle<JSArray> Splice(Handle<JSArray> receiver,
146 Handle<FixedArrayBase> backing_store,
147 uint32_t start, uint32_t delete_count,
148 Arguments* args, uint32_t add_count) = 0;
150 virtual Handle<Object> Pop(Handle<JSArray> receiver,
151 Handle<FixedArrayBase> backing_store) = 0;
153 virtual Handle<Object> Shift(Handle<JSArray> receiver,
154 Handle<FixedArrayBase> backing_store) = 0;
157 friend class LookupIterator;
159 static ElementsAccessor* ForArray(FixedArrayBase* array);
161 virtual uint32_t GetCapacity(JSObject* holder,
162 FixedArrayBase* backing_store) = 0;
164 // Element handlers distinguish between entries and indices when they
165 // manipulate elements. Entries refer to elements in terms of their location
166 // in the underlying storage's backing store representation, and are between 0
167 // and GetCapacity. Indices refer to elements in terms of the value that would
168 // be specified in JavaScript to access the element. In most implementations,
169 // indices are equivalent to entries. In the NumberDictionary
170 // ElementsAccessor, entries are mapped to an index using the KeyAt method on
171 // the NumberDictionary.
172 virtual uint32_t GetEntryForIndex(JSObject* holder,
173 FixedArrayBase* backing_store,
175 virtual PropertyDetails GetDetails(FixedArrayBase* backing_store,
179 static ElementsAccessor** elements_accessors_;
182 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
185 void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t index,
186 bool allow_appending = false);
188 MUST_USE_RESULT MaybeHandle<Object> ArrayConstructInitializeElements(
189 Handle<JSArray> array,
192 } } // namespace v8::internal
194 #endif // V8_ELEMENTS_H_