V8 Project
elements.h
Go to the documentation of this file.
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
3 // found in the LICENSE file.
4 
5 #ifndef V8_ELEMENTS_H_
6 #define V8_ELEMENTS_H_
7 
8 #include "src/elements-kind.h"
9 #include "src/heap/heap.h"
10 #include "src/isolate.h"
11 #include "src/objects.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Abstract base class for handles that can operate on objects with differing
17 // ElementsKinds.
19  public:
20  explicit ElementsAccessor(const char* name) : name_(name) { }
21  virtual ~ElementsAccessor() { }
22 
23  virtual ElementsKind kind() const = 0;
24  const char* name() const { return name_; }
25 
26  // Checks the elements of an object for consistency, asserting when a problem
27  // is found.
28  virtual void Validate(Handle<JSObject> obj) = 0;
29 
30  // Returns true if a holder contains an element with the specified key
31  // without iterating up the prototype chain. The caller can optionally pass
32  // in the backing store to use for the check, which must be compatible with
33  // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the
34  // holder->elements() is used as the backing store.
35  virtual bool HasElement(
36  Handle<Object> receiver,
37  Handle<JSObject> holder,
38  uint32_t key,
39  Handle<FixedArrayBase> backing_store) = 0;
40 
41  inline bool HasElement(
42  Handle<Object> receiver,
43  Handle<JSObject> holder,
44  uint32_t key) {
45  return HasElement(receiver, holder, key, handle(holder->elements()));
46  }
47 
48  // Returns the element with the specified key or undefined if there is no such
49  // element. This method doesn't iterate up the prototype chain. The caller
50  // can optionally pass in the backing store to use for the check, which must
51  // be compatible with the ElementsKind of the ElementsAccessor. If
52  // backing_store is NULL, the holder->elements() is used as the backing store.
54  Handle<Object> receiver,
55  Handle<JSObject> holder,
56  uint32_t key,
57  Handle<FixedArrayBase> backing_store) = 0;
58 
60  Handle<Object> receiver,
61  Handle<JSObject> holder,
62  uint32_t key) {
63  return Get(receiver, holder, key, handle(holder->elements()));
64  }
65 
66  // Returns an element's attributes, or ABSENT if there is no such
67  // element. This method doesn't iterate up the prototype chain. The caller
68  // can optionally pass in the backing store to use for the check, which must
69  // be compatible with the ElementsKind of the ElementsAccessor. If
70  // backing_store is NULL, the holder->elements() is used as the backing store.
72  Handle<Object> receiver,
73  Handle<JSObject> holder,
74  uint32_t key,
75  Handle<FixedArrayBase> backing_store) = 0;
76 
78  Handle<Object> receiver,
79  Handle<JSObject> holder,
80  uint32_t key) {
81  return GetAttributes(receiver, holder, key, handle(holder->elements()));
82  }
83 
84  // Returns an element's accessors, or NULL if the element does not exist or
85  // is plain. This method doesn't iterate up the prototype chain. The caller
86  // can optionally pass in the backing store to use for the check, which must
87  // be compatible with the ElementsKind of the ElementsAccessor. If
88  // backing_store is NULL, the holder->elements() is used as the backing store.
90  Handle<Object> receiver,
91  Handle<JSObject> holder,
92  uint32_t key,
93  Handle<FixedArrayBase> backing_store) = 0;
94 
96  Handle<Object> receiver,
97  Handle<JSObject> holder,
98  uint32_t key) {
99  return GetAccessorPair(receiver, holder, key, handle(holder->elements()));
100  }
101 
102  // Modifies the length data property as specified for JSArrays and resizes the
103  // underlying backing store accordingly. The method honors the semantics of
104  // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
105  // have non-deletable elements can only be shrunk to the size of highest
106  // element that is non-deletable.
108  Handle<JSArray> holder,
109  Handle<Object> new_length) = 0;
110 
111  // Modifies both the length and capacity of a JSArray, resizing the underlying
112  // backing store as necessary. This method does NOT honor the semantics of
113  // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable
114  // elements. This method should only be called for array expansion OR by
115  // runtime JavaScript code that use InternalArrays and don't care about
116  // EcmaScript 5.1 semantics.
117  virtual void SetCapacityAndLength(
118  Handle<JSArray> array,
119  int capacity,
120  int length) = 0;
121 
122  // Deletes an element in an object, returning a new elements backing store.
124  Handle<JSObject> holder,
125  uint32_t key,
127 
128  // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
129  // of elements from source after source_start to the destination array.
130  static const int kCopyToEnd = -1;
131  // If kCopyToEndAndInitializeToHole is specified as the copy_size to
132  // CopyElements, it copies all of elements from source after source_start to
133  // destination array, padding any remaining uninitialized elements in the
134  // destination array with the hole.
135  static const int kCopyToEndAndInitializeToHole = -2;
136 
137  // Copy elements from one backing store to another. Typically, callers specify
138  // the source JSObject or JSArray in source_holder. If the holder's backing
139  // store is available, it can be passed in source and source_holder is
140  // ignored.
141  virtual void CopyElements(
142  Handle<FixedArrayBase> source,
143  uint32_t source_start,
144  ElementsKind source_kind,
145  Handle<FixedArrayBase> destination,
146  uint32_t destination_start,
147  int copy_size) = 0;
148 
149  // NOTE: this method violates the handlified function signature convention:
150  // raw pointer parameter |source_holder| in the function that allocates.
151  // This is done intentionally to avoid ArrayConcat() builtin performance
152  // degradation.
153  virtual void CopyElements(
154  JSObject* source_holder,
155  uint32_t source_start,
156  ElementsKind source_kind,
157  Handle<FixedArrayBase> destination,
158  uint32_t destination_start,
159  int copy_size) = 0;
160 
161  inline void CopyElements(
162  Handle<JSObject> from_holder,
164  ElementsKind from_kind) {
165  CopyElements(
166  *from_holder, 0, from_kind, to, 0, kCopyToEndAndInitializeToHole);
167  }
168 
170  Handle<Object> receiver,
171  Handle<JSObject> holder,
173  Handle<FixedArrayBase> from) = 0;
174 
176  Handle<Object> receiver,
177  Handle<JSObject> holder,
180  receiver, holder, to, handle(holder->elements()));
181  }
182 
183  // Returns a shared ElementsAccessor for the specified ElementsKind.
184  static ElementsAccessor* ForKind(ElementsKind elements_kind) {
185  DCHECK(elements_kind < kElementsKindCount);
186  return elements_accessors_[elements_kind];
187  }
188 
190 
191  static void InitializeOncePerProcess();
192  static void TearDown();
193 
194  protected:
196 
197  virtual uint32_t GetCapacity(Handle<FixedArrayBase> backing_store) = 0;
198 
199  // Element handlers distinguish between indexes and keys when they manipulate
200  // elements. Indexes refer to elements in terms of their location in the
201  // underlying storage's backing store representation, and are between 0 and
202  // GetCapacity. Keys refer to elements in terms of the value that would be
203  // specified in JavaScript to access the element. In most implementations,
204  // keys are equivalent to indexes, and GetKeyForIndex returns the same value
205  // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps
206  // the index to a key using the KeyAt method on the NumberDictionary.
208  uint32_t index) = 0;
209 
210  private:
212  const char* name_;
213 
215 };
216 
217 void CheckArrayAbuse(Handle<JSObject> obj, const char* op, uint32_t key,
218  bool allow_appending = false);
219 
221  Handle<JSArray> array,
222  Arguments* args);
223 
224 } } // namespace v8::internal
225 
226 #endif // V8_ELEMENTS_H_
virtual void CopyElements(Handle< FixedArrayBase > source, uint32_t source_start, ElementsKind source_kind, Handle< FixedArrayBase > destination, uint32_t destination_start, int copy_size)=0
MUST_USE_RESULT MaybeHandle< Object > Get(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key)
Definition: elements.h:59
static const int kCopyToEndAndInitializeToHole
Definition: elements.h:135
virtual void SetCapacityAndLength(Handle< JSArray > array, int capacity, int length)=0
virtual MUST_USE_RESULT PropertyAttributes GetAttributes(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key, Handle< FixedArrayBase > backing_store)=0
virtual bool HasElement(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key, Handle< FixedArrayBase > backing_store)=0
void CopyElements(Handle< JSObject > from_holder, Handle< FixedArrayBase > to, ElementsKind from_kind)
Definition: elements.h:161
DISALLOW_COPY_AND_ASSIGN(ElementsAccessor)
virtual MUST_USE_RESULT MaybeHandle< FixedArray > AddElementsToFixedArray(Handle< Object > receiver, Handle< JSObject > holder, Handle< FixedArray > to, Handle< FixedArrayBase > from)=0
virtual void Validate(Handle< JSObject > obj)=0
virtual MUST_USE_RESULT MaybeHandle< Object > Get(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key, Handle< FixedArrayBase > backing_store)=0
virtual ElementsKind kind() const =0
static ElementsAccessor ** elements_accessors_
Definition: elements.h:211
MUST_USE_RESULT MaybeHandle< FixedArray > AddElementsToFixedArray(Handle< Object > receiver, Handle< JSObject > holder, Handle< FixedArray > to)
Definition: elements.h:175
static void InitializeOncePerProcess()
Definition: elements.cc:1736
static const int kCopyToEnd
Definition: elements.h:130
const char * name() const
Definition: elements.h:24
virtual uint32_t GetCapacity(Handle< FixedArrayBase > backing_store)=0
virtual MUST_USE_RESULT MaybeHandle< Object > Delete(Handle< JSObject > holder, uint32_t key, JSReceiver::DeleteMode mode)=0
MUST_USE_RESULT MaybeHandle< AccessorPair > GetAccessorPair(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key)
Definition: elements.h:95
MUST_USE_RESULT PropertyAttributes GetAttributes(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key)
Definition: elements.h:77
virtual void CopyElements(JSObject *source_holder, uint32_t source_start, ElementsKind source_kind, Handle< FixedArrayBase > destination, uint32_t destination_start, int copy_size)=0
virtual uint32_t GetKeyForIndex(Handle< FixedArrayBase > backing_store, uint32_t index)=0
bool HasElement(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key)
Definition: elements.h:41
static ElementsAccessor * ForArray(Handle< FixedArrayBase > array)
Definition: elements.cc:1731
virtual MUST_USE_RESULT MaybeHandle< AccessorPair > GetAccessorPair(Handle< Object > receiver, Handle< JSObject > holder, uint32_t key, Handle< FixedArrayBase > backing_store)=0
static ElementsAccessor * ForKind(ElementsKind elements_kind)
Definition: elements.h:184
virtual MUST_USE_RESULT MaybeHandle< Object > SetLength(Handle< JSArray > holder, Handle< Object > new_length)=0
ElementsAccessor(const char *name)
Definition: elements.h:20
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long mode(MIPS only)") DEFINE_BOOL(enable_always_align_csp
#define DCHECK(condition)
Definition: logging.h:205
#define MUST_USE_RESULT
Definition: macros.h:266
void CheckArrayAbuse(Handle< JSObject > obj, const char *op, uint32_t key, bool allow_appending)
Definition: elements.cc:496
const int kElementsKindCount
Definition: elements-kind.h:66
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:146
MaybeHandle< Object > ArrayConstructInitializeElements(Handle< JSArray > array, Arguments *args)
Definition: elements.cc:1828
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
PropertyAttributes