1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
# ArrayBuffer
Class `Napi::ArrayBuffer` inherits from class [`Napi::Object`][].
The `Napi::ArrayBuffer` class corresponds to the
[JavaScript `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
class.
## Methods
### New
Allocates a new `Napi::ArrayBuffer` instance with a given length.
```cpp
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, size_t byteLength);
```
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance.
- `[in] byteLength`: The length to be allocated, in bytes.
Returns a new `Napi::ArrayBuffer` instance.
### New
Wraps the provided external data into a new `Napi::ArrayBuffer` instance.
The `Napi::ArrayBuffer` instance does not assume ownership for the data and
expects it to be valid for the lifetime of the instance. Since the
`Napi::ArrayBuffer` is subject to garbage collection this overload is only
suitable for data which is static and never needs to be freed.
This factory method will not provide the caller with an opportunity to free the
data when the `Napi::ArrayBuffer` gets garbage-collected. If you need to free
the data retained by the `Napi::ArrayBuffer` object please use other
variants of the `Napi::ArrayBuffer::New` factory method that accept
`Napi::Finalizer`, which is a function that will be invoked when the
`Napi::ArrayBuffer` object has been destroyed.
```cpp
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength);
```
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance.
- `[in] externalData`: The pointer to the external data to wrap.
- `[in] byteLength`: The length of the `externalData`, in bytes.
Returns a new `Napi::ArrayBuffer` instance.
### New
Wraps the provided external data into a new `Napi::ArrayBuffer` instance.
The `Napi::ArrayBuffer` instance does not assume ownership for the data and
expects it to be valid for the lifetime of the instance. The data can only be
freed once the `finalizeCallback` is invoked to indicate that the
`Napi::ArrayBuffer` has been released.
```cpp
template <typename Finalizer>
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env,
void* externalData,
size_t byteLength,
Finalizer finalizeCallback);
```
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance.
- `[in] externalData`: The pointer to the external data to wrap.
- `[in] byteLength`: The length of the `externalData`, in bytes.
- `[in] finalizeCallback`: A function to be called when the `Napi::ArrayBuffer` is
destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the
`externalData` pointer), and return `void`.
Returns a new `Napi::ArrayBuffer` instance.
### New
Wraps the provided external data into a new `Napi::ArrayBuffer` instance.
The `Napi::ArrayBuffer` instance does not assume ownership for the data and expects it
to be valid for the lifetime of the instance. The data can only be freed once
the `finalizeCallback` is invoked to indicate that the `Napi::ArrayBuffer` has been
released.
```cpp
template <typename Finalizer, typename Hint>
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env,
void* externalData,
size_t byteLength,
Finalizer finalizeCallback,
Hint* finalizeHint);
```
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance.
- `[in] externalData`: The pointer to the external data to wrap.
- `[in] byteLength`: The length of the `externalData`, in bytes.
- `[in] finalizeCallback`: The function to be called when the `Napi::ArrayBuffer` is
destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the
`externalData` pointer) and `Hint*`, and return `void`.
- `[in] finalizeHint`: The hint to be passed as the second parameter of the
finalize callback.
Returns a new `Napi::ArrayBuffer` instance.
### Constructor
Initializes an empty instance of the `Napi::ArrayBuffer` class.
```cpp
Napi::ArrayBuffer::ArrayBuffer();
```
### Constructor
Initializes a wrapper instance of an existing `Napi::ArrayBuffer` object.
```cpp
Napi::ArrayBuffer::ArrayBuffer(napi_env env, napi_value value);
```
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance.
- `[in] value`: The `Napi::ArrayBuffer` reference to wrap.
### ByteLength
```cpp
size_t Napi::ArrayBuffer::ByteLength() const;
```
Returns the length of the wrapped data, in bytes.
### Data
```cpp
void* Napi::ArrayBuffer::Data() const;
```
Returns a pointer the wrapped data.
### Detach
```cpp
void Napi::ArrayBuffer::Detach();
```
Invokes the `ArrayBuffer` detach operation on a detachable `ArrayBuffer`.
### IsDetached
```cpp
bool Napi::ArrayBuffer::IsDetached() const;
```
Returns `true` if this `ArrayBuffer` has been detached.
[`Napi::Object`]: ./object.md
|