| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_ELEMENTS_H_ | 5 #ifndef V8_ELEMENTS_H_ |
| 6 #define V8_ELEMENTS_H_ | 6 #define V8_ELEMENTS_H_ |
| 7 | 7 |
| 8 #include "src/elements-kind.h" | 8 #include "src/elements-kind.h" |
| 9 #include "src/heap/heap.h" | 9 #include "src/heap/heap.h" |
| 10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
| 11 #include "src/objects.h" | 11 #include "src/objects.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 // Abstract base class for handles that can operate on objects with differing | 16 // Abstract base class for handles that can operate on objects with differing |
| 17 // ElementsKinds. | 17 // ElementsKinds. |
| 18 class ElementsAccessor { | 18 class ElementsAccessor { |
| 19 public: | 19 public: |
| 20 explicit ElementsAccessor(const char* name) : name_(name) { } | 20 explicit ElementsAccessor(const char* name) : name_(name) { } |
| 21 virtual ~ElementsAccessor() { } | 21 virtual ~ElementsAccessor() { } |
| 22 | 22 |
| 23 const char* name() const { return name_; } | 23 const char* name() const { return name_; } |
| 24 | 24 |
| 25 // Checks the elements of an object for consistency, asserting when a problem | 25 // Checks the elements of an object for consistency, asserting when a problem |
| 26 // is found. | 26 // is found. |
| 27 virtual void Validate(Handle<JSObject> obj) = 0; | 27 virtual void Validate(Handle<JSObject> obj) = 0; |
| 28 | 28 |
| 29 // Returns true if a holder contains an element with the specified key | 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 | 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 | 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 | 32 // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the |
| 33 // holder->elements() is used as the backing store. | 33 // holder->elements() is used as the backing store. |
| 34 virtual bool HasElement( | 34 virtual bool HasElement(Handle<JSObject> holder, uint32_t index, |
| 35 Handle<JSObject> holder, | 35 Handle<FixedArrayBase> backing_store) = 0; |
| 36 uint32_t key, | |
| 37 Handle<FixedArrayBase> backing_store) = 0; | |
| 38 | 36 |
| 39 inline bool HasElement( | 37 inline bool HasElement(Handle<JSObject> holder, uint32_t index) { |
| 40 Handle<JSObject> holder, | 38 return HasElement(holder, index, handle(holder->elements())); |
| 41 uint32_t key) { | |
| 42 return HasElement(holder, key, handle(holder->elements())); | |
| 43 } | 39 } |
| 44 | 40 |
| 45 // Returns the element with the specified key or undefined if there is no such | 41 // Returns the element with the specified index or undefined if there is no |
| 46 // element. This method doesn't iterate up the prototype chain. The caller | 42 // such element. This method doesn't iterate up the prototype chain. The |
| 47 // can optionally pass in the backing store to use for the check, which must | 43 // caller can optionally pass in the backing store to use for the check, which |
| 48 // be compatible with the ElementsKind of the ElementsAccessor. If | 44 // must be compatible with the ElementsKind of the ElementsAccessor. If |
| 49 // backing_store is NULL, the holder->elements() is used as the backing store. | 45 // backing_store is NULL, the holder->elements() is used as the backing store. |
| 50 virtual Handle<Object> Get(Handle<JSObject> holder, uint32_t key, | 46 virtual Handle<Object> Get(Handle<JSObject> holder, uint32_t index, |
| 51 Handle<FixedArrayBase> backing_store) = 0; | 47 Handle<FixedArrayBase> backing_store) = 0; |
| 52 | 48 |
| 53 inline Handle<Object> Get(Handle<JSObject> holder, uint32_t key) { | 49 inline Handle<Object> Get(Handle<JSObject> holder, uint32_t index) { |
| 54 return Get(holder, key, handle(holder->elements())); | 50 return Get(holder, index, handle(holder->elements())); |
| 55 } | 51 } |
| 56 | 52 |
| 57 // Modifies the length data property as specified for JSArrays and resizes the | 53 // Modifies the length data property as specified for JSArrays and resizes the |
| 58 // underlying backing store accordingly. The method honors the semantics of | 54 // underlying backing store accordingly. The method honors the semantics of |
| 59 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that | 55 // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that |
| 60 // have non-deletable elements can only be shrunk to the size of highest | 56 // have non-deletable elements can only be shrunk to the size of highest |
| 61 // element that is non-deletable. | 57 // element that is non-deletable. |
| 62 virtual void SetLength(Handle<JSArray> holder, uint32_t new_length) = 0; | 58 virtual void SetLength(Handle<JSArray> holder, uint32_t new_length) = 0; |
| 63 | 59 |
| 64 // Deletes an element in an object. | 60 // Deletes an element in an object. |
| 65 virtual void Delete(Handle<JSObject> holder, uint32_t index) = 0; | 61 virtual void Delete(Handle<JSObject> holder, uint32_t entry) = 0; |
| 66 | 62 |
| 67 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all | 63 // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all |
| 68 // of elements from source after source_start to the destination array. | 64 // of elements from source after source_start to the destination array. |
| 69 static const int kCopyToEnd = -1; | 65 static const int kCopyToEnd = -1; |
| 70 // If kCopyToEndAndInitializeToHole is specified as the copy_size to | 66 // If kCopyToEndAndInitializeToHole is specified as the copy_size to |
| 71 // CopyElements, it copies all of elements from source after source_start to | 67 // CopyElements, it copies all of elements from source after source_start to |
| 72 // destination array, padding any remaining uninitialized elements in the | 68 // destination array, padding any remaining uninitialized elements in the |
| 73 // destination array with the hole. | 69 // destination array with the hole. |
| 74 static const int kCopyToEndAndInitializeToHole = -2; | 70 static const int kCopyToEndAndInitializeToHole = -2; |
| 75 | 71 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 static ElementsAccessor* ForKind(ElementsKind elements_kind) { | 112 static ElementsAccessor* ForKind(ElementsKind elements_kind) { |
| 117 DCHECK(static_cast<int>(elements_kind) < kElementsKindCount); | 113 DCHECK(static_cast<int>(elements_kind) < kElementsKindCount); |
| 118 return elements_accessors_[elements_kind]; | 114 return elements_accessors_[elements_kind]; |
| 119 } | 115 } |
| 120 | 116 |
| 121 static ElementsAccessor* ForArray(Handle<FixedArrayBase> array); | 117 static ElementsAccessor* ForArray(Handle<FixedArrayBase> array); |
| 122 | 118 |
| 123 static void InitializeOncePerProcess(); | 119 static void InitializeOncePerProcess(); |
| 124 static void TearDown(); | 120 static void TearDown(); |
| 125 | 121 |
| 126 virtual void Set(FixedArrayBase* backing_store, uint32_t key, | 122 virtual void Set(FixedArrayBase* backing_store, uint32_t index, |
| 127 Object* value) = 0; | 123 Object* value) = 0; |
| 128 virtual void Reconfigure(Handle<JSObject> object, | 124 virtual void Reconfigure(Handle<JSObject> object, |
| 129 Handle<FixedArrayBase> backing_store, uint32_t index, | 125 Handle<FixedArrayBase> backing_store, uint32_t entry, |
| 130 Handle<Object> value, | 126 Handle<Object> value, |
| 131 PropertyAttributes attributes) = 0; | 127 PropertyAttributes attributes) = 0; |
| 132 virtual void Add(Handle<JSObject> object, uint32_t index, | 128 virtual void Add(Handle<JSObject> object, uint32_t entry, |
| 133 Handle<Object> value, PropertyAttributes attributes, | 129 Handle<Object> value, PropertyAttributes attributes, |
| 134 uint32_t new_capacity) = 0; | 130 uint32_t new_capacity) = 0; |
| 135 | 131 |
| 136 protected: | 132 protected: |
| 137 friend class LookupIterator; | 133 friend class LookupIterator; |
| 138 | 134 |
| 139 static ElementsAccessor* ForArray(FixedArrayBase* array); | 135 static ElementsAccessor* ForArray(FixedArrayBase* array); |
| 140 | 136 |
| 141 virtual uint32_t GetCapacity(JSObject* holder, | 137 virtual uint32_t GetCapacity(JSObject* holder, |
| 142 FixedArrayBase* backing_store) = 0; | 138 FixedArrayBase* backing_store) = 0; |
| 143 | 139 |
| 144 // Element handlers distinguish between indexes and keys when they manipulate | 140 // Element handlers distinguish between entries and indices when they |
| 145 // elements. Indexes refer to elements in terms of their location in the | 141 // manipulate elements. Entries refer to elements in terms of their location |
| 146 // underlying storage's backing store representation, and are between 0 and | 142 // in the underlying storage's backing store representation, and are between 0 |
| 147 // GetCapacity. Keys refer to elements in terms of the value that would be | 143 // and GetCapacity. Indices refer to elements in terms of the value that would |
| 148 // specified in JavaScript to access the element. In most implementations, | 144 // be specified in JavaScript to access the element. In most implementations, |
| 149 // keys are equivalent to indexes, and GetKeyForIndex returns the same value | 145 // indices are equivalent to entries. In the NumberDictionary |
| 150 // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps | 146 // ElementsAccessor, entries are mapped to an index using the KeyAt method on |
| 151 // the index to a key using the KeyAt method on the NumberDictionary. | 147 // the NumberDictionary. |
| 152 virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store, | 148 virtual uint32_t GetEntryForIndex(JSObject* holder, |
| 153 uint32_t index) = 0; | 149 FixedArrayBase* backing_store, |
| 154 virtual uint32_t GetIndexForKey(JSObject* holder, | 150 uint32_t index) = 0; |
| 155 FixedArrayBase* backing_store, | |
| 156 uint32_t key) = 0; | |
| 157 virtual PropertyDetails GetDetails(FixedArrayBase* backing_store, | 151 virtual PropertyDetails GetDetails(FixedArrayBase* backing_store, |
| 158 uint32_t index) = 0; | 152 uint32_t entry) = 0; |
| 159 virtual bool HasIndex(FixedArrayBase* backing_store, uint32_t key) = 0; | |
| 160 | 153 |
| 161 private: | 154 private: |
| 162 static ElementsAccessor** elements_accessors_; | 155 static ElementsAccessor** elements_accessors_; |
| 163 const char* name_; | 156 const char* name_; |
| 164 | 157 |
| 165 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor); | 158 DISALLOW_COPY_AND_ASSIGN(ElementsAccessor); |
| 166 }; | 159 }; |
| 167 | 160 |
| 168 void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t key, | 161 void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t index, |
| 169 bool allow_appending = false); | 162 bool allow_appending = false); |
| 170 | 163 |
| 171 MUST_USE_RESULT MaybeHandle<Object> ArrayConstructInitializeElements( | 164 MUST_USE_RESULT MaybeHandle<Object> ArrayConstructInitializeElements( |
| 172 Handle<JSArray> array, | 165 Handle<JSArray> array, |
| 173 Arguments* args); | 166 Arguments* args); |
| 174 | 167 |
| 175 } } // namespace v8::internal | 168 } } // namespace v8::internal |
| 176 | 169 |
| 177 #endif // V8_ELEMENTS_H_ | 170 #endif // V8_ELEMENTS_H_ |
| OLD | NEW |