node-addon-api
Advanced tools
Comparing version 1.4.0 to 1.5.0
# node-addon-api Changelog | ||
## 2018-07-19 Version 1.4.0 (Current), @NickNasso | ||
## 2018-10-03 Version 1.5.0 (Current), @NickNasso | ||
@@ -9,2 +9,67 @@ ### Notable changes: | ||
- Completed the documentation to cover all the API surface. | ||
- Numerous fixes to make documentation more consistent in all of its parts. | ||
#### API | ||
- Add `Napi::AsyncContext` class to handle asynchronous operation. | ||
- Add B`Napi::igInt` class to work with BigInt type. | ||
- Add `Napi::VersionManagement` class to retrieve the versions of Node.js and N-API. | ||
- Fix potential memory leaks. | ||
- DataView feature is enabled by default | ||
- Add descriptor for Symbols | ||
- Add new methods on `Napi::FunctionReference`. | ||
- Add the possibility to retrieve the environment on `Napi::Promise::Deferred` | ||
#### TOOL | ||
- Add tool to check if a native add-on is built using N-API | ||
#### TEST | ||
- Start to increase the test coverage | ||
- Fix in the test suite to better handle the experimental features that are not | ||
yet backported in the previous Node.js version. | ||
### Commits | ||
* [[`2009c019af`](https://github.com/nodejs/node-addon-api/commit/2009c019af)] - Merge pull request #292 from devsnek/feature/bigint (Gus Caplan) | ||
* [[`e44aca985e`](https://github.com/nodejs/node-addon-api/commit/e44aca985e)] - add bigint class (Gus Caplan) | ||
* [[`a3951ab973`](https://github.com/nodejs/node-addon-api/commit/a3951ab973)] - Add documentation for Env(). (Rolf Timmermans) [#318](https://github.com/nodejs/node-addon-api/pull/318) | ||
* [[`a6f7a6ad51`](https://github.com/nodejs/node-addon-api/commit/a6f7a6ad51)] - Add Env() to Promise::Deferred. (Rolf Timmermans) | ||
* [[`0097e96b92`](https://github.com/nodejs/node-addon-api/commit/0097e96b92)] - Fixed broken links for Symbol and String (NickNaso) | ||
* [[`b0ecd38d76`](https://github.com/nodejs/node-addon-api/commit/b0ecd38d76)] - Fix Code of conduct link properly (#323) (Jake Yoon) [#323](https://github.com/nodejs/node-addon-api/pull/323) | ||
* [[`223474900f`](https://github.com/nodejs/node-addon-api/commit/223474900f)] - **doc**: update Version management (Dongjin Na) [#360](https://github.com/nodejs/node-addon-api/pull/360) | ||
* [[`4f76262a10`](https://github.com/nodejs/node-addon-api/commit/4f76262a10)] - **doc**: some fix on `Napi::Boolean` documentation (NickNaso) [#354](https://github.com/nodejs/node-addon-api/pull/354) | ||
* [[`78374f72d2`](https://github.com/nodejs/node-addon-api/commit/78374f72d2)] - **doc**: number documentation (NickNaso) [#356](https://github.com/nodejs/node-addon-api/pull/356) | ||
* [[`51ffe453f8`](https://github.com/nodejs/node-addon-api/commit/51ffe453f8)] - **doc**: doc cleanup (NickNaso) [#353](https://github.com/nodejs/node-addon-api/pull/353) | ||
* [[`fc11c944b2`](https://github.com/nodejs/node-addon-api/commit/fc11c944b2)] - **doc**: major doc cleanup (NickNaso) [#335](https://github.com/nodejs/node-addon-api/pull/335) | ||
* [[`100d0a7cb2`](https://github.com/nodejs/node-addon-api/commit/100d0a7cb2)] - **doc**: first pass on objectwrap documentation (NickNaso) [#321](https://github.com/nodejs/node-addon-api/pull/321) | ||
* [[`c7d54180ff`](https://github.com/nodejs/node-addon-api/commit/c7d54180ff)] - **doc**: the Napi::ObjectWrap example does not compile (Arnaud Botella) [#339](https://github.com/nodejs/node-addon-api/pull/339) | ||
* [[`7cdd78726a`](https://github.com/nodejs/node-addon-api/commit/7cdd78726a)] - **doc**: added cpp highlight for string.md (Jaeseok Yoon) [#329](https://github.com/nodejs/node-addon-api/pull/329) | ||
* [[`8ed29f547c`](https://github.com/nodejs/node-addon-api/commit/8ed29f547c)] - **doc**: add blurb about ABI stability (Gabriel Schulhof) [#326](https://github.com/nodejs/node-addon-api/pull/326) | ||
* [[`757eb1f5a3`](https://github.com/nodejs/node-addon-api/commit/757eb1f5a3)] - **doc**: add function and function reference doc (NickNaso) [#299](https://github.com/nodejs/node-addon-api/pull/299) | ||
* [[`2885c18591`](https://github.com/nodejs/node-addon-api/commit/2885c18591)] - **doc**: Create changelog for release 1.4.0 (Nicola Del Gobbo) | ||
* [[`917bd60baa`](https://github.com/nodejs/node-addon-api/commit/917bd60baa)] - **src**: remove TODOs by fixing memory leaks (Gabriel Schulhof) [#343](https://github.com/nodejs/node-addon-api/pull/343) | ||
* [[`dfcb93945f`](https://github.com/nodejs/node-addon-api/commit/dfcb93945f)] - **src**: implement AsyncContext class (Jinho Bang) [#252](https://github.com/nodejs/node-addon-api/pull/252) | ||
* [[`211ed38d0d`](https://github.com/nodejs/node-addon-api/commit/211ed38d0d)] - **src**: make 'nothing' target a static library (Gabriel Schulhof) [#348](https://github.com/nodejs/node-addon-api/pull/348) | ||
* [[`97c4ab5cf2`](https://github.com/nodejs/node-addon-api/commit/97c4ab5cf2)] - **src**: add Call and MakeCallback that accept cargs (NickNaso) [#344](https://github.com/nodejs/node-addon-api/pull/344) | ||
* [[`b6e2d92c09`](https://github.com/nodejs/node-addon-api/commit/b6e2d92c09)] - **src**: enable DataView feature by default (Jinho) [#331](https://github.com/nodejs/node-addon-api/pull/331) | ||
* [[`0a00e7c97b`](https://github.com/nodejs/node-addon-api/commit/0a00e7c97b)] - **src**: implement missing descriptor defs for symbols (Philipp Renoth) [#280](https://github.com/nodejs/node-addon-api/pull/280) | ||
* [[`38e01b7e3b`](https://github.com/nodejs/node-addon-api/commit/38e01b7e3b)] - **src**: first pass on adding version management apis (NickNaso) [#325](https://github.com/nodejs/node-addon-api/pull/325) | ||
* [[`79ee8381d2`](https://github.com/nodejs/node-addon-api/commit/79ee8381d2)] - **src**: fix compile failure in test (Michael Dawson) [#345](https://github.com/nodejs/node-addon-api/pull/345) | ||
* [[`4d92a6066f`](https://github.com/nodejs/node-addon-api/commit/4d92a6066f)] - **src**: Add ObjectReference test case (Anisha Rohra) [#212](https://github.com/nodejs/node-addon-api/pull/212) | ||
* [[`779560f397`](https://github.com/nodejs/node-addon-api/commit/779560f397)] - **test**: add operator overloading tests in Number (Your Name) [#355](https://github.com/nodejs/node-addon-api/pull/355) | ||
* [[`73fed84ceb`](https://github.com/nodejs/node-addon-api/commit/73fed84ceb)] - **test**: add ability to control experimental tests (Michael Dawson) [#350](https://github.com/nodejs/node-addon-api/pull/350) | ||
* [[`14c69abd46`](https://github.com/nodejs/node-addon-api/commit/14c69abd46)] - **test**: write tests for Boolean class (Jaeseok Yoon) [#328](https://github.com/nodejs/node-addon-api/pull/328) | ||
* [[`2ad47a83b1`](https://github.com/nodejs/node-addon-api/commit/2ad47a83b1)] - **test**: explicitly cast to uint32\_t in test (Gabriel Schulhof) [#341](https://github.com/nodejs/node-addon-api/pull/341) | ||
* [[`622ffaea76`](https://github.com/nodejs/node-addon-api/commit/622ffaea76)] - **test**: Tighten up compiler warnings (Mikhail Cheshkov) [#315](https://github.com/nodejs/node-addon-api/pull/315) | ||
* [[`fd3c37b0f2`](https://github.com/nodejs/node-addon-api/commit/fd3c37b0f2)] - **tools**: add tool to check for N-API modules (Gabriel Schulhof) [#346](https://github.com/nodejs/node-addon-api/pull/346) | ||
## 2018-07-19 Version 1.4.0, @NickNasso | ||
### Notable changes: | ||
#### Documentation | ||
- Numerous additions to the documentation, filling out coverage | ||
@@ -65,3 +130,3 @@ of API surface | ||
#### Tests | ||
- Fix test failures on linuxOne and AIX | ||
- Fix test failures on linuxOne and AIX | ||
- Added basic tests for Scopes | ||
@@ -68,0 +133,0 @@ - Fix MSVC warning C4244 in tests |
# Code of Conduct | ||
The Node.js Code of Conduct, which applies to this project, can be found at | ||
https://github.com/nodejs/TSC/blob/master/CODE_OF_CONDUCT.md. | ||
https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md. |
# ArrayBuffer | ||
The `ArrayBuffer` class corresponds to the | ||
The `Napi::ArrayBuffer` class corresponds to the | ||
[JavaScript `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) | ||
@@ -11,44 +11,44 @@ class. | ||
Allocates a new `ArrayBuffer` instance with a given length. | ||
Allocates a new `Napi::ArrayBuffer` instance with a given length. | ||
```cpp | ||
static ArrayBuffer New(napi_env env, size_t byteLength); | ||
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, size_t byteLength); | ||
``` | ||
- `[in] env`: The environment in which to create the `ArrayBuffer` instance. | ||
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. | ||
- `[in] byteLength`: The length to be allocated, in bytes. | ||
Returns a new `ArrayBuffer` instance. | ||
Returns a new `Napi::ArrayBuffer` instance. | ||
### New | ||
Wraps the provided external data into a new `ArrayBuffer` instance. | ||
Wraps the provided external data into a new `Napi::ArrayBuffer` instance. | ||
The `ArrayBuffer` instance does not assume ownership for the data and expects it | ||
to be valid for the lifetime of the instance. Since the `ArrayBuffer` is subject | ||
to garbage collection this overload is only suitable for data which is static | ||
and never needs to be freed. | ||
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. | ||
```cpp | ||
static ArrayBuffer New(napi_env env, void* externalData, size_t byteLength); | ||
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength); | ||
``` | ||
- `[in] env`: The environment in which to create the `ArrayBuffer` instance. | ||
- `[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 `ArrayBuffer` instance. | ||
Returns a new `Napi::ArrayBuffer` instance. | ||
### New | ||
Wraps the provided external data into a new `ArrayBuffer` instance. | ||
Wraps the provided external data into a new `Napi::ArrayBuffer` instance. | ||
The `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 `ArrayBuffer` has been | ||
released. | ||
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 ArrayBuffer New(napi_env env, | ||
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, | ||
void* externalData, | ||
@@ -59,18 +59,18 @@ size_t byteLength, | ||
- `[in] env`: The environment in which to create the `ArrayBuffer` instance. | ||
- `[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 `ArrayBuffer` is | ||
- `[in] finalizeCallback`: A function to be called when the `Napi::ArrayBuffer` is | ||
destroyed. It must implement `operator()`, accept a `void*` (which is the | ||
`externalData` pointer), and return `void`. | ||
Returns a new `ArrayBuffer` instance. | ||
Returns a new `Napi::ArrayBuffer` instance. | ||
### New | ||
Wraps the provided external data into a new `ArrayBuffer` instance. | ||
Wraps the provided external data into a new `Napi::ArrayBuffer` instance. | ||
The `ArrayBuffer` instance does not assume ownership for the data and expects it | ||
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 `ArrayBuffer` has been | ||
the `finalizeCallback` is invoked to indicate that the `Napi::ArrayBuffer` has been | ||
released. | ||
@@ -80,3 +80,3 @@ | ||
template <typename Finalizer, typename Hint> | ||
static ArrayBuffer New(napi_env env, | ||
static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, | ||
void* externalData, | ||
@@ -88,6 +88,6 @@ size_t byteLength, | ||
- `[in] env`: The environment in which to create the `ArrayBuffer` instance. | ||
- `[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 `ArrayBuffer` is | ||
- `[in] finalizeCallback`: The function to be called when the `Napi::ArrayBuffer` is | ||
destroyed. It must implement `operator()`, accept a `void*` (which is the | ||
@@ -98,10 +98,10 @@ `externalData` pointer) and `Hint*`, and return `void`. | ||
Returns a new `ArrayBuffer` instance. | ||
Returns a new `Napi::ArrayBuffer` instance. | ||
### Constructor | ||
Initializes an empty instance of the `ArrayBuffer` class. | ||
Initializes an empty instance of the `Napi::ArrayBuffer` class. | ||
```cpp | ||
ArrayBuffer(); | ||
Napi::ArrayBuffer::ArrayBuffer(); | ||
``` | ||
@@ -111,10 +111,10 @@ | ||
Initializes a wrapper instance of an existing `ArrayBuffer` object. | ||
Initializes a wrapper instance of an existing `Napi::ArrayBuffer` object. | ||
```cpp | ||
ArrayBuffer(napi_env env, napi_value value); | ||
Napi::ArrayBuffer::ArrayBuffer(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The environment in which to create the `ArrayBuffer` instance. | ||
- `[in] value`: The `ArrayBuffer` reference to wrap. | ||
- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. | ||
- `[in] value`: The `Napi::ArrayBuffer` reference to wrap. | ||
@@ -124,3 +124,3 @@ ### ByteLength | ||
```cpp | ||
size_t ByteLength() const; | ||
size_t Napi::ArrayBuffer::ByteLength() const; | ||
``` | ||
@@ -133,5 +133,5 @@ | ||
```cpp | ||
T* Data() const; | ||
T* Napi::ArrayBuffer::Data() const; | ||
``` | ||
Returns a pointer the wrapped data. |
@@ -20,5 +20,11 @@ # Asynchronous operations | ||
- **[AsyncWorker](async_worker.md)** | ||
- **[`Napi::AsyncWorker`](async_worker.md)** | ||
These class helps manage asynchronous operations through an abstraction | ||
of the concept of moving data between the **event loop** and **worker threads**. | ||
Also, the above class may not be appropriate for every scenario. When using any | ||
other asynchronous mechanism, the following API is necessary to ensure an | ||
asynchronous operation is properly tracked by the runtime: | ||
- **[AsyncContext](async_context.md)** |
# AsyncWorker | ||
`AsyncWorker` is an abstract class that you can subclass to remove many of the | ||
tedious tasks of moving data between the event loop and worker threads. This | ||
`Napi::AsyncWorker` is an abstract class that you can subclass to remove many of | ||
the tedious tasks of moving data between the event loop and worker threads. This | ||
class internally handles all the details of creating and executing an asynchronous | ||
operation. | ||
Once created, execution is requested by calling `Queue`. When a thread is | ||
available for execution the `Execute` method will be invoked. Once `Execute` | ||
complets either `OnOK` or `OnError` will be invoked. Once the `OnOK` or | ||
`OnError` methods are complete the AsyncWorker instance is destructed. | ||
Once created, execution is requested by calling `Napi::AsyncWorker::Queue`. When | ||
a thread is available for execution the `Napi::AsyncWorker::Execute` method will | ||
be invoked. Once `Napi::AsyncWorker::Execute` completes either | ||
`Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` will be invoked. Once | ||
the `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` methods are | ||
complete the `Napi::AsyncWorker` instance is destructed. | ||
For the most basic use, only the `Execute` method must be implemented in a | ||
subclass. | ||
For the most basic use, only the `Napi::AsyncWorker::Execute` method must be | ||
implemented in a subclass. | ||
@@ -23,3 +25,3 @@ ## Methods | ||
```cpp | ||
Env Env() const; | ||
Napi::Env Napi::AsyncWorker::Env() const; | ||
``` | ||
@@ -34,3 +36,3 @@ | ||
```cpp | ||
void Queue(); | ||
void Napi::AsyncWorker::Queue(); | ||
``` | ||
@@ -45,3 +47,3 @@ | ||
```cpp | ||
void Cancel(); | ||
void Napi::AsyncWorker::Cancel(); | ||
``` | ||
@@ -52,3 +54,3 @@ | ||
```cpp | ||
ObjectReference& Receiver(); | ||
Napi::ObjectReference& Napi::AsyncWorker::Receiver(); | ||
``` | ||
@@ -62,3 +64,3 @@ | ||
```cpp | ||
FunctionReference& Callback(); | ||
Napi::FunctionReference& Napi::AsyncWorker::Callback(); | ||
``` | ||
@@ -68,4 +70,5 @@ | ||
worker was created. The returned function reference will receive the results of | ||
the computation that happened in the `Execute` method, unless the default | ||
implementation of `OnOK` or `OnError` is overridden. | ||
the computation that happened in the `Napi::AsyncWorker::Execute` method, unless | ||
the default implementation of `Napi::AsyncWorker::OnOK` or | ||
`Napi::AsyncWorker::OnError` is overridden. | ||
@@ -75,7 +78,8 @@ ### SetError | ||
Sets the error message for the error that happened during the execution. Setting | ||
an error message will cause the `OnError` method to be invoked instead of `OnOK` | ||
once the `Execute` method completes. | ||
an error message will cause the `Napi::AsyncWorker::OnError` method to be | ||
invoked instead of `Napi::AsyncWorker::OnOKOnOK` once the | ||
`Napi::AsyncWorker::Execute` method completes. | ||
```cpp | ||
void SetError(const std::string& error); | ||
void Napi::AsyncWorker::SetError(const std::string& error); | ||
``` | ||
@@ -91,9 +95,9 @@ | ||
running on the main event loop, it must avoid calling any methods from node-addon-api | ||
or running any code that might invoke JavaScript. Instead once this method is | ||
or running any code that might invoke JavaScript. Instead, once this method is | ||
complete any interaction through node-addon-api with JavaScript should be implemented | ||
in the `OnOK` method which runs on the main thread and is invoked when the `Execute` | ||
method completes. | ||
in the `Napi::AsyncWorker::OnOK` method which runs on the main thread and is | ||
invoked when the `Napi::AsyncWorker::Execute` method completes. | ||
```cpp | ||
virtual void Execute() = 0; | ||
virtual void Napi::AsyncWorker::Execute() = 0; | ||
``` | ||
@@ -108,3 +112,3 @@ | ||
```cpp | ||
virtual void OnOK(); | ||
virtual void Napi::AsyncWorker::OnOK(); | ||
``` | ||
@@ -114,10 +118,10 @@ | ||
This method is invoked afer Execute() completes if an error occurs | ||
while `Execute` is running and C++ exceptions are enabled or if an | ||
error was set through a call to `SetError`. The default implementation | ||
calls the callback provided when the AsyncWorker class was created, passing | ||
in the error as the first parameter. | ||
This method is invoked afer `Napi::AsyncWorker::Execute` completes if an error | ||
occurs while `Napi::AsyncWorker::Execute` is running and C++ exceptions are | ||
enabled or if an error was set through a call to `Napi::AsyncWorker::SetError`. | ||
The default implementation calls the callback provided when the `Napi::AsyncWorker` | ||
class was created, passing in the error as the first parameter. | ||
```cpp | ||
virtual void OnError(const Error& e); | ||
virtual void Napi::AsyncWorker::OnError(const Napi::Error& e); | ||
``` | ||
@@ -127,6 +131,6 @@ | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Function& callback); | ||
explicit Napi::AsyncWorker(const Napi::Function& callback); | ||
``` | ||
@@ -137,3 +141,3 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
Returns a`Napi::AsyncWork` instance which can later be queued for execution by calling | ||
`Queue`. | ||
@@ -143,6 +147,6 @@ | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Function& callback, const char* resource_name); | ||
explicit Napi::AsyncWorker(const Napi::Function& callback, const char* resource_name); | ||
``` | ||
@@ -156,12 +160,11 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
`Queue`. | ||
Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||
calling `Napi::AsyncWork::Queue`. | ||
### Constructor | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Function& callback, const char* resource_name, const Object& resource); | ||
explicit Napi::AsyncWorker(const Napi::Function& callback, const char* resource_name, const Napi::Object& resource); | ||
``` | ||
@@ -177,11 +180,11 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
`Queue`. | ||
Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||
calling `Napi::AsyncWork::Queue`. | ||
### Constructor | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Object& receiver, const Function& callback); | ||
explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback); | ||
``` | ||
@@ -193,12 +196,11 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
`Queue`. | ||
Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||
calling `Napi::AsyncWork::Queue`. | ||
### Constructor | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Object& receiver, const Function& callback,const char* resource_name); | ||
explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback,const char* resource_name); | ||
``` | ||
@@ -213,12 +215,11 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
`Queue`. | ||
Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||
calling `Napi::AsyncWork::Queue`. | ||
### Constructor | ||
Creates a new `AsyncWorker`. | ||
Creates a new `Napi::AsyncWorker`. | ||
```cpp | ||
explicit AsyncWorker(const Object& receiver, const Function& callback, const char* resource_name, const Object& resource); | ||
explicit Napi::AsyncWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource); | ||
``` | ||
@@ -235,4 +236,4 @@ | ||
Returns an AsyncWork instance which can later be queued for execution by calling | ||
`Queue`. | ||
Returns a `Napi::AsyncWork` instance which can later be queued for execution by | ||
calling `Napi::AsyncWork::Queue`. | ||
@@ -244,3 +245,3 @@ ### Destructor | ||
```cpp | ||
virtual ~AsyncWorker(); | ||
virtual Napi::AsyncWorker::~AsyncWorker(); | ||
``` | ||
@@ -251,24 +252,26 @@ | ||
```cpp | ||
operator napi_async_work() const; | ||
Napi::AsyncWorker::operator napi_async_work() const; | ||
``` | ||
Returns the N-API napi_async_work wrapped by the AsyncWorker object. This can be | ||
used to mix usage of the C N-API and node-addon-api. | ||
Returns the N-API napi_async_work wrapped by the `Napi::AsyncWorker` object. This | ||
can be used to mix usage of the C N-API and node-addon-api. | ||
## Example | ||
The first step to use the `AsyncWorker` class is to create a new class that inherit | ||
from it and implement the `Execute` abstract method. Typically input to your | ||
worker will be saved within class' fields generally passed in through its | ||
constructor. | ||
The first step to use the `Napi::AsyncWorker` class is to create a new class that | ||
inherits from it and implement the `Napi::AsyncWorker::Execute` abstract method. | ||
Typically input to your worker will be saved within class' fields generally | ||
passed in through its constructor. | ||
When the `Execute` method completes without errors the `OnOK` function callback | ||
will be invoked. In this function the results of the computation will be | ||
reassembled and returned back to the initial JavaScript context. | ||
When the `Napi::AsyncWorker::Execute` method completes without errors the | ||
`Napi::AsyncWorker::OnOK` function callback will be invoked. In this function the | ||
results of the computation will be reassembled and returned back to the initial | ||
JavaScript context. | ||
`AsyncWorker` ensures that all the code in the `Execute` function runs in the | ||
background out of the **event loop** thread and at the end the `OnOK` or `OnError` | ||
function will be called and are executed as part of the event loop. | ||
`Napi::AsyncWorker` ensures that all the code in the `Napi::AsyncWorker::Execute` | ||
function runs in the background out of the **event loop** thread and at the end | ||
the `Napi::AsyncWorker::OnOK` or `Napi::AsyncWorker::OnError` function will be | ||
called and are executed as part of the event loop. | ||
The code below show a basic example of `AsyncWorker` the implementation: | ||
The code below show a basic example of `Napi::AsyncWorker` the implementation: | ||
@@ -306,9 +309,17 @@ ```cpp | ||
The `EchoWorker`'s contructor calls the base class' constructor to pass in the | ||
callback that the `AsyncWorker` base class will store persistently. When the work | ||
on the `Execute` method is done the `OnOk` method is called and the results return | ||
back to JavaScript invoking the stored callback with its associated environment. | ||
callback that the `Napi::AsyncWorker` base class will store persistently. When | ||
the work on the `Napi::AsyncWorker::Execute` method is done the | ||
`Napi::AsyncWorker::OnOk` method is called and the results return back to | ||
JavaScript invoking the stored callback with its associated environment. | ||
The following code shows an example on how to create and and use an `AsyncWorker` | ||
The following code shows an example on how to create and and use an `Napi::AsyncWorker` | ||
```cpp | ||
#include<napi.h> | ||
// Include EchoWorker class | ||
// .. | ||
use namespace Napi; | ||
Value Echo(const CallbackInfo& info) { | ||
@@ -323,6 +334,6 @@ // You need to check the input data here | ||
Using the implementation of an `AsyncWorker` is straight forward. You need only create | ||
a new instance and pass to its constructor the callback you want to execute when | ||
your asynchronous task ends and other data you need for your computation. Once created the | ||
only other action you have to do is to call the `Queue` method that will that will | ||
queue the created worker for execution. | ||
Using the implementation of a `Napi::AsyncWorker` is straight forward. You only | ||
need to create a new instance and pass to its constructor the callback you want to | ||
execute when your asynchronous task ends and other data you need for your | ||
computation. Once created the only other action you have to do is to call the | ||
`Napi::AsyncWorker::Queue` method that will queue the created worker for execution. |
@@ -9,3 +9,3 @@ # Basic Types | ||
Value is the base class of Node Addon API's fundamental object type hierarchy. | ||
`Napi::Value` is the base class of Node Addon API's fundamental object type hierarchy. | ||
It represents a JavaScript value of an unknown type. It is a thin wrapper around | ||
@@ -19,17 +19,17 @@ the N-API datatype `napi_value`. Methods on this class can be used to check | ||
```cpp | ||
Value(); | ||
Napi::Value::Value(); | ||
``` | ||
Used to create a Node Addon API `Value` that represents an **empty** value. | ||
Used to create a Node Addon API `Napi::Value` that represents an **empty** value. | ||
```cpp | ||
Value(napi_env env, napi_value value); | ||
Napi::Value::Value(napi_env env, napi_value value); | ||
``` | ||
- `[in] env` - The `napi_env` environment in which to construct the Value | ||
- `[in] env` - The `napi_env` environment in which to construct the `Napi::Value` | ||
object. | ||
- `[in] value` - The underlying JavaScript value that the `Value` instance | ||
- `[in] value` - The underlying JavaScript value that the `Napi::Value` instance | ||
represents. | ||
Returns a Node.js Addon API `Value` that represents the `napi_value` passed | ||
Returns a Node.js Addon API `Napi::Value` that represents the `napi_value` passed | ||
in. | ||
@@ -42,3 +42,3 @@ | ||
```cpp | ||
operator napi_value() const; | ||
Napi::Value::operator napi_value() const; | ||
``` | ||
@@ -52,3 +52,3 @@ | ||
```cpp | ||
bool operator ==(const Value& other) const; | ||
bool Napi::Value::operator ==(const Value& other) const; | ||
``` | ||
@@ -61,3 +61,3 @@ | ||
```cpp | ||
bool operator !=(const Value& other) const; | ||
bool Napi::Value::operator !=(const Value& other) const; | ||
``` | ||
@@ -72,6 +72,6 @@ | ||
template <typename T> | ||
static Value From(napi_env env, const T& value); | ||
static Napi::Value Napi::Value::From(napi_env env, const T& value); | ||
``` | ||
- `[in] env` - The `napi_env` environment in which to construct the Value object. | ||
- `[in] env` - The `napi_env` environment in which to construct the `Napi::Value` object. | ||
- `[in] value` - The C++ type to represent in JavaScript. | ||
@@ -95,3 +95,3 @@ | ||
```cpp | ||
template <typename T> T As() const; | ||
template <typename T> T Napi::Value::As() const; | ||
``` | ||
@@ -109,3 +109,3 @@ | ||
```cpp | ||
bool StrictEquals(const Value& other) const; | ||
bool Napi::Value::StrictEquals(const Value& other) const; | ||
``` | ||
@@ -119,3 +119,3 @@ | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::Value::Env() const; | ||
``` | ||
@@ -128,3 +128,3 @@ | ||
```cpp | ||
bool IsEmpty() const; | ||
bool Napi::Value::IsEmpty() const; | ||
``` | ||
@@ -138,3 +138,3 @@ | ||
When C++ exceptions are disabled at compile time, a method with a `Value` | ||
When C++ exceptions are disabled at compile time, a method with a `Napi::Value` | ||
return type may return an empty value to indicate a pending exception. If C++ | ||
@@ -146,3 +146,3 @@ exceptions are not being used, callers should check the result of | ||
```cpp | ||
napi_valuetype Type() const; | ||
napi_valuetype Napi::Value::Type() const; | ||
``` | ||
@@ -154,3 +154,3 @@ | ||
```cpp | ||
bool IsUndefined() const; | ||
bool Napi::Value::IsUndefined() const; | ||
``` | ||
@@ -163,3 +163,3 @@ | ||
```cpp | ||
bool IsNull() const; | ||
bool Napi::Value::IsNull() const; | ||
``` | ||
@@ -172,14 +172,14 @@ | ||
```cpp | ||
bool IsBoolean() const; | ||
bool Napi::Value::IsBoolean() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `true` or JavaScript | ||
`false`, or `false` if the value is not a Boolean value in JavaScript. | ||
`false`, or `false` if the value is not a `Napi::Boolean` value in JavaScript. | ||
#### IsNumber | ||
```cpp | ||
bool IsNumber() const; | ||
bool Napi::Value::IsNumber() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Number` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Number` or `false` | ||
otherwise. | ||
@@ -189,6 +189,6 @@ | ||
```cpp | ||
bool IsString() const; | ||
bool Napi::Value::IsString() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `String` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::String` or `false` | ||
otherwise. | ||
@@ -198,6 +198,6 @@ | ||
```cpp | ||
bool IsSymbol() const; | ||
bool Napi::Value::IsSymbol() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Symbol` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Symbol` or `false` | ||
otherwise. | ||
@@ -207,6 +207,6 @@ | ||
```cpp | ||
bool IsArray() const; | ||
bool Napi::Value::IsArray() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Array` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Array` or `false` | ||
otherwise. | ||
@@ -216,6 +216,6 @@ | ||
```cpp | ||
bool IsArrayBuffer() const; | ||
bool Napi::Value::IsArrayBuffer() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `ArrayBuffer` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::ArrayBuffer` or `false` | ||
otherwise. | ||
@@ -225,6 +225,6 @@ | ||
```cpp | ||
bool IsTypedArray() const; | ||
bool Napi::Value::IsTypedArray() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `TypedArray` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::TypedArray` or `false` | ||
otherwise. | ||
@@ -234,6 +234,6 @@ | ||
```cpp | ||
bool IsObject() const; | ||
bool Napi::Value::IsObject() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Object` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Object` or `false` | ||
otherwise. | ||
@@ -243,6 +243,6 @@ | ||
```cpp | ||
bool IsFunction() const; | ||
bool Napi::Value::IsFunction() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Function` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Function` or `false` | ||
otherwise. | ||
@@ -252,6 +252,6 @@ | ||
```cpp | ||
bool IsPromise() const; | ||
bool Napi::Value::IsPromise() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `Promise` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::Promise` or `false` | ||
otherwise. | ||
@@ -261,6 +261,6 @@ | ||
```cpp | ||
bool IsDataView() const; | ||
bool Napi::Value::IsDataView() const; | ||
``` | ||
Returns `true` if the underlying value is a JavaScript `DataView` or `false` | ||
Returns `true` if the underlying value is a JavaScript `Napi::DataView` or `false` | ||
otherwise. | ||
@@ -270,6 +270,6 @@ | ||
```cpp | ||
bool IsBuffer() const; | ||
bool Napi::Value::IsBuffer() const; | ||
``` | ||
Returns `true` if the underlying value is a Node.js `Buffer` or `false` | ||
Returns `true` if the underlying value is a Node.js `Napi::Buffer` or `false` | ||
otherwise. | ||
@@ -279,3 +279,3 @@ | ||
```cpp | ||
bool IsExternal() const; | ||
bool Napi::Value::IsExternal() const; | ||
``` | ||
@@ -288,3 +288,3 @@ | ||
```cpp | ||
Boolean ToBoolean() const; | ||
Napi::Boolean Napi::Value::ToBoolean() const; | ||
``` | ||
@@ -299,6 +299,5 @@ | ||
#### ToNumber | ||
```cpp | ||
Number ToNumber() const; | ||
Napi::Number Napi::Value::ToNumber() const; | ||
``` | ||
@@ -315,6 +314,5 @@ | ||
#### ToString | ||
```cpp | ||
String ToString() const; | ||
Napi::String Napi::Value::ToString() const; | ||
``` | ||
@@ -330,6 +328,5 @@ | ||
#### ToObject | ||
```cpp | ||
Object ToObject() const; | ||
Napi::Object Napi::Value::ToObject() const; | ||
``` | ||
@@ -344,8 +341,7 @@ | ||
## Name | ||
Names are JavaScript values that can be used as a property name. There are two | ||
specialized types of names supported in Node.js Addon API- [`String`](String.md) | ||
and [`Symbol`](Symbol.md). | ||
specialized types of names supported in Node.js Addon API [`Napi::String`](string.md) | ||
and [`Napi::Symbol`](symbol.md). | ||
@@ -356,9 +352,9 @@ ### Methods | ||
```cpp | ||
Name(); | ||
Napi::Name::Name(); | ||
``` | ||
Returns an empty `Name`. | ||
Returns an empty `Napi::Name`. | ||
```cpp | ||
Name(napi_env env, napi_value value); | ||
Napi::Name::Name(napi_env env, napi_value value); | ||
``` | ||
@@ -368,3 +364,3 @@ - `[in] env` - The environment in which to create the array. | ||
Returns a Name created from the JavaScript primitive. | ||
Returns a `Napi::Name` created from the JavaScript primitive. | ||
@@ -381,3 +377,3 @@ Note: | ||
```cpp | ||
Array(); | ||
Napi::Array::Array(); | ||
``` | ||
@@ -391,5 +387,4 @@ | ||
```cpp | ||
Array(napi_env env, napi_value value); | ||
Napi::Array::Array(napi_env env, napi_value value); | ||
``` | ||
@@ -409,3 +404,3 @@ - `[in] env` - The environment in which to create the array. | ||
```cpp | ||
static Array New(napi_env env); | ||
static Napi::Array Napi::Array::New(napi_env env); | ||
``` | ||
@@ -423,3 +418,3 @@ - `[in] env` - The environment in which to create the array. | ||
```cpp | ||
static Array New(napi_env env, size_t length); | ||
static Napi::Array Napi::Array::New(napi_env env, size_t length); | ||
``` | ||
@@ -435,5 +430,5 @@ - `[in] env` - The environment in which to create the array. | ||
#### New | ||
#### Length | ||
```cpp | ||
uint32_t Length() const; | ||
uint32_t Napi::Array::Length() const; | ||
``` | ||
@@ -440,0 +435,0 @@ |
# Boolean | ||
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/) | ||
`Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The | ||
`Napi::Boolean` class inherits its behavior from the `Napi::Value` class | ||
(for more info see: [`Napi::Value`](value.md)). | ||
# Methods | ||
## Methods | ||
### Constructor | ||
Creates a new empty instance of an `Napi::Boolean` object. | ||
```cpp | ||
Napi::Boolean::New(Napi::Env env, bool value); | ||
Napi::Boolean::Boolean(); | ||
``` | ||
- `[in] env`: The `napi_env` Environment | ||
- `[in] value`: The Javascript boolean value | ||
Returns a new _empty_ `Napi::Boolean` object. | ||
### Contructor | ||
Creates a new instance of the `Napi::Boolean` object. | ||
```cpp | ||
Napi::Boolean(); | ||
Napi::Boolean(napi_env env, napi_value value); | ||
``` | ||
returns a new empty Javascript Boolean value type. | ||
### operator bool | ||
Converts a Boolean value to a boolean primitive. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. | ||
- `[in] value`: The `napi_value` which is a handle for a JavaScript `Boolean`. | ||
Returns a non-empty `Napi::Boolean` object. | ||
### New | ||
Initializes a new instance of the `Napi::Boolean` object. | ||
```cpp | ||
operator bool() const; | ||
Napi::Boolean Napi::Boolean::New(napi_env env, bool value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. | ||
- `[in] value`: The primitive boolean value (`true` or `false`). | ||
Returns a new instance of the `Napi::Boolean` object. | ||
### Value | ||
Converts a Boolean value to a boolean primitive. | ||
Converts a `Napi::Boolean` value to a boolean primitive. | ||
```cpp | ||
bool Value() const; | ||
``` | ||
bool Napi::Boolean::Value() const; | ||
``` | ||
Returns the boolean primitive type of the corresponding `Napi::Boolean` object. | ||
## Operators | ||
### operator bool | ||
Converts a `Napi::Boolean` value to a boolean primitive. | ||
```cpp | ||
Napi::Boolean::operator bool() const; | ||
``` | ||
Returns the boolean primitive type of the corresponding `Napi::Boolean` object. |
# Buffer | ||
The `Buffer` class creates a projection of raw data that can be consumed by | ||
The `Napi::Buffer` class creates a projection of raw data that can be consumed by | ||
script. | ||
@@ -10,19 +10,19 @@ | ||
Allocates a new `Buffer` object with a given length. | ||
Allocates a new `Napi::Buffer` object with a given length. | ||
```cpp | ||
static Buffer<T> New(napi_env env, size_t length); | ||
static Napi::Buffer<T> Napi::Buffer::New(napi_env env, size_t length); | ||
``` | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] length`: The number of `T` elements to allocate. | ||
Returns a new `Buffer` object. | ||
Returns a new `Napi::Buffer` object. | ||
### New | ||
Wraps the provided external data into a new `Buffer` object. | ||
Wraps the provided external data into a new `Napi::Buffer` object. | ||
The `Buffer` object does not assume ownership for the data and expects it to be | ||
valid for the lifetime of the object. Since the `Buffer` is subject to garbage | ||
The `Napi::Buffer` object does not assume ownership for the data and expects it to be | ||
valid for the lifetime of the object. Since the `Napi::Buffer` is subject to garbage | ||
collection this overload is only suitable for data which is static and never | ||
@@ -32,22 +32,22 @@ needs to be freed. | ||
```cpp | ||
static Buffer<T> New(napi_env env, T* data, size_t length); | ||
static Napi::Buffer<T> Napi::Buffer::New(napi_env env, T* data, size_t length); | ||
``` | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] data`: The pointer to the external data to expose. | ||
- `[in] length`: The number of `T` elements in the external data. | ||
Returns a new `Buffer` object. | ||
Returns a new `Napi::Buffer` object. | ||
### New | ||
Wraps the provided external data into a new `Buffer` object. | ||
Wraps the provided external data into a new `Napi::Buffer` object. | ||
The `Buffer` object does not assume ownership for the data and expects it | ||
The `Napi::Buffer` object does not assume ownership for the data and expects it | ||
to be valid for the lifetime of the object. The data can only be freed once the | ||
`finalizeCallback` is invoked to indicate that the `Buffer` has been released. | ||
`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. | ||
```cpp | ||
template <typename Finalizer> | ||
static Buffer<T> New(napi_env env, | ||
static Napi::Buffer<T> Napi::Buffer::New(napi_env env, | ||
T* data, | ||
@@ -58,22 +58,22 @@ size_t length, | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] data`: The pointer to the external data to expose. | ||
- `[in] length`: The number of `T` elements in the external data. | ||
- `[in] finalizeCallback`: The function to be called when the `Buffer` is | ||
- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is | ||
destroyed. It must implement `operator()`, accept a `T*` (which is the | ||
external data pointer), and return `void`. | ||
Returns a new `Buffer` object. | ||
Returns a new `Napi::Buffer` object. | ||
### New | ||
Wraps the provided external data into a new `Buffer` object. | ||
Wraps the provided external data into a new `Napi::Buffer` object. | ||
The `Buffer` object does not assume ownership for the data and expects it to be | ||
The `Napi::Buffer` object does not assume ownership for the data and expects it to be | ||
valid for the lifetime of the object. The data can only be freed once the | ||
`finalizeCallback` is invoked to indicate that the `Buffer` has been released. | ||
`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. | ||
```cpp | ||
template <typename Finalizer, typename Hint> | ||
static Buffer<T> New(napi_env env, | ||
static Napi::Buffer<T> Napi::Buffer::New(napi_env env, | ||
T* data, | ||
@@ -85,6 +85,6 @@ size_t length, | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] data`: The pointer to the external data to expose. | ||
- `[in] length`: The number of `T` elements in the external data. | ||
- `[in] finalizeCallback`: The function to be called when the `Buffer` is | ||
- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is | ||
destroyed. It must implement `operator()`, accept a `T*` (which is the | ||
@@ -95,24 +95,24 @@ external data pointer) and `Hint*`, and return `void`. | ||
Returns a new `Buffer` object. | ||
Returns a new `Napi::Buffer` object. | ||
### Copy | ||
Allocates a new `Buffer` object and copies the provided external data into it. | ||
Allocates a new `Napi::Buffer` object and copies the provided external data into it. | ||
```cpp | ||
static Buffer<T> Copy(napi_env env, const T* data, size_t length); | ||
static Napi::Buffer<T> Napi::Buffer::Copy(napi_env env, const T* data, size_t length); | ||
``` | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] data`: The pointer to the external data to copy. | ||
- `[in] length`: The number of `T` elements in the external data. | ||
Returns a new `Buffer` object containing a copy of the data. | ||
Returns a new `Napi::Buffer` object containing a copy of the data. | ||
### Constructor | ||
Initializes an empty instance of the `Buffer` class. | ||
Initializes an empty instance of the `Napi::Buffer` class. | ||
```cpp | ||
Buffer(); | ||
Napi::Buffer::Buffer(); | ||
``` | ||
@@ -122,9 +122,9 @@ | ||
Initializes the `Buffer` object using an existing Uint8Array. | ||
Initializes the `Napi::Buffer` object using an existing Uint8Array. | ||
```cpp | ||
Buffer(napi_env env, napi_value value); | ||
Napi::Buffer::Buffer(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The environment in which to create the `Buffer` object. | ||
- `[in] env`: The environment in which to create the `Napi::Buffer` object. | ||
- `[in] value`: The Uint8Array reference to wrap. | ||
@@ -135,3 +135,3 @@ | ||
```cpp | ||
T* Data() const; | ||
T* Napi::Buffer::Data() const; | ||
``` | ||
@@ -144,5 +144,5 @@ | ||
```cpp | ||
size_t Length() const; | ||
size_t Napi::Buffer::Length() const; | ||
``` | ||
Returns the number of `T` elements in the external data. |
@@ -1,3 +0,1 @@ | ||
**WORK IN PROGRESS, NOT YET COMPLETE** | ||
# CallbackInfo | ||
@@ -7,7 +5,7 @@ | ||
The CallbackInfo object is usually created and passed by the Node.js runtime or node-addon-api infrastructure. | ||
The `Napi::CallbackInfo` object is usually created and passed by the Node.js runtime or node-addon-api infrastructure. | ||
The CallbackInfo object contains the arguments passed by the caller. The number of arguments is returned by the `Length` method. Each individual argument can be accessed using the `operator[]` method. | ||
The `Napi::CallbackInfo` object contains the arguments passed by the caller. The number of arguments is returned by the `Length` method. Each individual argument can be accessed using the `operator[]` method. | ||
The `SetData` and `Data` methods are used to set and retrieve the data pointer contained in the CallbackInfo object. | ||
The `SetData` and `Data` methods are used to set and retrieve the data pointer contained in the `Napi::CallbackInfo` object. | ||
@@ -19,7 +17,7 @@ ## Methods | ||
```cpp | ||
CallbackInfo(napi_env env, napi_callback_info info); | ||
Napi::CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `CallbackInfo` object. | ||
- `[in] info`: The `napi_callback_info` data structure from which to construct the `CallbackInfo` object. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::CallbackInfo` object. | ||
- `[in] info`: The `napi_callback_info` data structure from which to construct the `Napi::CallbackInfo` object. | ||
@@ -29,3 +27,3 @@ ### Env | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::CallbackInfo::Env() const; | ||
``` | ||
@@ -38,6 +36,6 @@ | ||
```cpp | ||
Value NewTarget() const; | ||
Napi::Value Napi::CallbackInfo::NewTarget() const; | ||
``` | ||
Returns the `new.target` value of the constructor call. If the function that was invoked (and for which the CallbackInfo was passed) is not a constructor call, a call to `IsEmpty()` on the returned value returns true. | ||
Returns the `new.target` value of the constructor call. If the function that was invoked (and for which the `Napi::NCallbackInfo` was passed) is not a constructor call, a call to `IsEmpty()` on the returned value returns true. | ||
@@ -47,15 +45,14 @@ ### IsConstructCall | ||
```cpp | ||
bool IsConstructCall() const; | ||
bool Napi::CallbackInfo::IsConstructCall() const; | ||
``` | ||
Returns a `bool` indicating if the function that was invoked (and for which the CallbackInfo was passed) is a constructor call. | ||
Returns a `bool` indicating if the function that was invoked (and for which the `Napi::CallbackInfo` was passed) is a constructor call. | ||
### Length | ||
```cpp | ||
size_t Length() const; | ||
size_t Napi::CallbackInfo::Length() const; | ||
``` | ||
Returns the number of arguments passed in the CallbackInfo object. | ||
Returns the number of arguments passed in the `Napi::CallbackInfo` object. | ||
@@ -65,3 +62,3 @@ ### operator [] | ||
```cpp | ||
const Value operator [](size_t index) const; | ||
const Napi::Value operator [](size_t index) const; | ||
``` | ||
@@ -71,3 +68,3 @@ | ||
Returns a `Value` object containing the requested argument. | ||
Returns a `Napi::Value` object containing the requested argument. | ||
@@ -77,3 +74,3 @@ ### This | ||
```cpp | ||
Value This() const; | ||
Napi::Value Napi::CallbackInfo::This() const; | ||
``` | ||
@@ -86,3 +83,3 @@ | ||
```cpp | ||
void* Data() const; | ||
void* Napi::CallbackInfo::Data() const; | ||
``` | ||
@@ -95,6 +92,6 @@ | ||
```cpp | ||
void SetData(void* data); | ||
void Napi::CallbackInfo::SetData(void* data); | ||
``` | ||
- `[in] data`: The new data pointer to associate with this CallbackInfo object. | ||
- `[in] data`: The new data pointer to associate with this `Napi::CallbackInfo` object. | ||
@@ -106,6 +103,6 @@ Returns `void`. | ||
```cpp | ||
~CallbackInfo(); | ||
Napi::CallbackInfo::~CallbackInfo(); | ||
// Disallow copying to prevent multiple free of _dynamicArgs | ||
CallbackInfo(CallbackInfo const &) = delete; | ||
void operator=(CallbackInfo const &) = delete; | ||
Napi::CallbackInfo::CallbackInfo(CallbackInfo const &) = delete; | ||
void Napi::CallbackInfo::operator=(CallbackInfo const &) = delete; | ||
``` |
@@ -1,5 +0,36 @@ | ||
# Class propertry and descriptior | ||
# Class propertry and descriptor | ||
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/) | ||
Property descriptor for use with `Napi::ObjectWrap::DefineClass()`. | ||
This is different from the standalone `Napi::PropertyDescriptor` because it is | ||
specific to each `Napi::ObjectWrap<T>` subclass. | ||
This prevents using descriptors from a different class when defining a new class | ||
(preventing the callbacks from having incorrect `this` pointers). | ||
## Methods | ||
### Contructor | ||
Creates new instance of `Napi::ClassPropertyDescriptor` descriptor object. | ||
```cpp | ||
Napi::ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {} | ||
``` | ||
- `[in] desc`: The `napi_property_descriptor` | ||
Returns new instance of `Napi::ClassPropertyDescriptor` that is used as property descriptor | ||
inside the `Napi::ObjectWrap<T>` class. | ||
### Operator | ||
```cpp | ||
operator napi_property_descriptor&() { return _desc; } | ||
``` | ||
Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor` | ||
```cpp | ||
operator const napi_property_descriptor&() const { return _desc; } | ||
``` | ||
Returns the original N-API `napi_property_descriptor` wrapped inside the `Napi::ClassPropertyDescriptor` |
@@ -1,3 +0,1 @@ | ||
**WORK IN PROGRESS, NOT YET COMPLETE** | ||
# Env | ||
@@ -14,6 +12,6 @@ | ||
```cpp | ||
Env(napi_env env); | ||
Napi::Env::Env(napi_env env); | ||
``` | ||
- `[in] env`: The `napi_env` environment from which to construct the `Env` object. | ||
- `[in] env`: The `napi_env` environment from which to construct the `Napi::Env` object. | ||
@@ -31,6 +29,6 @@ ### napi_env | ||
```cpp | ||
Object Global() const; | ||
Napi::Object Napi::Env::Global() const; | ||
``` | ||
Returns the `Object` representing the environment's JavaScript Global Object. | ||
Returns the `Napi::Object` representing the environment's JavaScript Global Object. | ||
@@ -40,6 +38,6 @@ ### Undefined | ||
```cpp | ||
Value Undefined() const; | ||
Napi::Value Napi::Env::Undefined() const; | ||
``` | ||
Returns the `Value` representing the environment's JavaScript Undefined Object. | ||
Returns the `Napi::Value` representing the environment's JavaScript Undefined Object. | ||
@@ -49,6 +47,6 @@ ### Null | ||
```cpp | ||
Value Null() const; | ||
Napi::Value Napi::Env::Null() const; | ||
``` | ||
Returns the `Value` representing the environment's JavaScript Null Object. | ||
Returns the `Napi::Value` representing the environment's JavaScript Null Object. | ||
@@ -58,3 +56,3 @@ ### IsExceptionPending | ||
```cpp | ||
bool IsExceptionPending() const; | ||
bool Napi::Env::IsExceptionPending() const; | ||
``` | ||
@@ -67,5 +65,5 @@ | ||
```cpp | ||
Error GetAndClearPendingException(); | ||
Napi::Error Napi::Env::GetAndClearPendingException(); | ||
``` | ||
Returns an `Error` object representing the environment's pending exception, if any. | ||
Returns an `Napi::Error` object representing the environment's pending exception, if any. |
@@ -9,3 +9,3 @@ # Error handling | ||
The **Error** is a persistent reference (for more info see: [Object reference](object_reference.md)) | ||
The `Napi::Error` is a persistent reference (for more info see: [`Napi::ObjectReference`](object_reference.md)) | ||
to a JavaScript error object. Use of this class depends on whether C++ | ||
@@ -15,3 +15,3 @@ exceptions are enabled at compile time. | ||
If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the | ||
**Error** class extends `std::exception` and enables integrated | ||
`Napi::Error` class extends `std::exception` and enables integrated | ||
error-handling for C++ exceptions and JavaScript exceptions. | ||
@@ -39,10 +39,10 @@ | ||
an invalid argument), then node-addon-api automatically converts and throws | ||
the error as a C++ exception of type **Error**. | ||
the error as a C++ exception of type `Napi::Error`. | ||
If a JavaScript function called by C++ code via node-addon-api throws a JavaScript | ||
exception, then node-addon-api automatically converts and throws it as a C++ | ||
exception of type **Error** on return from the JavaScript code to the native | ||
exception of type `Napi:Error` on return from the JavaScript code to the native | ||
method. | ||
If a C++ exception of type **Error** escapes from a N-API C++ callback, then | ||
If a C++ exception of type `Napi::Error` escapes from a N-API C++ callback, then | ||
the N-API wrapper automatically converts and throws it as a JavaScript exception. | ||
@@ -63,3 +63,3 @@ | ||
Env env = ... | ||
throw Error::New(env, "Example exception"); | ||
throw Napi::Error::New(env, "Example exception"); | ||
// other C++ statements | ||
@@ -70,3 +70,3 @@ // ... | ||
The statements following the throw statement will not be executed. The exception | ||
will bubble up as a C++ exception of type **Error**, until it is either caught | ||
will bubble up as a C++ exception of type `Napi::Error`, until it is either caught | ||
while still in C++, or else automatically propagated as a JavaScript exception | ||
@@ -78,4 +78,4 @@ when returning to JavaScript. | ||
```cpp | ||
Function jsFunctionThatThrows = someObj.As<Function>(); | ||
Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>(); | ||
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
// other C++ statements | ||
@@ -86,3 +86,3 @@ // ... | ||
The C++ statements following the call to the JavaScript function will not be | ||
executed. The exception will bubble up as a C++ exception of type **Error**, | ||
executed. The exception will bubble up as a C++ exception of type `Napi::Error`, | ||
until it is either caught while still in C++, or else automatically propagated as | ||
@@ -94,4 +94,4 @@ a JavaScript exception when returning to JavaScript. | ||
```cpp | ||
Function jsFunctionThatThrows = someObj.As<Function>(); | ||
Value result; | ||
Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>(); | ||
Napi::Value result; | ||
try { | ||
@@ -112,10 +112,10 @@ result = jsFunctionThatThrows({ arg1, arg2 }); | ||
If C++ exceptions are disabled (for more info see: [Setup](setup.md)), then the | ||
**Error** class does not extend `std::exception`. This means that any calls to | ||
`Napi::Error` class does not extend `std::exception`. This means that any calls to | ||
node-addon-api function do not throw a C++ exceptions. Instead, it raises | ||
_pending_ JavaScript exceptions and returns an _empty_ **Value**. | ||
_pending_ JavaScript exceptions and returns an _empty_ `Napi::Value`. | ||
The calling code should check `env.IsExceptionPending()` before attempting to use a | ||
returned value, and may use methods on the **Env** class | ||
returned value, and may use methods on the `Napi::Env` class | ||
to check for, get, and clear a pending JavaScript exception (for more info see: [Env](env.md)). | ||
If the pending exception is not cleared, it will be thrown when the native code | ||
returns to JavaScript. | ||
returns to JavaScript. | ||
@@ -127,4 +127,4 @@ ## Examples with C++ exceptions disabled | ||
```cpp | ||
Env env = ... | ||
Error::New(env, "Example exception").ThrowAsJavaScriptException(); | ||
Napi::Env env = ... | ||
Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException(); | ||
return; | ||
@@ -139,5 +139,5 @@ ``` | ||
```cpp | ||
Env env = ... | ||
Function jsFunctionThatThrows = someObj.As<Function>(); | ||
Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
Napi::Env env = ... | ||
Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>(); | ||
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
if (env.IsExceptionPending()) { | ||
@@ -149,6 +149,5 @@ Error e = env.GetAndClearPendingException(); | ||
If env.IsExceptionPending() is returns true a | ||
JavaScript exception is pending. To let the exception propagate, the code should | ||
generally return immediately from the native callback, after performing any | ||
necessary cleanup. | ||
If env.IsExceptionPending() returns true a JavaScript exception is pending. To | ||
let the exception propagate, the code should generally return immediately from | ||
the native callback, after performing any necessary cleanup. | ||
@@ -158,7 +157,7 @@ ### Handling a N-API JS exception | ||
```cpp | ||
Env env = ... | ||
Function jsFunctionThatThrows = someObj.As<Function>(); | ||
Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
Napi::Env env = ... | ||
Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>(); | ||
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 }); | ||
if (env.IsExceptionPending()) { | ||
Error e = env.GetAndClearPendingException(); | ||
Napi::Error e = env.GetAndClearPendingException(); | ||
cerr << "Caught JavaScript exception: " + e.Message(); | ||
@@ -165,0 +164,0 @@ } |
# Error | ||
The **Error** class is a representation of the JavaScript Error object that is thrown | ||
The `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown | ||
when runtime errors occur. The Error object can also be used as a base object for | ||
user-defined exceptions. | ||
The **Error** class is a persistent reference to a JavaScript error object thus | ||
inherits its behavior from the `ObjectReference` class (for more info see: [ObjectReference](object_reference.md)). | ||
The `Napi::Error` class is a persistent reference to a JavaScript error object thus | ||
inherits its behavior from the `Napi::ObjectReference` class (for more info see: [`Napi::ObjectReference`](object_reference.md)). | ||
If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the | ||
**Error** class extends `std::exception` and enables integrated | ||
`Napi::Error` class extends `std::exception` and enables integrated | ||
error-handling for C++ exceptions and JavaScript exceptions. | ||
@@ -20,37 +20,37 @@ | ||
Creates empty instance of an `Error` object for the specified environment. | ||
Creates empty instance of an `Napi::Error` object for the specified environment. | ||
```cpp | ||
Error::New(Napi:Env env); | ||
Napi::Error::New(Napi::Env env); | ||
``` | ||
- `[in] Env`: The environment in which to construct the Error object. | ||
- `[in] env`: The environment in which to construct the `Napi::Error` object. | ||
Returns an instance of `Error` object. | ||
Returns an instance of `Napi::Error` object. | ||
### New | ||
Creates instance of an `Error` object. | ||
Creates instance of an `Napi::Error` object. | ||
```cpp | ||
Error::New(Napi:Env env, const char* message); | ||
Napi::Error::New(Napi::Env env, const char* message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the Error object. | ||
- `[in] message`: Null-terminated string to be used as the message for the Error. | ||
- `[in] env`: The environment in which to construct the `Napi::Error` object. | ||
- `[in] message`: Null-terminated string to be used as the message for the `Napi::Error`. | ||
Returns instance of an `Error` object. | ||
Returns instance of an `Napi::Error` object. | ||
### New | ||
Creates instance of an `Error` object | ||
Creates instance of an `Napi::Error` object | ||
```cpp | ||
Error::New(Napi:Env env, const std::string& message); | ||
Napi::Error::New(Napi::Env env, const std::string& message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `Error` object. | ||
- `[in] message`: Reference string to be used as the message for the `Error`. | ||
- `[in] env`: The environment in which to construct the `Napi::Error` object. | ||
- `[in] message`: Reference string to be used as the message for the `Napi::Error`. | ||
Returns instance of an `Error` object. | ||
Returns instance of an `Napi::Error` object. | ||
@@ -63,3 +63,3 @@ ### Fatal | ||
```cpp | ||
static NAPI_NO_RETURN void Fatal(const char* location, const char* message); | ||
static NAPI_NO_RETURN void Napi::Error::Fatal(const char* location, const char* message); | ||
``` | ||
@@ -71,22 +71,22 @@ | ||
Creates empty instance of an `Error`. | ||
Creates empty instance of an `Napi::Error`. | ||
```cpp | ||
Error(); | ||
Napi::Error::Error(); | ||
``` | ||
Returns an instance of `Error` object. | ||
Returns an instance of `Napi::Error` object. | ||
### Constructor | ||
Initializes an `Error` instance from an existing JavaScript error object. | ||
Initializes an `Napi::Error` instance from an existing JavaScript error object. | ||
```cpp | ||
Error(napi_env env, napi_value value); | ||
Napi::Error::Error(napi_env env, napi_value value); | ||
``` | ||
- `[in] Env`: The environment in which to construct the Error object. | ||
- `[in] value`: The `Error` reference to wrap. | ||
- `[in] env`: The environment in which to construct the error object. | ||
- `[in] value`: The `Napi::Error` reference to wrap. | ||
Returns instance of an `Error` object. | ||
Returns instance of an `Napi::Error` object. | ||
@@ -96,3 +96,3 @@ ### Message | ||
```cpp | ||
std::string& Message() const NAPI_NOEXCEPT; | ||
std::string& Napi::Error::Message() const NAPI_NOEXCEPT; | ||
``` | ||
@@ -107,3 +107,3 @@ | ||
```cpp | ||
void ThrowAsJavaScriptException() const; | ||
void Napi::Error::ThrowAsJavaScriptException() const; | ||
``` | ||
@@ -116,3 +116,3 @@ | ||
```cpp | ||
const char* what() const NAPI_NOEXCEPT override; | ||
const char* Napi::Error::what() const NAPI_NOEXCEPT override; | ||
``` | ||
@@ -119,0 +119,0 @@ |
# EscapableHandleScope | ||
The EscapableHandleScope class is used to manage the lifetime of object handles | ||
The `Napi::EscapableHandleScope` class is used to manage the lifetime of object handles | ||
which are created through the use of node-addon-api. These handles | ||
@@ -10,7 +10,7 @@ keep an object alive in the heap in order to ensure that the objects | ||
An EscapableHandleScope is a special type of HandleScope | ||
The `Napi::EscapableHandleScope` is a special type of `Napi::HandleScope` | ||
which allows a single handle to be "promoted" to an outer scope. | ||
For more details refer to the section titled | ||
(Object lifetime management)[object_lifetime_management]. | ||
[Object lifetime management](object_lifetime_management.md). | ||
@@ -24,8 +24,8 @@ ## Methods | ||
```cpp | ||
EscapableHandleScope EscapableHandleScope::New(Napi:Env env); | ||
Napi::EscapableHandleScope Napi::EscapableHandleScope::New(Napi:Env env); | ||
``` | ||
- `[in] Env`: The environment in which to construct the EscapableHandleScope object. | ||
- `[in] Env`: The environment in which to construct the `Napi::EscapableHandleScope` object. | ||
Returns a new EscapableHandleScope | ||
Returns a new `Napi::EscapableHandleScope` | ||
@@ -37,3 +37,3 @@ ### Constructor | ||
```cpp | ||
EscapableHandleScope EscapableHandleScope::New(napi_env env, napi_handle_scope scope); | ||
Napi::EscapableHandleScope Napi::EscapableHandleScope::New(napi_env env, napi_handle_scope scope); | ||
``` | ||
@@ -44,3 +44,3 @@ | ||
Returns a new EscapableHandleScope instance which wraps the | ||
Returns a new `Napi::EscapableHandleScope` instance which wraps the | ||
napi_escapable_handle_scope handle passed in. This can be used | ||
@@ -52,6 +52,6 @@ to mix usage of the C N-API and node-addon-api. | ||
```cpp | ||
operator EscapableHandleScope::napi_escapable_handle_scope() const | ||
operator Napi::EscapableHandleScope::napi_escapable_handle_scope() const | ||
``` | ||
Returns the N-API napi_escapable_handle_scope wrapped by the EscapableHandleScope object. | ||
Returns the N-API napi_escapable_handle_scope wrapped by the `Napi::EscapableHandleScope` object. | ||
This can be used to mix usage of the C N-API and node-addon-api by allowing | ||
@@ -62,6 +62,6 @@ the class to be used be converted to a napi_escapable_handle_scope. | ||
```cpp | ||
~EscapableHandleScope(); | ||
Napi::EscapableHandleScope::~EscapableHandleScope(); | ||
``` | ||
Deletes the EscapableHandleScope instance and allows any objects/handles created | ||
Deletes the `Napi::EscapableHandleScope` instance and allows any objects/handles created | ||
in the scope to be collected by the garbage collector. There is no | ||
@@ -73,3 +73,3 @@ guarantee as to when the gargbage collector will do this. | ||
```cpp | ||
napi::Value EscapableHandleScope::Escape(napi_value escapee); | ||
napi::Value Napi::EscapableHandleScope::Escape(napi_value escapee); | ||
``` | ||
@@ -79,4 +79,4 @@ | ||
Returns Napi:Value which can be used in the outer scope. This method can | ||
be called at most once on a given EscapableHandleScope. If it is called | ||
Returns `Napi::Value` which can be used in the outer scope. This method can | ||
be called at most once on a given `Napi::EscapableHandleScope`. If it is called | ||
more than once an exception will be thrown. | ||
@@ -87,5 +87,5 @@ | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::EscapableHandleScope::Env() const; | ||
``` | ||
Returns the Napi:Env associated with the EscapableHandleScope. | ||
Returns the `Napi::Env` associated with the `Napi::EscapableHandleScope`. |
@@ -1,8 +0,6 @@ | ||
**WORK IN PROGRESS, NOT YET COMPLETE** | ||
# External (template) | ||
The External template class implements the ability to create a Value object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data. | ||
The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data. | ||
External objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your External object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function. | ||
`Napi::External` objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your `Napi::External` object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function. | ||
@@ -15,9 +13,9 @@ ## Methods | ||
template <typename T> | ||
static External New(napi_env env, T* data); | ||
static Napi::External Napi::External::New(napi_env env, T* data); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the External object. | ||
- `[in] data`: The arbitrary C++ data to be held by the External object. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object. | ||
- `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object. | ||
Returns the created `External<T>` object. | ||
Returns the created `Napi::External<T>` object. | ||
@@ -28,3 +26,3 @@ ### New | ||
template <typename T> | ||
static External New(napi_env env, | ||
static Napi::External Napi::External::New(napi_env env, | ||
T* data, | ||
@@ -34,7 +32,7 @@ Finalizer finalizeCallback); | ||
- `[in] env`: The `napi_env` environment in which to construct the External object. | ||
- `[in] data`: The arbitrary C++ data to be held by the External object. | ||
- `[in] finalizeCallback`: A function called when the External object is released by the garbage collector accepting a T* and returning void. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object. | ||
- `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object. | ||
- `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting a T* and returning void. | ||
Returns the created `External<T>` object. | ||
Returns the created `Napi::External<T>` object. | ||
@@ -45,3 +43,3 @@ ### New | ||
template <typename T> | ||
static External New(napi_env env, | ||
static Napi::External Napi::External::New(napi_env env, | ||
T* data, | ||
@@ -52,8 +50,8 @@ Finalizer finalizeCallback, | ||
- `[in] env`: The `napi_env` environment in which to construct the External object. | ||
- `[in] data`: The arbitrary C++ data to be held by the External object. | ||
- `[in] finalizeCallback`: A function called when the External object is released by the garbage collector accepting T* and Hint* parameters and returning void. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object. | ||
- `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object. | ||
- `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting T* and Hint* parameters and returning void. | ||
- `[in] finalizeHint`: A hint value passed to the `finalizeCallback` function. | ||
Returns the created `External<T>` object. | ||
Returns the created `Napi::External<T>` object. | ||
@@ -63,5 +61,5 @@ ### Data | ||
```cpp | ||
T* Data() const; | ||
T* Napi::External::Data() const; | ||
``` | ||
Returns a pointer to the arbitrary C++ data held by the External object. | ||
Returns a pointer to the arbitrary C++ data held by the `Napi::External` object. |
@@ -1,5 +0,238 @@ | ||
# Function reference | ||
# FunctionReference | ||
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/) | ||
`Napi::FunctionReference` is a subclass of [`Napi::Reference`](reference.md), and | ||
is equivalent to an instance of `Napi::Reference<Napi::Function>`. This means | ||
that a `Napi::FunctionReference` holds a [`Napi::Function`](function.md), and a | ||
count of the number of references to that `Napi::Function`. When the count is | ||
greater than 0, a `Napi::FunctionReference` is not eligible for garbage collection. | ||
This ensures that the `Function` will remain accessible, even if the original | ||
reference to it is no longer available. | ||
`Napi::FunctionReference` allows the referenced JavaScript function object to be | ||
called from a native add-on with two different methods: `Call` and `MakeCallback`. | ||
See the documentation for [`Napi::Function`](function.md) for when `Call` should | ||
be used instead of `MakeCallback` and vice-versa. | ||
The `Napi::FunctionReference` class inherits its behavior from the `Napi::Reference` | ||
class (for more info see: [`Napi::Reference`](reference.md)). | ||
## Methods | ||
### Weak | ||
Creates a "weak" reference to the value, in that the initial reference count is | ||
set to 0. | ||
```cpp | ||
static Napi::FunctionReference Napi::FunctionReference::Weak(const Napi::Function& value); | ||
``` | ||
- `[in] value`: The value which is to be referenced. | ||
Returns the newly created reference. | ||
### Persistent | ||
Creates a "persistent" reference to the value, in that the initial reference | ||
count is set to 1. | ||
```cpp | ||
static Napi::FunctionReference Napi::FunctionReference::Persistent(const Napi::Function& value); | ||
``` | ||
- `[in] value`: The value which is to be referenced. | ||
Returns the newly created reference. | ||
### Constructor | ||
Creates a new empty instance of `Napi::FunctionReference`. | ||
```cpp | ||
Napi::FunctionReference::FunctionReference(); | ||
``` | ||
### Constructor | ||
Creates a new instance of the `Napi::FunctionReference`. | ||
```cpp | ||
Napi::FunctionReference::FunctionReference(napi_env env, napi_ref ref); | ||
``` | ||
- `[in] env`: The environment in which to construct the `Napi::FunctionReference` object. | ||
- `[in] ref`: The N-API reference to be held by the `Napi::FunctionReference`. | ||
Returns a newly created `Napi::FunctionReference` object. | ||
### New | ||
Constructs a new instance by calling the constructor held by this reference. | ||
```cpp | ||
Napi::Object Napi::FunctionReference::New(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the contructor function. | ||
Returns a new JavaScript object. | ||
### New | ||
Constructs a new instance by calling the constructor held by this reference. | ||
```cpp | ||
Napi::Object Napi::FunctionReference::New(const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the constructor function. | ||
Returns a new JavaScript object. | ||
### Call | ||
Calls a referenced Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::Call(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the referenced function. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### Call | ||
Calls a referenced JavaScript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::Call(const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### Call | ||
Calls a referenced JavaScript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::Call(napi_value recv, const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the referenced function. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### Call | ||
Calls a referenced JavaScript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::Call(napi_value recv, const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### Call | ||
Calls a referenced JavaScript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::Call(napi_value recv, size_t argc, const napi_value* args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] argc`: The number of arguments passed to the referenced function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### MakeCallback | ||
Calls a referenced JavaScript function from a native add-on after an asynchronous | ||
operation. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args, napi_async_context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### MakeCallback | ||
Calls a referenced JavaScript function from a native add-on after an asynchronous | ||
operation. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, const std::vector<napi_value>& args, napi_async_context context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
### MakeCallback | ||
Calls a referenced JavaScript function from a native add-on after an asynchronous | ||
operation. | ||
```cpp | ||
Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, size_t argc, const napi_value* args, napi_async_context context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] argc`: The number of arguments passed to the referenced function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript object returned by the referenced | ||
function. | ||
## Operator | ||
```cpp | ||
Napi::Value operator ()(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of reference to JavaScript values as `napi_value` | ||
Returns a `Napi::Value` representing the JavaScript value returned by the referenced | ||
function. |
# Function | ||
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 `Napi::Function` class provides a set of methods for creating a function object in | ||
native code that can later be called from JavaScript. The created function is not | ||
automatically visible from JavaScript. Instead it needs to be part of the add-on's | ||
module exports or be returned by one of the module's exported functions. | ||
In addition the `Napi::Function` class also provides methods that can be used to call | ||
functions that were created in JavaScript and passed to the native add-on. | ||
The `Napi::Function` class inherits its behavior from the `Napi::Object` class (for more info | ||
see: [`Napi::Object`](object.md)). | ||
## Example | ||
```cpp | ||
#include <napi.h> | ||
using namespace Napi; | ||
Value Fn(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
// ... | ||
return String::New(env, "Hello World"); | ||
} | ||
Object Init(Env env, Object exports) { | ||
exports.Set(String::New(env, "fn"), Function::New(env, Fn)); | ||
} | ||
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
``` | ||
The above code can be used from JavaScript as follows: | ||
```js | ||
const addon = require('./addon'); | ||
addon.fn(); | ||
``` | ||
With the `Napi::Function` class it is possible to call a JavaScript function object | ||
from a native add-on with two different methods: `Call` and `MakeCallback`. | ||
The API of these two methods is very similar, but they are used in different | ||
contexts. The `MakeCallback` method is used to call from native code back into | ||
JavaScript after returning from an [asynchronous operation](async_operations.md) | ||
and in general in situations which don't have an existing JavaScript function on | ||
the stack. The `Call` method is used when there is already a JavaScript function | ||
on the stack (for example when running a native method called from JavaScript). | ||
## Methods | ||
### Constructor | ||
Creates a new empty instance of `Napi::Function`. | ||
```cpp | ||
Napi::Function::Function(); | ||
``` | ||
### Constructor | ||
Creates a new instance of the `Napi::Function` object. | ||
```cpp | ||
Napi::Function::Function(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. | ||
- `[in] value`: The `napi_value` which is a handle for a JavaScript function. | ||
Returns a non-empty `Napi::Function` instance. | ||
### New | ||
Creates an instance of a `Napi::Function` object. | ||
```cpp | ||
template <typename Callable> | ||
static Napi::Function Napi::Function::New(napi_env env, Callable cb, const char* utf8name = nullptr, void* data = nullptr); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. | ||
- `[in] cb`: Object that implements `Callable`. | ||
- `[in] utf8name`: Null-terminated string to be used as the name of the function. | ||
- `[in] data`: User-provided data context. This will be passed back into the | ||
function when invoked later. | ||
Returns an instance of a `Napi::Function` object. | ||
### New | ||
```cpp | ||
template <typename Callable> | ||
static Napi::Function Napi::Function::New(napi_env env, Callable cb, const std::string& utf8name, void* data = nullptr); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. | ||
- `[in] cb`: Object that implements `Callable`. | ||
- `[in] utf8name`: String to be used as the name of the function. | ||
- `[in] data`: User-provided data context. This will be passed back into the | ||
function when invoked later. | ||
Returns an instance of a `Napi::Function` object. | ||
### New | ||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
```cpp | ||
Napi::Object Napi::Function::New(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the contructor function. | ||
Returns a new JavaScript object. | ||
### New | ||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
```cpp | ||
Napi::Object Napi::Function::New(const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the constructor function. | ||
Returns a new JavaScript object. | ||
### New | ||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
```cpp | ||
Napi::Object Napi::Function::New(size_t argc, const napi_value* args) const; | ||
``` | ||
- `[in] argc`: The number of the arguments passed to the contructor function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the constructor function. | ||
Returns a new JavaScript object. | ||
### Call | ||
Calls a Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### Call | ||
Calls a JavaScript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### Call | ||
Calls a Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(size_t argc, const napi_value* args) const; | ||
``` | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### Call | ||
Calls a Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(napi_value recv, const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### Call | ||
Calls a Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(napi_value recv, const std::vector<napi_value>& args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### Call | ||
Calls a Javascript function from a native add-on. | ||
```cpp | ||
Napi::Value Napi::Function::Call(napi_value recv, size_t argc, const napi_value* args) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### MakeCallback | ||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
```cpp | ||
Napi::Value Napi::Function::MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args, napi_async_context context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### MakeCallback | ||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
```cpp | ||
Napi::Value Napi::Function::MakeCallback(napi_value recv, const std::vector<napi_value>& args, napi_async_context context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: List of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
### MakeCallback | ||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
```cpp | ||
Napi::Value Napi::Function::MakeCallback(napi_value recv, size_t argc, const napi_value* args, napi_async_context context = nullptr) const; | ||
``` | ||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the current async context | ||
(if any) is to be used for the callback. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. | ||
## Operator | ||
```cpp | ||
Napi::Value Napi::Function::operator ()(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value`. | ||
Returns a `Napi::Value` representing the JavaScript value returned by the function. |
@@ -9,3 +9,3 @@ # HandleScope | ||
of its subclasses is created or returned. For more details refer to | ||
the section titled (Object lifetime management)[object_lifetime_management]. | ||
the section titled [Object lifetime management](object_lifetime_management.md). | ||
@@ -19,10 +19,9 @@ ## Methods | ||
```cpp | ||
HandleScope(Napi:Env env); | ||
Napi::HandleScope::HandleScope(Napi::Env env); | ||
``` | ||
- `[in] env`: The environment in which to construct the HandleScope object. | ||
- `[in] env`: The environment in which to construct the `Napi::HandleScope` object. | ||
Returns a new HandleScope | ||
Returns a new `Napi::HandleScope` | ||
### Constructor | ||
@@ -33,9 +32,9 @@ | ||
```cpp | ||
HandleScope(Napi::Env env, Napi::HandleScope scope); | ||
Napi::HandleScope::HandleScope(Napi::Env env, Napi::HandleScope scope); | ||
``` | ||
- `[in] env`: Napi::Env in which the scope passed in was created. | ||
- `[in] scope`: pre-existing Napi::HandleScope. | ||
- `[in] env`: `Napi::Env` in which the scope passed in was created. | ||
- `[in] scope`: pre-existing `Napi::HandleScope`. | ||
Returns a new HandleScope instance which wraps the napi_handle_scope | ||
Returns a new `Napi::HandleScope` instance which wraps the napi_handle_scope | ||
handle passed in. This can be used to mix usage of the C N-API | ||
@@ -47,6 +46,6 @@ and node-addon-api. | ||
```cpp | ||
operator napi_handle_scope() const | ||
operator Napi::HandleScope::napi_handle_scope() const | ||
``` | ||
Returns the N-API napi_handle_scope wrapped by the EscapableHandleScope object. | ||
Returns the N-API napi_handle_scope wrapped by the `Napi::EscapableHandleScope` object. | ||
This can be used to mix usage of the C N-API and node-addon-api by allowing | ||
@@ -57,6 +56,6 @@ the class to be used be converted to a napi_handle_scope. | ||
```cpp | ||
~HandleScope(); | ||
Napi::HandleScope::~HandleScope(); | ||
``` | ||
Deletes the HandleScope instance and allows any objects/handles created | ||
Deletes the `Napi::HandleScope` instance and allows any objects/handles created | ||
in the scope to be collected by the garbage collector. There is no | ||
@@ -68,5 +67,5 @@ guarantee as to when the gargbage collector will do this. | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::HandleScope::Env() const; | ||
``` | ||
Returns the Napi:Env associated with the HandleScope. | ||
Returns the `Napi::Env` associated with the `Napi::HandleScope`. |
# MemoryManagement | ||
The `MemoryManagement` class contains functions that give the JavaScript engine | ||
The `Napi::MemoryManagement` class contains functions that give the JavaScript engine | ||
an indication of the amount of externally allocated memory that is kept alive by | ||
@@ -20,3 +20,3 @@ JavaScript objects. | ||
```cpp | ||
static int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in_bytes); | ||
static int64_t Napi::MemoryManagement::AdjustExternalMemory(Napi::Env env, int64_t change_in_bytes); | ||
``` | ||
@@ -23,0 +23,0 @@ |
# Number | ||
A Javascript number value. | ||
`Napi::Number` class is a representation of the JavaScript `Number` object. The | ||
`Napi::Number` class inherits its behavior from `Napi::Value` class | ||
(for more info see [`Napi::Value`](value.md)) | ||
## Methods | ||
### Constructor | ||
Creates a new _empty_ instance of a `Napi::Number` object. | ||
```cpp | ||
Napi::Number::New(Napi::Env env, double value); | ||
Napi::Number(); | ||
``` | ||
- `[in] env`: The `napi_env` Environment | ||
- `[in] value`: The value the Javascript Number will contain | ||
Returns a new _empty_ `Napi::Number` object. | ||
### Contructor | ||
Creates a new instance of a `Napi::Number` object. | ||
```cpp | ||
Napi::Number(); | ||
Napi::Number(napi_env env, napi_value value); | ||
``` | ||
returns a new empty Javascript Number | ||
You can easily cast a Javascript number to one of: | ||
- int32_t | ||
- uint32_t | ||
- int64_t | ||
- float | ||
- double | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object. | ||
- `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`. | ||
The following shows an example of casting a number to an uint32_t value. | ||
Returns a non-empty `Napi::Number` object. | ||
### New | ||
Creates a new instance of a `Napi::Number` object. | ||
```cpp | ||
uint32_t operatorVal = Number::New(Env(), 10.0); // Number to unsigned 32 bit integer | ||
Napi::Number Napi::Number::New(napi_env env, double value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object. | ||
- `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`. | ||
Creates a new instance of a `Napi::Number` object. | ||
### Int32Value | ||
Converts a `Napi::Number` value to a `uint32_t` primitive type. | ||
```cpp | ||
Napi::Number::Int32Value() const; | ||
``` | ||
Returns the `int32_t` primitive type of the corresponding `Napi::Number` object. | ||
### Uint32Value | ||
Converts a `Napi::Number` value to a `uint32_t` primitive type. | ||
```cpp | ||
Napi::Number::Uint32Value() const; | ||
``` | ||
Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object. | ||
### Int64Value | ||
Converts a `Napi::Number` value to a `int64_t` primitive type. | ||
```cpp | ||
Napi::Number::Int64Value() const; | ||
``` | ||
Returns the `int64_t` primitive type of the corresponding `Napi::Number` object. | ||
### FloatValue | ||
Converts a `Napi::Number` value to a `float` primitive type. | ||
```cpp | ||
Napi::Number::FloatValue() const; | ||
``` | ||
Returns the `float` primitive type of the corresponding `Napi::Number` object. | ||
### DoubleValue | ||
Converts a `Napi::Number` value to a `double` primitive type. | ||
```cpp | ||
Napi::Number::DoubleValue() const; | ||
``` | ||
Returns the `double` primitive type of the corresponding `Napi::Number` object. | ||
## Operators | ||
The `Napi::Number` class contains a set of operators to easily cast JavaScript | ||
`Number` object to one of the following primitive types: | ||
- `int32_t` | ||
- `uint32_t` | ||
- `int64_t` | ||
- `float` | ||
- `double` | ||
### operator int32_t | ||
Converts a `Napi::Number` value to a `int32_t` primitive. | ||
```cpp | ||
Napi::Number::operator int32_t() const; | ||
``` | ||
Returns the `int32_t` primitive type of the corresponding `Napi::Number` object. | ||
### operator uint32_t | ||
Converts a `Napi::Number` value to a `uint32_t` primitive type. | ||
```cpp | ||
Napi::Number::operator uint32_t() const; | ||
``` | ||
Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object. | ||
### operator int64_t | ||
Converts a `Napi::Number` value to a `int64_t` primitive type. | ||
```cpp | ||
Napi::Number::operator int64_t() const; | ||
``` | ||
Returns the `int64_t` primitive type of the corresponding `Napi::Number` object. | ||
### operator float | ||
Converts a `Napi::Number` value to a `float` primitive type. | ||
```cpp | ||
Napi::Number::operator float() const; | ||
``` | ||
Returns the `float` primitive type of the corresponding `Napi::Number` object. | ||
### operator double | ||
Converts a `Napi::Number` value to a `double` primitive type. | ||
```cpp | ||
Napi::Number::operator double() const; | ||
``` | ||
Returns the `double` primitive type of the corresponding `Napi::Number` object. | ||
### Example | ||
The following shows an example of casting a number to an `uint32_t` value. | ||
```cpp | ||
uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer | ||
// or | ||
auto instanceVal = info[0].As<Number>().Uint32Value(); | ||
auto instanceVal = info[0].As<Napi::Number>().Uint32Value(); | ||
``` |
@@ -37,3 +37,3 @@ # Object lifetime management | ||
std::string name = std::string("inner-scope") + std::to_string(i); | ||
Value newValue = String::New(info.Env(), name.c_str()); | ||
Napi::Value newValue = Napi::String::New(info.Env(), name.c_str()); | ||
// do something with newValue | ||
@@ -51,4 +51,4 @@ }; | ||
handles are no longer required, the scope can be deleted and any handles | ||
associated with the scope are invalidated. The `HandleScope` | ||
and `EscapableHandleScope` classes are provided by node-addon-api for | ||
associated with the scope are invalidated. The `Napi::HandleScope` | ||
and `Napi::EscapableHandleScope` classes are provided by node-addon-api for | ||
creating additional scopes. | ||
@@ -61,7 +61,7 @@ | ||
a native method must be deleted before returning from that method. Since | ||
HandleScopes are typically stack allocated the compiler will take care of | ||
`Napi::HandleScopes` are typically stack allocated the compiler will take care of | ||
deletion, however, care must be taken to create the scope in the right | ||
place such that you achieve the desired lifetime. | ||
Taking the earlier example, creating a HandleScope in the innner loop | ||
Taking the earlier example, creating a `Napi::HandleScope` in the innner loop | ||
would ensure that at most a single new value is held alive throughout the | ||
@@ -72,5 +72,5 @@ execution of the loop: | ||
for (int i = 0; i < LOOP_MAX; i++) { | ||
HandleScope scope(info.Env()); | ||
Napi::HandleScope scope(info.Env()); | ||
std::string name = std::string("inner-scope") + std::to_string(i); | ||
Value newValue = String::New(info.Env(), name.c_str()); | ||
Napi::Value newValue = Napi::String::New(info.Env(), name.c_str()); | ||
// do something with neValue | ||
@@ -82,7 +82,7 @@ }; | ||
inner scope needs to live beyond the lifespan of that scope. node-addon-api | ||
provides the `EscapableHandleScope` with the Escape method | ||
provides the `Napi::EscapableHandleScope` with the `Escape` method | ||
in order to support this case. An escapable scope | ||
allows one object to be 'promoted' so that it 'escapes' the | ||
current scope and the lifespan of the handle changes from the current | ||
scope to that of the outer scope. The Escape method can only be called | ||
once for a given EscapableHandleScope. | ||
scope to that of the outer scope. The `Escape` method can only be called | ||
once for a given `Napi::EscapableHandleScope`. |
# Object Reference | ||
ObjectReference is a subclass of [Reference](reference.md), and is equivalent to an instance of `Reference<Object>`. This means that an ObjectReference holds an [Object](object.md), and a count of the number of references to that Object. When the count is greater than 0, an ObjectReference is not eligible for garbage collection. This ensures that the Object being held as a value of the ObjectReference will remain accessible, even if the original Object no longer is. However, ObjectReference is unique from a Reference since properties can be set and get to the Object itself that can be accessed through the ObjectReference. | ||
`Napi::ObjectReference` is a subclass of [`Napi::Reference`](reference.md), and is equivalent to an instance of `Napi::Reference<Object>`. This means that a `Napi::ObjectReference` holds a [`Napi::Object`](object.md), and a count of the number of references to that Object. When the count is greater than 0, an ObjectReference is not eligible for garbage collection. This ensures that the Object being held as a value of the ObjectReference will remain accessible, even if the original Object no longer is. However, ObjectReference is unique from a Reference since properties can be set and get to the Object itself that can be accessed through the ObjectReference. | ||
For more general information on references, please consult [Reference](referenc.md). | ||
For more general information on references, please consult [`Napi::Reference`](referenc.md). | ||
@@ -33,6 +33,6 @@ ## Example | ||
```cpp | ||
static ObjectReference New(const Object& value, uint32_t initialRefcount = 0); | ||
static Napi::ObjectReference Napi::ObjectReference::New(const Napi::Object& value, uint32_t initialRefcount = 0); | ||
``` | ||
* `[in] value`: The Object which is to be referenced. | ||
* `[in] value`: The `Napi::Object` which is to be referenced. | ||
@@ -44,3 +44,3 @@ * `[in] initialRefcount`: The initial reference count. | ||
```cpp | ||
static ObjectReference Weak(const Object& value); | ||
static Napi::ObjectReference Napi::ObjectReference::Weak(const Napi::Object& value); | ||
``` | ||
@@ -55,3 +55,3 @@ | ||
```cpp | ||
static ObjectReference Persistent(const Object& value); | ||
static Napi::ObjectReference Napi::ObjectReference::Persistent(const Napi::Object& value); | ||
``` | ||
@@ -68,6 +68,6 @@ | ||
```cpp | ||
ObjectReference(); | ||
Napi::ObjectReference::ObjectReference(); | ||
``` | ||
Returns a new _empty_ ObjectReference instance. | ||
Returns a new _empty_ `Napi::ObjectReference` instance. | ||
@@ -77,8 +77,8 @@ ### Constructor | ||
```cpp | ||
ObjectReference(napi_env env, napi_value value); | ||
Napi::ObjectReference::ObjectReference(napi_env env, napi_value value); | ||
``` | ||
* `[in] env`: The `napi_env` environment in which to construct the ObjectReference object. | ||
* `[in] env`: The `napi_env` environment in which to construct the `Napi::ObjectReference` object. | ||
* `[in] value`: The N-API primitive value to be held by the ObjectReference. | ||
* `[in] value`: The N-API primitive value to be held by the `Napi::ObjectReference`. | ||
@@ -89,3 +89,3 @@ Returns the newly created reference. | ||
```cpp | ||
void Set(___ key, ___ value); | ||
void Napi::ObjectReference::Set(___ key, ___ value); | ||
``` | ||
@@ -112,3 +112,3 @@ | ||
```cpp | ||
Value Get(___ key); | ||
Napi::Value Napi::ObjectReference::Get(___ key); | ||
``` | ||
@@ -118,3 +118,3 @@ | ||
Returns the [Value](value.md) associated with the key property. Returns NULL if no such key exists. | ||
Returns the [`Napi::Value`](value.md) associated with the key property. Returns NULL if no such key exists. | ||
@@ -121,0 +121,0 @@ The `key` can be any of the following types: |
@@ -1,13 +0,546 @@ | ||
## Object Wrap | ||
# Object Wrap | ||
The ```ObjectWrap``` class can be used to expose C++ code to JavaScript. To do | ||
this you need to extend the ObjectWrap class that contain all the plumbing to connect | ||
JavaScript code to a C++ object. | ||
Classes extending ```ObjectWrap``` can be instantiated from JavaScript using the | ||
**new** operator, and their methods can be directly invoked from JavaScript. | ||
The **wrap** word refers to a way to group methods and state of your class because it | ||
will be your responsibility write custom code to bridge each of your C++ class methods. | ||
The `Napi::ObjectWrap` class is used to bind the lifetime of C++ code to a | ||
JavaScript object. Once bound, each time an instance of the JavaScript object | ||
is created, an instance of the C++ class will also be created. When a method | ||
is called on the JavaScript object which is defined as an InstanceMethod, the | ||
corresponding C++ method on the wrapped C++ class will be invoked. | ||
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/) | ||
In order to create a wrapper it's necessary to extend the | ||
`Napi::ObjectWrap`class which contains all the plumbing to connect JavaScript code | ||
with a C++ object. Classes extending `Napi::ObjectWrap` can be instantiated from | ||
JavaScript using the **new** operator, and their methods can be directly invoked | ||
from JavaScript. The **wrap** word refers to a way of grouping methods and state | ||
of the class because it will be necessary write custom code to bridge each of | ||
your C++ class methods. | ||
## Example | ||
```cpp | ||
#include <napi.h> | ||
class Example : public Napi::ObjectWrap<Example> { | ||
public: | ||
static Napi::Object Init(Napi::Env env, Napi::Object exports); | ||
Example(const Napi::CallbackInfo &info); | ||
private: | ||
static Napi::FunctionReference constructor; | ||
double _value; | ||
Napi::Value GetValue(const Napi::CallbackInfo &info); | ||
Napi::Value SetValue(const Napi::CallbackInfo &info); | ||
}; | ||
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) { | ||
// This method is used to hook the accessor and method callbacks | ||
Napi::Function func = DefineClass(env, "Example", { | ||
InstanceMethod("GetValue", &Example::GetValue), | ||
InstanceMethod("SetValue", &Example::SetValue) | ||
}); | ||
// Create a peristent reference to the class constructor. This will allow | ||
// a function called on a class prototype and a function | ||
// called on instance of a class to be distinguished from each other. | ||
constructor = Napi::Persistent(func); | ||
// Call the SuppressDestruct() method on the static data prevent the calling | ||
// to this destructor to reset the reference when the environment is no longer | ||
// available. | ||
constructor.SuppressDestruct(); | ||
exports.Set("Example", func); | ||
return exports; | ||
} | ||
Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) { | ||
Napi::Env env = info.Env(); | ||
// ... | ||
Napi::Number value = info[0].As<Napi::Number>(); | ||
this->_value = value.DoubleValue(); | ||
} | ||
Napi::FunctionReference Example::constructor; | ||
Napi::Value Example::GetValue(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
return Napi::Number::New(env, this->_value); | ||
} | ||
Napi::Value Example::SetValue(const Napi::CallbackInfo &info){ | ||
Napi::Env env = info.Env(); | ||
// ... | ||
Napi::Number value = info[0].As<Napi::Number>(); | ||
this->_value = value.DoubleValue(); | ||
return this->GetValue(info); | ||
} | ||
// Initialize native add-on | ||
Napi::Object Init (Napi::Env env, Napi::Object exports) { | ||
Example::Init(env, exports); | ||
return exports; | ||
} | ||
// Regisgter and initialize native add-on | ||
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
``` | ||
The above code can be used from JavaScript as follows: | ||
```js | ||
'use strict' | ||
const { Example } = require('bindings')('addon') | ||
const example = new Example(11) | ||
console.log(example.GetValue()) | ||
// It prints 11 | ||
example.SetValue(19) | ||
console.log(example.GetValue()); | ||
// It prints 19 | ||
``` | ||
At initialization time, the `Napi::ObjectWrap::DefineClass()` method must be used | ||
to hook up the accessor and method callbacks. It takes a list of property | ||
descriptors, which can be constructed via the various static methods on the base | ||
class. | ||
When JavaScript code invokes the constructor, the constructor callback will create | ||
a new C++ instance and "wrap" it into the newly created JavaScript object. | ||
When JavaScript code invokes a method or a property accessor on the class the | ||
corresponding C++ callback function will be executed. | ||
For a wrapped object it could be difficult to distinguish between a function called | ||
on a class prototype and a function called on instance of a class. Therefore it is | ||
good practice to save a persistent reference to the class constructor. This allows | ||
the two cases to be distinguished from each other by checking the this object | ||
against the class constructor. | ||
## Methods | ||
### Contructor | ||
Creates a new instance of a JavaScript object that wraps native instance. | ||
```cpp | ||
Napi::ObjectWrap(const Napi::CallbackInfo& callbackInfo); | ||
``` | ||
- `[in] callbackInfo`: The object representing the components of the JavaScript | ||
request being made. | ||
### Unwrap | ||
Retrieves a native instance wrapped in a JavaScript object. | ||
```cpp | ||
static T* Napi::ObjectWrap::Unwrap(Napi::Object wrapper); | ||
``` | ||
* `[in] wrapper`: The JavaScript object that wraps the native instance. | ||
Returns a native instace wrapped in a JavaScript object. Given the | ||
Napi:Object, this allows a method to get a pointer to the wrapped | ||
C++ object and then reference fields, call methods, etc. within that class. | ||
In many cases calling Unwrap is not required, as methods can | ||
use the `this` field for ObjectWrap when running in a method on a | ||
class that extends ObjectWrap. | ||
### DefineClass | ||
Defnines a JavaScript class with constructor, static and instance properties and | ||
methods. | ||
```cpp | ||
static Napi::Function Napi::ObjectWrap::DefineClass(Napi::Env env, | ||
const char* utf8name, | ||
const std::initializer_list<PropertyDescriptor>& properties, | ||
void* data = nullptr); | ||
``` | ||
* `[in] env`: The environment in which to construct a JavaScript class. | ||
* `[in] utf8name`: Null-terminated string that represents the name of the | ||
JavaScript constructor function. | ||
* `[in] properties`: Initializer list of class property descriptor describing | ||
static and instance properties and methods of the class. | ||
See: [`Class propertry and descriptor`](class_property_descriptor.md). | ||
* `[in] data`: User-provided data passed to the constructor callback as `data` | ||
property of the `Napi::CallbackInfo`. | ||
Returns a `Napi::Function` representing the constructor function for the class. | ||
### DefineClass | ||
Defnines a JavaScript class with constructor, static and instance properties and | ||
methods. | ||
```cpp | ||
static Napi::Function Napi::ObjectWrap::DefineClass(Napi::Env env, | ||
const char* utf8name, | ||
const std::vector<PropertyDescriptor>& properties, | ||
void* data = nullptr); | ||
``` | ||
* `[in] env`: The environment in which to construct a JavaScript class. | ||
* `[in] utf8name`: Null-terminated string that represents the name of the | ||
JavaScript constructor function. | ||
* `[in] properties`: Vector of class property descriptor describing static and | ||
instance properties and methods of the class. | ||
See: [`Class propertry and descriptor`](class_property_descriptor.md). | ||
* `[in] data`: User-provided data passed to the constructor callback as `data` | ||
property of the `Napi::CallbackInfo`. | ||
Returns a `Napi::Function` representing the constructor function for the class. | ||
### StaticMethod | ||
Creates property descriptor that represents a static method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name, | ||
StaticVoidMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of a static | ||
method for the class. | ||
- `[in] method`: The native function that represents a static method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents the static method of a | ||
JavaScript class. | ||
### StaticMethod | ||
Creates property descriptor that represents a static method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(const char* utf8name, | ||
StaticMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of a static | ||
method for the class. | ||
- `[in] method`: The native function that represents a static method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents a static method of a | ||
JavaScript class. | ||
### StaticMethod | ||
Creates property descriptor that represents a static method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, | ||
StaticVoidMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] name`: Napi:Symbol that represents the name of a static | ||
method for the class. | ||
- `[in] method`: The native function that represents a static method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents the static method of a | ||
JavaScript class. | ||
### StaticMethod | ||
Creates property descriptor that represents a static method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name, | ||
StaticMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
method for the class. | ||
- `[in] name`: Napi:Symbol that represents the name of a static. | ||
- `[in] method`: The native function that represents a static method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents a static method of a | ||
JavaScript class. | ||
### StaticAccessor | ||
Creates property descriptor that represents a static accessor property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(const char* utf8name, | ||
StaticGetterCallback getter, | ||
StaticSetterCallback setter, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of a static | ||
accessor property for the class. | ||
- `[in] getter`: The native function to call when a get access to the property of | ||
a JavaScript class is performed. | ||
- `[in] setter`: The native function to call when a set access to the property of | ||
a JavaScript class is performed. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into getter or setter when | ||
is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents a static accessor | ||
property of a JavaScript class. | ||
### StaticAccessor | ||
Creates property descriptor that represents a static accessor property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name, | ||
StaticGetterCallback getter, | ||
StaticSetterCallback setter, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] name`: Napi:Symbol that represents the name of a static accessor. | ||
- `[in] getter`: The native function to call when a get access to the property of | ||
a JavaScript class is performed. | ||
- `[in] setter`: The native function to call when a set access to the property of | ||
a JavaScript class is performed. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into getter or setter when | ||
is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents a static accessor | ||
property of a JavaScript class. | ||
### InstanceMethod | ||
Creates property descriptor that represents an instance method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name, | ||
InstanceVoidMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of an instance | ||
method for the class. | ||
- `[in] method`: The native function that represents an instance method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance method of a | ||
JavaScript class. | ||
### InstanceMethod | ||
Creates property descriptor that represents an instance method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(const char* utf8name, | ||
InstanceMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of an instance | ||
method for the class. | ||
- `[in] method`: The native function that represents an instance method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance method of a | ||
JavaScript class. | ||
### InstanceMethod | ||
Creates property descriptor that represents an instance method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name, | ||
InstanceVoidMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the | ||
instance method for the class. | ||
- `[in] method`: The native function that represents an instance method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance method of a | ||
JavaScript class. | ||
### InstanceMethod | ||
Creates property descriptor that represents an instance method of a JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceMethod(Napi::Symbol name, | ||
InstanceMethodCallback method, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the | ||
instance method for the class. | ||
- `[in] method`: The native function that represents an instance method of a | ||
JavaScript class. | ||
- `[in] attributes`: The attributes associated with a particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into method when it is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance method of a | ||
JavaScript class. | ||
### InstanceAccessor | ||
Creates property descriptor that represents an instance accessor property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(const char* utf8name, | ||
InstanceGetterCallback getter, | ||
InstanceSetterCallback setter, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of an instance | ||
accessor property for the class. | ||
- `[in] getter`: The native function to call when a get access to the property of | ||
a JavaScript class is performed. | ||
- `[in] setter`: The native function to call when a set access to the property of | ||
a JavaScript class is performed. | ||
- `[in] attributes`: The attributes associated with the particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into getter or setter when this is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance accessor | ||
property of a JavaScript class. | ||
### InstanceAccessor | ||
Creates property descriptor that represents an instance accessor property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(Symbol name, | ||
InstanceGetterCallback getter, | ||
InstanceSetterCallback setter, | ||
napi_property_attributes attributes = napi_default, | ||
void* data = nullptr); | ||
``` | ||
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the | ||
instance accessor. | ||
- `[in] getter`: The native function to call when a get access to the property of | ||
a JavaScript class is performed. | ||
- `[in] setter`: The native function to call when a set access to the property of | ||
a JavaScript class is performed. | ||
- `[in] attributes`: The attributes associated with the particular property. | ||
One or more of `napi_property_attributes`. | ||
- `[in] data`: User-provided data passed into getter or setter when this is invoked. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance accessor | ||
property of a JavaScript class. | ||
### StaticValue | ||
Creates property descriptor that represents an static value property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(const char* utf8name, | ||
Napi::Value value, | ||
napi_property_attributes attributes = napi_default); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of the static | ||
property. | ||
- `[in] value`: The value that's retrieved by a get access of the property. | ||
- `[in] attributes`: The attributes to be associated with the property in addition | ||
to the napi_static attribute. One or more of `napi_property_attributes`. | ||
Returns `Napi::PropertyDescriptor` object that represents an static value | ||
property of a JavaScript class | ||
### StaticValue | ||
Creates property descriptor that represents an static value property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(Symbol name, | ||
Napi::Value value, | ||
napi_property_attributes attributes = napi_default); | ||
``` | ||
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the | ||
name of the static property. | ||
- `[in] value`: The value that's retrieved by a get access of the property. | ||
- `[in] attributes`: The attributes to be associated with the property in addition | ||
to the napi_static attribute. One or more of `napi_property_attributes`. | ||
Returns `Napi::PropertyDescriptor` object that represents an static value | ||
property of a JavaScript class | ||
### InstanceValue | ||
Creates property descriptor that represents an instance value property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(const char* utf8name, | ||
Napi::Value value, | ||
napi_property_attributes attributes = napi_default); | ||
``` | ||
- `[in] utf8name`: Null-terminated string that represents the name of the property. | ||
- `[in] value`: The value that's retrieved by a get access of the property. | ||
- `[in] attributes`: The attributes to be associated with the property. | ||
One or more of `napi_property_attributes`. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance value | ||
property of a JavaScript class. | ||
### InstanceValue | ||
Creates property descriptor that represents an instance value property of a | ||
JavaScript class. | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(Symbol name, | ||
Napi::Value value, | ||
napi_property_attributes attributes = napi_default); | ||
``` | ||
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the | ||
name of the property. | ||
- `[in] value`: The value that's retrieved by a get access of the property. | ||
- `[in] attributes`: The attributes to be associated with the property. | ||
One or more of `napi_property_attributes`. | ||
Returns `Napi::PropertyDescriptor` object that represents an instance value |
# Object | ||
The Object class corresponds to a JavaScript object. It is extended by the following node-addon-api classes that you may use when working with more specific types: | ||
The `Napi::Object` class corresponds to a JavaScript object. It is extended by the following node-addon-api classes that you may use when working with more specific types: | ||
- [Value](value.md) and extends [Array](array.md) | ||
- [ArrayBuffer](array_buffer.md) | ||
- [Buffer<T>](buffer.md) | ||
- [Function](function.md) | ||
- [TypedArray](typed_array.md). | ||
- [`Napi::Value`](value.md) and extends [`Napi::Array`](array.md) | ||
- [`Napi::ArrayBuffer`](array_buffer.md) | ||
- [`Napi::Buffer<T>`](buffer.md) | ||
- [`Napi::Function`](function.md) | ||
- [`Napi::TypedArray`](typed_array.md). | ||
@@ -65,6 +65,6 @@ This class provides a number of convenience methods, most of which are used to set or get properties on a JavaScript object. For example, Set() and Get(). | ||
- std::u16string | ||
- napi::Value | ||
- Napi::Value | ||
- napi_value | ||
Creates a non-empty Object instance. | ||
Creates a non-empty `Napi::Object` instance. | ||
@@ -74,7 +74,7 @@ ### New() | ||
```cpp | ||
Object Napi::Object::New(napi_env env); | ||
Napi::Object Napi::Object::New(napi_env env); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the Value object. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object. | ||
Creates a new Object value. | ||
Creates a new `Napi::Object` value. | ||
@@ -93,3 +93,3 @@ ### Set() | ||
- `napi_value` | ||
- [Value](value.md) | ||
- [`Napi::Value`](value.md) | ||
- `const char*` | ||
@@ -101,3 +101,3 @@ - `const std::string&` | ||
- `napi_value` | ||
- [Value](value.md) | ||
- [`Napi::Value`](value.md) | ||
- `const char*` | ||
@@ -111,11 +111,11 @@ - `std::string&` | ||
```cpp | ||
Value Napi::Object::Get(____ key); | ||
Napi::Value Napi::Object::Get(____ key); | ||
``` | ||
- `[in] key`: The name of the property to return the value for. | ||
Returns the [Value](value.md) associated with the key property. Returns NULL if no such key exists. | ||
Returns the [`Napi::Value`](value.md) associated with the key property. Returns NULL if no such key exists. | ||
The `key` can be any of the following types: | ||
- `napi_value` | ||
- [Value](value.md) | ||
- [`Napi::Value`](value.md) | ||
- `const char *` | ||
@@ -139,5 +139,5 @@ - `const std::string &` | ||
``` | ||
- `[in] constructor`: The constructor [Function](function.md) of the value that is being compared with the object. | ||
- `[in] constructor`: The constructor [`Napi::Function`](function.md) of the value that is being compared with the object. | ||
Returns a `bool` that is true if the Object is an instance created by the `constructor` and false otherwise. | ||
Returns a `bool` that is true if the `Napi::Object` is an instance created by the `constructor` and false otherwise. | ||
@@ -149,5 +149,5 @@ Note: This is equivalent to the JavaScript instanceof operator. | ||
```cpp | ||
void Napi::Object::DefineProperty (const PropertyDescriptor& property); | ||
void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property); | ||
``` | ||
- `[in] property`: A [PropertyDescriptor](propertydescriptor.md). | ||
- `[in] property`: A [`Napi::PropertyDescriptor`](propertydescriptor.md). | ||
@@ -161,5 +161,5 @@ Define a property on the object. | ||
``` | ||
- `[in] properties`: A list of [PropertyDescriptor](propertydescriptor.md). Can be one of the following types: | ||
- const std::initializer_list<PropertyDescriptor>& | ||
- const std::vector<PropertyDescriptor>& | ||
- `[in] properties`: A list of [`Napi::PropertyDescriptor`](propertydescriptor.md). Can be one of the following types: | ||
- const std::initializer_list<Napi::PropertyDescriptor>& | ||
- const std::vector<Napi::PropertyDescriptor>& | ||
@@ -171,41 +171,41 @@ Defines properties on the object. | ||
```cpp | ||
PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name); | ||
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const char* utf8name); | ||
``` | ||
- `[in] utf8name`: UTF-8 encoded null-terminated property name. | ||
Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property. | ||
Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property. | ||
```cpp | ||
PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name); | ||
Napi::PropertyLValue<std::string> Napi::Object::operator[] (const std::string& utf8name); | ||
``` | ||
- `[in] utf8name`: UTF-8 encoded property name. | ||
Returns a [PropertyLValue](propertylvalue.md) as the named property or sets the named property. | ||
Returns a [`Napi::PropertyLValue`](propertylvalue.md) as the named property or sets the named property. | ||
```cpp | ||
PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index); | ||
Napi::PropertyLValue<uint32_t> Napi::Object::operator[] (uint32_t index); | ||
``` | ||
- `[in] index`: Element index. | ||
Returns a [PropertyLValue](propertylvalue.md) or sets an indexed property or array element. | ||
Returns a [`Napi::PropertyLValue`](propertylvalue.md) or sets an indexed property or array element. | ||
```cpp | ||
Value Napi::Object::operator[] (const char* utf8name) const; | ||
Napi::Value Napi::Object::operator[] (const char* utf8name) const; | ||
``` | ||
- `[in] utf8name`: UTF-8 encoded null-terminated property name. | ||
Returns the named property as a [Value](value.md). | ||
Returns the named property as a [`Napi::Value`](value.md). | ||
```cpp | ||
Value Napi::Object::operator[] (const std::string& utf8name) const; | ||
Napi::Value Napi::Object::operator[] (const std::string& utf8name) const; | ||
``` | ||
- `[in] utf8name`: UTF-8 encoded property name. | ||
Returns the named property as a [Value](value.md). | ||
Returns the named property as a [`Napi::Value`](value.md). | ||
```cpp | ||
Value Napi::Object::operator[] (uint32_t index) const; | ||
Napi::Value Napi::Object::operator[] (uint32_t index) const; | ||
``` | ||
- `[in] index`: Element index. | ||
Returns an indexed property or array element as a [Value](value.md). | ||
Returns an indexed property or array element as a [`Napi::Value`](value.md). |
@@ -1,15 +0,11 @@ | ||
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/) | ||
# Promise | ||
The Promise class, along with its Promise::Deferred class, implement the ability to create, resolve, and reject Promise objects. | ||
The `Napi::Promise` class, along with its `Napi::Promise::Deferred` class, implement the ability to create, resolve, and reject Promise objects. | ||
The basic approach is to create a Promise::Deferred object and return to your caller the value returned by the Promise::Deferred::Promise method. For example: | ||
The basic approach is to create a `Napi::Promise::Deferred` object and return to your caller the value returned by the `Napi::Promise::Deferred::Promise` method. For example: | ||
```cpp | ||
Value YourFunction(const CallbackInfo& info) { | ||
Napi::Value YourFunction(const Napi::CallbackInfo& info) { | ||
// your code goes here... | ||
Promise::Deferred deferred = Promise::Deferred::New(info.Env()); | ||
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env()); | ||
// deferred needs to survive this call... | ||
@@ -20,3 +16,3 @@ return deferred.Promise(); | ||
Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the Promise::Deferred object created earlier: | ||
Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the `Napi::Promise::Deferred` object created earlier: | ||
@@ -32,6 +28,6 @@ ```cpp | ||
```cpp | ||
static Promise::Deferred Promise::Deferred::New(napi_env env); | ||
static Napi::Promise::Deferred Napi::Promise::Deferred::New(napi_env env); | ||
``` | ||
* `[in] env`: The `napi_env` environment in which to create the Deferred object. | ||
* `[in] env`: The `napi_env` environment in which to create the `Napi::Promise::Deferred` object. | ||
@@ -41,14 +37,22 @@ ### Constructor | ||
```cpp | ||
Promise::Deferred(napi_env env); | ||
Napi::Promise::Deferred(napi_env env); | ||
``` | ||
* `[in] env`: The `napi_env` environment in which to construct the Deferred object. | ||
* `[in] env`: The `napi_env` environment in which to construct the `Napi::Promise::Deferred` object. | ||
### Env | ||
```cpp | ||
Napi::Env Napi::Promise::Deferred::Env() const; | ||
``` | ||
Returns the Env environment this `Napi::Promise::Deferred` object is associated with. | ||
### Promise | ||
```cpp | ||
Promise Promise::Deferred::Promise() const; | ||
Napi::Promise Napi::Promise::Deferred::Promise() const; | ||
``` | ||
Returns the Promise object held by the Promise::Deferred object. | ||
Returns the `Napi::Promise` object held by the `Napi::Promise::Deferred` object. | ||
@@ -58,8 +62,8 @@ ### Resolve | ||
```cpp | ||
void Promise::Deferred::Resolve(napi_value value) const; | ||
void Napi::Promise::Deferred::Resolve(napi_value value) const; | ||
``` | ||
Resolves the Promise object held by the Promise::Deferred object. | ||
Resolves the `Napi::Promise` object held by the `Napi::Promise::Deferred` object. | ||
* `[in] value`: The N-API primitive value with which to resolve the Promise. | ||
* `[in] value`: The N-API primitive value with which to resolve the `Napi::Promise`. | ||
@@ -69,7 +73,7 @@ ### Reject | ||
```cpp | ||
void Promise::Deferred::Reject(napi_value value) const; | ||
void Napi::Promise::Deferred::Reject(napi_value value) const; | ||
``` | ||
Rejects the Promise object held by the Promise::Deferred object. | ||
Rejects the Promise object held by the `Napi::Promise::Deferred` object. | ||
* `[in] value`: The N-API primitive value with which to reject the Promise. | ||
* `[in] value`: The N-API primitive value with which to reject the `Napi::Promise`. |
# Property Descriptor | ||
An [Object](object.md) can be assigned properites via its [DefineProperty](object.md#defineproperty) and [DefineProperties](object.md#defineproperties) function, which take PropertyDescrptor(s) as their parameters. The PropertyDescriptor can contain either values or functions, which are then assigned to the Object. Note that a single instance of a PropertyDescriptor class can only contain either one value, or at most two functions. PropertyDescriptors can only be created through the class methods [Accessor](#accessor), [Function](#function), or [Value](#value), each of which return a new static instance of a PropertyDescriptor. | ||
A [`Napi::Object`](object.md) can be assigned properites via its [`DefineProperty`](object.md#defineproperty) and [`DefineProperties`](object.md#defineproperties) functions, which take PropertyDescrptor(s) as their parameters. The `Napi::PropertyDescriptor` can contain either values or functions, which are then assigned to the `Napi::Object`. Note that a single instance of a `Napi::PropertyDescriptor` class can only contain either one value, or at most two functions. PropertyDescriptors can only be created through the class methods [`Accessor`](#accessor), [`Function`](#function), or [`Value`](#value), each of which return a new static instance of a `Napi::PropertyDescriptor`. | ||
@@ -25,15 +25,27 @@ ## Example | ||
Void Init(Env env) { | ||
// Accessor | ||
PropertyDescriptor pd1 = PropertyDescriptor::Accessor("pd1", TestGetter); | ||
PropertyDescriptor pd2 = PropertyDescriptor::Accessor("pd2", TestGetter, TestSetter); | ||
// Create an object. | ||
Object obj = Object::New(env); | ||
// Function | ||
PropertyDescriptor pd3 = PropertyDescriptor::Function("function", TestFunction); | ||
// Accessor | ||
PropertyDescriptor pd1 = PropertyDescriptor::Accessor(env, | ||
obj, | ||
"pd1", | ||
TestGetter); | ||
PropertyDescriptor pd2 = PropertyDescriptor::Accessor(env, | ||
obj, | ||
"pd2", | ||
TestGetter, | ||
TestSetter); | ||
// Function | ||
PropertyDescriptor pd3 = PropertyDescriptor::Function(env, | ||
"function", | ||
TestFunction); | ||
// Value | ||
Boolean true_bool = Boolean::New(env, true); | ||
PropertyDescriptor pd4 = | ||
PropertyDescriptor::Value("boolean value", | ||
Napi::Boolean::New(env, true), | ||
napi_writable); | ||
// Value | ||
Boolean true_bool = Boolean::New(env, true); | ||
PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", TestFunction, napi_writable); | ||
// Assign to an Object | ||
Object obj = Object::New(env); | ||
// Assign properties to the object. | ||
obj.DefineProperties({pd1, pd2, pd3, pd4}); | ||
@@ -56,3 +68,3 @@ } | ||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
Getter getter, | ||
@@ -74,7 +86,33 @@ napi_property_attributes attributes = napi_default, | ||
- `napi_value value` | ||
- `Name` | ||
- `Napi::Name` | ||
**This signature is deprecated. It will result in a memory leak if used.** | ||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor ( | ||
Napi::Env env, | ||
Napi::Object object, | ||
___ name, | ||
Getter getter, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
* `[in] env`: The environemnt in which to create this accessor. | ||
* `[in] object`: The object on which the accessor will be defined. | ||
* `[in] name`: The name used for the getter function. | ||
* `[in] getter`: A getter function. | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
Returns a `Napi::PropertyDescriptor` that contains a `Getter` accessor. | ||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `Napi::Name` | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, | ||
Getter getter, | ||
Setter setter, | ||
@@ -91,3 +129,3 @@ napi_property_attributes attributes = napi_default, | ||
Returns a PropertyDescriptor that contains a Getter and Setter function. | ||
Returns a `Napi::PropertyDescriptor` that contains a `Getter` and `Setter` function. | ||
@@ -98,8 +136,36 @@ The name of the property can be any of the following types: | ||
- `napi_value value` | ||
- `Name` | ||
- `Napi::Name` | ||
**This signature is deprecated. It will result in a memory leak if used.** | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor ( | ||
Napi::Env env, | ||
Napi::Object object, | ||
___ name, | ||
Getter getter, | ||
Setter setter, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
* `[in] env`: The environemnt in which to create this accessor. | ||
* `[in] object`: The object on which the accessor will be defined. | ||
* `[in] name`: The name of the getter and setter function. | ||
* `[in] getter`: The getter function. | ||
* `[in] setter`: The setter function. | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
Returns a `Napi::PropertyDescriptor` that contains a `Getter` and `Setter` function. | ||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `Napi::Name` | ||
### Function | ||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, | ||
Callable cb, | ||
@@ -115,3 +181,3 @@ napi_property_attributes attributes = napi_default, | ||
Returns a PropertyDescriptor that contains a callable Function. | ||
Returns a `Napi::PropertyDescriptor` that contains a callable `Napi::Function`. | ||
@@ -122,8 +188,32 @@ The name of the property can be any of the following types: | ||
- `napi_value value` | ||
- `Name` | ||
- `Napi::Name` | ||
**This signature is deprecated. It will result in a memory leak if used.** | ||
```cpp | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function ( | ||
Napi::Env env, | ||
___ name, | ||
Callable cb, | ||
napi_property_attributes attributes = napi_default, | ||
void *data = nullptr); | ||
``` | ||
* `[in] env`: The environemnt in which to create this accessor. | ||
* `[in] name`: The name of the Callable function. | ||
* `[in] cb`: The function | ||
* `[in] attributes`: Potential attributes for the getter function. | ||
* `[in] data`: A pointer to data of any type, default is a null pointer. | ||
Returns a `Napi::PropertyDescriptor` that contains a callable `Napi::Function`. | ||
The name of the property can be any of the following types: | ||
- `const char*` | ||
- `const std::string &` | ||
- `Napi::Name` | ||
### Value | ||
```cpp | ||
static PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, | ||
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, | ||
napi_value value, | ||
@@ -137,3 +227,3 @@ napi_property_attributes attributes = napi_default); | ||
- `napi_value value` | ||
- `Name` | ||
- `Napi::Name` | ||
@@ -140,0 +230,0 @@ ## Related Information |
# RangeError | ||
The **RangeError** class is a representation of the JavaScript RangeError that is | ||
The `Napi::RangeError` class is a representation of the JavaScript `RangeError` that is | ||
thrown when trying to pass a value as an argument to a function that does not allow | ||
a range that includes the value. | ||
The **RangeError** class inherits its behaviors from the **Error** class (for | ||
more info see: [Error](error.md)). | ||
The `Napi::RangeError` class inherits its behaviors from the `Napi::Error` class (for | ||
more info see: [`Napi::Error`](error.md)). | ||
@@ -16,32 +16,32 @@ For more details about error handling refer to the section titled [Error handling](error_handling.md). | ||
Creates a new instance of a `RangeError` object. | ||
Creates a new instance of a `Napi::RangeError` object. | ||
```cpp | ||
RangeError::New(Napi:Env env, const char* message); | ||
Napi::RangeError::New(Napi::Env env, const char* message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `RangeError` object. | ||
- `[in] message`: Null-terminated string to be used as the message for the `RangeError`. | ||
- `[in] Env`: The environment in which to construct the `Napi::RangeError` object. | ||
- `[in] message`: Null-terminated string to be used as the message for the `Napi::RangeError`. | ||
Returns an instance of a `RangeError` object. | ||
Returns an instance of a `Napi::RangeError` object. | ||
### New | ||
Creates a new instance of a `RangeError` object. | ||
Creates a new instance of a `Napi::RangeError` object. | ||
```cpp | ||
RangeError::New(Napi:Env env, const std::string& message); | ||
Napi::RangeError::New(Napi::Env env, const std::string& message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `RangeError` object. | ||
- `[in] message`: Reference string to be used as the message for the `RangeError`. | ||
- `[in] Env`: The environment in which to construct the `Napi::RangeError` object. | ||
- `[in] message`: Reference string to be used as the message for the `Napi::RangeError`. | ||
Returns an instance of a `RangeError` object. | ||
Returns an instance of a `Napi::RangeError` object. | ||
### Constructor | ||
Creates a new empty instance of a `RangeError`. | ||
Creates a new empty instance of a `Napi::RangeError`. | ||
```cpp | ||
RangeError(); | ||
Napi::RangeError::RangeError(); | ||
``` | ||
@@ -51,11 +51,11 @@ | ||
Initializes a `RangeError` instance from an existing Javascript error object. | ||
Initializes a `Napi::RangeError` instance from an existing Javascript error object. | ||
```cpp | ||
RangeError(napi_env env, napi_value value); | ||
Napi::RangeError::RangeError(napi_env env, napi_value value); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `RangeError` object. | ||
- `[in] value`: The `Error` reference to wrap. | ||
- `[in] Env`: The environment in which to construct the `Napi::RangeError` object. | ||
- `[in] value`: The `Napi::Error` reference to wrap. | ||
Returns an instance of a `RangeError` object. | ||
Returns an instance of a `Napi::RangeError` object. |
@@ -1,18 +0,14 @@ | ||
You are reading a draft of the next documentation and it's in continuos 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/) | ||
# Reference (template) | ||
Holds a counted reference to a [Value](value.md) object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount. | ||
Holds a counted reference to a [`Napi::Value`](value.md) object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount. | ||
The referenced Value is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the Value. | ||
The referenced `Napi::Value` is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the `Napi::Value`. | ||
Reference objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. | ||
`Napi::Reference` objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. | ||
The following classes inherit, either directly or indirectly, from Reference: | ||
The following classes inherit, either directly or indirectly, from `Napi::Reference`: | ||
* [ObjectWrap](object_wrap.md) | ||
* [ObjectReference](object_reference.md) | ||
* [FunctionReference](function_reference.md) | ||
* [`Napi::ObjectWrap`](object_wrap.md) | ||
* [`Napi::ObjectReference`](object_reference.md) | ||
* [`Napi::FunctionReference`](function_reference.md) | ||
@@ -24,3 +20,3 @@ ## Methods | ||
```cpp | ||
static Reference<T> New(const T& value, uint32_t initialRefcount = 0); | ||
static Napi::Reference<T> Napi::Reference::New(const T& value, uint32_t initialRefcount = 0); | ||
``` | ||
@@ -35,6 +31,6 @@ | ||
```cpp | ||
Reference(); | ||
Napi::Reference::Reference(); | ||
``` | ||
Creates a new _empty_ Reference instance. | ||
Creates a new _empty_ `Napi::Reference` instance. | ||
@@ -44,8 +40,8 @@ ### Constructor | ||
```cpp | ||
Reference(napi_env env, napi_value value); | ||
Napi::Reference::Reference(napi_env env, napi_value value); | ||
``` | ||
* `[in] env`: The `napi_env` environment in which to construct the Reference object. | ||
* `[in] env`: The `napi_env` environment in which to construct the `Napi::Reference` object. | ||
* `[in] value`: The N-API primitive value to be held by the Reference. | ||
* `[in] value`: The N-API primitive value to be held by the `Napi::Reference`. | ||
@@ -55,6 +51,6 @@ ### Env | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::Reference::Env() const; | ||
``` | ||
Returns the `Env` value in which the Reference was instantiated. | ||
Returns the `Napi::Env` value in which the `Napi::Reference` was instantiated. | ||
@@ -64,6 +60,6 @@ ### IsEmpty | ||
```cpp | ||
bool IsEmpty() const; | ||
bool Napi::Reference::IsEmpty() const; | ||
``` | ||
Determines whether the value held by the Reference is empty. | ||
Determines whether the value held by the `Napi::Reference` is empty. | ||
@@ -73,6 +69,6 @@ ### Value | ||
```cpp | ||
T Value() const; | ||
T Napi::Reference::Value() const; | ||
``` | ||
Returns the value held by the Reference. | ||
Returns the value held by the `Napi::Reference`. | ||
@@ -82,6 +78,6 @@ ### Ref | ||
```cpp | ||
uint32_t Ref(); | ||
uint32_t Napi::Reference::Ref(); | ||
``` | ||
Increments the reference count for the Reference and returns the resulting reference count. Throws an error if the increment fails. | ||
Increments the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the increment fails. | ||
@@ -91,6 +87,6 @@ ### Unref | ||
```cpp | ||
uint32_t Unref(); | ||
uint32_t Napi::Reference::Unref(); | ||
``` | ||
Decrements the reference count for the Reference and returns the resulting reference count. Throws an error if the decrement fails. | ||
Decrements the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the decrement fails. | ||
@@ -100,6 +96,6 @@ ### Reset (Empty) | ||
```cpp | ||
void Reset(); | ||
void Napi::Reference::Reset(); | ||
``` | ||
Sets the value held by the Reference to be empty. | ||
Sets the value held by the `Napi::Reference` to be empty. | ||
@@ -109,3 +105,3 @@ ### Reset | ||
```cpp | ||
void Reset(const T& value, uint32_t refcount = 0); | ||
void Napi::Reference::Reset(const T& value, uint32_t refcount = 0); | ||
``` | ||
@@ -117,3 +113,3 @@ | ||
Sets the value held by the Reference. | ||
Sets the value held by the `Napi::Reference`. | ||
@@ -123,5 +119,5 @@ ### SuppressDestruct | ||
```cpp | ||
void SuppressDestruct(); | ||
void Napi::Reference::SuppressDestruct(); | ||
``` | ||
Call this method on a Reference that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. | ||
Call this method on a `Napi::Reference` that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid. |
@@ -22,3 +22,3 @@ # Setup | ||
"dependencies": { | ||
"node-addon-api": "1.2.0", | ||
"node-addon-api": "*", | ||
} | ||
@@ -70,1 +70,4 @@ ``` | ||
targeted node version *does not* have N-API built-in. | ||
The preprocessor directive `NODE_ADDON_API_DISABLE_DEPRECATED` can be defined at | ||
compile time before including `napi.h` to skip the definition of deprecated APIs. |
@@ -6,6 +6,6 @@ # String | ||
```cpp | ||
String(); | ||
Napi::String::String(); | ||
``` | ||
Returns a new **empty** String instance. | ||
Returns a new **empty** `Napi::String` instance. | ||
@@ -16,5 +16,5 @@ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not | ||
```cpp | ||
Napi::String::String(napi_env env, napi_value value); ///< Wraps a N-API value primitive. | ||
``` | ||
String(napi_env env, napi_value value); ///< Wraps a N-API value primitive. | ||
``` | ||
- `[in] env` - The environment in which to create the string. | ||
@@ -34,3 +34,3 @@ - `[in] value` - The primitive to wrap. | ||
```cpp | ||
operator std::string() const; | ||
Napi::String::operator std::string() const; | ||
``` | ||
@@ -42,3 +42,3 @@ | ||
```cpp | ||
operator std::u16string() const; | ||
Napi::String::operator std::u16string() const; | ||
``` | ||
@@ -52,17 +52,17 @@ | ||
```cpp | ||
String::New(); | ||
Napi::String::New(); | ||
``` | ||
Returns a new empty String | ||
Returns a new empty `Napi::String`. | ||
### New | ||
```cpp | ||
String::New(napi_env env, const std::string& value); | ||
String::New(napi_env env, const std::u16string& value); | ||
String::New(napi_env env, const char* value); | ||
String::New(napi_env env, const char16_t* value); | ||
Napi::String::New(napi_env env, const std::string& value); | ||
Napi::String::New(napi_env env, const std::u16::string& value); | ||
Napi::String::New(napi_env env, const char* value); | ||
Napi::String::New(napi_env env, const char16_t* value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the Value object. | ||
- `[in] value`: The C++ primitive from which to instantiate the Value. `value` may be any of: | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object. | ||
- `[in] value`: The C++ primitive from which to instantiate the `Napi::Value`. `value` may be any of: | ||
- `std::string&` - represents an ANSI string. | ||
@@ -81,3 +81,3 @@ - `std::u16string&` - represents a UTF16-LE string. | ||
```cpp | ||
std::string Utf8Value() const; | ||
std::string Napi::String::Utf8Value() const; | ||
``` | ||
@@ -89,5 +89,5 @@ | ||
```cpp | ||
std::u16string Utf16Value() const; | ||
std::u16string Napi::String::Utf16Value() const; | ||
``` | ||
Returns a UTF-16 encoded C++ string. | ||
Returns a UTF-16 encoded C++ string. |
@@ -7,20 +7,20 @@ # Symbol | ||
Instantiates a new `Symbol` value | ||
Instantiates a new `Napi::Symbol` value. | ||
```cpp | ||
Symbol(); | ||
Napi::Symbol::Symbol(); | ||
``` | ||
Returns a new empty Symbol. | ||
Returns a new empty `Napi::Symbol`. | ||
### New | ||
```cpp | ||
Symbol::New(napi_env env, const std::string& description); | ||
Symbol::New(napi_env env, const char* description); | ||
Symbol::New(napi_env env, String description); | ||
Symbol::New(napi_env env, napi_value description); | ||
Napi::Symbol::New(napi_env env, const std::string& description); | ||
Napi::Symbol::New(napi_env env, const char* description); | ||
Napi::Symbol::New(napi_env env, Napi::String description); | ||
Napi::Symbol::New(napi_env env, napi_value description); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the Symbol object. | ||
- `[in] value`: The C++ primitive which represents the description hint for the Symbol. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Symbol` object. | ||
- `[in] value`: The C++ primitive which represents the description hint for the `Napi::Symbol`. | ||
`description` may be any of: | ||
@@ -33,3 +33,3 @@ - `std::string&` - ANSI string description. | ||
If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not | ||
being used, callers should check the result of `Env::IsExceptionPending` before | ||
being used, callers should check the result of `Napi::Env::IsExceptionPending` before | ||
attempting to use the returned value. | ||
@@ -39,9 +39,9 @@ | ||
```cpp | ||
static Symbol WellKnown(napi_env env, const std::string& name); | ||
static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& name); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the Symbol object. | ||
- `[in] name`: The C++ string representing the `Symbol` to retrieve. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Symbol` object. | ||
- `[in] name`: The C++ string representing the `Napi::Symbol` to retrieve. | ||
Returns a `Napi::Symbol` representing a well-known Symbol from the | ||
Symbol registry. | ||
Returns a `Napi::Symbol` representing a well-known `Symbol` from the | ||
`Symbol` registry. |
# TypeError | ||
The **TypeError** class is a representation of the JavaScript `TypeError` that is | ||
The `Napi::TypeError` class is a representation of the JavaScript `TypeError` that is | ||
thrown when an operand or argument passed to a function is incompatible with the | ||
type expected by the operator or function. | ||
The **TypeError** class inherits its behaviors from the **Error** class (for more info | ||
see: [Error](error.md)). | ||
The `Napi::TypeError` class inherits its behaviors from the `Napi::Error` class (for more info | ||
see: [`Napi::Error`](error.md)). | ||
@@ -16,32 +16,32 @@ For more details about error handling refer to the section titled [Error handling](error_handling.md). | ||
Creates a new instance of the `TypeError` object. | ||
Creates a new instance of the `Napi::TypeError` object. | ||
```cpp | ||
TypeError::New(Napi:Env env, const char* message); | ||
Napi::TypeError::New(Napi:Env env, const char* message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `TypeError` object. | ||
- `[in] message`: Null-terminated string to be used as the message for the `TypeError`. | ||
- `[in] Env`: The environment in which to construct the `Napi::TypeError` object. | ||
- `[in] message`: Null-terminated string to be used as the message for the `Napi::TypeError`. | ||
Returns an instance of a `TypeError` object. | ||
Returns an instance of a `Napi::TypeError` object. | ||
### New | ||
Creates a new instance of a `TypeError` object. | ||
Creates a new instance of a `Napi::TypeError` object. | ||
```cpp | ||
TypeError::New(Napi:Env env, const std::string& message); | ||
Napi::TypeError::New(Napi:Env env, const std::string& message); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `TypeError` object. | ||
- `[in] message`: Reference string to be used as the message for the `TypeError`. | ||
- `[in] Env`: The environment in which to construct the `Napi::TypeError` object. | ||
- `[in] message`: Reference string to be used as the message for the `Napi::TypeError`. | ||
Returns an instance of a `TypeError` object. | ||
Returns an instance of a `Napi::TypeError` object. | ||
### Constructor | ||
Creates a new empty instance of a `TypeError`. | ||
Creates a new empty instance of a `Napi::TypeError`. | ||
```cpp | ||
TypeError(); | ||
Napi::TypeError::TypeError(); | ||
``` | ||
@@ -51,11 +51,11 @@ | ||
Initializes a ```TypeError``` instance from an existing JavaScript error object. | ||
Initializes a `Napi::TypeError` instance from an existing JavaScript error object. | ||
```cpp | ||
TypeError(napi_env env, napi_value value); | ||
Napi::TypeError::TypeError(napi_env env, napi_value value); | ||
``` | ||
- `[in] Env`: The environment in which to construct the `TypeError` object. | ||
- `[in] value`: The `Error` reference to wrap. | ||
- `[in] Env`: The environment in which to construct the `Napi::TypeError` object. | ||
- `[in] value`: The `Napi::Error` reference to wrap. | ||
Returns an instance of a `TypeError` object. | ||
Returns an instance of a `Napi::TypeError` object. |
# TypedArrayOf | ||
The `TypedArrayOf` class corresponds to the various | ||
The `Napi::TypedArrayOf` class corresponds to the various | ||
[JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) | ||
@@ -12,10 +12,10 @@ classes. | ||
```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; | ||
typedef Napi::TypedArrayOf<int8_t> Int8Array; | ||
typedef Napi::TypedArrayOf<uint8_t> Uint8Array; | ||
typedef Napi::TypedArrayOf<int16_t> Int16Array; | ||
typedef Napi::TypedArrayOf<uint16_t> Uint16Array; | ||
typedef Napi::TypedArrayOf<int32_t> Int32Array; | ||
typedef Napi::TypedArrayOf<uint32_t> Uint32Array; | ||
typedef Napi::TypedArrayOf<float> Float32Array; | ||
typedef Napi::TypedArrayOf<double> Float64Array; | ||
``` | ||
@@ -37,4 +37,4 @@ | ||
Allocates a new `TypedArray` instance with a given length. The underlying | ||
`ArrayBuffer` is allocated automatically to the desired number of elements. | ||
Allocates a new `Napi::TypedArray` instance with a given length. The underlying | ||
`Napi::ArrayBuffer` is allocated automatically to the desired number of elements. | ||
@@ -45,3 +45,3 @@ The array type parameter can normally be omitted (because it is inferred from | ||
```cpp | ||
static TypedArrayOf New(napi_env env, | ||
static Napi::TypedArrayOf Napi::TypedArrayOf::New(napi_env env, | ||
size_t elementLength, | ||
@@ -51,11 +51,11 @@ napi_typedarray_type type); | ||
- `[in] env`: The environment in which to create the `TypedArrayOf` instance. | ||
- `[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 `TypedArrayOf` instance. | ||
Returns a new `Napi::TypedArrayOf` instance. | ||
### New | ||
Wraps the provided `ArrayBuffer` into a new `TypedArray` instance. | ||
Wraps the provided `Napi::ArrayBuffer` into a new `Napi::TypedArray` instance. | ||
@@ -66,3 +66,3 @@ The array `type` parameter can normally be omitted (because it is inferred from | ||
```cpp | ||
static TypedArrayOf New(napi_env env, | ||
static Napi::TypedArrayOf Napi::TypedArrayOf::New(napi_env env, | ||
size_t elementLength, | ||
@@ -74,17 +74,17 @@ Napi::ArrayBuffer arrayBuffer, | ||
- `[in] env`: The environment in which to create the `TypedArrayOf` instance. | ||
- `[in] env`: The environment in which to create the `Napi::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] 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 `TypedArrayOf` instance. | ||
Returns a new `Napi::TypedArrayOf` instance. | ||
### Constructor | ||
Initializes an empty instance of the `TypedArrayOf` class. | ||
Initializes an empty instance of the `Napi::TypedArrayOf` class. | ||
```cpp | ||
TypedArrayOf(); | ||
Napi::TypedArrayOf::TypedArrayOf(); | ||
``` | ||
@@ -94,10 +94,10 @@ | ||
Initializes a wrapper instance of an existing `TypedArrayOf` object. | ||
Initializes a wrapper instance of an existing `Napi::TypedArrayOf` object. | ||
```cpp | ||
TypedArrayOf(napi_env env, napi_value value); | ||
Napi::TypedArrayOf::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. | ||
- `[in] env`: The environment in which to create the `Napi::TypedArrayOf` object. | ||
- `[in] value`: The `Napi::TypedArrayOf` reference to wrap. | ||
@@ -107,3 +107,3 @@ ### operator [] | ||
```cpp | ||
T& operator [](size_t index); | ||
T& Napi::TypedArrayOf::operator [](size_t index); | ||
``` | ||
@@ -118,3 +118,3 @@ | ||
```cpp | ||
const T& operator [](size_t index) const; | ||
const T& Napi::TypedArrayOf::operator [](size_t index) const; | ||
``` | ||
@@ -129,6 +129,6 @@ | ||
```cpp | ||
T* Data() const; | ||
T* Napi::TypedArrayOf::Data() const; | ||
``` | ||
Returns a pointer into the backing `ArrayBuffer` which is offset to point to the | ||
Returns a pointer into the backing `Napi::ArrayBuffer` which is offset to point to the | ||
start of the array. | ||
@@ -139,6 +139,6 @@ | ||
```cpp | ||
const T* Data() const | ||
const T* Napi::TypedArrayOf::Data() const | ||
``` | ||
Returns a pointer into the backing `ArrayBuffer` which is offset to point to the | ||
Returns a pointer into the backing `Napi::ArrayBuffer` which is offset to point to the | ||
start of the array. |
# TypedArray | ||
The `TypedArray` class corresponds to the | ||
The `Napi::TypedArray` class corresponds to the | ||
[JavaScript `TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) | ||
@@ -11,6 +11,6 @@ class. | ||
Initializes an empty instance of the `TypedArray` class. | ||
Initializes an empty instance of the `Napi::TypedArray` class. | ||
```cpp | ||
TypedArray(); | ||
Napi::TypedArray::TypedArray(); | ||
``` | ||
@@ -20,10 +20,10 @@ | ||
Initializes a wrapper instance of an existing `TypedArray` instance. | ||
Initializes a wrapper instance of an existing `Napi::TypedArray` instance. | ||
```cpp | ||
TypedArray(napi_env env, napi_value value); | ||
Napi::TypedArray::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. | ||
- `[in] env`: The environment in which to create the `Napi::TypedArray` instance. | ||
- `[in] value`: The `Napi::TypedArray` reference to wrap. | ||
@@ -33,3 +33,3 @@ ### TypedArrayType | ||
```cpp | ||
napi_typedarray_type TypedArrayType() const; | ||
napi_typedarray_type Napi::TypedArray::TypedArrayType() const; | ||
``` | ||
@@ -42,3 +42,3 @@ | ||
```cpp | ||
Napi::ArrayBuffer ArrayBuffer() const; | ||
Napi::ArrayBuffer Napi::TypedArray::ArrayBuffer() const; | ||
``` | ||
@@ -51,3 +51,3 @@ | ||
```cpp | ||
uint8_t ElementSize() const; | ||
uint8_t Napi::TypedArray::ElementSize() const; | ||
``` | ||
@@ -60,3 +60,3 @@ | ||
```cpp | ||
size_t ElementLength() const; | ||
size_t Napi::TypedArray::ElementLength() const; | ||
``` | ||
@@ -69,6 +69,6 @@ | ||
```cpp | ||
size_t ByteOffset() const; | ||
size_t Napi::TypedArray::ByteOffset() const; | ||
``` | ||
Returns the offset into the `ArrayBuffer` where the array starts, in bytes. | ||
Returns the offset into the `Napi::ArrayBuffer` where the array starts, in bytes. | ||
@@ -78,5 +78,5 @@ ### ByteLength | ||
```cpp | ||
size_t ByteLength() const; | ||
size_t Napi::TypedArray::ByteLength() const; | ||
``` | ||
Returns the length of the array, in bytes. |
168
doc/value.md
@@ -1,24 +0,22 @@ | ||
**WORK IN PROGRESS, NOT YET COMPLETE** | ||
# Value | ||
Value is the C++ manifestation of a JavaScript value. | ||
`Napi::Value` is the C++ manifestation of a JavaScript value. | ||
Value is a the base class upon which other JavaScript values such as Number, Boolean, String, and Object are based. | ||
The following classes inherit, either directly or indirectly, from Value: | ||
The following classes inherit, either directly or indirectly, from `Napi::Value`: | ||
- [Array](array.md) | ||
- [ArrayBuffer](array_buffer.md) | ||
- [Boolean](boolean.md) | ||
- [Buffer](buffer.md) | ||
- [External](external.md) | ||
- [Function](function.md) | ||
- [Name](name.md) | ||
- [Number](number.md) | ||
- [Object](object.md) | ||
- [String](string.md) | ||
- [Symbol](symbol.md) | ||
- [TypedArray](typed_array.md) | ||
- [TypedArrayOf](typed_array_of.md) | ||
- [`Napi::Array`](array.md) | ||
- [`Napi::ArrayBuffer`](array_buffer.md) | ||
- [`Napi::Boolean`](boolean.md) | ||
- [`Napi::Buffer`](buffer.md) | ||
- [`Napi::External`](external.md) | ||
- [`Napi::Function`](function.md) | ||
- [`Napi::Name`](name.md) | ||
- [`Napi::Number`](number.md) | ||
- [`Napi::Object`](object.md) | ||
- [`Napi::String`](string.md) | ||
- [`Napi::Symbol`](symbol.md) | ||
- [`Napi::TypedArray`](typed_array.md) | ||
- [`Napi::TypedArrayOf`](typed_array_of.md) | ||
@@ -30,6 +28,6 @@ ## Methods | ||
```cpp | ||
Value(); | ||
Napi::Value::Value(); | ||
``` | ||
Creates a new *empty* Value instance. | ||
Creates a new *empty* `Napi::Value` instance. | ||
@@ -39,17 +37,17 @@ ### Constructor | ||
```cpp | ||
Value(napi_env env, napi_value value); | ||
Napi::Value::Value(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to construct the Value object. | ||
- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object. | ||
- `[in] value`: The C++ primitive from which to instantiate the Value. `value` may be any of: | ||
- bool | ||
- `[in] value`: The C++ primitive from which to instantiate the `Napi::Value`. `value` may be any of: | ||
- `bool` | ||
- Any integer type | ||
- Any floating point type | ||
- const char* (encoded using UTF-8, null-terminated) | ||
- const char16_t* (encoded using UTF-16-LE, null-terminated) | ||
- std::string (encoded using UTF-8) | ||
- std::u16string | ||
- napi::Value | ||
- napi_value | ||
- `const char*` (encoded using UTF-8, null-terminated) | ||
- `const char16_t*` (encoded using UTF-16-LE, null-terminated) | ||
- `std::string` (encoded using UTF-8) | ||
- `std::u16string` | ||
- `Napi::Value` | ||
- `napi_value` | ||
@@ -59,10 +57,10 @@ ### From | ||
```cpp | ||
template <typename T> static Value From(napi_env env, const T& value); | ||
template <typename T> static Napi::Value Napi::Value::From(napi_env env, const T& value); | ||
``` | ||
- `[in] env`: The `napi_env` environment in which to create the Value object. | ||
- `[in] env`: The `napi_env` environment in which to create the `Napi::Value` object. | ||
- `[in] value`: The N-API primitive value from which to create the Value object. | ||
- `[in] value`: The N-API primitive value from which to create the `Napi::Value` object. | ||
Returns a Value object from an N-API primitive value. | ||
Returns a `Napi::Value` object from an N-API primitive value. | ||
@@ -77,3 +75,3 @@ ### operator napi_value | ||
Returns `nullptr` if this Value is *empty*. | ||
Returns `nullptr` if this `Napi::Value` is *empty*. | ||
@@ -83,8 +81,9 @@ ### operator == | ||
```cpp | ||
bool operator ==(const Value& other) const; | ||
bool Napi::Value::operator ==(const Napi::Value& other) const; | ||
``` | ||
- `[in] other`: The Value object to be compared. | ||
- `[in] other`: The `Napi::Value` object to be compared. | ||
Returns a `bool` indicating if this Value strictly equals another Value. | ||
Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi::Value`. | ||
@@ -94,8 +93,8 @@ ### operator != | ||
```cpp | ||
bool operator !=(const Value& other) const; | ||
bool Napi::Value::operator !=(const Napi::Value& other) const; | ||
``` | ||
- `[in] other`: The Value object to be compared. | ||
- `[in] other`: The `Napi::Value` object to be compared. | ||
Returns a `bool` indicating if this Value does not strictly equal another Value. | ||
Returns a `bool` indicating if this `Napi::Value` does not strictly equal another `Napi::Value`. | ||
@@ -105,7 +104,7 @@ ### StrictEquals | ||
```cpp | ||
bool StrictEquals(const Value& other) const; | ||
bool Napi::Value::StrictEquals(const Napi::Value& other) const; | ||
``` | ||
- `[in] other`: The Value object to be compared. | ||
- `[in] other`: The `Napi::Value` object to be compared. | ||
Returns a `bool` indicating if this Value strictly equals another Value. | ||
Returns a `bool` indicating if this `Napi::Value` strictly equals another `Napi::Value`. | ||
@@ -115,6 +114,6 @@ ### Env | ||
```cpp | ||
Napi::Env Env() const; | ||
Napi::Env Napi::Value::Env() const; | ||
``` | ||
Returns the `Env` environment this value is associated with. | ||
Returns the `Napi::Env` environment this value is associated with. | ||
@@ -124,10 +123,11 @@ ### IsEmpty | ||
```cpp | ||
bool IsEmpty() const; | ||
bool Napi::Value::IsEmpty() const; | ||
``` | ||
Returns a `bool` indicating if this Value is *empty* (uninitialized). | ||
Returns a `bool` indicating if this `Napi::Value` is *empty* (uninitialized). | ||
An empty Value is invalid, and most attempts to perform an operation on an empty Value will result in an exception. Note an empty Value is distinct from JavaScript `null` or `undefined`, which are valid values. | ||
An empty `Napi::Value` is invalid, and most attempts to perform an operation on an empty Value will result in an exception. | ||
Note an empty `Napi::Value` is distinct from JavaScript `null` or `undefined`, which are valid values. | ||
When C++ exceptions are disabled at compile time, a method with a `Value` return type may return an empty Value to indicate a pending exception. So when not using C++ exceptions, callers should check whether this Value is empty before attempting to use it. | ||
When C++ exceptions are disabled at compile time, a method with a `Napi::Value` return type may return an empty Value to indicate a pending exception. So when not using C++ exceptions, callers should check whether this `Napi::Value` is empty before attempting to use it. | ||
@@ -137,6 +137,6 @@ ### Type | ||
```cpp | ||
napi_valuetype Type() const; | ||
napi_valuetype Napi::Value::Type() const; | ||
``` | ||
Returns the `napi_valuetype` type of the Value. | ||
Returns the `napi_valuetype` type of the `Napi::Value`. | ||
@@ -146,6 +146,6 @@ ### IsUndefined | ||
```cpp | ||
bool IsUndefined() const; | ||
bool Napi::Value::IsUndefined() const; | ||
``` | ||
Returns a `bool` indicating if this Value is an undefined JavaScript value. | ||
Returns a `bool` indicating if this `Napi::Value` is an undefined JavaScript value. | ||
@@ -155,6 +155,6 @@ ### IsNull | ||
```cpp | ||
bool IsNull() const; | ||
bool Napi::Value::IsNull() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a null JavaScript value. | ||
Returns a `bool` indicating if this `Napi::Value` is a null JavaScript value. | ||
@@ -164,6 +164,6 @@ ### IsBoolean | ||
```cpp | ||
bool IsBoolean() const; | ||
bool Napi::Value::IsBoolean() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript boolean. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript boolean. | ||
@@ -173,6 +173,6 @@ ### IsNumber | ||
```cpp | ||
bool IsNumber() const; | ||
bool Napi::Value::IsNumber() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript number. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript number. | ||
@@ -182,6 +182,6 @@ ### IsString | ||
```cpp | ||
bool IsString() const; | ||
bool Napi::Value::IsString() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript string. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript string. | ||
@@ -191,6 +191,6 @@ ### IsSymbol | ||
```cpp | ||
bool IsSymbol() const; | ||
bool Napi::Value::IsSymbol() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript symbol. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript symbol. | ||
@@ -200,6 +200,6 @@ ### IsArray | ||
```cpp | ||
bool IsArray() const; | ||
bool Napi::Value::IsArray() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript array. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript array. | ||
@@ -209,6 +209,6 @@ ### IsArrayBuffer | ||
```cpp | ||
bool IsArrayBuffer() const; | ||
bool Napi::Value::IsArrayBuffer() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript array buffer. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript array buffer. | ||
@@ -218,6 +218,6 @@ ### IsTypedArray | ||
```cpp | ||
bool IsTypedArray() const; | ||
bool Napi::Value::IsTypedArray() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript typed array. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript typed array. | ||
@@ -227,6 +227,6 @@ ### IsObject | ||
```cpp | ||
bool IsObject() const; | ||
bool Napi::Value::IsObject() const; | ||
``` | ||
Returns a `bool` indicating if this Value is JavaScript object. | ||
Returns a `bool` indicating if this `Napi::Value` is JavaScript object. | ||
@@ -236,6 +236,6 @@ ### IsFunction | ||
```cpp | ||
bool IsFunction() const; | ||
bool Napi::Value::IsFunction() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a JavaScript function. | ||
Returns a `bool` indicating if this `Napi::Value` is a JavaScript function. | ||
@@ -245,6 +245,6 @@ ### IsBuffer | ||
```cpp | ||
bool IsBuffer() const; | ||
bool Napi::Value::IsBuffer() const; | ||
``` | ||
Returns a `bool` indicating if this Value is a Node buffer. | ||
Returns a `bool` indicating if this `Napi::Value` is a Node buffer. | ||
@@ -254,3 +254,3 @@ ### As | ||
```cpp | ||
template <typename T> T As() const; | ||
template <typename T> T Napi::Value::As() const; | ||
``` | ||
@@ -265,6 +265,6 @@ | ||
```cpp | ||
Boolean ToBoolean() const; | ||
Napi::Boolean Napi::Value::ToBoolean() const; | ||
``` | ||
Returns the Value coerced to a JavaScript boolean. | ||
Returns the `Napi::Value` coerced to a JavaScript boolean. | ||
@@ -274,6 +274,6 @@ ### ToNumber | ||
```cpp | ||
Number ToNumber() const; | ||
Napi::Number Napi::Value::ToNumber() const; | ||
``` | ||
Returns the Value coerced to a JavaScript number. | ||
Returns the `Napi::Value` coerced to a JavaScript number. | ||
@@ -283,6 +283,6 @@ ### ToString | ||
```cpp | ||
String ToString() const; | ||
Napi::String Napi::Value::ToString() const; | ||
``` | ||
Returns the Value coerced to a JavaScript string. | ||
Returns the `Napi::Value` coerced to a JavaScript string. | ||
@@ -292,5 +292,5 @@ ### ToObject | ||
```cpp | ||
Object ToObject() const; | ||
Napi::Object Napi::Value::ToObject() const; | ||
``` | ||
Returns the Value coerced to a JavaScript object. | ||
Returns the `Napi::Value` coerced to a JavaScript object. |
# Working with JavaScript Values | ||
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/) | ||
`node-addon-api` provides a set of classes that allow to create and manage | ||
JavaScript object: | ||
- [Function](function.md) | ||
- [FunctionReference](function_reference.md) | ||
- [ObjectWrap](object_wrap.md) | ||
- [ClassPropertyDescriptor](class_property_descriptor.md) | ||
- [Buffer](buffer.md) | ||
- [ArrayBuffer](array_buffer.md) | ||
- [TypedArray](typed_array.md) | ||
- [TypedArrayOf](typed_array_of.md) | ||
- [DataView](dataview.md) |
@@ -9,2 +9,3 @@ { | ||
"Anna Henningsen (https://github.com/addaleax)", | ||
"Arnaud Botella (https://github.com/BotellaA)", | ||
"Arunesh Chandra (https://github.com/aruneshchandra)", | ||
@@ -15,5 +16,8 @@ "Ben Berman (https://github.com/rivertam)", | ||
"David Halls (https://github.com/davedoesdev)", | ||
"Dongjin Na (https://github.com/nadongguri)", | ||
"Eric Bickle (https://github.com/ebickle)", | ||
"Gabriel Schulhof (https://github.com/gabrielschulhof)", | ||
"Gus Caplan (https://github.com/devsnek)", | ||
"Hitesh Kanwathirtha (https://github.com/digitalinfinity)", | ||
"Jake Yoon (https://github.com/yjaeseok)", | ||
"Jason Ginchereau (https://github.com/jasongin)", | ||
@@ -28,4 +32,6 @@ "Jim Schlight (https://github.com/jschlight)", | ||
"Michele Campus (https://github.com/kYroL01)", | ||
"Mikhail Cheshkov (https://github.com/mcheshkov)", | ||
"Nicola Del Gobbo (https://github.com/NickNaso)", | ||
"Nick Soggin (https://github.com/iSkore)", | ||
"Philipp Renoth (https://github.com/DaAitch)", | ||
"Rolf Timmermans (https://github.com/rolftimmermans)", | ||
@@ -56,3 +62,3 @@ "Sampson Gao (https://github.com/sampsongao)", | ||
}, | ||
"version": "1.4.0" | ||
"version": "1.5.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
# **node-addon-api module** | ||
# **node-addon-api module** | ||
This module contains **header-only C++ wrapper classes** which simplify | ||
@@ -7,7 +7,7 @@ the use of the C based [N-API](https://nodejs.org/dist/latest/docs/api/n-api.html) | ||
N-API is an ABI stable C interface provided by Node.js for building native | ||
N-API is an ABI stable C interface provided by Node.js for building native | ||
addons. It is independent from the underlying JavaScript runtime (e.g. V8 or ChakraCore) | ||
and is maintained as part of Node.js itself. It is intended to insulate | ||
native addons from changes in the underlying JavaScript engine and allow | ||
modules compiled for one version to run on later versions of Node.js without | ||
and is maintained as part of Node.js itself. It is intended to insulate | ||
native addons from changes in the underlying JavaScript engine and allow | ||
modules compiled for one version to run on later versions of Node.js without | ||
recompilation. | ||
@@ -17,6 +17,13 @@ | ||
of the N-API as it consists only of inline code that depends only on the stable API | ||
provided by N-API. As such, modules built against one version of Node.js | ||
provided by N-API. As such, modules built against one version of Node.js | ||
using node-addon-api should run without having to be rebuilt with newer versions | ||
of Node.js. | ||
It is important to remember that *other* Node.js interfaces such as | ||
`libuv` (included in a project via `#include <uv.h>`) are not ABI-stable across | ||
Node.js major versions. Thus, and addon must use N-API and/or `node-addon-api` | ||
exclusively and build against a version of Node.js that includes an | ||
implementation of N-API (meaning a version of Node.js newer than 6.14.2) in | ||
order to benefit from ABI stability across Node.js major versions. | ||
As new APIs are added to N-API, node-addon-api must be updated to provide | ||
@@ -42,3 +49,3 @@ wrappers for those new APIs. For this reason node-addon-api provides | ||
## **Current version: 1.4** | ||
## **Current version: 1.5** | ||
@@ -56,3 +63,5 @@ (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog) | ||
- [Conversion tool](doc/conversion-tool.md) | ||
- [Checker tool](doc/checker-tool.md) | ||
- [Generator](doc/generator.md) | ||
- [Prebuild tools](doc/prebuild_tools.md) | ||
@@ -63,4 +72,3 @@ <a name="api"></a> | ||
The following is the documentation for node-addon-api (NOTE: | ||
still a work in progress as its not yet complete). | ||
The following is the documentation for node-addon-api. | ||
@@ -73,2 +81,3 @@ - [Basic Types](doc/basic_types.md) | ||
- [Number](doc/number.md) | ||
- [BigInt](doc/bigint.md) | ||
- [Boolean](doc/boolean.md) | ||
@@ -99,6 +108,9 @@ - [Env](doc/env.md) | ||
- [TypedArrayOf](doc/typed_array_of.md) | ||
- [DataView](doc/dataview.md) | ||
- [Memory Management](doc/memory_management.md) | ||
- [Async Operations](doc/async_operations.md) | ||
- [AsyncWorker](doc/async_worker.md) | ||
- [AsyncContext](doc/async_context.md) | ||
- [Promises](doc/promises.md) | ||
- [Version management](doc/version_management.md) | ||
@@ -131,2 +143,8 @@ <a name="examples"></a> | ||
To avoid testing the deprecated portions of the API run | ||
``` | ||
npm install | ||
npm test --disable-deprecated | ||
``` | ||
Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/master/test)** | ||
@@ -133,0 +151,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
686906
71
383
170
2