5 #ifndef V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_ 6 #define V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_ 8 #include "src/objects/js-array-buffer.h" 10 #include "src/objects-inl.h" 11 #include "src/wasm/wasm-engine.h" 14 #include "src/objects/object-macros.h" 19 CAST_ACCESSOR(JSArrayBuffer)
20 CAST_ACCESSOR(JSArrayBufferView)
21 CAST_ACCESSOR(JSTypedArray)
23 size_t JSArrayBuffer::byte_length()
const {
24 return READ_UINTPTR_FIELD(
this, kByteLengthOffset);
27 void JSArrayBuffer::set_byte_length(
size_t value) {
28 WRITE_UINTPTR_FIELD(
this, kByteLengthOffset, value);
31 void* JSArrayBuffer::backing_store()
const {
32 intptr_t ptr = READ_INTPTR_FIELD(
this, kBackingStoreOffset);
33 return reinterpret_cast<void*
>(ptr);
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);
41 size_t JSArrayBuffer::allocation_length()
const {
42 if (backing_store() ==
nullptr) {
46 if (is_wasm_memory()) {
48 GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
50 DCHECK_NOT_NULL(data);
51 return data->allocation_length;
56 void* JSArrayBuffer::allocation_base()
const {
57 if (backing_store() ==
nullptr) {
61 if (is_wasm_memory()) {
63 GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
65 DCHECK_NOT_NULL(data);
66 return data->allocation_base;
68 return backing_store();
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(
76 return is_wasm_memory;
79 void JSArrayBuffer::set_is_wasm_memory(
bool is_wasm_memory) {
80 set_bit_field(IsWasmMemoryBit::update(bit_field(), is_wasm_memory));
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));
91 void JSArrayBuffer::set_bit_field(
uint32_t bits) {
92 WRITE_UINT32_FIELD(
this, kBitFieldOffset, bits);
95 uint32_t JSArrayBuffer::bit_field()
const {
96 return READ_UINT32_FIELD(
this, kBitFieldOffset);
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)
111 size_t JSArrayBufferView::byte_offset()
const {
112 return READ_UINTPTR_FIELD(
this, kByteOffsetOffset);
115 void JSArrayBufferView::set_byte_offset(
size_t value) {
116 WRITE_UINTPTR_FIELD(
this, kByteOffsetOffset, value);
119 size_t JSArrayBufferView::byte_length()
const {
120 return READ_UINTPTR_FIELD(
this, kByteLengthOffset);
123 void JSArrayBufferView::set_byte_length(
size_t value) {
124 WRITE_UINTPTR_FIELD(
this, kByteLengthOffset, value);
127 ACCESSORS(JSArrayBufferView, buffer, Object, kBufferOffset)
129 bool JSArrayBufferView::WasNeutered()
const {
130 return JSArrayBuffer::cast(buffer())->was_neutered();
133 Object* JSTypedArray::length()
const {
134 return Object::cast(READ_FIELD(
this, kLengthOffset));
137 size_t JSTypedArray::length_value()
const {
138 double val = length()->Number();
139 DCHECK_LE(val, kMaxSafeInteger);
140 DCHECK_GE(val, -kMaxSafeInteger);
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);
146 void JSTypedArray::set_length(Object* value, WriteBarrierMode mode) {
147 WRITE_FIELD(
this, kLengthOffset, value);
148 CONDITIONAL_WRITE_BARRIER(
this, kLengthOffset, value, mode);
151 bool JSTypedArray::is_on_heap()
const {
152 DisallowHeapAllocation no_gc;
155 FixedTypedArrayBase fta = FixedTypedArrayBase::cast(elements());
156 return fta->base_pointer()->ptr() == fta.ptr();
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);
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);
182 ACCESSORS(JSTypedArray, raw_length, Object, kLengthOffset)
188 #include "src/objects/object-macros-undef.h" 190 #endif // V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_