v8 10.2.154 (node 18.16.0)
V8 is Google's open source JavaScript engine
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-object.h" // NOLINT(build/include_directory)
14#include "v8config.h" // NOLINT(build/include_directory)
15
16namespace v8 {
17
18class SharedArrayBuffer;
19
20#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
21// The number of required internal fields can be defined by embedder.
22#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
23#endif
24
26
41 public:
43
49 void* Data() const;
50
54 size_t ByteLength() const;
55
60 bool IsShared() const;
61
67 void operator delete(void* ptr) { ::operator delete(ptr); }
68
74 static std::unique_ptr<BackingStore> Reallocate(
75 v8::Isolate* isolate, std::unique_ptr<BackingStore> backing_store,
76 size_t byte_length);
77
83 using DeleterCallback = void (*)(void* data, size_t length,
84 void* deleter_data);
85
95 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
96
97 private:
102 BackingStore();
103};
104
105#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
106// Use v8::BackingStore::DeleterCallback instead.
107using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
108 void* deleter_data);
109
110#endif
111
116 public:
133 public:
134 virtual ~Allocator() = default;
135
140 virtual void* Allocate(size_t length) = 0;
141
146 virtual void* AllocateUninitialized(size_t length) = 0;
147
152 virtual void Free(void* data, size_t length) = 0;
153
166 virtual void* Reallocate(void* data, size_t old_length, size_t new_length);
167
173 enum class AllocationMode { kNormal, kReservation };
174
185 };
186
190 size_t ByteLength() const;
191
198 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
199
213 std::shared_ptr<BackingStore> backing_store);
214
224 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
225 size_t byte_length);
234 static std::unique_ptr<BackingStore> NewBackingStore(
235 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
236 void* deleter_data);
237
241 bool IsDetachable() const;
242
249 void Detach();
250
257 std::shared_ptr<BackingStore> GetBackingStore();
258
259 V8_INLINE static ArrayBuffer* Cast(Value* value) {
260#ifdef V8_ENABLE_CHECKS
261 CheckCast(value);
262#endif
263 return static_cast<ArrayBuffer*>(value);
264 }
265
266 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
267 static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
268
269 private:
270 ArrayBuffer();
271 static void CheckCast(Value* obj);
272};
273
274#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
275// The number of required internal fields can be defined by embedder.
276#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
277#endif
278
284 public:
292 size_t ByteOffset();
296 size_t ByteLength();
297
307 size_t CopyContents(void* dest, size_t byte_length);
308
313 bool HasBuffer() const;
314
316#ifdef V8_ENABLE_CHECKS
317 CheckCast(value);
318#endif
319 return static_cast<ArrayBufferView*>(value);
320 }
321
322 static const int kInternalFieldCount =
324 static const int kEmbedderFieldCount =
326
327 private:
329 static void CheckCast(Value* obj);
330};
331
336 public:
338 size_t byte_offset, size_t length);
339 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
340 size_t byte_offset, size_t length);
341 V8_INLINE static DataView* Cast(Value* value) {
342#ifdef V8_ENABLE_CHECKS
343 CheckCast(value);
344#endif
345 return static_cast<DataView*>(value);
346 }
347
348 private:
349 DataView();
350 static void CheckCast(Value* obj);
351};
352
357 public:
361 size_t ByteLength() const;
362
369 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
370
384 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
385
395 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
396 size_t byte_length);
405 static std::unique_ptr<BackingStore> NewBackingStore(
406 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
407 void* deleter_data);
408
415 std::shared_ptr<BackingStore> GetBackingStore();
416
418#ifdef V8_ENABLE_CHECKS
419 CheckCast(value);
420#endif
421 return static_cast<SharedArrayBuffer*>(value);
422 }
423
424 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
425
426 private:
428 static void CheckCast(Value* obj);
429};
430
431} // namespace v8
432
433#endif // INCLUDE_V8_ARRAY_BUFFER_H_
static Allocator * NewDefaultAllocator()
virtual void * Reallocate(void *data, size_t old_length, size_t new_length)
virtual void * AllocateUninitialized(size_t length)=0
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
virtual void * Allocate(size_t length)=0
static Local< ArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
static V8_INLINE ArrayBuffer * Cast(Value *value)
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
bool IsDetachable() const
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length)
std::shared_ptr< BackingStore > GetBackingStore()
size_t ByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length)
Local< ArrayBuffer > Buffer()
bool HasBuffer() const
size_t CopyContents(void *dest, size_t byte_length)
static V8_INLINE ArrayBufferView * Cast(Value *value)
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
bool IsShared() const
static void EmptyDeleter(void *data, size_t length, void *deleter_data)
void * Data() const
static std::unique_ptr< BackingStore > Reallocate(v8::Isolate *isolate, std::unique_ptr< BackingStore > backing_store, size_t byte_length)
size_t ByteLength() 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)
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
static V8_INLINE SharedArrayBuffer * Cast(Value *value)
std::shared_ptr< BackingStore > GetBackingStore()
size_t ByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length)
static Local< SharedArrayBuffer > New(Isolate *isolate, size_t byte_length)
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
ArrayBufferCreationMode
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425