v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
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 "v8-platform.h" // NOLINT(build/include_directory)
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace v8 {
19
20class SharedArrayBuffer;
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
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
85 bool IsResizableByUserJavaScript() const;
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:
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
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
184 enum class AllocationMode { kNormal, kReservation };
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
221 static Allocator* NewDefaultAllocator();
222 };
223
227 size_t ByteLength() const;
228
232 size_t MaxByteLength() const;
233
241 static MaybeLocal<ArrayBuffer> MaybeNew(
242 Isolate* isolate, size_t byte_length,
243 BackingStoreInitializationMode initialization_mode =
244 BackingStoreInitializationMode::kZeroInitialized);
245
252 static Local<ArrayBuffer> New(
253 Isolate* isolate, size_t byte_length,
254 BackingStoreInitializationMode initialization_mode =
255 BackingStoreInitializationMode::kZeroInitialized);
256
269 static Local<ArrayBuffer> New(Isolate* isolate,
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 =
290 BackingStoreInitializationMode::kZeroInitialized,
291 BackingStoreOnFailureMode on_failure =
292 BackingStoreOnFailureMode::kOutOfMemory);
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
350 V8_WARN_UNUSED_RESULT Maybe<bool> Detach(v8::Local<v8::Value> key);
351
355 void SetDetachKey(v8::Local<v8::Value> key);
356
366 std::shared_ptr<BackingStore> GetBackingStore();
367
372 bool IsResizableByUserJavaScript() const;
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 =
388 V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
389 static constexpr int kEmbedderFieldCount = 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:
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
418 public:
422 Local<ArrayBuffer> Buffer();
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
460#ifdef V8_ENABLE_CHECKS
461 CheckCast(value);
462#endif
463 return static_cast<ArrayBufferView*>(value);
464 }
465
466 static constexpr int kInternalFieldCount =
467 V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
468 static const int kEmbedderFieldCount = kInternalFieldCount;
469
470 private:
472 static void CheckCast(Value* obj);
473};
474
479 public:
480 static Local<DataView> New(Local<ArrayBuffer> array_buffer,
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:
493 static void CheckCast(Value* obj);
494};
495
500 public:
504 size_t ByteLength() const;
505
509 size_t MaxByteLength() const;
510
517 static Local<SharedArrayBuffer> New(
518 Isolate* isolate, size_t byte_length,
519 BackingStoreInitializationMode initialization_mode =
520 BackingStoreInitializationMode::kZeroInitialized);
521
529 static MaybeLocal<SharedArrayBuffer> MaybeNew(
530 Isolate* isolate, size_t byte_length,
531 BackingStoreInitializationMode initialization_mode =
532 BackingStoreInitializationMode::kZeroInitialized);
533
546 static Local<SharedArrayBuffer> New(
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 =
568 BackingStoreInitializationMode::kZeroInitialized,
569 BackingStoreOnFailureMode on_failure =
570 BackingStoreOnFailureMode::kOutOfMemory);
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
599#ifdef V8_ENABLE_CHECKS
600 CheckCast(value);
601#endif
602 return static_cast<SharedArrayBuffer*>(value);
603 }
604
605 static constexpr int kInternalFieldCount =
606 V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
607
608 private:
610 static void CheckCast(Value* obj);
611};
612
613} // namespace v8
614
615#endif // INCLUDE_V8_ARRAY_BUFFER_H_
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
static V8_INLINE ArrayBufferView * Cast(Value *value)
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
virtual void * AllocateUninitialized(size_t length)=0
virtual size_t MaxAllocationSize() const
virtual PageAllocator * GetPageAllocator()
virtual void * Allocate(size_t length)=0
V8_DEPRECATED("Use the version which takes a key parameter (passing a null handle is " "ok).") void Detach()
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
static V8_INLINE DataView * Cast(Value *value)
static V8_INLINE SharedArrayBuffer * Cast(Value *value)
STL namespace.
BackingStoreInitializationMode
ArrayBufferCreationMode
BackingStoreOnFailureMode
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
#define V8_EXPORT
Definition v8config.h:800
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
std::unique_ptr< ValueMirror > value
std::unique_ptr< ValueMirror > key