ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2 | // 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.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 31 | #include "elements-kind.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 32 | #include "objects.h" |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 33 | #include "heap.h" |
| 34 | #include "isolate.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | // Abstract base class for handles that can operate on objects with differing |
| 40 | // ElementsKinds. |
| 41 | class ElementsAccessor { |
| 42 | public: |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 43 | explicit ElementsAccessor(const char* name) : name_(name) { } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 44 | virtual ~ElementsAccessor() { } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 45 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 46 | virtual ElementsKind kind() const = 0; |
| 47 | const char* name() const { return name_; } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 48 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 49 | // 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.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 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, |
| 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.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 68 | MUST_USE_RESULT virtual MaybeObject* Get( |
| 69 | Object* receiver, |
| 70 | JSObject* holder, |
| 71 | uint32_t key, |
| 72 | FixedArrayBase* backing_store = NULL) = 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 73 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 74 | // Modifies the length data property as specified for JSArrays and resizes the |
| 75 | // underlying backing store accordingly. The method honors the semantics of |
| 76 | // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that |
| 77 | // have non-deletable elements can only be shrunk to the size of highest |
| 78 | // element that is non-deletable. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 79 | MUST_USE_RESULT virtual MaybeObject* SetLength(JSArray* holder, |
| 80 | Object* new_length) = 0; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 81 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 82 | // Modifies both the length and capacity of a JSArray, resizing the underlying |
| 83 | // backing store as necessary. This method does NOT honor the semantics of |
| 84 | // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable |
| 85 | // elements. This method should only be called for array expansion OR by |
| 86 | // runtime JavaScript code that use InternalArrays and don't care about |
| 87 | // EcmaScript 5.1 semantics. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 88 | MUST_USE_RESULT virtual MaybeObject* SetCapacityAndLength(JSArray* array, |
| 89 | int capacity, |
| 90 | int length) = 0; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 91 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 92 | // Deletes an element in an object, returning a new elements backing store. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 93 | MUST_USE_RESULT virtual MaybeObject* Delete(JSObject* holder, |
| 94 | uint32_t key, |
| 95 | JSReceiver::DeleteMode mode) = 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 96 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 97 | // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all |
| 98 | // of elements from source after source_start to the destination array. |
| 99 | static const int kCopyToEnd = -1; |
| 100 | // If kCopyToEndAndInitializeToHole is specified as the copy_size to |
| 101 | // CopyElements, it copies all of elements from source after source_start to |
| 102 | // destination array, padding any remaining uninitialized elements in the |
| 103 | // destination array with the hole. |
| 104 | static const int kCopyToEndAndInitializeToHole = -2; |
| 105 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 106 | // Copy elements from one backing store to another. Typically, callers specify |
| 107 | // the source JSObject or JSArray in source_holder. If the holder's backing |
| 108 | // store is available, it can be passed in source and source_holder is |
| 109 | // ignored. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 110 | MUST_USE_RESULT virtual MaybeObject* CopyElements( |
| 111 | JSObject* source_holder, |
| 112 | uint32_t source_start, |
| 113 | FixedArrayBase* destination, |
| 114 | ElementsKind destination_kind, |
| 115 | uint32_t destination_start, |
| 116 | int copy_size, |
| 117 | FixedArrayBase* source = NULL) = 0; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 118 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 119 | MUST_USE_RESULT MaybeObject* CopyElements(JSObject* from_holder, |
| 120 | FixedArrayBase* to, |
| 121 | ElementsKind to_kind, |
| 122 | FixedArrayBase* from = NULL) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 123 | return CopyElements(from_holder, 0, to, to_kind, 0, |
danno@chromium.org | c740616 | 2012-03-26 09:46:02 +0000 | [diff] [blame] | 124 | kCopyToEndAndInitializeToHole, from); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 125 | } |
| 126 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 127 | MUST_USE_RESULT virtual MaybeObject* AddElementsToFixedArray( |
| 128 | Object* receiver, |
| 129 | JSObject* holder, |
| 130 | FixedArray* to, |
| 131 | FixedArrayBase* from = NULL) = 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 132 | |
| 133 | // Returns a shared ElementsAccessor for the specified ElementsKind. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 134 | static ElementsAccessor* ForKind(ElementsKind elements_kind) { |
| 135 | ASSERT(elements_kind < kElementsKindCount); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 136 | return elements_accessors_[elements_kind]; |
| 137 | } |
| 138 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 139 | static ElementsAccessor* ForArray(FixedArrayBase* array); |
| 140 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 141 | static void InitializeOncePerProcess(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 142 | static void TearDown(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 143 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 144 | protected: |
| 145 | friend class NonStrictArgumentsElementsAccessor; |
| 146 | |
| 147 | virtual uint32_t GetCapacity(FixedArrayBase* backing_store) = 0; |
| 148 | |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 149 | // Element handlers distinguish between indexes and keys when they manipulate |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 150 | // elements. Indexes refer to elements in terms of their location in the |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 151 | // underlying storage's backing store representation, and are between 0 and |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 152 | // GetCapacity. Keys refer to elements in terms of the value that would be |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 153 | // specified in JavaScript to access the element. In most implementations, |
| 154 | // keys are equivalent to indexes, and GetKeyForIndex returns the same value |
| 155 | // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps |
| 156 | // the index to a key using the KeyAt method on the NumberDictionary. |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 157 | virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store, |
| 158 | uint32_t index) = 0; |
| 159 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 160 | private: |
| 161 | static ElementsAccessor** elements_accessors_; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 162 | const char* name_; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 163 | |
| 164 | DISALLOW_COPY_AND_ASSIGN(ElementsAccessor); |
| 165 | }; |
| 166 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 167 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 168 | void CopyObjectToObjectElements(FixedArray* from_obj, |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 169 | ElementsKind from_kind, |
| 170 | uint32_t from_start, |
| 171 | FixedArray* to_obj, |
| 172 | ElementsKind to_kind, |
| 173 | uint32_t to_start, |
danno@chromium.org | c740616 | 2012-03-26 09:46:02 +0000 | [diff] [blame] | 174 | int copy_size); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 175 | |
| 176 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 177 | } } // namespace v8::internal |
| 178 | |
| 179 | #endif // V8_ELEMENTS_H_ |