File: typed_array_of.md

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (137 lines) | stat: -rw-r--r-- 3,919 bytes parent folder | download | duplicates (3)
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
# TypedArrayOf

Class `Napi::TypedArrayOf<T>` inherits from class [`Napi::TypedArray`][].

The `Napi::TypedArrayOf` class corresponds to the various
[JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
classes.

## Typedefs

The common JavaScript `TypedArray` types are pre-defined for each of use:

```cpp
using Int8Array = Napi::TypedArrayOf<int8_t>;
using Uint8Array = Napi::TypedArrayOf<uint8_t>;
using Int16Array = Napi::TypedArrayOf<int16_t>;
using Uint16Array = Napi::TypedArrayOf<uint16_t>;
using Int32Array = Napi::TypedArrayOf<int32_t>;
using Uint32Array = Napi::TypedArrayOf<uint32_t>;
using Float32Array = Napi::TypedArrayOf<float>;
using Float64Array = Napi::TypedArrayOf<double>;
```

The one exception is the `Uint8ClampedArray` which requires explicit
initialization:

```cpp
Uint8Array::New(env, length, napi_uint8_clamped_array)
```

Note that while it's possible to create a "clamped" array the _clamping_
behavior is only applied in JavaScript.

## Methods

### New

Allocates a new `Napi::TypedArray` instance with a given length. The underlying
`Napi::ArrayBuffer` is allocated automatically to the desired number of elements.

The array type parameter can normally be omitted (because it is inferred from
the template parameter T), except when creating a "clamped" array.

```cpp
static Napi::TypedArrayOf Napi::TypedArrayOf::New(napi_env env,
                        size_t elementLength,
                        napi_typedarray_type type);
```

- `[in] env`: The environment in which to create the `Napi::TypedArrayOf` instance.
- `[in] elementLength`: The length to be allocated, in elements.
- `[in] type`: The type of array to allocate (optional).

Returns a new `Napi::TypedArrayOf` instance.

### New

Wraps the provided `Napi::ArrayBuffer` into a new `Napi::TypedArray` instance.

The array `type` parameter can normally be omitted (because it is inferred from
the template parameter `T`), except when creating a "clamped" array.

```cpp
static Napi::TypedArrayOf Napi::TypedArrayOf::New(napi_env env,
                        size_t elementLength,
                        Napi::ArrayBuffer arrayBuffer,
                        size_t bufferOffset,
                        napi_typedarray_type type);
```

- `[in] env`: The environment in which to create the `Napi::TypedArrayOf` instance.
- `[in] elementLength`: The length to array, in elements.
- `[in] arrayBuffer`: The backing `Napi::ArrayBuffer` instance.
- `[in] bufferOffset`: The offset into the `Napi::ArrayBuffer` where the array starts,
                       in bytes.
- `[in] type`: The type of array to allocate (optional).

Returns a new `Napi::TypedArrayOf` instance.

### Constructor

Initializes an empty instance of the `Napi::TypedArrayOf` class.

```cpp
Napi::TypedArrayOf::TypedArrayOf();
```

### Constructor

Initializes a wrapper instance of an existing `Napi::TypedArrayOf` object.

```cpp
Napi::TypedArrayOf::TypedArrayOf(napi_env env, napi_value value);
```

- `[in] env`: The environment in which to create the `Napi::TypedArrayOf` object.
- `[in] value`: The `Napi::TypedArrayOf` reference to wrap.

### operator []

```cpp
T& Napi::TypedArrayOf::operator [](size_t index);
```

- `[in] index: The element index into the array.

Returns the element found at the given index.

### operator []

```cpp
const T& Napi::TypedArrayOf::operator [](size_t index) const;
```

- `[in] index: The element index into the array.

Returns the element found at the given index.

### Data

```cpp
T* Napi::TypedArrayOf::Data() const;
```

Returns a pointer into the backing `Napi::ArrayBuffer` which is offset to point to the
start of the array.

### Data

```cpp
const T* Napi::TypedArrayOf::Data() const
```

Returns a pointer into the backing `Napi::ArrayBuffer` which is offset to point to the
start of the array.

[`Napi::TypedArray`]: ./typed_array.md