• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
CAST_ACCESSOR(JSArrayBufferView)20 CAST_ACCESSOR(JSArrayBufferView)
21 CAST_ACCESSOR(JSTypedArray)
22 
23 void* JSArrayBuffer::backing_store() const {
24   intptr_t ptr = READ_INTPTR_FIELD(this, kBackingStoreOffset);
25   return reinterpret_cast<void*>(ptr);
26 }
27 
set_backing_store(void * value,WriteBarrierMode mode)28 void JSArrayBuffer::set_backing_store(void* value, WriteBarrierMode mode) {
29   intptr_t ptr = reinterpret_cast<intptr_t>(value);
30   WRITE_INTPTR_FIELD(this, kBackingStoreOffset, ptr);
31 }
32 
ACCESSORS(JSArrayBuffer,byte_length,Object,kByteLengthOffset)33 ACCESSORS(JSArrayBuffer, byte_length, Object, kByteLengthOffset)
34 
35 size_t JSArrayBuffer::allocation_length() const {
36   if (backing_store() == nullptr) {
37     return 0;
38   }
39   // If this buffer is managed by the WasmMemoryTracker
40   if (is_wasm_memory()) {
41     const auto* data =
42         GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
43             backing_store());
44     DCHECK_NOT_NULL(data);
45     return data->allocation_length;
46   }
47   return byte_length()->Number();
48 }
49 
allocation_base()50 void* JSArrayBuffer::allocation_base() const {
51   if (backing_store() == nullptr) {
52     return nullptr;
53   }
54   // If this buffer is managed by the WasmMemoryTracker
55   if (is_wasm_memory()) {
56     const auto* data =
57         GetIsolate()->wasm_engine()->memory_tracker()->FindAllocationData(
58             backing_store());
59     DCHECK_NOT_NULL(data);
60     return data->allocation_base;
61   }
62   return backing_store();
63 }
64 
is_wasm_memory()65 bool JSArrayBuffer::is_wasm_memory() const {
66   bool const is_wasm_memory = IsWasmMemory::decode(bit_field());
67   DCHECK_EQ(is_wasm_memory,
68             GetIsolate()->wasm_engine()->memory_tracker()->IsWasmMemory(
69                 backing_store()));
70   return is_wasm_memory;
71 }
72 
set_bit_field(uint32_t bits)73 void JSArrayBuffer::set_bit_field(uint32_t bits) {
74   if (kInt32Size != kPointerSize) {
75 #if V8_TARGET_LITTLE_ENDIAN
76     WRITE_UINT32_FIELD(this, kBitFieldSlot + kInt32Size, 0);
77 #else
78     WRITE_UINT32_FIELD(this, kBitFieldSlot, 0);
79 #endif
80   }
81   WRITE_UINT32_FIELD(this, kBitFieldOffset, bits);
82 }
83 
bit_field()84 uint32_t JSArrayBuffer::bit_field() const {
85   return READ_UINT32_FIELD(this, kBitFieldOffset);
86 }
87 
is_external()88 bool JSArrayBuffer::is_external() { return IsExternal::decode(bit_field()); }
89 
set_is_external(bool value)90 void JSArrayBuffer::set_is_external(bool value) {
91   set_bit_field(IsExternal::update(bit_field(), value));
92 }
93 
is_neuterable()94 bool JSArrayBuffer::is_neuterable() {
95   return IsNeuterable::decode(bit_field());
96 }
97 
set_is_neuterable(bool value)98 void JSArrayBuffer::set_is_neuterable(bool value) {
99   set_bit_field(IsNeuterable::update(bit_field(), value));
100 }
101 
was_neutered()102 bool JSArrayBuffer::was_neutered() { return WasNeutered::decode(bit_field()); }
103 
set_was_neutered(bool value)104 void JSArrayBuffer::set_was_neutered(bool value) {
105   set_bit_field(WasNeutered::update(bit_field(), value));
106 }
107 
is_shared()108 bool JSArrayBuffer::is_shared() { return IsShared::decode(bit_field()); }
109 
set_is_shared(bool value)110 void JSArrayBuffer::set_is_shared(bool value) {
111   set_bit_field(IsShared::update(bit_field(), value));
112 }
113 
is_growable()114 bool JSArrayBuffer::is_growable() { return IsGrowable::decode(bit_field()); }
115 
set_is_growable(bool value)116 void JSArrayBuffer::set_is_growable(bool value) {
117   set_bit_field(IsGrowable::update(bit_field(), value));
118 }
119 
byte_offset()120 Object* JSArrayBufferView::byte_offset() const {
121   if (WasNeutered()) return Smi::kZero;
122   return Object::cast(READ_FIELD(this, kByteOffsetOffset));
123 }
124 
set_byte_offset(Object * value,WriteBarrierMode mode)125 void JSArrayBufferView::set_byte_offset(Object* value, WriteBarrierMode mode) {
126   WRITE_FIELD(this, kByteOffsetOffset, value);
127   CONDITIONAL_WRITE_BARRIER(this, kByteOffsetOffset, value, mode);
128 }
129 
byte_length()130 Object* JSArrayBufferView::byte_length() const {
131   if (WasNeutered()) return Smi::kZero;
132   return Object::cast(READ_FIELD(this, kByteLengthOffset));
133 }
134 
set_byte_length(Object * value,WriteBarrierMode mode)135 void JSArrayBufferView::set_byte_length(Object* value, WriteBarrierMode mode) {
136   WRITE_FIELD(this, kByteLengthOffset, value);
137   CONDITIONAL_WRITE_BARRIER(this, kByteLengthOffset, value, mode);
138 }
139 
ACCESSORS(JSArrayBufferView,buffer,Object,kBufferOffset)140 ACCESSORS(JSArrayBufferView, buffer, Object, kBufferOffset)
141 #ifdef VERIFY_HEAP
142 ACCESSORS(JSArrayBufferView, raw_byte_offset, Object, kByteOffsetOffset)
143 ACCESSORS(JSArrayBufferView, raw_byte_length, Object, kByteLengthOffset)
144 #endif
145 
146 bool JSArrayBufferView::WasNeutered() const {
147   return JSArrayBuffer::cast(buffer())->was_neutered();
148 }
149 
length()150 Object* JSTypedArray::length() const {
151   if (WasNeutered()) return Smi::kZero;
152   return Object::cast(READ_FIELD(this, kLengthOffset));
153 }
154 
length_value()155 size_t JSTypedArray::length_value() const {
156   if (WasNeutered()) return 0;
157   double val = Object::cast(READ_FIELD(this, kLengthOffset))->Number();
158   DCHECK_LE(val, kMaxSafeInteger);   // 2^53-1
159   DCHECK_GE(val, -kMaxSafeInteger);  // -2^53+1
160   DCHECK_LE(val, std::numeric_limits<size_t>::max());
161   DCHECK_GE(val, std::numeric_limits<size_t>::min());
162   return static_cast<size_t>(val);
163 }
164 
set_length(Object * value,WriteBarrierMode mode)165 void JSTypedArray::set_length(Object* value, WriteBarrierMode mode) {
166   WRITE_FIELD(this, kLengthOffset, value);
167   CONDITIONAL_WRITE_BARRIER(this, kLengthOffset, value, mode);
168 }
169 
is_on_heap()170 bool JSTypedArray::is_on_heap() const {
171   DisallowHeapAllocation no_gc;
172   // Checking that buffer()->backing_store() is not nullptr is not sufficient;
173   // it will be nullptr when byte_length is 0 as well.
174   FixedTypedArrayBase* fta(FixedTypedArrayBase::cast(elements()));
175   return fta->base_pointer() == fta;
176 }
177 
178 // static
Validate(Isolate * isolate,Handle<Object> receiver,const char * method_name)179 MaybeHandle<JSTypedArray> JSTypedArray::Validate(Isolate* isolate,
180                                                  Handle<Object> receiver,
181                                                  const char* method_name) {
182   if (V8_UNLIKELY(!receiver->IsJSTypedArray())) {
183     const MessageTemplate::Template message = MessageTemplate::kNotTypedArray;
184     THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray);
185   }
186 
187   Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
188   if (V8_UNLIKELY(array->WasNeutered())) {
189     const MessageTemplate::Template message =
190         MessageTemplate::kDetachedOperation;
191     Handle<String> operation =
192         isolate->factory()->NewStringFromAsciiChecked(method_name);
193     THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray);
194   }
195 
196   // spec describes to return `buffer`, but it may disrupt current
197   // implementations, and it's much useful to return array for now.
198   return array;
199 }
200 
201 #ifdef VERIFY_HEAP
202 ACCESSORS(JSTypedArray, raw_length, Object, kLengthOffset)
203 #endif
204 
205 }  // namespace internal
206 }  // namespace v8
207 
208 #include "src/objects/object-macros-undef.h"
209 
210 #endif  // V8_OBJECTS_JS_ARRAY_BUFFER_INL_H_
211