v8 12.4.254 (node 22.4.1)
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
63 size_t MaxByteLength() const;
64
69 bool IsShared() const;
70
77
83 void operator delete(void* ptr) { ::operator delete(ptr); }
84
90 static std::unique_ptr<BackingStore> Reallocate(
91 v8::Isolate* isolate, std::unique_ptr<BackingStore> backing_store,
92 size_t byte_length);
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
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
182 virtual void* Reallocate(void* data, size_t old_length, size_t new_length);
183
189 enum class AllocationMode { kNormal, kReservation };
190
201 };
202
206 size_t ByteLength() const;
207
211 size_t MaxByteLength() const;
212
219 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
220
234 std::shared_ptr<BackingStore> backing_store);
235
245 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
246 size_t byte_length);
255 static std::unique_ptr<BackingStore> NewBackingStore(
256 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
257 void* deleter_data);
258
271 static std::unique_ptr<BackingStore> NewResizableBackingStore(
272 size_t byte_length, size_t max_byte_length);
273
277 bool IsDetachable() const;
278
282 bool WasDetached() const;
283
291 "Use the version which takes a key parameter (passing a null handle is "
292 "ok).")
293 void Detach();
294
304
308 void SetDetachKey(v8::Local<v8::Value> key);
309
319 std::shared_ptr<BackingStore> GetBackingStore();
320
325 bool IsResizableByUserJavaScript() const;
326
331 void* Data() const;
332
333 V8_INLINE static ArrayBuffer* Cast(Value* value) {
334#ifdef V8_ENABLE_CHECKS
335 CheckCast(value);
336#endif
337 return static_cast<ArrayBuffer*>(value);
338 }
339
340 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
341 static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
342
343 private:
344 ArrayBuffer();
345 static void CheckCast(Value* obj);
346};
347
348#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
349// The number of required internal fields can be defined by embedder.
350#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
351#endif
352
358 public:
366 size_t ByteOffset();
370 size_t ByteLength();
371
381 size_t CopyContents(void* dest, size_t byte_length);
382
387 bool HasBuffer() const;
388
390#ifdef V8_ENABLE_CHECKS
391 CheckCast(value);
392#endif
393 return static_cast<ArrayBufferView*>(value);
394 }
395
396 static const int kInternalFieldCount =
398 static const int kEmbedderFieldCount =
400
401 private:
403 static void CheckCast(Value* obj);
404};
405
410 public:
412 size_t byte_offset, size_t length);
413 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
414 size_t byte_offset, size_t length);
415 V8_INLINE static DataView* Cast(Value* value) {
416#ifdef V8_ENABLE_CHECKS
417 CheckCast(value);
418#endif
419 return static_cast<DataView*>(value);
420 }
421
422 private:
423 DataView();
424 static void CheckCast(Value* obj);
425};
426
431 public:
435 size_t ByteLength() const;
436
440 size_t MaxByteLength() const;
441
448 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
449
463 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
464
474 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
475 size_t byte_length);
484 static std::unique_ptr<BackingStore> NewBackingStore(
485 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
486 void* deleter_data);
487
494 std::shared_ptr<BackingStore> GetBackingStore();
495
500 void* Data() const;
501
503#ifdef V8_ENABLE_CHECKS
504 CheckCast(value);
505#endif
506 return static_cast<SharedArrayBuffer*>(value);
507 }
508
509 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
510
511 private:
513 static void CheckCast(Value* obj);
514};
515
516} // namespace v8
517
518#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)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
bool IsDetachable() const
V8_DEPRECATE_SOON("Use the version which takes a key parameter (passing a null handle is " "ok).") void Detach()
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length)
size_t ByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length)
bool WasDetached() const
static std::unique_ptr< BackingStore > NewResizableBackingStore(size_t byte_length, size_t max_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
size_t MaxByteLength() 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
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)
void * Data() const
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:753
#define V8_INLINE
Definition v8config.h:477
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:628