blob: 6353aaecf5cce58cc3129eb7f229302ac3227f05 [file] [log] [blame]
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
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.
15//
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.
27
28#ifndef V8_ELEMENTS_H_
29#define V8_ELEMENTS_H_
30
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +000031#include "elements-kind.h"
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000032#include "objects.h"
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +000033#include "heap.h"
34#include "isolate.h"
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000035
36namespace v8 {
37namespace internal {
38
39// Abstract base class for handles that can operate on objects with differing
40// ElementsKinds.
41class ElementsAccessor {
42 public:
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000043 explicit ElementsAccessor(const char* name) : name_(name) { }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000044 virtual ~ElementsAccessor() { }
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000045
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +000046 virtual ElementsKind kind() const = 0;
47 const char* name() const { return name_; }
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000048
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +000049 // Checks the elements of an object for consistency, asserting when a problem
50 // is found.
51 virtual void Validate(JSObject* obj) = 0;
52
erik.corry@gmail.combbceb572012-03-09 10:52:05 +000053 // 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,
59 JSObject* holder,
60 uint32_t key,
61 FixedArrayBase* backing_store = NULL) = 0;
62
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.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +000068 MUST_USE_RESULT virtual MaybeObject* Get(
69 Object* receiver,
70 JSObject* holder,
71 uint32_t key,
72 FixedArrayBase* backing_store = NULL) = 0;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000073
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000074 // Returns an element's attributes, or ABSENT if there is no such
75 // element. This method doesn't iterate up the prototype chain. The caller
76 // can optionally pass in the backing store to use for the check, which must
77 // be compatible with the ElementsKind of the ElementsAccessor. If
78 // backing_store is NULL, the holder->elements() is used as the backing store.
79 MUST_USE_RESULT virtual PropertyAttributes GetAttributes(
80 Object* receiver,
81 JSObject* holder,
82 uint32_t key,
83 FixedArrayBase* backing_store = NULL) = 0;
84
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000085 // Returns an element's type, or NONEXISTENT if there is no such
86 // element. This method doesn't iterate up the prototype chain. The caller
87 // can optionally pass in the backing store to use for the check, which must
88 // be compatible with the ElementsKind of the ElementsAccessor. If
89 // backing_store is NULL, the holder->elements() is used as the backing store.
90 MUST_USE_RESULT virtual PropertyType GetType(
91 Object* receiver,
92 JSObject* holder,
93 uint32_t key,
94 FixedArrayBase* backing_store = NULL) = 0;
95
96 // Returns an element's accessors, or NULL if the element does not exist or
97 // is plain. This method doesn't iterate up the prototype chain. The caller
98 // can optionally pass in the backing store to use for the check, which must
99 // be compatible with the ElementsKind of the ElementsAccessor. If
100 // backing_store is NULL, the holder->elements() is used as the backing store.
101 MUST_USE_RESULT virtual AccessorPair* GetAccessorPair(
102 Object* receiver,
103 JSObject* holder,
104 uint32_t key,
105 FixedArrayBase* backing_store = NULL) = 0;
106
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000107 // Modifies the length data property as specified for JSArrays and resizes the
108 // underlying backing store accordingly. The method honors the semantics of
109 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
110 // have non-deletable elements can only be shrunk to the size of highest
111 // element that is non-deletable.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000112 MUST_USE_RESULT virtual MaybeObject* SetLength(JSArray* holder,
113 Object* new_length) = 0;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000114
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000115 // Modifies both the length and capacity of a JSArray, resizing the underlying
116 // backing store as necessary. This method does NOT honor the semantics of
117 // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable
118 // elements. This method should only be called for array expansion OR by
119 // runtime JavaScript code that use InternalArrays and don't care about
120 // EcmaScript 5.1 semantics.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000121 MUST_USE_RESULT virtual MaybeObject* SetCapacityAndLength(JSArray* array,
122 int capacity,
123 int length) = 0;
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000124
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000125 // Deletes an element in an object, returning a new elements backing store.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000126 MUST_USE_RESULT virtual MaybeObject* Delete(JSObject* holder,
127 uint32_t key,
128 JSReceiver::DeleteMode mode) = 0;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000129
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000130 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
131 // of elements from source after source_start to the destination array.
132 static const int kCopyToEnd = -1;
133 // If kCopyToEndAndInitializeToHole is specified as the copy_size to
134 // CopyElements, it copies all of elements from source after source_start to
135 // destination array, padding any remaining uninitialized elements in the
136 // destination array with the hole.
137 static const int kCopyToEndAndInitializeToHole = -2;
138
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000139 // Copy elements from one backing store to another. Typically, callers specify
140 // the source JSObject or JSArray in source_holder. If the holder's backing
141 // store is available, it can be passed in source and source_holder is
142 // ignored.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000143 MUST_USE_RESULT virtual MaybeObject* CopyElements(
144 JSObject* source_holder,
145 uint32_t source_start,
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000146 ElementsKind source_kind,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000147 FixedArrayBase* destination,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000148 uint32_t destination_start,
149 int copy_size,
150 FixedArrayBase* source = NULL) = 0;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000151
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000152 MUST_USE_RESULT MaybeObject* CopyElements(JSObject* from_holder,
153 FixedArrayBase* to,
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000154 ElementsKind from_kind,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000155 FixedArrayBase* from = NULL) {
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +0000156 return CopyElements(from_holder, 0, from_kind, to, 0,
danno@chromium.orgc7406162012-03-26 09:46:02 +0000157 kCopyToEndAndInitializeToHole, from);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000158 }
159
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000160 MUST_USE_RESULT virtual MaybeObject* AddElementsToFixedArray(
161 Object* receiver,
162 JSObject* holder,
163 FixedArray* to,
164 FixedArrayBase* from = NULL) = 0;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000165
166 // Returns a shared ElementsAccessor for the specified ElementsKind.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000167 static ElementsAccessor* ForKind(ElementsKind elements_kind) {
168 ASSERT(elements_kind < kElementsKindCount);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000169 return elements_accessors_[elements_kind];
170 }
171
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000172 static ElementsAccessor* ForArray(FixedArrayBase* array);
173
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000174 static void InitializeOncePerProcess();
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000175 static void TearDown();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000176
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000177 protected:
178 friend class NonStrictArgumentsElementsAccessor;
179
180 virtual uint32_t GetCapacity(FixedArrayBase* backing_store) = 0;
181
ulan@chromium.org9a21ec42012-03-06 08:42:24 +0000182 // Element handlers distinguish between indexes and keys when they manipulate
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000183 // elements. Indexes refer to elements in terms of their location in the
ulan@chromium.org9a21ec42012-03-06 08:42:24 +0000184 // underlying storage's backing store representation, and are between 0 and
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000185 // GetCapacity. Keys refer to elements in terms of the value that would be
ulan@chromium.org9a21ec42012-03-06 08:42:24 +0000186 // specified in JavaScript to access the element. In most implementations,
187 // keys are equivalent to indexes, and GetKeyForIndex returns the same value
188 // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps
189 // the index to a key using the KeyAt method on the NumberDictionary.
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000190 virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store,
191 uint32_t index) = 0;
192
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000193 private:
194 static ElementsAccessor** elements_accessors_;
erik.corry@gmail.combbceb572012-03-09 10:52:05 +0000195 const char* name_;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000196
197 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
198};
199
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000200void CheckArrayAbuse(JSObject* obj, const char* op, uint32_t key,
201 bool allow_appending = false);
202
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000203MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements(
204 JSArray* array, Arguments* args);
205
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000206} } // namespace v8::internal
207
208#endif // V8_ELEMENTS_H_