elements.h 10.9 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_OBJECTS_ELEMENTS_H_
#define V8_OBJECTS_ELEMENTS_H_
7

8
#include "src/builtins/builtins-utils.h"
9
#include "src/objects/elements-kind.h"
10
#include "src/objects/internal-index.h"
11 12
#include "src/objects/keys.h"
#include "src/objects/objects.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18
class JSTypedArray;

19 20 21 22
// Abstract base class for handles that can operate on objects with differing
// ElementsKinds.
class ElementsAccessor {
 public:
23
  ElementsAccessor() = default;
24
  virtual ~ElementsAccessor() = default;
25 26
  ElementsAccessor(const ElementsAccessor&) = delete;
  ElementsAccessor& operator=(const ElementsAccessor&) = delete;
27

28 29
  // Returns a shared ElementsAccessor for the specified ElementsKind.
  static ElementsAccessor* ForKind(ElementsKind elements_kind) {
30
    DCHECK_LT(static_cast<int>(elements_kind), kElementsKindCount);
31 32 33
    return elements_accessors_[elements_kind];
  }

34 35
  // Checks the elements of an object for consistency, asserting when a problem
  // is found.
36
  virtual void Validate(JSObject obj) = 0;
37

38
  // Returns true if a holder contains an element with the specified index
39 40 41 42 43 44 45
  // without iterating up the prototype chain. The first version takes the
  // backing store to use for the check, which must be compatible with the
  // ElementsKind of the ElementsAccessor; the second version uses
  // holder->elements() as the backing store. If a |filter| is specified,
  // the PropertyAttributes of the element at the given index are compared
  // to the given |filter|. If they match/overlap, the given index is ignored.
  // Note that only Dictionary elements have custom
46 47
  // PropertyAttributes associated, hence the |filter| argument is ignored for
  // all but DICTIONARY_ELEMENTS and SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
48
  virtual bool HasElement(JSObject holder, uint32_t index,
49
                          FixedArrayBase backing_store,
50
                          PropertyFilter filter = ALL_PROPERTIES) = 0;
51

52
  inline bool HasElement(JSObject holder, uint32_t index,
53
                         PropertyFilter filter = ALL_PROPERTIES);
54

55 56
  // Note: this is currently not implemented for string wrapper and
  // typed array elements.
57
  virtual bool HasEntry(JSObject holder, InternalIndex entry) = 0;
58

59
  virtual Handle<Object> Get(Handle<JSObject> holder, InternalIndex entry) = 0;
60

61
  virtual bool HasAccessors(JSObject holder) = 0;
62
  virtual size_t NumberOfElements(JSObject holder) = 0;
63

64 65 66 67 68
  // Modifies the length data property as specified for JSArrays and resizes the
  // underlying backing store accordingly. The method honors the semantics of
  // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
  // have non-deletable elements can only be shrunk to the size of highest
  // element that is non-deletable.
69 70
  V8_WARN_UNUSED_RESULT virtual Maybe<bool> SetLength(Handle<JSArray> holder,
                                                      uint32_t new_length) = 0;
71

72 73 74
  // Copy all indices that have elements from |object| into the given
  // KeyAccumulator. For Dictionary-based element-kinds we filter out elements
  // whose PropertyAttribute match |filter|.
75 76 77
  V8_WARN_UNUSED_RESULT virtual ExceptionStatus CollectElementIndices(
      Handle<JSObject> object, Handle<FixedArrayBase> backing_store,
      KeyAccumulator* keys) = 0;
78

79 80
  V8_WARN_UNUSED_RESULT inline ExceptionStatus CollectElementIndices(
      Handle<JSObject> object, KeyAccumulator* keys);
81

82 83 84 85 86
  virtual Maybe<bool> CollectValuesOrEntries(
      Isolate* isolate, Handle<JSObject> object,
      Handle<FixedArray> values_or_entries, bool get_entries, int* nof_items,
      PropertyFilter filter = ALL_PROPERTIES) = 0;

87
  virtual MaybeHandle<FixedArray> PrependElementIndices(
88 89 90 91
      Handle<JSObject> object, Handle<FixedArrayBase> backing_store,
      Handle<FixedArray> keys, GetKeysConversion convert,
      PropertyFilter filter = ALL_PROPERTIES) = 0;

92
  inline MaybeHandle<FixedArray> PrependElementIndices(
93
      Handle<JSObject> object, Handle<FixedArray> keys,
94
      GetKeysConversion convert, PropertyFilter filter = ALL_PROPERTIES);
95

96 97 98
  V8_WARN_UNUSED_RESULT virtual ExceptionStatus AddElementsToKeyAccumulator(
      Handle<JSObject> receiver, KeyAccumulator* accumulator,
      AddKeyConversion convert) = 0;
99

100 101 102 103
  V8_WARN_UNUSED_RESULT virtual Maybe<bool> TransitionElementsKind(
      Handle<JSObject> object, Handle<Map> map) = 0;
  V8_WARN_UNUSED_RESULT virtual Maybe<bool> GrowCapacityAndConvert(
      Handle<JSObject> object, uint32_t capacity) = 0;
104 105
  // Unlike GrowCapacityAndConvert do not attempt to convert the backing store
  // and simply return false in this case.
106 107
  V8_WARN_UNUSED_RESULT virtual Maybe<bool> GrowCapacity(
      Handle<JSObject> object, uint32_t index) = 0;
108

109
  static void InitializeOncePerProcess();
110
  static void TearDown();
111

112 113
  virtual void Set(Handle<JSObject> holder, InternalIndex entry,
                   Object value) = 0;
114

115 116 117 118 119
  V8_WARN_UNUSED_RESULT virtual Maybe<bool> Add(Handle<JSObject> object,
                                                uint32_t index,
                                                Handle<Object> value,
                                                PropertyAttributes attributes,
                                                uint32_t new_capacity) = 0;
120

121
  static Handle<JSArray> Concat(Isolate* isolate, BuiltinArguments* args,
122
                                uint32_t concat_size, uint32_t result_length);
123

124 125 126
  V8_WARN_UNUSED_RESULT virtual Maybe<uint32_t> Push(Handle<JSArray> receiver,
                                                     BuiltinArguments* args,
                                                     uint32_t push_size) = 0;
127

128 129 130
  V8_WARN_UNUSED_RESULT virtual Maybe<uint32_t> Unshift(
      Handle<JSArray> receiver, BuiltinArguments* args,
      uint32_t unshift_size) = 0;
131

132 133
  V8_WARN_UNUSED_RESULT virtual MaybeHandle<Object> Pop(
      Handle<JSArray> receiver) = 0;
cbruni's avatar
cbruni committed
134

135 136
  V8_WARN_UNUSED_RESULT virtual MaybeHandle<Object> Shift(
      Handle<JSArray> receiver) = 0;
137

138
  virtual Handle<NumberDictionary> Normalize(Handle<JSObject> object) = 0;
139

140
  virtual size_t GetCapacity(JSObject holder, FixedArrayBase backing_store) = 0;
141

142 143 144
  V8_WARN_UNUSED_RESULT virtual MaybeHandle<Object> Fill(
      Handle<JSObject> receiver, Handle<Object> obj_value, size_t start,
      size_t end) = 0;
145

146 147 148
  // Check an Object's own elements for an element (using SameValueZero
  // semantics)
  virtual Maybe<bool> IncludesValue(Isolate* isolate, Handle<JSObject> receiver,
149 150
                                    Handle<Object> value, size_t start,
                                    size_t length) = 0;
151

152 153 154 155
  // Check an Object's own elements for the index of an element (using SameValue
  // semantics)
  virtual Maybe<int64_t> IndexOfValue(Isolate* isolate,
                                      Handle<JSObject> receiver,
156 157
                                      Handle<Object> value, size_t start,
                                      size_t length) = 0;
158

159
  virtual Maybe<int64_t> LastIndexOfValue(Handle<JSObject> receiver,
160
                                          Handle<Object> value,
161
                                          size_t start) = 0;
162

163
  virtual void Reverse(JSObject receiver) = 0;
164

165
  virtual void CopyElements(Isolate* isolate, Handle<FixedArrayBase> source,
166 167 168
                            ElementsKind source_kind,
                            Handle<FixedArrayBase> destination, int size) = 0;

169 170
  virtual Object CopyElements(Handle<Object> source,
                              Handle<JSObject> destination, size_t length,
171
                              size_t offset) = 0;
172

173 174 175
  virtual Handle<FixedArray> CreateListFromArrayLike(Isolate* isolate,
                                                     Handle<JSObject> object,
                                                     uint32_t length) = 0;
176

177 178
  virtual void CopyTypedArrayElementsSlice(JSTypedArray source,
                                           JSTypedArray destination,
179 180
                                           size_t start, size_t end) = 0;

181
 protected:
182
  friend class LookupIterator;
183

184 185 186 187 188 189 190 191
  // Element handlers distinguish between entries and indices when they
  // manipulate elements. Entries refer to elements in terms of their location
  // in the underlying storage's backing store representation, and are between 0
  // and GetCapacity. Indices refer to elements in terms of the value that would
  // be specified in JavaScript to access the element. In most implementations,
  // indices are equivalent to entries. In the NumberDictionary
  // ElementsAccessor, entries are mapped to an index using the KeyAt method on
  // the NumberDictionary.
192 193
  virtual InternalIndex GetEntryForIndex(Isolate* isolate, JSObject holder,
                                         FixedArrayBase backing_store,
194
                                         size_t index) = 0;
195

196
  virtual PropertyDetails GetDetails(JSObject holder, InternalIndex entry) = 0;
197
  virtual void Reconfigure(Handle<JSObject> object,
198 199
                           Handle<FixedArrayBase> backing_store,
                           InternalIndex entry, Handle<Object> value,
200 201 202
                           PropertyAttributes attributes) = 0;

