Loading...
Searching...
No Matches
v8-array-buffer.h
Go to the documentation of this file.
1// Copyright 2021 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 INCLUDE_V8_ARRAY_BUFFER_H_
6#define INCLUDE_V8_ARRAY_BUFFER_H_
7
8#include <stddef.h>
9
10#include <memory>
11
12#include "v8-local-handle.h" // NOLINT(build/include_directory)
13#include "v8-memory-span.h" // NOLINT(build/include_directory)
14#include "v8-object.h" // NOLINT(build/include_directory)
15#include "v8config.h" // NOLINT(build/include_directory)
16
17namespace v8 {
18
19class SharedArrayBuffer;
20
21#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
22// Defined using gn arg `v8_array_buffer_internal_field_count`.
23#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
24#endif
25
28
43 public:
45
51 void* Data() const;
52
56 size_t ByteLength() const;
57
65 size_t MaxByteLength() const;
66
71 bool IsShared() const;
72
79
85 void operator delete(void* ptr) { ::operator delete(ptr); }
86
92 using DeleterCallback = void (*)(void* data, size_t length,
93 void* deleter_data);
94
104 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
105
106 private:
111 BackingStore();
112};
113
114#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
115// Use v8::BackingStore::DeleterCallback instead.
116using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
117 void* deleter_data);
118
119#endif
120
125 public:
142 public:
143 virtual ~Allocator() = default;
144
149 virtual void* Allocate(size_t length) = 0;
150
155 virtual void* AllocateUninitialized(size_t length) = 0;
156
161 virtual void Free(void* data, size_t length) = 0;
162
168 enum class AllocationMode { kNormal, kReservation };
169
180 };
181
185 size_t ByteLength() const;
186
190 size_t MaxByteLength() const;
191
200 Isolate* isolate, size_t byte_length,
201 BackingStoreInitializationMode initialization_mode =
202 BackingStoreInitializationMode::kZeroInitialized);
203
211 Isolate* isolate, size_t byte_length,
212 BackingStoreInitializationMode initialization_mode =
213 BackingStoreInitializationMode::kZeroInitialized);
214
228 std::shared_ptr<BackingStore> backing_store);
229
240 static std::unique_ptr<BackingStore> NewBackingStore(
241 Isolate* isolate, size_t byte_length,
242 BackingStoreInitializationMode initialization_mode =
243 BackingStoreInitializationMode::kZeroInitialized);
252 static std::unique_ptr<BackingStore> NewBackingStore(
253 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
254 void* deleter_data);
255
268 static std::unique_ptr<BackingStore> NewResizableBackingStore(
269 size_t byte_length, size_t max_byte_length);
270
274 bool IsDetachable() const;
275
279 bool WasDetached() const;
280
288 "Use the version which takes a key parameter (passing a null handle is "
289 "ok).")
290 void Detach();
291
301
305 void SetDetachKey(v8::Local<v8::Value> key);
306
316 std::shared_ptr<BackingStore> GetBackingStore();
317
322 bool IsResizableByUserJavaScript() const;
323
328 void* Data() const;
329
330 V8_INLINE static ArrayBuffer* Cast(Value* value) {
331#ifdef V8_ENABLE_CHECKS
332 CheckCast(value);
333#endif
334 return static_cast<ArrayBuffer*>(value);
335 }
336
337 static constexpr int kInternalFieldCount =
339 static constexpr int kEmbedderFieldCount = kInternalFieldCount;
340
341 private:
342 ArrayBuffer();
343 static void CheckCast(Value* obj);
344};
345
346#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
347// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
348#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
349#endif
350
356 public:
364 size_t ByteOffset();
368 size_t ByteLength();
369
379 size_t CopyContents(void* dest, size_t byte_length);
380
390
395 bool HasBuffer() const;
396
398#ifdef V8_ENABLE_CHECKS
399 CheckCast(value);
400#endif
401 return static_cast<ArrayBufferView*>(value);
402 }
403
404 static constexpr int kInternalFieldCount =
406 static const int kEmbedderFieldCount = kInternalFieldCount;
407
408 private:
410 static void CheckCast(Value* obj);
411};
412
417 public:
419 size_t byte_offset, size_t length);
420 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
421 size_t byte_offset, size_t length);
422 V8_INLINE static DataView* Cast(Value* value) {
423#ifdef V8_ENABLE_CHECKS
424 CheckCast(value);
425#endif
426 return static_cast<DataView*>(value);
427 }
428
429 private:
430 DataView();
431 static void CheckCast(Value* obj);
432};
433
438 public:
442 size_t ByteLength() const;
443
447 size_t MaxByteLength() const;
448
456 Isolate* isolate, size_t byte_length,
457 BackingStoreInitializationMode initialization_mode =
458 BackingStoreInitializationMode::kZeroInitialized);
459
473 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
474
485 static std::unique_ptr<BackingStore> NewBackingStore(
486 Isolate* isolate, size_t byte_length,
487 BackingStoreInitializationMode initialization_mode =
488 BackingStoreInitializationMode::kZeroInitialized);
497 static std::unique_ptr<BackingStore> NewBackingStore(
498 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
499 void* deleter_data);
500
507 std::shared_ptr<BackingStore> GetBackingStore();
508
513 void* Data() const;
514
516#ifdef V8_ENABLE_CHECKS
517 CheckCast(value);
518#endif
519 return static_cast<SharedArrayBuffer*>(value);
520 }
521
522 static constexpr int kInternalFieldCount =
524
525 private:
527 static void CheckCast(Value* obj);
528};
529
530} // namespace v8
531
532#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:355
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:397
Local< ArrayBuffer > Buffer()
v8::MemorySpan< uint8_t > GetContents(v8::MemorySpan< uint8_t > storage)
size_t CopyContents(void *dest, size_t byte_length)
bool HasBuffer() const
Definition: v8-array-buffer.h:141
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
static Allocator * NewDefaultAllocator()
virtual void * AllocateUninitialized(size_t length)=0
AllocationMode
Definition: v8-array-buffer.h:168
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:124
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewResizableBackingStore(size_t byte_length, size_t max_byte_length)
static Local< ArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
bool IsDetachable() const
bool WasDetached() const
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
size_t ByteLength() const
static MaybeLocal< ArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-array-buffer.h:42
size_t MaxByteLength() const
bool IsShared() const
size_t ByteLength() const
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
Definition: v8-array-buffer.h:93
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
bool IsResizableByUserJavaScript() const
void * Data() const
Definition: v8-array-buffer.h:416
static Local< DataView > New(Local< SharedArrayBuffer > shared_array_buffer, size_t byte_offset, size_t length)
static DataView * Cast(Value *value)
Definition: v8-array-buffer.h:422
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:273
Definition: v8-local-handle.h:266
Definition: v8-local-handle.h:632
Definition: v8-maybe.h:32
Definition: v8-memory-span.h:64
Definition: v8-object.h:233
Definition: v8-array-buffer.h:437
void * Data() const
static Local< SharedArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
std::shared_ptr< BackingStore > GetBackingStore()
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
size_t ByteLength() const
size_t MaxByteLength() const
static Local< SharedArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
static SharedArrayBuffer * Cast(Value *value)
Definition: v8-array-buffer.h:515
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
Definition: v8-value.h:32
Definition: v8-internal.h:1340
Definition: libplatform.h:15
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
Definition: v8-array-buffer.h:117
BackingStoreInitializationMode
Definition: v8-array-buffer.h:27
ArrayBufferCreationMode
Definition: v8-array-buffer.h:26
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:23
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:348
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_DEPRECATED(message)
Definition: v8config.h:595
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:660