1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_ELEMENTS_H_
29 #define V8_ELEMENTS_H_
31 #include "elements-kind.h"
39 // Abstract base class for handles that can operate on objects with differing
41 class ElementsAccessor {
43 explicit ElementsAccessor(const char* name) : name_(name) { }
44 virtual ~ElementsAccessor() { }
46 virtual ElementsKind kind() const = 0;
47 const char* name() const { return name_; }
49 // Checks the elements of an object for consistency, asserting when a problem
51 virtual void Validate(JSObject* obj) = 0;
53 // Returns true if a holder contains an element with the specified key
54 // without iterating up the prototype chain. The caller can optionally pass
55 // in the backing store to use for the check, which must be compatible with
56 // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the
57 // holder->elements() is used as the backing store.
58 virtual bool HasElement(Object* receiver,
61 FixedArrayBase* backing_store = NULL) = 0;
63 // Returns the element with the specified key or undefined if there is no such
64 // element. This method doesn't iterate up the prototype chain. The caller
65 // can optionally pass in the backing store to use for the check, which must
66 // be compatible with the ElementsKind of the ElementsAccessor. If
67 // backing_store is NULL, the holder->elements() is used as the backing store.
68 MUST_USE_RESULT virtual Handle<Object> Get(
69 Handle<Object> receiver,
70 Handle<JSObject> holder,
72 Handle<FixedArrayBase> backing_store =
73 Handle<FixedArrayBase>::null()) = 0;
75 MUST_USE_RESULT virtual MaybeObject* Get(
79 FixedArrayBase* backing_store = NULL) = 0;
81 // Returns an element's attributes, or ABSENT if there is no such
82 // element. This method doesn't iterate up the prototype chain. The caller
83 // can optionally pass in the backing store to use for the check, which must
84 // be compatible with the ElementsKind of the ElementsAccessor. If
85 // backing_store is NULL, the holder->elements() is used as the backing store.
86 MUST_USE_RESULT virtual PropertyAttributes GetAttributes(
90 FixedArrayBase* backing_store = NULL) = 0;
92 // Returns an element's type, or NONEXISTENT if there is no such
93 // element. This method doesn't iterate up the prototype chain. The caller
94 // can optionally pass in the backing store to use for the check, which must
95 // be compatible with the ElementsKind of the ElementsAccessor. If
96 // backing_store is NULL, the holder->elements() is used as the backing store.
97 MUST_USE_RESULT virtual PropertyType GetType(
101 FixedArrayBase* backing_store = NULL) = 0;
103 // Returns an element's accessors, or NULL if the element does not exist or
104 // is plain. This method doesn't iterate up the prototype chain. The caller
105 // can optionally pass in the backing store to use for the check, which must
106 // be compatible with the ElementsKind of the ElementsAccessor. If
107 // backing_store is NULL, the holder->elements() is used as the backing store.
108 MUST_USE_RESULT virtual AccessorPair* GetAccessorPair(
112 FixedArrayBase* backing_store = NULL) = 0;
114 // Modifies the length data property as specified for JSArrays and resizes the
115 // underlying backing store accordingly. The method honors the semantics of
116 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
117 // have non-deletable elements can only be shrunk to the size of highest
118 // element that is non-deletable.
119 MUST_USE_RESULT virtual Handle<Object> SetLength(
120 Handle<JSArray> holder,
121 Handle<Object> new_length) = 0;
123 // Modifies both the length and capacity of a JSArray, resizing the underlying
124 // backing store as necessary. This method does NOT honor the semantics of
125 // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable
126 // elements. This method should only be called for array expansion OR by
127 // runtime JavaScript code that use InternalArrays and don't care about
128 // EcmaScript 5.1 semantics.
129 virtual void SetCapacityAndLength(
130 Handle<JSArray> array,
134 // Deletes an element in an object, returning a new elements backing store.
135 MUST_USE_RESULT virtual Handle<Object> Delete(
136 Handle<JSObject> holder,
138 JSReceiver::DeleteMode mode) = 0;
140 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
141 // of elements from source after source_start to the destination array.
142 static const int kCopyToEnd = -1;
143 // If kCopyToEndAndInitializeToHole is specified as the copy_size to
144 // CopyElements, it copies all of elements from source after source_start to
145 // destination array, padding any remaining uninitialized elements in the
146 // destination array with the hole.
147 static const int kCopyToEndAndInitializeToHole = -2;
149 // Copy elements from one backing store to another. Typically, callers specify
150 // the source JSObject or JSArray in source_holder. If the holder's backing
151 // store is available, it can be passed in source and source_holder is
153 virtual void CopyElements(
154 Handle<JSObject> source_holder,
155 uint32_t source_start,
156 ElementsKind source_kind,
157 Handle<FixedArrayBase> destination,
158 uint32_t destination_start,
160 Handle<FixedArrayBase> source = Handle<FixedArrayBase>::null()) = 0;
161 MUST_USE_RESULT virtual MaybeObject* CopyElements(
162 JSObject* source_holder,
163 uint32_t source_start,
164 ElementsKind source_kind,
165 FixedArrayBase* destination,
166 uint32_t destination_start,
168 FixedArrayBase* source = NULL) = 0;
171 Handle<JSObject> from_holder,
172 Handle<FixedArrayBase> to,
173 ElementsKind from_kind,
174 Handle<FixedArrayBase> from = Handle<FixedArrayBase>::null()) {
175 CopyElements(from_holder, 0, from_kind, to, 0,
176 kCopyToEndAndInitializeToHole, from);
179 MUST_USE_RESULT MaybeObject* CopyElements(JSObject* from_holder,
181 ElementsKind from_kind,
182 FixedArrayBase* from = NULL) {
183 return CopyElements(from_holder, 0, from_kind, to, 0,
184 kCopyToEndAndInitializeToHole, from);
187 MUST_USE_RESULT virtual MaybeObject* AddElementsToFixedArray(
191 FixedArrayBase* from = NULL) = 0;
193 // Returns a shared ElementsAccessor for the specified ElementsKind.
194 static ElementsAccessor* ForKind(ElementsKind elements_kind) {
195 ASSERT(elements_kind < kElementsKindCount);
196 return elements_accessors_[elements_kind];
199 static ElementsAccessor* ForArray(FixedArrayBase* array);
201 static void InitializeOncePerProcess();
202 static void TearDown();
205 friend class SloppyArgumentsElementsAccessor;
207 virtual uint32_t GetCapacity(FixedArrayBase* backing_store) = 0;
209 // Element handlers distinguish between indexes and keys when they manipulate
210 // elements. Indexes refer to elements in terms of their location in the
211 // underlying storage's backing store representation, and are between 0 and
212 // GetCapacity. Keys refer to elements in terms of the value that would be
213 // specified in JavaScript to access the element. In most implementations,
214 // keys are equivalent to indexes, and GetKeyForIndex returns the same value
215 // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps
216 // the index to a key using the KeyAt method on the NumberDictionary.
217 virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store,
221 static ElementsAccessor** elements_accessors_;
224 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
227 void CheckArrayAbuse(JSObject* obj, const char* op, uint32_t key,
228 bool allow_appending = false);
230 Handle<Object> ArrayConstructInitializeElements(Handle<JSArray> array,
233 } } // namespace v8::internal
235 #endif // V8_ELEMENTS_H_