  // Deletes an element in an object.
203
  virtual void Delete(Handle<JSObject> holder, InternalIndex entry) = 0;
204

205 206 207 208
  // NOTE: this method violates the handlified function signature convention:
  // raw pointer parameter |source_holder| in the function that allocates.
  // This is done intentionally to avoid ArrayConcat() builtin performance
  // degradation.
209
  virtual void CopyElements(JSObject source_holder, uint32_t source_start,
210 211 212
                            ElementsKind source_kind,
                            Handle<FixedArrayBase> destination,
                            uint32_t destination_start, int copy_size) = 0;
213

214
 private:
215
  V8_EXPORT_PRIVATE static ElementsAccessor** elements_accessors_;
216 217
};

218
V8_WARN_UNUSED_RESULT MaybeHandle<Object> ArrayConstructInitializeElements(
219
    Handle<JSArray> array, JavaScriptArguments* args);
220

221
// Called directly from CSA.
222 223 224
// {raw_context}: Context pointer.
// {raw_source}: JSArray pointer.
// {raw_destination}: JSTypedArray pointer.
225
void CopyFastNumberJSArrayElementsToTypedArray(Address raw_context,
226 227
                                               Address raw_source,
                                               Address raw_destination,
228 229
                                               uintptr_t length,
                                               uintptr_t offset);
230 231 232
// {raw_source}, {raw_destination}: JSTypedArray pointers.
void CopyTypedArrayElementsToTypedArray(Address raw_source,
                                        Address raw_destination,
233
                                        uintptr_t length, uintptr_t offset);
234 235 236
// {raw_source}, {raw_destination}: JSTypedArray pointers.
void CopyTypedArrayElementsSlice(Address raw_source, Address raw_destination,
                                 uintptr_t start, uintptr_t end);
237

238 239
}  // namespace internal
}  // namespace v8
240

241
#endif  // V8_OBJECTS_ELEMENTS_H_