fcc90024ba72a24c9385537d9fb8db491a0f5a0f
[platform/upstream/v8.git] / src / elements.h
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/heap/heap.h"
10 #include "src/isolate.h"
11 #include "src/objects.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // Abstract base class for handles that can operate on objects with differing
17 // ElementsKinds.
18 class ElementsAccessor {
19  public:
20   explicit ElementsAccessor(const char* name) : name_(name) { }
21   virtual ~ElementsAccessor() { }
22
23   const char* name() const { return name_; }
24
25   // Checks the elements of an object for consistency, asserting when a problem
26   // is found.
27   virtual void Validate(Handle<JSObject> obj) = 0;
28
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;
36
37   inline bool HasElement(Handle<JSObject> holder, uint32_t index) {
38     return HasElement(holder, index, handle(holder->elements()));
39   }
40
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,
44                         uint32_t end) = 0;
45
46   virtual Handle<Object> Get(Handle<FixedArrayBase> backing_store,
47                              uint32_t entry) = 0;
48
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;
55
56   // Deletes an element in an object.
57   virtual void Delete(Handle<JSObject> holder, uint32_t entry) = 0;
58
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;
67
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
71   // ignored.
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,
78       int copy_size) = 0;
79
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
83   // degradation.
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,
90       int copy_size) = 0;
91
92   inline void CopyElements(
93       Handle<JSObject> from_holder,
94       Handle<FixedArrayBase> to,
95       ElementsKind from_kind) {
96     CopyElements(
97       *from_holder, 0, from_kind, to, 0, kCopyToEndAndInitializeToHole);
98   }
99
100   virtual void GrowCapacityAndConvert(Handle<JSObject> object,
101                                       uint32_t capacity) = 0;
102
103   virtual void AddElementsToKeyAccumulator(Handle<JSObject> receiver,
104                                            KeyAccumulator* accumulator,
105                                            FixedArray::KeyFilter filter) = 0;
106
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];
111   }
112
113   static ElementsAccessor* ForArray(Handle<FixedArrayBase> array);
114
115   static void InitializeOncePerProcess();
116   static void TearDown();
117
118   virtual void Set(FixedArrayBase* backing_store, uint32_t entry,
119                    Object* value) = 0;
120
121   virtual void Reconfigure(Handle<JSObject> object,
122                            Handle<FixedArrayBase> backing_store, uint32_t entry,
123                            Handle<Object> value,
124                            PropertyAttributes attributes) = 0;
125
126   virtual void Add(Handle<JSObject> object, uint32_t index,
127                    Handle<Object> value, PropertyAttributes attributes,
128                    uint32_t new_capacity) = 0;
129
130   static Handle<JSArray> Concat(Isolate* isolate, Arguments* args,
131                                 uint32_t concat_size);
132
133   virtual uint32_t Push(Handle<JSArray> receiver,
134                         Handle<FixedArrayBase> backing_store, Arguments* args,
135                         uint32_t push_size) = 0;
136
137   virtual uint32_t Unshift(Handle<JSArray> receiver,
138                            Handle<FixedArrayBase> backing_store,
139                            Arguments* args, uint32_t unshift_size) = 0;
140
141   virtual Handle<JSArray> Slice(Handle<JSObject> receiver,
142                                 Handle<FixedArrayBase> backing_store,
143                                 uint32_t start, uint32_t end) = 0;
144
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;
149
150   virtual Handle<Object> Pop(Handle<JSArray> receiver,
151                              Handle<FixedArrayBase> backing_store) = 0;
152
153   virtual Handle<Object> Shift(Handle<JSArray> receiver,
154                                Handle<FixedArrayBase> backing_store) = 0;
155
156  protected:
157   friend class LookupIterator;
158
159   static ElementsAccessor* ForArray(FixedArrayBase* array);
160
161   virtual uint32_t GetCapacity(JSObject* holder,
162                                FixedArrayBase* backing_store) = 0;
163
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,
174                                     uint32_t index) = 0;
175   virtual PropertyDetails GetDetails(FixedArrayBase* backing_store,
176                                      uint32_t entry) = 0;
177
178  private:
179   static ElementsAccessor** elements_accessors_;
180   const char* name_;
181
182   DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
183 };
184
185 void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t index,
186                      bool allow_appending = false);
187
188 MUST_USE_RESULT MaybeHandle<Object> ArrayConstructInitializeElements(
189     Handle<JSArray> array,
190     Arguments* args);
191
192 } }  // namespace v8::internal
193
194 #endif  // V8_ELEMENTS_H_