V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
fixed-array.h
1 // Copyright 2017 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_OBJECTS_FIXED_ARRAY_H_
6 #define V8_OBJECTS_FIXED_ARRAY_H_
7 
8 #include "src/maybe-handles.h"
9 #include "src/objects/instance-type.h"
10 #include "src/objects/slots.h"
11 #include "src/objects/smi.h"
12 
13 // Has to be the last include (doesn't have include guards):
14 #include "src/objects/object-macros.h"
15 
16 namespace v8 {
17 namespace internal {
18 typedef FlexibleWeakBodyDescriptor<HeapObject::kHeaderSize>
19  WeakArrayBodyDescriptor;
20 
21 #define FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(V) \
22  V(BYTECODE_ARRAY_CONSTANT_POOL_SUB_TYPE) \
23  V(BYTECODE_ARRAY_HANDLER_TABLE_SUB_TYPE) \
24  V(CODE_STUBS_TABLE_SUB_TYPE) \
25  V(COMPILATION_CACHE_TABLE_SUB_TYPE) \
26  V(CONTEXT_SUB_TYPE) \
27  V(COPY_ON_WRITE_SUB_TYPE) \
28  V(DEOPTIMIZATION_DATA_SUB_TYPE) \
29  V(DESCRIPTOR_ARRAY_SUB_TYPE) \
30  V(EMBEDDED_OBJECT_SUB_TYPE) \
31  V(ENUM_CACHE_SUB_TYPE) \
32  V(ENUM_INDICES_CACHE_SUB_TYPE) \
33  V(DEPENDENT_CODE_SUB_TYPE) \
34  V(DICTIONARY_ELEMENTS_SUB_TYPE) \
35  V(DICTIONARY_PROPERTIES_SUB_TYPE) \
36  V(EMPTY_PROPERTIES_DICTIONARY_SUB_TYPE) \
37  V(PACKED_ELEMENTS_SUB_TYPE) \
38  V(FAST_PROPERTIES_SUB_TYPE) \
39  V(FAST_TEMPLATE_INSTANTIATIONS_CACHE_SUB_TYPE) \
40  V(HANDLER_TABLE_SUB_TYPE) \
41  V(JS_COLLECTION_SUB_TYPE) \
42  V(JS_WEAK_COLLECTION_SUB_TYPE) \
43  V(NOSCRIPT_SHARED_FUNCTION_INFOS_SUB_TYPE) \
44  V(NUMBER_STRING_CACHE_SUB_TYPE) \
45  V(OBJECT_TO_CODE_SUB_TYPE) \
46  V(OPTIMIZED_CODE_LITERALS_SUB_TYPE) \
47  V(OPTIMIZED_CODE_MAP_SUB_TYPE) \
48  V(PROTOTYPE_USERS_SUB_TYPE) \
49  V(REGEXP_MULTIPLE_CACHE_SUB_TYPE) \
50  V(RETAINED_MAPS_SUB_TYPE) \
51  V(SCOPE_INFO_SUB_TYPE) \
52  V(SCRIPT_LIST_SUB_TYPE) \
53  V(SERIALIZED_OBJECTS_SUB_TYPE) \
54  V(SHARED_FUNCTION_INFOS_SUB_TYPE) \
55  V(SINGLE_CHARACTER_STRING_CACHE_SUB_TYPE) \
56  V(SLOW_TEMPLATE_INSTANTIATIONS_CACHE_SUB_TYPE) \
57  V(STRING_SPLIT_CACHE_SUB_TYPE) \
58  V(STRING_TABLE_SUB_TYPE) \
59  V(TEMPLATE_INFO_SUB_TYPE) \
60  V(FEEDBACK_METADATA_SUB_TYPE) \
61  V(WEAK_NEW_SPACE_OBJECT_TO_CODE_SUB_TYPE)
62 
63 enum FixedArraySubInstanceType {
64 #define DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE(name) name,
65  FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE)
66 #undef DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE
67  LAST_FIXED_ARRAY_SUB_TYPE = WEAK_NEW_SPACE_OBJECT_TO_CODE_SUB_TYPE
68 };
69 
70 // Common superclass for FixedArrays that allow implementations to share
71 // common accessors and some code paths.
72 class FixedArrayBase : public HeapObjectPtr {
73  public:
74  // [length]: length of the array.
75  inline int length() const;
76  inline void set_length(int value);
77 
78  // Get and set the length using acquire loads and release stores.
79  inline int synchronized_length() const;
80  inline void synchronized_set_length(int value);
81 
82  inline Object* unchecked_synchronized_length() const;
83 
84  DECL_CAST2(FixedArrayBase)
85 
86  static int GetMaxLengthForNewSpaceAllocation(ElementsKind kind);
87 
88  bool IsCowArray() const;
89 
90 // Maximal allowed size, in bytes, of a single FixedArrayBase.
91 // Prevents overflowing size computations, as well as extreme memory
92 // consumption.
93 #ifdef V8_HOST_ARCH_32_BIT
94  static const int kMaxSize = 512 * MB;
95 #else
96  static const int kMaxSize = 1024 * MB;
97 #endif // V8_HOST_ARCH_32_BIT
98 
99  // Layout description.
100 #define FIXED_ARRAY_BASE_FIELDS(V) \
101  V(kLengthOffset, kTaggedSize) \
102  /* Header size. */ \
103  V(kHeaderSize, 0)
104 
105  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
106  FIXED_ARRAY_BASE_FIELDS)
107 #undef FIXED_ARRAY_BASE_FIELDS
108 
109  protected:
110  // Special-purpose constructor for subclasses that have fast paths where
111  // their ptr() is a Smi.
112  inline FixedArrayBase(Address ptr, AllowInlineSmiStorage allow_smi);
113 
114  OBJECT_CONSTRUCTORS(FixedArrayBase, HeapObjectPtr)
115 };
116 
117 // FixedArray describes fixed-sized arrays with element type Object*.
118 class FixedArray : public FixedArrayBase {
119  public:
120  // Setter and getter for elements.
121  inline Object* get(int index) const;
122  static inline Handle<Object> get(FixedArray array, int index,
123  Isolate* isolate);
124  template <class T>
125  MaybeHandle<T> GetValue(Isolate* isolate, int index) const;
126 
127  template <class T>
128  Handle<T> GetValueChecked(Isolate* isolate, int index) const;
129 
130  // Return a grown copy if the index is bigger than the array's length.
131  static Handle<FixedArray> SetAndGrow(Isolate* isolate,
132  Handle<FixedArray> array, int index,
133  Handle<Object> value,
134  PretenureFlag pretenure = NOT_TENURED);
135 
136  // Setter that uses write barrier.
137  inline void set(int index, Object* value);
138  inline bool is_the_hole(Isolate* isolate, int index);
139 
140  // Setter that doesn't need write barrier.
141  inline void set(int index, Smi value);
142  // Setter with explicit barrier mode.
143  inline void set(int index, Object* value, WriteBarrierMode mode);
144 
145  // Setters for frequently used oddballs located in old space.
146  inline void set_undefined(int index);
147  inline void set_undefined(Isolate* isolate, int index);
148  inline void set_null(int index);
149  inline void set_null(Isolate* isolate, int index);
150  inline void set_the_hole(int index);
151  inline void set_the_hole(Isolate* isolate, int index);
152 
153  inline ObjectSlot GetFirstElementAddress();
154  inline bool ContainsOnlySmisOrHoles();
155  // Returns true iff the elements are Numbers and sorted ascending.
156  bool ContainsSortedNumbers();
157 
158  // Gives access to raw memory which stores the array's data.
159  inline ObjectSlot data_start();
160 
161  inline void MoveElements(Heap* heap, int dst_index, int src_index, int len,
162  WriteBarrierMode mode);
163 
164  inline void FillWithHoles(int from, int to);
165 
166  // Shrink the array and insert filler objects. {new_length} must be > 0.
167  void Shrink(Isolate* isolate, int new_length);
168  // If {new_length} is 0, return the canonical empty FixedArray. Otherwise
169  // like above.
170  static Handle<FixedArray> ShrinkOrEmpty(Isolate* isolate,
171  Handle<FixedArray> array,
172  int new_length);
173 
174  // Copy a sub array from the receiver to dest.
175  void CopyTo(int pos, FixedArray dest, int dest_pos, int len) const;
176 
177  // Garbage collection support.
178  static constexpr int SizeFor(int length) {
179  return kHeaderSize + length * kTaggedSize;
180  }
181 
182  // Code Generation support.
183  static constexpr int OffsetOfElementAt(int index) { return SizeFor(index); }
184 
185  // Garbage collection support.
186  inline ObjectSlot RawFieldOfElementAt(int index);
187 
188  DECL_CAST2(FixedArray)
189  // Maximally allowed length of a FixedArray.
190  static const int kMaxLength = (kMaxSize - kHeaderSize) / kTaggedSize;
191  static_assert(Internals::IsValidSmi(kMaxLength),
192  "FixedArray maxLength not a Smi");
193 
194  // Maximally allowed length for regular (non large object space) object.
195  STATIC_ASSERT(kMaxRegularHeapObjectSize < kMaxSize);
196  static const int kMaxRegularLength =
197  (kMaxRegularHeapObjectSize - kHeaderSize) / kTaggedSize;
198 
199  // Dispatched behavior.
200  DECL_PRINTER(FixedArray)
201  DECL_VERIFIER(FixedArray)
202 
204 
205  protected:
206  // Set operation on FixedArray without using write barriers. Can
207  // only be used for storing old space objects or smis.
208  static inline void NoWriteBarrierSet(FixedArray array, int index,
209  Object* value);
210 
211  private:
212  STATIC_ASSERT(kHeaderSize == Internals::kFixedArrayHeaderSize);
213 
214  inline void set_undefined(ReadOnlyRoots ro_roots, int index);
215  inline void set_null(ReadOnlyRoots ro_roots, int index);
216  inline void set_the_hole(ReadOnlyRoots ro_roots, int index);
217 
218  OBJECT_CONSTRUCTORS(FixedArray, FixedArrayBase);
219 };
220 
221 // FixedArray alias added only because of IsFixedArrayExact() predicate, which
222 // checks for the exact instance type FIXED_ARRAY_TYPE instead of a range
223 // check: [FIRST_FIXED_ARRAY_TYPE, LAST_FIXED_ARRAY_TYPE].
224 class FixedArrayExact final : public FixedArray {};
225 
226 // FixedDoubleArray describes fixed-sized arrays with element type double.
228  public:
229  // Setter and getter for elements.
230  inline double get_scalar(int index);
231  inline uint64_t get_representation(int index);
232  static inline Handle<Object> get(FixedDoubleArray array, int index,
233  Isolate* isolate);
234  inline void set(int index, double value);
235  inline void set_the_hole(Isolate* isolate, int index);
236  inline void set_the_hole(int index);
237 
238  // Checking for the hole.
239  inline bool is_the_hole(Isolate* isolate, int index);
240  inline bool is_the_hole(int index);
241 
242  // Garbage collection support.
243  inline static int SizeFor(int length) {
244  return kHeaderSize + length * kDoubleSize;
245  }
246 
247  inline void MoveElements(Heap* heap, int dst_index, int src_index, int len,
248  WriteBarrierMode mode);
249 
250  inline void FillWithHoles(int from, int to);
251 
252  // Code Generation support.
253  static int OffsetOfElementAt(int index) { return SizeFor(index); }
254 
255  DECL_CAST2(FixedDoubleArray)
256 
257  // Maximally allowed length of a FixedArray.
258  static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
259  static_assert(Internals::IsValidSmi(kMaxLength),
260  "FixedDoubleArray maxLength not a Smi");
261 
262  // Dispatched behavior.
263  DECL_PRINTER(FixedDoubleArray)
264  DECL_VERIFIER(FixedDoubleArray)
265 
266  class BodyDescriptor;
267 
268  OBJECT_CONSTRUCTORS(FixedDoubleArray, FixedArrayBase);
269 };
270 
271 // WeakFixedArray describes fixed-sized arrays with element type
272 // MaybeObject.
273 class WeakFixedArray : public HeapObject {
274  public:
275  DECL_CAST(WeakFixedArray)
276 
277  inline MaybeObject Get(int index) const;
278 
279  // Setter that uses write barrier.
280  inline void Set(int index, MaybeObject value);
281 
282  // Setter with explicit barrier mode.
283  inline void Set(int index, MaybeObject value, WriteBarrierMode mode);
284 
285  static constexpr int SizeFor(int length) {
286  return kHeaderSize + length * kTaggedSize;
287  }
288 
289  DECL_INT_ACCESSORS(length)
290 
291  // Get and set the length using acquire loads and release stores.
292  inline int synchronized_length() const;
293  inline void synchronized_set_length(int value);
294 
295  // Gives access to raw memory which stores the array's data.
296  inline MaybeObjectSlot data_start();
297 
298  inline MaybeObjectSlot RawFieldOfElementAt(int index);
299 
300  DECL_PRINTER(WeakFixedArray)
301  DECL_VERIFIER(WeakFixedArray)
302 
304 
305  // Layout description.
306 #define WEAK_FIXED_ARRAY_FIELDS(V) \
307  V(kLengthOffset, kTaggedSize) \
308  /* Header size. */ \
309  V(kHeaderSize, 0)
310 
311  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
312  WEAK_FIXED_ARRAY_FIELDS)
313 #undef WEAK_FIXED_ARRAY_FIELDS
314 
315  static const int kMaxLength =
316  (FixedArray::kMaxSize - kHeaderSize) / kTaggedSize;
317  static_assert(Internals::IsValidSmi(kMaxLength),
318  "WeakFixedArray maxLength not a Smi");
319 
320  protected:
321  static int OffsetOfElementAt(int index) {
322  return kHeaderSize + index * kTaggedSize;
323  }
324 
325  private:
326  friend class Heap;
327 
328  static const int kFirstIndex = 1;
329 
330  DISALLOW_IMPLICIT_CONSTRUCTORS(WeakFixedArray);
331 };
332 
333 // WeakArrayList is like a WeakFixedArray with static convenience methods for
334 // adding more elements. length() returns the number of elements in the list and
335 // capacity() returns the allocated size. The number of elements is stored at
336 // kLengthOffset and is updated with every insertion. The array grows
337 // dynamically with O(1) amortized insertion.
339  public:
340  DECL_CAST(WeakArrayList)
341  DECL_VERIFIER(WeakArrayList)
342  DECL_PRINTER(WeakArrayList)
343 
344  static Handle<WeakArrayList> AddToEnd(Isolate* isolate,
345  Handle<WeakArrayList> array,
346  const MaybeObjectHandle& value);
347 
348  inline MaybeObject Get(int index) const;
349 
350  // Set the element at index to obj. The underlying array must be large enough.
351  // If you need to grow the WeakArrayList, use the static AddToEnd() method
352  // instead.
353  inline void Set(int index, MaybeObject value,
354  WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
355 
356  static constexpr int SizeForCapacity(int capacity) {
357  return kHeaderSize + capacity * kTaggedSize;
358  }
359 
360  // Gives access to raw memory which stores the array's data.
361  inline MaybeObjectSlot data_start();
362 
363  bool IsFull();
364 
365  DECL_INT_ACCESSORS(capacity)
366  DECL_INT_ACCESSORS(length)
367 
368  // Get and set the capacity using acquire loads and release stores.
369  inline int synchronized_capacity() const;
370  inline void synchronized_set_capacity(int value);
371 
372 
373  // Layout description.
374 #define WEAK_ARRAY_LIST_FIELDS(V) \
375  V(kCapacityOffset, kTaggedSize) \
376  V(kLengthOffset, kTaggedSize) \
377  /* Header size. */ \
378  V(kHeaderSize, 0)
379 
380  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize, WEAK_ARRAY_LIST_FIELDS)
381 #undef WEAK_ARRAY_LIST_FIELDS
382 
384 
385  static const int kMaxCapacity =
386  (FixedArray::kMaxSize - kHeaderSize) / kTaggedSize;
387 
389  Isolate* isolate, Handle<WeakArrayList> array, int length,
390  PretenureFlag pretenure = NOT_TENURED);
391 
392  // Returns the number of non-cleaned weak references in the array.
393  int CountLiveWeakReferences() const;
394 
395  // Returns whether an entry was found and removed. Will move the elements
396  // around in the array - this method can only be used in cases where the user
397  // doesn't care about the indices! Users should make sure there are no
398  // duplicates.
399  bool RemoveOne(const MaybeObjectHandle& value);
400 
401  class Iterator {
402  public:
403  explicit Iterator(WeakArrayList* array) : index_(0), array_(array) {}
404 
405  inline HeapObject* Next();
406 
407  private:
408  int index_;
409  WeakArrayList* array_;
410 #ifdef DEBUG
411  DisallowHeapAllocation no_gc_;
412 #endif // DEBUG
413  DISALLOW_COPY_AND_ASSIGN(Iterator);
414  };
415 
416  private:
417  static int OffsetOfElementAt(int index) {
418  return kHeaderSize + index * kTaggedSize;
419  }
420 
421  DISALLOW_IMPLICIT_CONSTRUCTORS(WeakArrayList);
422 };
423 
424 // Generic array grows dynamically with O(1) amortized insertion.
425 //
426 // ArrayList is a FixedArray with static convenience methods for adding more
427 // elements. The Length() method returns the number of elements in the list, not
428 // the allocated size. The number of elements is stored at kLengthIndex and is
429 // updated with every insertion. The elements of the ArrayList are stored in the
430 // underlying FixedArray starting at kFirstIndex.
431 class ArrayList : public FixedArray {
432  public:
433  static Handle<ArrayList> Add(Isolate* isolate, Handle<ArrayList> array,
434  Handle<Object> obj);
435  static Handle<ArrayList> Add(Isolate* isolate, Handle<ArrayList> array,
436  Handle<Object> obj1, Handle<Object> obj2);
437  static Handle<ArrayList> New(Isolate* isolate, int size);
438 
439  // Returns the number of elements in the list, not the allocated size, which
440  // is length(). Lower and upper case length() return different results!
441  inline int Length() const;
442 
443  // Sets the Length() as used by Elements(). Does not change the underlying
444  // storage capacity, i.e., length().
445  inline void SetLength(int length);
446  inline Object* Get(int index) const;
447  inline ObjectSlot Slot(int index);
448 
449  // Set the element at index to obj. The underlying array must be large enough.
450  // If you need to grow the ArrayList, use the static Add() methods instead.
451  inline void Set(int index, Object* obj,
452  WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
453 
454  // Set the element at index to undefined. This does not change the Length().
455  inline void Clear(int index, Object* undefined);
456 
457  // Return a copy of the list of size Length() without the first entry. The
458  // number returned by Length() is stored in the first entry.
459  static Handle<FixedArray> Elements(Isolate* isolate, Handle<ArrayList> array);
460  DECL_CAST2(ArrayList)
461 
462  private:
463  static Handle<ArrayList> EnsureSpace(Isolate* isolate,
464  Handle<ArrayList> array, int length);
465  static const int kLengthIndex = 0;
466  static const int kFirstIndex = 1;
467  OBJECT_CONSTRUCTORS(ArrayList, FixedArray);
468 };
469 
470 enum SearchMode { ALL_ENTRIES, VALID_ENTRIES };
471 
472 template <SearchMode search_mode, typename T>
473 inline int Search(T* array, Name name, int valid_entries = 0,
474  int* out_insertion_index = nullptr);
475 
476 // ByteArray represents fixed sized byte arrays. Used for the relocation info
477 // that is attached to code objects.
478 class ByteArray : public FixedArrayBase {
479  public:
480  inline int Size();
481 
482  // Setter and getter.
483  inline byte get(int index) const;
484  inline void set(int index, byte value);
485 
486  // Copy in / copy out whole byte slices.
487  inline void copy_out(int index, byte* buffer, int length);
488  inline void copy_in(int index, const byte* buffer, int length);
489 
490  // Treat contents as an int array.
491  inline int get_int(int index) const;
492  inline void set_int(int index, int value);
493 
494  inline uint32_t get_uint32(int index) const;
495  inline void set_uint32(int index, uint32_t value);
496 
497  // Clear uninitialized padding space. This ensures that the snapshot content
498  // is deterministic.
499  inline void clear_padding();
500 
501  static int SizeFor(int length) {
502  return OBJECT_POINTER_ALIGN(kHeaderSize + length);
503  }
504  // We use byte arrays for free blocks in the heap. Given a desired size in
505  // bytes that is a multiple of the word size and big enough to hold a byte
506  // array, this function returns the number of elements a byte array should
507  // have.
508  static int LengthFor(int size_in_bytes) {
509  DCHECK(IsAligned(size_in_bytes, kTaggedSize));
510  DCHECK_GE(size_in_bytes, kHeaderSize);
511  return size_in_bytes - kHeaderSize;
512  }
513 
514  // Returns data start address.
515  inline byte* GetDataStartAddress();
516  // Returns address of the past-the-end element.
517  inline byte* GetDataEndAddress();
518 
519  inline int DataSize() const;
520 
521  // Returns a pointer to the ByteArray object for a given data start address.
522  static inline ByteArray FromDataStartAddress(Address address);
523 
524  DECL_CAST2(ByteArray)
525 
526  // Dispatched behavior.
527  inline int ByteArraySize();
528  DECL_PRINTER(ByteArray)
529  DECL_VERIFIER(ByteArray)
530 
531  // Layout description.
532  static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
533 
534  // Maximal length of a single ByteArray.
535  static const int kMaxLength = kMaxSize - kHeaderSize;
536  static_assert(Internals::IsValidSmi(kMaxLength),
537  "ByteArray maxLength not a Smi");
538 
539  class BodyDescriptor;
540 
541  protected:
542  // Special-purpose constructor for subclasses that have fast paths where
543  // their ptr() is a Smi.
544  inline ByteArray(Address ptr, AllowInlineSmiStorage allow_smi);
545 
546  OBJECT_CONSTRUCTORS(ByteArray, FixedArrayBase);
547 };
548 
549 // Wrapper class for ByteArray which can store arbitrary C++ classes, as long
550 // as they can be copied with memcpy.
551 template <class T>
552 class PodArray : public ByteArray {
553  public:
554  static Handle<PodArray<T>> New(Isolate* isolate, int length,
555  PretenureFlag pretenure = NOT_TENURED);
556  void copy_out(int index, T* result) {
557  ByteArray::copy_out(index * sizeof(T), reinterpret_cast<byte*>(result),
558  sizeof(T));
559  }
560  T get(int index) {
561  T result;
562  copy_out(index, &result);
563  return result;
564  }
565  void set(int index, const T& value) {
566  copy_in(index * sizeof(T), reinterpret_cast<const byte*>(&value),
567  sizeof(T));
568  }
569  inline int length() const;
570  DECL_CAST2(PodArray<T>)
571 
572  OBJECT_CONSTRUCTORS(PodArray<T>, ByteArray);
573 };
574 
576  public:
577  // [base_pointer]: Either points to the FixedTypedArrayBase itself or nullptr.
578  DECL_ACCESSORS(base_pointer, Object);
579 
580  // [external_pointer]: Contains the offset between base_pointer and the start
581  // of the data. If the base_pointer is a nullptr, the external_pointer
582  // therefore points to the actual backing store.
583  DECL_ACCESSORS(external_pointer, void)
584 
585  // Dispatched behavior.
586  DECL_CAST2(FixedTypedArrayBase)
587 
588 #define FIXED_TYPED_ARRAY_BASE_FIELDS(V) \
589  V(kBasePointerOffset, kTaggedSize) \
590  V(kExternalPointerOffset, kSystemPointerSize) \
591  /* Header size. */ \
592  V(kHeaderSize, 0)
593 
594  DEFINE_FIELD_OFFSET_CONSTANTS(FixedArrayBase::kHeaderSize,
595  FIXED_TYPED_ARRAY_BASE_FIELDS)
596 #undef FIXED_TYPED_ARRAY_BASE_FIELDS
597 
598  STATIC_ASSERT(IsAligned(kHeaderSize, kDoubleAlignment));
599 
600  static const int kDataOffset = kHeaderSize;
601 
602  static const int kMaxElementSize = 8;
603 
604 #ifdef V8_HOST_ARCH_32_BIT
605  static const size_t kMaxByteLength = std::numeric_limits<size_t>::max();
606 #else
607  static const size_t kMaxByteLength =
608  static_cast<size_t>(Smi::kMaxValue) * kMaxElementSize;
609 #endif // V8_HOST_ARCH_32_BIT
610 
611  static const size_t kMaxLength = Smi::kMaxValue;
612 
613  class BodyDescriptor;
614 
615  inline int size() const;
616 
617  static inline int TypedArraySize(InstanceType type, int length);
618  inline int TypedArraySize(InstanceType type) const;
619 
620  // Use with care: returns raw pointer into heap.
621  inline void* DataPtr();
622 
623  inline int DataSize() const;
624 
625  inline size_t ByteLength() const;
626 
627  private:
628  static inline int ElementSize(InstanceType type);
629 
630  inline int DataSize(InstanceType type) const;
631 
632  OBJECT_CONSTRUCTORS(FixedTypedArrayBase, FixedArrayBase);
633 };
634 
635 template <class Traits>
637  public:
638  typedef typename Traits::ElementType ElementType;
639  static const InstanceType kInstanceType = Traits::kInstanceType;
640 
641  DECL_CAST2(FixedTypedArray<Traits>)
642 
643  static inline ElementType get_scalar_from_data_ptr(void* data_ptr, int index);
644  inline ElementType get_scalar(int index);
645  static inline Handle<Object> get(Isolate* isolate, FixedTypedArray array,
646  int index);
647  inline void set(int index, ElementType value);
648 
649  static inline ElementType from(int value);
650  static inline ElementType from(uint32_t value);
651  static inline ElementType from(double value);
652  static inline ElementType from(int64_t value);
653  static inline ElementType from(uint64_t value);
654 
655  static inline ElementType FromHandle(Handle<Object> value,
656  bool* lossless = nullptr);
657 
658  // This accessor applies the correct conversion from Smi, HeapNumber
659  // and undefined.
660  inline void SetValue(uint32_t index, Object* value);
661 
662  DECL_PRINTER(FixedTypedArray)
663  DECL_VERIFIER(FixedTypedArray)
664 
665  private:
666  OBJECT_CONSTRUCTORS(FixedTypedArray, FixedTypedArrayBase);
667 };
668 
669 #define FIXED_TYPED_ARRAY_TRAITS(Type, type, TYPE, elementType) \
670  STATIC_ASSERT(sizeof(elementType) <= FixedTypedArrayBase::kMaxElementSize); \
671  class Type##ArrayTraits { \
672  public: /* NOLINT */ \
673  typedef elementType ElementType; \
674  static const InstanceType kInstanceType = FIXED_##TYPE##_ARRAY_TYPE; \
675  static const char* Designator() { return #type " array"; } \
676  static inline Handle<Object> ToHandle(Isolate* isolate, \
677  elementType scalar); \
678  static inline elementType defaultValue(); \
679  }; \
680  \
681  typedef FixedTypedArray<Type##ArrayTraits> Fixed##Type##Array;
682 
683 TYPED_ARRAYS(FIXED_TYPED_ARRAY_TRAITS)
684 
685 #undef FIXED_TYPED_ARRAY_TRAITS
686 
687 class TemplateList : public FixedArray {
688  public:
689  static Handle<TemplateList> New(Isolate* isolate, int size);
690  inline int length() const;
691  inline Object* get(int index) const;
692  inline void set(int index, Object* value);
693  static Handle<TemplateList> Add(Isolate* isolate, Handle<TemplateList> list,
694  Handle<Object> value);
695  DECL_CAST2(TemplateList)
696  private:
697  static const int kLengthIndex = 0;
698  static const int kFirstElementIndex = kLengthIndex + 1;
699 
700  OBJECT_CONSTRUCTORS(TemplateList, FixedArray);
701 };
702 
703 } // namespace internal
704 } // namespace v8
705 
706 #include "src/objects/object-macros-undef.h"
707 
708 #endif // V8_OBJECTS_FIXED_ARRAY_H_
Definition: v8.h:2497
Definition: libplatform.h:13
Definition: v8.h:3740