-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add
TypedArray
and TypedArrayOf
* Added documentation for `TypedArray` and `TypedArrayOf` * Tweaked the documentation for `ArrayBuffer` PR-URL: #305 Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
Showing
3 changed files
with
229 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,74 @@ | ||
# Typed array | ||
# TypedArray | ||
|
||
You are reading a draft of the next documentation and it's in continuous update so | ||
if you don't find what you need please refer to: | ||
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) | ||
The `TypedArray` class corresponds to the | ||
[JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) | ||
class. | ||
|
||
## Methods | ||
|
||
### Constructor | ||
|
||
Initializes an empty instance of the `TypedArray` class. | ||
|
||
```cpp | ||
TypedArray(); | ||
``` | ||
|
||
### Constructor | ||
|
||
Initializes a wrapper instance of an existing `TypedArray` instance. | ||
|
||
```cpp | ||
TypedArray(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The environment in which to create the `TypedArray` instance. | ||
- `[in] value`: The `TypedArray` reference to wrap. | ||
### TypedArrayType | ||
```cpp | ||
napi_typedarray_type TypedArrayType() const; | ||
``` | ||
|
||
Returns the type of this instance. | ||
|
||
### ArrayBuffer | ||
|
||
```cpp | ||
Napi::ArrayBuffer ArrayBuffer() const; | ||
``` | ||
|
||
Returns the backing array buffer. | ||
|
||
### ElementSize | ||
|
||
```cpp | ||
uint8_t ElementSize() const; | ||
``` | ||
|
||
Returns the size of one element, in bytes. | ||
|
||
### ElementLength | ||
|
||
```cpp | ||
size_t ElementLength() const; | ||
``` | ||
|
||
Returns the number of elements. | ||
|
||
### ByteOffset | ||
|
||
```cpp | ||
size_t ByteOffset() const; | ||
``` | ||
|
||
Returns the offset into the `ArrayBuffer` where the array starts, in bytes. | ||
|
||
### ByteLength | ||
|
||
```cpp | ||
size_t ByteLength() const; | ||
``` | ||
|
||
Returns the length of the array, in bytes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,133 @@ | ||
# Typed array of | ||
# TypedArrayOf | ||
|
||
You are reading a draft of the next documentation and it's in continuous update so | ||
if you don't find what you need please refer to: | ||
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) | ||
The `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 | ||
typedef TypedArrayOf<int8_t> Int8Array; | ||
typedef TypedArrayOf<uint8_t> Uint8Array; | ||
typedef TypedArrayOf<int16_t> Int16Array; | ||
typedef TypedArrayOf<uint16_t> Uint16Array; | ||
typedef TypedArrayOf<int32_t> Int32Array; | ||
typedef TypedArrayOf<uint32_t> Uint32Array; | ||
typedef TypedArrayOf<float> Float32Array; | ||
typedef TypedArrayOf<double> Float64Array; | ||
``` | ||
|
||
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 `TypedArray` instance with a given length. The underlying | ||
`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 TypedArrayOf New(napi_env env, | ||
size_t elementLength, | ||
napi_typedarray_type type); | ||
``` | ||
|
||
- `[in] env`: The environment in which to create the `TypedArrayOf` instance. | ||
- `[in] elementLength`: The length to be allocated, in elements. | ||
- `[in] type`: The type of array to allocate (optional). | ||
|
||
Returns a new `TypedArrayOf` instance. | ||
|
||
### New | ||
|
||
Wraps the provided `ArrayBuffer` into a new `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 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 `TypedArrayOf` instance. | ||
- `[in] elementLength`: The length to array, in elements. | ||
- `[in] arrayBuffer`: The backing `ArrayBuffer` instance. | ||
- `[in] bufferOffset`: The offset into the `ArrayBuffer` where the array starts, | ||
in bytes. | ||
- `[in] type`: The type of array to allocate (optional). | ||
Returns a new `TypedArrayOf` instance. | ||
### Constructor | ||
Initializes an empty instance of the `TypedArrayOf` class. | ||
```cpp | ||
TypedArrayOf(); | ||
``` | ||
|
||
### Constructor | ||
|
||
Initializes a wrapper instance of an existing `TypedArrayOf` object. | ||
|
||
```cpp | ||
TypedArrayOf(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The environment in which to create the `TypedArrayOf` object. | ||
- `[in] value`: The `TypedArrayOf` reference to wrap. | ||
### operator [] | ||
```cpp | ||
T& operator [](size_t index); | ||
``` | ||
|
||
- `[in] index: The element index into the array. | ||
|
||
Returns the element found at the given index. | ||
|
||
### operator [] | ||
|
||
```cpp | ||
const T& operator [](size_t index) const; | ||
``` | ||
|
||
- `[in] index: The element index into the array. | ||
|
||
Returns the element found at the given index. | ||
|
||
### Data | ||
|
||
```cpp | ||
T* Data() const; | ||
``` | ||
|
||
Returns a pointer into the backing `ArrayBuffer` which is offset to point to the | ||
start of the array. | ||
|
||
### Data | ||
|
||
```cpp | ||
const T* Data() const | ||
``` | ||
|
||
Returns a pointer into the backing `ArrayBuffer` which is offset to point to the | ||
start of the array. |