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
91 "Reallocate is unsafe, please do not use. Please allocate a new "
92 "BackingStore and copy instead.")
93 static std::unique_ptr<BackingStore> Reallocate(
94 v8::Isolate* isolate, std::unique_ptr<BackingStore> backing_store,
95 size_t byte_length);
96
102 using DeleterCallback = void (*)(void* data, size_t length,
103 void* deleter_data);
104
114 static void EmptyDeleter(void* data, size_t length, void* deleter_data);
115
116 private:
121 BackingStore();
122};
123
124#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
125// Use v8::BackingStore::DeleterCallback instead.
126using BackingStoreDeleterCallback = void (*)(void* data, size_t length,
127 void* deleter_data);
128
129#endif
130
135 public:
152 public:
153 virtual ~Allocator() = default;
154
159 virtual void* Allocate(size_t length) = 0;
160
165 virtual void* AllocateUninitialized(size_t length) = 0;
166
171 virtual void Free(void* data, size_t length) = 0;
172
186 "Reallocate is unsafe, please do not use. Please allocate new memory "
187 "and copy instead.")
188 virtual void* Reallocate(void* data, size_t old_length, size_t new_length);
189
195 enum class AllocationMode { kNormal, kReservation };
196
207 };
208
212 size_t ByteLength() const;
213
217 size_t MaxByteLength() const;
218
225 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
226
240 std::shared_ptr<BackingStore> backing_store);
241
251 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
252 size_t byte_length);
261 static std::unique_ptr<BackingStore> NewBackingStore(
262 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
263 void* deleter_data);
264
277 static std::unique_ptr<BackingStore> NewResizableBackingStore(
278 size_t byte_length, size_t max_byte_length);
279
283 bool IsDetachable() const;
284
288 bool WasDetached() const;
289
297 "Use the version which takes a key parameter (passing a null handle is "
298 "ok).")
299 void Detach();
300
310
314 void SetDetachKey(v8::Local<v8::Value> key);
315
325 std::shared_ptr<BackingStore> GetBackingStore();
326
331 bool IsResizableByUserJavaScript() const;
332
337 void* Data() const;
338
339 V8_INLINE static ArrayBuffer* Cast(Value* value) {
340#ifdef V8_ENABLE_CHECKS
341 CheckCast(value);
342#endif
343 return static_cast<ArrayBuffer*>(value);
344 }
345
346 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
347 static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
348
349 private:
350 ArrayBuffer();
351 static void CheckCast(Value* obj);
352};
353
354#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
355// The number of required internal fields can be defined by embedder.
356#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
357#endif
358
364 public:
372 size_t ByteOffset();
376 size_t ByteLength();
377
387 size_t CopyContents(void* dest, size_t byte_length);
388
393 bool HasBuffer() const;
394
396#ifdef V8_ENABLE_CHECKS
397 CheckCast(value);
398#endif
399 return static_cast<ArrayBufferView*>(value);
400 }
401
402 static const int kInternalFieldCount =
404 static const int kEmbedderFieldCount =
406
407 private:
409 static void CheckCast(Value* obj);
410};
411
416 public:
418 size_t byte_offset, size_t length);
419 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
420 size_t byte_offset, size_t length);
421 V8_INLINE static DataView* Cast(Value* value) {
422#ifdef V8_ENABLE_CHECKS
423 CheckCast(value);
424#endif
425 return static_cast<DataView*>(value);
426 }
427
428 private:
429 DataView();
430 static void CheckCast(Value* obj);
431};
432
437 public:
441 size_t ByteLength() const;
442
446 size_t MaxByteLength() const;
447
454 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
455
469 Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
470
480 static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
481 size_t byte_length);
490 static std::unique_ptr<BackingStore> NewBackingStore(
491 void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
492 void* deleter_data);
493
500 std::shared_ptr<BackingStore> GetBackingStore();
501
506 void* Data() const;
507
509#ifdef V8_ENABLE_CHECKS
510 CheckCast(value);
511#endif
512 return static_cast<SharedArrayBuffer*>(value);
513 }
514
515 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
516
517 private:
519 static void CheckCast(Value* obj);
520};
521
522} // namespace v8
523
524#endif // INCLUDE_V8_ARRAY_BUFFER_H_
Definition: v8-array-buffer.h:363
static ArrayBufferView * Cast(Value *value)
Definition: v8-array-buffer.h:395
Local< ArrayBuffer > Buffer()
size_t CopyContents(void *dest, size_t byte_length)
bool HasBuffer() const
Definition: v8-array-buffer.h:151
virtual ~Allocator()=default
virtual void Free(void *data, size_t length)=0
static Allocator * NewDefaultAllocator()
virtual void * AllocateUninitialized(size_t length)=0
AllocationMode
Definition: v8-array-buffer.h:195
virtual void * Allocate(size_t length)=0
Definition: v8-array-buffer.h:134
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewResizableBackingStore(size_t byte_length, size_t max_byte_length)
static Local< ArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
static Local< ArrayBuffer > New(Isolate *isolate, size_t byte_length)
bool IsDetachable() const
bool WasDetached() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length)
size_t ByteLength() const
Definition: v8-array-buffer.h:40
size_t MaxByteLength() const
bool IsShared() const
size_t ByteLength() const
void(*)(void *data, size_t length, void *deleter_data) DeleterCallback
Definition: v8-array-buffer.h:103
bool IsResizableByUserJavaScript() const
void * Data() const
Definition: v8-array-buffer.h:415
static Local< DataView > New(Local< SharedArrayBuffer > shared_array_buffer, size_t byte_offset, size_t length)
static DataView * Cast(Value *value)
Definition: v8-array-buffer.h:421
static Local< DataView > New(Local< ArrayBuffer > array_buffer, size_t byte_offset, size_t length)
Definition: v8-data.h:18
Definition: v8-isolate.h:210
Definition: v8-local-handle.h:258
Definition: v8-maybe.h:32
Definition: v8-object.h:238
Definition: v8-array-buffer.h:436
void * Data() const
static Local< SharedArrayBuffer > New(Isolate *isolate, std::shared_ptr< BackingStore > backing_store)
std::shared_ptr< BackingStore > GetBackingStore()
static std::unique_ptr< BackingStore > NewBackingStore(void *data, size_t byte_length, v8::BackingStore::DeleterCallback deleter, void *deleter_data)
size_t ByteLength() const
size_t MaxByteLength() const
static std::unique_ptr< BackingStore > NewBackingStore(Isolate *isolate, size_t byte_length)
static SharedArrayBuffer * Cast(Value *value)
Definition: v8-array-buffer.h:508
static Local< SharedArrayBuffer > New(Isolate *isolate, size_t byte_length)
Definition: v8-value.h:32
Definition: v8-internal.h:1204
Definition: libplatform.h:15
void(*)(void *data, size_t length, void *deleter_data) BackingStoreDeleterCallback
Definition: v8-array-buffer.h:127
ArrayBufferCreationMode
Definition: v8-array-buffer.h:25
#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:22
#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
Definition: v8-array-buffer.h:356
#define V8_EXPORT
Definition: v8config.h:762
#define V8_INLINE
Definition: v8config.h:477
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:580
#define V8_DEPRECATED(message)
Definition: v8config.h:572
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:637