v8 13.6.233 (node 24.1.0)
V8 is Google's open source JavaScript engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
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 "v8-platform.h" // NOLINT(build/include_directory)
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace v8 {
19
21
22#if defined(V8_COMPRESS_POINTERS) && \
23 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
24class IsolateGroup;
25#endif
26
27#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
28// Defined using gn arg `v8_array_buffer_internal_field_count`.
29#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
30#endif
31
35
49class V8_EXPORT BackingStore : public v8::internal::BackingStoreBase {
50 public:
52
58 void* Data() const;
59
63 size_t ByteLength() const;
64
72 size_t MaxByteLength() const;
73
78 bool IsShared() const;
79
86
92 void operator delete(void* ptr) { ::operator delete(ptr); }
93
99 using DeleterCallback = void (*)(void* data, size_t length,
100 void* deleter_data);
101
111 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
112
113 private:
118 BackingStore();
119};
120
121#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
122// Use v8::BackingStore::DeleterCallback instead.
123using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
124 void* deleter_data);
125
126#endif
127
131class V8_EXPORT ArrayBuffer : public Object {
132 public:
149 public:
150 virtual ~Allocator() = default;
151
156 virtual void* Allocate(size_t length) = 0;
157
162 virtual void* AllocateUninitialized(size_t length) = 0;
163
168 virtual void Free(void* data, size_t length) = 0;
169
177 virtual size_t MaxAllocationSize() const { return kMaxByteLength; }
178
185
193 virtual PageAllocator* GetPageAllocator() { return nullptr; }
194
195#if defined(V8_COMPRESS_POINTERS) && \
196 !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
207 static Allocator* NewDefaultAllocator(const IsolateGroup& group);
208#endif // defined(V8_COMPRESS_POINTERS) &&
209 // !defined(V8_COMPRESS_POINTERS_IN_SHARED_CAGE)
210
222 };
223
227 size_t ByteLength() const;
228
232 size_t MaxByteLength() const;
233
242 Isolate* isolate, size_t byte_length,
243 BackingStoreInitializationMode initialization_mode =
245
253 Isolate* isolate, size_t byte_length,
254 BackingStoreInitializationMode initialization_mode =
256
270 std::shared_ptr<BackingStore> backing_store);
271
287 static std::unique_ptr<BackingStore> NewBackingStore(
288 Isolate* isolate, size_t byte_length,
289 BackingStoreInitializationMode initialization_mode =
291 BackingStoreOnFailureMode on_failure =
293
302 static std::unique_ptr<BackingStore> NewBackingStore(
303 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
304 void* deleter_data);
305
318 static std::unique_ptr<BackingStore> NewResizableBackingStore(
319 size_t byte_length, size_t max_byte_length);
320
324 bool IsDetachable() const;
325
329 bool WasDetached() const;
330
338 "Use the version which takes a key parameter (passing a null handle is "
339 "ok).")
340 void Detach();
341
351
356
366 std::shared_ptr<BackingStore> GetBackingStore();
367
373
378 void* Data() const;
379
380 V8_INLINE static ArrayBuffer* Cast(Value* value) {
381#ifdef V8_ENABLE_CHECKS
382 CheckCast(value);
383#endif
384 return static_cast<ArrayBuffer*>(value);
385 }
386
387 static constexpr int kInternalFieldCount =
390
391#if V8_ENABLE_SANDBOX
392 static constexpr size_t kMaxByteLength =
393 internal::kMaxSafeBufferSizeForSandbox;
394#elif V8_HOST_ARCH_32_BIT
395 static constexpr size_t kMaxByteLength = std::numeric_limits<int>::max();
396#else
397 // The maximum safe integer (2^53 - 1).
398 static constexpr size_t kMaxByteLength =
399 static_cast<size_t>((uint64_t{1} << 53) - 1);
400#endif
401
402 private:
403 ArrayBuffer();
404 static void CheckCast(Value* obj);
405 friend class TypedArray;
406};
407
408#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
409// Defined using gn arg `v8_array_buffer_view_internal_field_count`.
410#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
411#endif
412
417class V8_EXPORT ArrayBufferView : public Object {
418 public:
426 size_t ByteOffset();
430 size_t ByteLength();
431
441 size_t CopyContents(void* dest, size_t byte_length);
442
452
457 bool HasBuffer() const;
458
459 V8_INLINE static ArrayBufferView* Cast(Value* value) {
460#ifdef V8_ENABLE_CHECKS
461 CheckCast(value);
462#endif
463 return static_cast<ArrayBufferView*>(value);
464 }
465
466 static constexpr int kInternalFieldCount =
469
470 private:
471 ArrayBufferView();
472 static void CheckCast(Value* obj);
473};
474
478class V8_EXPORT DataView : public ArrayBufferView {
479 public:
481 size_t byte_offset, size_t length);
482 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
483 size_t byte_offset, size_t length);
484 V8_INLINE static DataView* Cast(Value* value) {
485#ifdef V8_ENABLE_CHECKS
486 CheckCast(value);
487#endif
488 return static_cast<DataView*>(value);
489 }
490
491 private:
492 DataView();
493 static void CheckCast(Value* obj);
494};
495
499class V8_EXPORT SharedArrayBuffer : public Object {
500 public:
504 size_t ByteLength() const;
505
509 size_t MaxByteLength() const;
510
518 Isolate* isolate, size_t byte_length,
519 BackingStoreInitializationMode initialization_mode =
521
530 Isolate* isolate, size_t byte_length,
531 BackingStoreInitializationMode initialization_mode =
533
547 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
548
565 static std::unique_ptr<BackingStore> NewBackingStore(
566 Isolate* isolate, size_t byte_length,
567 BackingStoreInitializationMode initialization_mode =
569 BackingStoreOnFailureMode on_failure =
571
580 static std::unique_ptr<BackingStore> NewBackingStore(
581 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
582 void* deleter_data);
583
590 std::shared_ptr<BackingStore> GetBackingStore();
591
596 void* Data() const;
597
598 V8_INLINE static SharedArrayBuffer* Cast(Value* value) {
599#ifdef V8_ENABLE_CHECKS
600 CheckCast(value);
601#endif
602 return static_cast<SharedArrayBuffer*>(value);
603 }
604
605 static constexpr int kInternalFieldCount =
607
608 private:
609 SharedArrayBuffer();
610 static void CheckCast(Value* obj);
611};
612
613} // namespace v8
614
615#endif // INCLUDE_V8_ARRAY_BUFFER_H_
static Allocator * NewDefaultAllocator()
virtual void * AllocateUninitialized(size_t length)=0
virtual size_t MaxAllocationSize() const
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
virtual void * Allocate(size_t length)=0
virtual PageAllocator * GetPageAllocator()
static constexpr int kEmbedderFieldCount
static Local< ArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
static V8_INLINE ArrayBuffer * Cast(Value *value)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
friend class TypedArray
void * Data() const
bool IsDetachable() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized, BackingStoreOnFailureMode on_failure=BackingStoreOnFailureMode::kOutOfMemory)
static constexpr size_t kMaxByteLength
static constexpr int kInternalFieldCount
std::shared_ptr< BackingStore > GetBackingStore()
size_t ByteLength() const
void SetDetachKey(v8::Local< v8::Value > key)
V8_WARN_UNUSED_RESULT Maybe< bool > Detach(v8::Local< v8::Value > key)
bool WasDetached() const
bool IsResizableByUserJavaScript() const
V8_DEPRECATED("Use the version which takes a key parameter (passing a null handle is " "ok).") void Detach()
static std::unique_ptr< BackingStore > NewResizableBackingStore(size_t byte_length, size_t max_byte_length)
static MaybeLocal< ArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
v8::MemorySpan< uint8_t > GetContents(v8::MemorySpan< uint8_t > storage)
Local< ArrayBuffer > Buffer()
bool HasBuffer() const
static const int kEmbedderFieldCount
size_t CopyContents(void *dest, size_t byte_length)
static constexpr int kInternalFieldCount
static V8_INLINE ArrayBufferView * Cast(Value *value)
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
bool IsShared() const
size_t MaxByteLength() const
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
void * Data() const
size_t ByteLength() const
bool IsResizableByUserJavaScript() const
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
static V8_INLINE DataView * Cast(Value *value)
static Local< DataView > New(Local< SharedArrayBuffer > shared_array_buffer, size_t byte_offset, size_t length)
static Local< SharedArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
static MaybeLocal< SharedArrayBuffer > MaybeNew(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
void * Data() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized, BackingStoreOnFailureMode on_failure=BackingStoreOnFailureMode::kOutOfMemory)
static V8_INLINE SharedArrayBuffer * Cast(Value *value)
static constexpr int kInternalFieldCount
static Local< SharedArrayBuffer > New(Isolate *isolate, size_t byte_length, BackingStoreInitializationMode initialization_mode=BackingStoreInitializationMode::kZeroInitialized)
std::shared_ptr< BackingStore > GetBackingStore()
size_t ByteLength() const
ArrayBufferCreationMode
BackingStoreOnFailureMode
BackingStoreInitializationMode
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
#define V8_EXPORT
Definition v8config.h:800
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671