js-array.h 6.79 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_JS_ARRAY_H_
#define V8_OBJECTS_JS_ARRAY_H_

8
#include "src/objects/allocation-site.h"
9
#include "src/objects/fixed-array.h"
10
#include "src/objects/js-objects.h"
11 12 13 14 15 16 17

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

18 19
#include "torque-generated/src/objects/js-array-tq.inc"

20 21 22 23 24
// The JSArray describes JavaScript Arrays
//  Such an array can be in one of two modes:
//    - fast, backing storage is a FixedArray and length <= elements.length();
//       Please note: push and pop can be used to grow and shrink the array.
//    - slow, backing storage is a HashTable with numbers as keys.
25
class JSArray : public TorqueGeneratedJSArray<JSArray, JSObject> {
26 27 28
 public:
  // [length]: The length property.
  DECL_ACCESSORS(length, Object)
29
  DECL_RELAXED_GETTER(length, Object)
30

31 32 33 34 35
  // Acquire/release semantics on this field are explicitly forbidden to avoid
  // confusion, since the default setter uses relaxed semantics. If
  // acquire/release semantics ever become necessary, the default setter should
  // be reverted to non-atomic behavior, and setters with explicit tags
  // introduced and used when required.
36
  Object length(PtrComprCageBase cage_base, AcquireLoadTag tag) const = delete;
37 38 39
  void set_length(Object value, ReleaseStoreTag tag,
                  WriteBarrierMode mode = UPDATE_WRITE_BARRIER) = delete;

40 41
  // Overload the length setter to skip write barrier when the length
  // is set to a smi. This matches the set function on FixedArray.
42
  inline void set_length(Smi length);
43

44
  static bool MayHaveReadOnlyLength(Map js_array_map);
45 46 47 48 49 50
  static bool HasReadOnlyLength(Handle<JSArray> array);
  static bool WouldChangeReadOnlyLength(Handle<JSArray> array, uint32_t index);

  // Initialize the array with the given capacity. The function may
  // fail due to out-of-memory situations, but only if the requested
  // capacity is non-zero.
51 52
  V8_EXPORT_PRIVATE static void Initialize(Handle<JSArray> array, int capacity,
                                           int length = 0);
53 54 55 56 57 58 59

  // If the JSArray has fast elements, and new_length would result in
  // normalization, returns true.
  bool SetLengthWouldNormalize(uint32_t new_length);
  static inline bool SetLengthWouldNormalize(Heap* heap, uint32_t new_length);

  // Initializes the array to a certain length.
60 61
  V8_EXPORT_PRIVATE static Maybe<bool> SetLength(Handle<JSArray> array,
                                                 uint32_t length);
62 63 64 65 66 67

  // Set the content of the array to the content of storage.
  static inline void SetContent(Handle<JSArray> array,
                                Handle<FixedArrayBase> storage);

  // ES6 9.4.2.1
68
  V8_WARN_UNUSED_RESULT static Maybe<bool> DefineOwnProperty(
69
      Isolate* isolate, Handle<JSArray> o, Handle<Object> name,
70
      PropertyDescriptor* desc, Maybe<ShouldThrow> should_throw);
71 72 73 74

  static bool AnythingToArrayLength(Isolate* isolate,
                                    Handle<Object> length_object,
                                    uint32_t* output);
75 76
  V8_WARN_UNUSED_RESULT static Maybe<bool> ArraySetLength(
      Isolate* isolate, Handle<JSArray> a, PropertyDescriptor* desc,
77
      Maybe<ShouldThrow> should_throw);
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  // Support for Array.prototype.join().
  // Writes a fixed array of strings and separators to a single destination
  // string. This helpers assumes the fixed array encodes separators in two
  // ways:
  //   1) Explicitly with a smi, whos value represents the number of repeated
  //      separators.
  //   2) Implicitly between two consecutive strings a single separator.
  //
  // Here are some input/output examples given the separator string is ',':
  //
  //   [1, 'hello', 2, 'world', 1] => ',hello,,world,'
  //   ['hello', 'world']          => 'hello,world'
  //
  // To avoid any allocations, this helper assumes the destination string is the
  // exact length necessary to write the strings and separators from the fixed
  // array.
95 96 97 98 99 100
  // Since this is called via ExternalReferences, it uses raw Address values:
  // - {raw_fixed_array} is a tagged FixedArray pointer.
  // - {raw_separator} and {raw_dest} are tagged String pointers.
  // - Returns a tagged String pointer.
  static Address ArrayJoinConcatToSequentialString(Isolate* isolate,
                                                   Address raw_fixed_array,
101
                                                   intptr_t length,
102 103
                                                   Address raw_separator,
                                                   Address raw_dest);
104

105 106 107 108 109 110 111
  // Checks whether the Array has the current realm's Array.prototype as its
  // prototype. This function is best-effort and only gives a conservative
  // approximation, erring on the side of false, in particular with respect
  // to Proxies and objects with a hidden prototype.
  inline bool HasArrayPrototype(Isolate* isolate);

  // Dispatched behavior.
112
  DECL_PRINTER(JSArray)
113 114 115 116 117
  DECL_VERIFIER(JSArray)

  // Number of element slots to pre-allocate for an empty array.
  static const int kPreallocatedArrayElements = 4;

118 119
  static const int kLengthDescriptorIndex = 0;

120 121 122
  // Max. number of elements being copied in Array builtins.
  static const int kMaxCopyElements = 100;

123 124 125
  // Valid array indices range from +0 <= i < 2^32 - 1 (kMaxUInt32).
  static constexpr uint32_t kMaxArrayLength = JSObject::kMaxElementCount;
  static constexpr uint32_t kMaxArrayIndex = JSObject::kMaxElementIndex;
126 127
  static_assert(kMaxArrayLength == kMaxUInt32);
  static_assert(kMaxArrayIndex == kMaxUInt32 - 1);
128

129
  // This constant is somewhat arbitrary. Any large enough value would work.
130
  static constexpr uint32_t kMaxFastArrayLength = 32 * 1024 * 1024;
131
  static_assert(kMaxFastArrayLength <= kMaxArrayLength);
132

133 134 135
  // Min. stack size for detecting an Array.prototype.join() call cycle.
  static const uint32_t kMinJoinStackSize = 2;

136
  static const int kInitialMaxFastElementArray =
137
      (kMaxRegularHeapObjectSize - FixedArray::kHeaderSize - kHeaderSize -
138 139 140
       AllocationMemento::kSize) >>
      kDoubleSizeLog2;

141
  TQ_OBJECT_CONSTRUCTORS(JSArray)
142 143
};

144 145
// The JSArrayIterator describes JavaScript Array Iterators Objects, as
// defined in ES section #sec-array-iterator-objects.
146 147
class JSArrayIterator
    : public TorqueGeneratedJSArrayIterator<JSArrayIterator, JSObject> {
148
 public:
149
  DECL_PRINTER(JSArrayIterator)
150 151
  DECL_VERIFIER(JSArrayIterator)

152 153 154
  // [kind]: the [[ArrayIterationKind]] inobject property.
  inline IterationKind kind() const;
  inline void set_kind(IterationKind kind);
155

156 157 158
 private:
  DECL_INT_ACCESSORS(raw_kind)

159
  TQ_OBJECT_CONSTRUCTORS(JSArrayIterator)
160 161 162 163 164 165 166 167
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_ARRAY_H_