Line data Source code
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.
4 :
5 : #ifndef V8_ELEMENTS_H_
6 : #define V8_ELEMENTS_H_
7 :
8 : #include "src/elements-kind.h"
9 : #include "src/keys.h"
10 : #include "src/objects.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : // Abstract base class for handles that can operate on objects with differing
16 : // ElementsKinds.
17 : class ElementsAccessor {
18 : public:
19 1079540 : explicit ElementsAccessor(const char* name) : name_(name) { }
20 589200 : virtual ~ElementsAccessor() { }
21 :
22 : const char* name() const { return name_; }
23 :
24 : // Returns a shared ElementsAccessor for the specified ElementsKind.
25 742701 : static ElementsAccessor* ForKind(ElementsKind elements_kind) {
26 : DCHECK_LT(static_cast<int>(elements_kind), kElementsKindCount);
27 347938132 : return elements_accessors_[elements_kind];
28 : }
29 :
30 : // Checks the elements of an object for consistency, asserting when a problem
31 : // is found.
32 : virtual void Validate(JSObject* obj) = 0;
33 :
34 : // Returns true if a holder contains an element with the specified index
35 : // without iterating up the prototype chain. The caller can optionally pass
36 : // in the backing store to use for the check, which must be compatible with
37 : // the ElementsKind of the ElementsAccessor. If backing_store is nullptr, the
38 : // holder->elements() is used as the backing store. If a |filter| is
39 : // specified the PropertyAttributes of the element at the given index
40 : // are compared to the given |filter|. If they match/overlap the given
41 : // index is ignored. Note that only Dictionary elements have custom
42 : // PropertyAttributes associated, hence the |filter| argument is ignored for
43 : // all but DICTIONARY_ELEMENTS and SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
44 : virtual bool HasElement(JSObject* holder, uint32_t index,
45 : FixedArrayBase* backing_store,
46 : PropertyFilter filter = ALL_PROPERTIES) = 0;
47 :
48 : inline bool HasElement(JSObject* holder, uint32_t index,
49 : PropertyFilter filter = ALL_PROPERTIES) {
50 0 : return HasElement(holder, index, holder->elements(), filter);
51 : }
52 :
53 : // Note: this is currently not implemented for string wrapper and
54 : // typed array elements.
55 : virtual bool HasEntry(JSObject* holder, uint32_t entry) = 0;
56 :
57 : virtual Handle<Object> Get(Handle<JSObject> holder, uint32_t entry) = 0;
58 :
59 : virtual PropertyDetails GetDetails(JSObject* holder, uint32_t entry) = 0;
60 : virtual bool HasAccessors(JSObject* holder) = 0;
61 : virtual uint32_t NumberOfElements(JSObject* holder) = 0;
62 :
63 : // Modifies the length data property as specified for JSArrays and resizes the
64 : // underlying backing store accordingly. The method honors the semantics of
65 : // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
66 : // have non-deletable elements can only be shrunk to the size of highest
67 : // element that is non-deletable.
68 : virtual void SetLength(Handle<JSArray> holder, uint32_t new_length) = 0;
69 :
70 : // Deletes an element in an object.
71 : virtual void Delete(Handle<JSObject> holder, uint32_t entry) = 0;
72 :
73 : // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
74 : // of elements from source after source_start to the destination array.
75 : static const int kCopyToEnd = -1;
76 : // If kCopyToEndAndInitializeToHole is specified as the copy_size to
77 : // CopyElements, it copies all of elements from source after source_start to
78 : // destination array, padding any remaining uninitialized elements in the
79 : // destination array with the hole.
80 : static const int kCopyToEndAndInitializeToHole = -2;
81 :
82 : // Copy all indices that have elements from |object| into the given
83 : // KeyAccumulator. For Dictionary-based element-kinds we filter out elements
84 : // whose PropertyAttribute match |filter|.
85 : virtual void CollectElementIndices(Handle<JSObject> object,
86 : Handle<FixedArrayBase> backing_store,
87 : KeyAccumulator* keys) = 0;
88 :
89 8277546 : inline void CollectElementIndices(Handle<JSObject> object,
90 8277546 : KeyAccumulator* keys) {
91 : CollectElementIndices(object, handle(object->elements(), keys->isolate()),
92 16555092 : keys);
93 8277546 : }
94 :
95 : virtual Maybe<bool> CollectValuesOrEntries(
96 : Isolate* isolate, Handle<JSObject> object,
97 : Handle<FixedArray> values_or_entries, bool get_entries, int* nof_items,
98 : PropertyFilter filter = ALL_PROPERTIES) = 0;
99 :
100 : virtual MaybeHandle<FixedArray> PrependElementIndices(
101 : Handle<JSObject> object, Handle<FixedArrayBase> backing_store,
102 : Handle<FixedArray> keys, GetKeysConversion convert,
103 : PropertyFilter filter = ALL_PROPERTIES) = 0;
104 :
105 1008267 : inline MaybeHandle<FixedArray> PrependElementIndices(
106 : Handle<JSObject> object, Handle<FixedArray> keys,
107 : GetKeysConversion convert, PropertyFilter filter = ALL_PROPERTIES) {
108 : return PrependElementIndices(object, handle(object->elements()), keys,
109 2016534 : convert, filter);
110 : }
111 :
112 : virtual void AddElementsToKeyAccumulator(Handle<JSObject> receiver,
113 : KeyAccumulator* accumulator,
114 : AddKeyConversion convert) = 0;
115 :
116 : virtual void TransitionElementsKind(Handle<JSObject> object,
117 : Handle<Map> map) = 0;
118 : virtual void GrowCapacityAndConvert(Handle<JSObject> object,
119 : uint32_t capacity) = 0;
120 : // Unlike GrowCapacityAndConvert do not attempt to convert the backing store
121 : // and simply return false in this case.
122 : virtual bool GrowCapacity(Handle<JSObject> object, uint32_t index) = 0;
123 :
124 : static void InitializeOncePerProcess();
125 : static void TearDown();
126 :
127 : virtual void Set(Handle<JSObject> holder, uint32_t entry, Object* value) = 0;
128 :
129 : virtual void Reconfigure(Handle<JSObject> object,
130 : Handle<FixedArrayBase> backing_store, uint32_t entry,
131 : Handle<Object> value,
132 : PropertyAttributes attributes) = 0;
133 :
134 : virtual void Add(Handle<JSObject> object, uint32_t index,
135 : Handle<Object> value, PropertyAttributes attributes,
136 : uint32_t new_capacity) = 0;
137 :
138 : static Handle<JSArray> Concat(Isolate* isolate, Arguments* args,
139 : uint32_t concat_size, uint32_t result_length);
140 :
141 : virtual uint32_t Push(Handle<JSArray> receiver, Arguments* args,
142 : uint32_t push_size) = 0;
143 :
144 : virtual uint32_t Unshift(Handle<JSArray> receiver,
145 : Arguments* args, uint32_t unshift_size) = 0;
146 :
147 : virtual Handle<JSObject> Slice(Handle<JSObject> receiver, uint32_t start,
148 : uint32_t end) = 0;
149 :
150 : virtual Handle<JSObject> Slice(Handle<JSObject> receiver, uint32_t start,
151 : uint32_t end, Handle<JSObject> result) = 0;
152 :
153 : virtual Handle<JSArray> Splice(Handle<JSArray> receiver,
154 : uint32_t start, uint32_t delete_count,
155 : Arguments* args, uint32_t add_count) = 0;
156 :
157 : virtual Handle<Object> Pop(Handle<JSArray> receiver) = 0;
158 :
159 : virtual Handle<Object> Shift(Handle<JSArray> receiver) = 0;
160 :
161 : virtual Handle<SeededNumberDictionary> Normalize(Handle<JSObject> object) = 0;
162 :
163 : virtual uint32_t GetCapacity(JSObject* holder,
164 : FixedArrayBase* backing_store) = 0;
165 :
166 : virtual Object* Fill(Isolate* isolate, Handle<JSObject> receiver,
167 : Handle<Object> obj_value, uint32_t start,
168 : uint32_t end) = 0;
169 :
170 : // Check an Object's own elements for an element (using SameValueZero
171 : // semantics)
172 : virtual Maybe<bool> IncludesValue(Isolate* isolate, Handle<JSObject> receiver,
173 : Handle<Object> value, uint32_t start,
174 : uint32_t length) = 0;
175 :
176 : // Check an Object's own elements for the index of an element (using SameValue
177 : // semantics)
178 : virtual Maybe<int64_t> IndexOfValue(Isolate* isolate,
179 : Handle<JSObject> receiver,
180 : Handle<Object> value, uint32_t start,
181 : uint32_t length) = 0;
182 :
183 : virtual Maybe<int64_t> LastIndexOfValue(Isolate* isolate,
184 : Handle<JSObject> receiver,
185 : Handle<Object> value,
186 : uint32_t start) = 0;
187 :
188 : virtual void Reverse(JSObject* receiver) = 0;
189 :
190 : virtual void CopyElements(Handle<FixedArrayBase> source,
191 : ElementsKind source_kind,
192 : Handle<FixedArrayBase> destination, int size) = 0;
193 :
194 : virtual Object* CopyElements(Handle<JSReceiver> source,
195 : Handle<JSObject> destination, size_t length,
196 : uint32_t offset = 0) = 0;
197 :
198 : virtual Handle<FixedArray> CreateListFromArrayLike(Isolate* isolate,
199 : Handle<JSObject> object,
200 : uint32_t length) = 0;
201 :
202 : protected:
203 : friend class LookupIterator;
204 :
205 : // Element handlers distinguish between entries and indices when they
206 : // manipulate elements. Entries refer to elements in terms of their location
207 : // in the underlying storage's backing store representation, and are between 0
208 : // and GetCapacity. Indices refer to elements in terms of the value that would
209 : // be specified in JavaScript to access the element. In most implementations,
210 : // indices are equivalent to entries. In the NumberDictionary
211 : // ElementsAccessor, entries are mapped to an index using the KeyAt method on
212 : // the NumberDictionary.
213 : virtual uint32_t GetEntryForIndex(Isolate* isolate, JSObject* holder,
214 : FixedArrayBase* backing_store,
215 : uint32_t index) = 0;
216 :
217 : // NOTE: this method violates the handlified function signature convention:
218 : // raw pointer parameter |source_holder| in the function that allocates.
219 : // This is done intentionally to avoid ArrayConcat() builtin performance
220 : // degradation.
221 : virtual void CopyElements(JSObject* source_holder, uint32_t source_start,
222 : ElementsKind source_kind,
223 : Handle<FixedArrayBase> destination,
224 : uint32_t destination_start, int copy_size) = 0;
225 :
226 : private:
227 : static ElementsAccessor** elements_accessors_;
228 : const char* name_;
229 :
230 : DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
231 : };
232 :
233 : void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t index,
234 : bool allow_appending = false);
235 :
236 : MUST_USE_RESULT MaybeHandle<Object> ArrayConstructInitializeElements(
237 : Handle<JSArray> array,
238 : Arguments* args);
239 :
240 : } // namespace internal
241 : } // namespace v8
242 :
243 : #endif // V8_ELEMENTS_H_
|