v8 11.3.244 (node 20.3.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
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 void* Data() const;
326
327 V8_INLINE static ArrayBuffer* Cast(Value* value) {
328#ifdef V8_ENABLE_CHECKS
329 CheckCast(value);
330#endif
331 return static_cast<ArrayBuffer*>(value);
332 }
333
334 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
335 static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
336
337 private:
338 ArrayBuffer();
339 static void CheckCast(Value* obj);
340};
341
342#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
343// The number of required internal fields can be defined by embedder.
344#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
345#endif
346
352 public:
360 size_t ByteOffset();
364 size_t ByteLength();
365
375 size_t CopyContents(void* dest, size_t byte_length);
376
381 bool HasBuffer() const;
382
384#ifdef V8_ENABLE_CHECKS
385 CheckCast(value);
386#endif
387 return static_cast<ArrayBufferView*>(value);
388 }
389
390 static const int kInternalFieldCount =
392 static const int kEmbedderFieldCount =
394
395 private:
397 static void CheckCast(Value* obj);
398};
399
404 public:
406 size_t byte_offset, size_t length);
407 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
408 size_t byte_offset, size_t length);
409 V8_INLINE static DataView* Cast(Value* value) {
410#ifdef V8_ENABLE_CHECKS
411 CheckCast(value);
412#endif
413 return static_cast<DataView*>(value);
414 }
415
416 private:
417 DataView();
418 static void CheckCast(Value* obj);
419};
420
425 public:
429 size_t ByteLength() const;
430
434 size_t MaxByteLength() const;
435
442 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
443
457 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
458
468 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
469 size_t byte_length);
478 static std::unique_ptr<BackingStore> NewBackingStore(
479 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
480 void* deleter_data);
481
488 std::shared_ptr<BackingStore> GetBackingStore();
489
494 void* Data() const;
495
497#ifdef V8_ENABLE_CHECKS
498 CheckCast(value);
499#endif
500 return static_cast<SharedArrayBuffer*>(value);
501 }
502
503 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
504
505 private:
507 static void CheckCast(Value* obj);
508};
509
510} // namespace v8
511
512#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:719
#define V8_INLINE
Definition v8config.h:460
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:609