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_
38 // Abstract base class for handles that can operate on objects with differing
40 class ElementsAccessor {
42 explicit ElementsAccessor(const char* name) : name_(name) { }
43 virtual ~ElementsAccessor() { }
45 virtual ElementsKind kind() const = 0;
46 const char* name() const { return name_; }
48 // Returns true if a holder contains an element with the specified key
49 // without iterating up the prototype chain. The caller can optionally pass
50 // in the backing store to use for the check, which must be compatible with
51 // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the
52 // holder->elements() is used as the backing store.
53 virtual bool HasElement(Object* receiver,
56 FixedArrayBase* backing_store = NULL) = 0;
58 // Returns the element with the specified key or undefined if there is no such
59 // element. This method doesn't iterate up the prototype chain. The caller
60 // can optionally pass in the backing store to use for the check, which must
61 // be compatible with the ElementsKind of the ElementsAccessor. If
62 // backing_store is NULL, the holder->elements() is used as the backing store.
63 MUST_USE_RESULT virtual MaybeObject* Get(
67 FixedArrayBase* backing_store = NULL) = 0;
69 // Modifies the length data property as specified for JSArrays and resizes the
70 // underlying backing store accordingly. The method honors the semantics of
71 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
72 // have non-deletable elements can only be shrunk to the size of highest
73 // element that is non-deletable.
74 MUST_USE_RESULT virtual MaybeObject* SetLength(JSArray* holder,
75 Object* new_length) = 0;
77 // Modifies both the length and capacity of a JSArray, resizing the underlying
78 // backing store as necessary. This method does NOT honor the semantics of
79 // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable
80 // elements. This method should only be called for array expansion OR by
81 // runtime JavaScript code that use InternalArrays and don't care about
82 // EcmaScript 5.1 semantics.
83 MUST_USE_RESULT virtual MaybeObject* SetCapacityAndLength(JSArray* array,
87 // Deletes an element in an object, returning a new elements backing store.
88 MUST_USE_RESULT virtual MaybeObject* Delete(JSObject* holder,
90 JSReceiver::DeleteMode mode) = 0;
92 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
93 // of elements from source after source_start to the destination array.
94 static const int kCopyToEnd = -1;
95 // If kCopyToEndAndInitializeToHole is specified as the copy_size to
96 // CopyElements, it copies all of elements from source after source_start to
97 // destination array, padding any remaining uninitialized elements in the
98 // destination array with the hole.
99 static const int kCopyToEndAndInitializeToHole = -2;
101 // Copy elements from one backing store to another. Typically, callers specify
102 // the source JSObject or JSArray in source_holder. If the holder's backing
103 // store is available, it can be passed in source and source_holder is
105 MUST_USE_RESULT virtual MaybeObject* CopyElements(
106 JSObject* source_holder,
107 uint32_t source_start,
108 FixedArrayBase* destination,
109 ElementsKind destination_kind,
110 uint32_t destination_start,
112 FixedArrayBase* source = NULL) = 0;
114 MUST_USE_RESULT MaybeObject* CopyElements(JSObject* from_holder,
116 ElementsKind to_kind,
117 FixedArrayBase* from = NULL) {
118 return CopyElements(from_holder, 0, to, to_kind, 0,
119 kCopyToEndAndInitializeToHole, from);
122 MUST_USE_RESULT virtual MaybeObject* AddElementsToFixedArray(
126 FixedArrayBase* from = NULL) = 0;
128 // Returns a shared ElementsAccessor for the specified ElementsKind.
129 static ElementsAccessor* ForKind(ElementsKind elements_kind) {
130 ASSERT(elements_kind < kElementsKindCount);
131 return elements_accessors_[elements_kind];
134 static ElementsAccessor* ForArray(FixedArrayBase* array);
136 static void InitializeOncePerProcess();
137 static void TearDown();
140 friend class NonStrictArgumentsElementsAccessor;
142 virtual uint32_t GetCapacity(FixedArrayBase* backing_store) = 0;
144 // Element handlers distinguish between indexes and keys when they manipulate
145 // elements. Indexes refer to elements in terms of their location in the
146 // underlying storage's backing store representation, and are between 0 and
147 // GetCapacity. Keys refer to elements in terms of the value that would be
148 // specified in JavaScript to access the element. In most implementations,
149 // keys are equivalent to indexes, and GetKeyForIndex returns the same value
150 // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps
151 // the index to a key using the KeyAt method on the NumberDictionary.
152 virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store,
156 static ElementsAccessor** elements_accessors_;
159 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
163 void CopyObjectToObjectElements(FixedArray* from_obj,
164 ElementsKind from_kind,
167 ElementsKind to_kind,
172 } } // namespace v8::internal
174 #endif // V8_ELEMENTS_H_