V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-array-buffer-inl.h
1 // Copyright 2018 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_JS_ARRAY_BUFFER_INL_H_
6 #define V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_
7 
8 #include "src/objects/js-array-buffer.h"
9 
10 #include "src/objects-inl.h" // Needed for write barriers
11 #include "src/wasm/wasm-engine.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 
19 CAST_ACCESSOR(JSArrayBuffer)
20 CAST_ACCESSOR(JSArrayBufferView)
21 CAST_ACCESSOR(JSTypedArray)
22 
23 size_t JSArrayBuffer::byte_length() const {
24  return READ_UINTPTR_FIELD(this, kByteLengthOffset);
25 }
26 
27 void JSArrayBuffer::set_byte_length(size_t value) {
28  WRITE_UINTPTR_FIELD(this, kByteLengthOffset, value);
29 }
30 
31 void* JSArrayBuffer::backing_store() const {
32  intptr_t ptr = READ_INTPTR_FIELD(this, kBackingStoreOffset);
33  return reinterpret_cast<void*>(ptr);
34 }
35 
36 void JSArrayBuffer::set_backing_store(void* value, WriteBarrierMode mode) {
37  intptr_t ptr = reinterpret_cast<intptr_t>(value);
38  WRITE_INTPTR_FIELD(this, kBackingStoreOffset, ptr);
39 }
40 
41 size_t JSArrayBuffer::allocation_length() const {
42  if (backing_store() == nullptr) {
43  return 0;
44  }
45  // If this buffer is managed by the WasmMemoryTracker
46  if (is_wasm_memory()) {
47  const auto* data =
48  GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
49  backing_store());
50  DCHECK_NOT_NULL(data);
51  return data->allocation_length;
52  }
53  return byte_length();
54 }
55 
56 void* JSArrayBuffer::allocation_base() const {
57  if (backing_store() == nullptr) {
58  return nullptr;
59  }
60  // If this buffer is managed by the WasmMemoryTracker
61  if (is_wasm_memory()) {
62  const auto* data =
63  GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
64  backing_store());
65  DCHECK_NOT_NULL(data);
66  return data->allocation_base;
67  }
68  return backing_store();
69 }
70 
71 bool JSArrayBuffer::is_wasm_memory() const {
72  bool const is_wasm_memory = IsWasmMemoryBit::decode(bit_field());
73  DCHECK_EQ(is_wasm_memory,
74  GetIsolate()->wasm_engine()->memory_tracker()->IsWasmMemory(
75  backing_store()));
76  return is_wasm_memory;
77 }
78 
79 void JSArrayBuffer::set_is_wasm_memory(bool is_wasm_memory) {
80  set_bit_field(IsWasmMemoryBit::update(bit_field(), is_wasm_memory));
81 }
82 
83 void JSArrayBuffer::clear_padding() {
84  if (FIELD_SIZE(kOptionalPaddingOffset)) {
85  DCHECK_EQ(4, FIELD_SIZE(kOptionalPaddingOffset));
86  memset(reinterpret_cast<void*>(address() + kOptionalPaddingOffset), 0,
87  FIELD_SIZE(kOptionalPaddingOffset));
88  }
89 }
90 
91 void JSArrayBuffer::set_bit_field(uint32_t bits) {
92  WRITE_UINT32_FIELD(this, kBitFieldOffset, bits);
93 }
94 
95 uint32_t JSArrayBuffer::bit_field() const {
96  return READ_UINT32_FIELD(this, kBitFieldOffset);
97 }
98 
99 // |bit_field| fields.
100 BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_external,
101  JSArrayBuffer::IsExternalBit)
102 BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_neuterable,
103  JSArrayBuffer::IsNeuterableBit)
104 BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, was_neutered,
105  JSArrayBuffer::WasNeuteredBit)
106 BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_shared,
107  JSArrayBuffer::IsSharedBit)
108 BIT_FIELD_ACCESSORS(JSArrayBuffer, bit_field, is_growable,
109  JSArrayBuffer::IsGrowableBit)
110 
111 size_t JSArrayBufferView::byte_offset() const {
112  return READ_UINTPTR_FIELD(this, kByteOffsetOffset);
113 }
114 
115 void JSArrayBufferView::set_byte_offset(size_t value) {
116  WRITE_UINTPTR_FIELD(this, kByteOffsetOffset, value);
117 }
118 
119 size_t JSArrayBufferView::byte_length() const {
120  return READ_UINTPTR_FIELD(this, kByteLengthOffset);
121 }
122 
123 void JSArrayBufferView::set_byte_length(size_t value) {
124  WRITE_UINTPTR_FIELD(this, kByteLengthOffset, value);
125 }
126 
127 ACCESSORS(JSArrayBufferView, buffer, Object, kBufferOffset)
128 
129 bool JSArrayBufferView::WasNeutered() const {
130  return JSArrayBuffer::cast(buffer())->was_neutered();
131 }
132 
133 Object* JSTypedArray::length() const {
134  return Object::cast(READ_FIELD(this, kLengthOffset));
135 }
136 
137 size_t JSTypedArray::length_value() const {
138  double val = length()->Number();
139  DCHECK_LE(val, kMaxSafeInteger); // 2^53-1
140  DCHECK_GE(val, -kMaxSafeInteger); // -2^53+1
141  DCHECK_LE(val, std::numeric_limits<size_t>::max());
142  DCHECK_GE(val, std::numeric_limits<size_t>::min());
143  return static_cast<size_t>(val);
144 }
145 
146 void JSTypedArray::set_length(Object* value, WriteBarrierMode mode) {
147  WRITE_FIELD(this, kLengthOffset, value);
148  CONDITIONAL_WRITE_BARRIER(this, kLengthOffset, value, mode);
149 }
150 
151 bool JSTypedArray::is_on_heap() const {
152  DisallowHeapAllocation no_gc;
153  // Checking that buffer()->backing_store() is not nullptr is not sufficient;
154  // it will be nullptr when byte_length is 0 as well.
155  FixedTypedArrayBase fta = FixedTypedArrayBase::cast(elements());
156  return fta->base_pointer()->ptr() == fta.ptr();
157 }
158 
159 // static
160 MaybeHandle<JSTypedArray> JSTypedArray::Validate(Isolate* isolate,
161  Handle<Object> receiver,
162  const char* method_name) {
163  if (V8_UNLIKELY(!receiver->IsJSTypedArray())) {
164  const MessageTemplate message = MessageTemplate::kNotTypedArray;
165  THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray);
166  }
167 
168  Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
169  if (V8_UNLIKELY(array->WasNeutered())) {
170  const MessageTemplate message = MessageTemplate::kDetachedOperation;
171  Handle<String> operation =
172  isolate->factory()->NewStringFromAsciiChecked(method_name);
173  THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray);
174  }
175 
176  // spec describes to return `buffer`, but it may disrupt current
177  // implementations, and it's much useful to return array for now.
178  return array;
179 }
180 
181 #ifdef VERIFY_HEAP
182 ACCESSORS(JSTypedArray, raw_length, Object, kLengthOffset)
183 #endif
184 
185 } // namespace internal
186 } // namespace v8
187 
188 #include "src/objects/object-macros-undef.h"
189 
190 #endif // V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_
Definition: libplatform.h:13