Refactoring day1

This commit is contained in:
2020-08-20 20:27:14 +02:00
parent 6aceefeb2f
commit b907489a75
481 changed files with 5321 additions and 5616 deletions

View File

@@ -1,5 +1,40 @@
# node-addon-api Changelog
## 2020-07-13 Version 3.0.1, @NickNaso
### Notable changes:
#### API
- Fixed the usage of `Napi::Reference` with `Napi::TypedArray`.
- Fixed `Napi::ObjectWrap` inheritance.
#### Documentation
- Updated the example for `Napi::ObjectWrap`.
- Added documentation for instance data APIs.
- Some minor corrections all over the documentation.
#### TEST
- Fixed test for `Napi::ArrayBuffer` and `Napi::Buffer`.
- Some minor corrections all over the test suite.
### Commits
* [[`40c7926342`](https://github.com/nodejs/node-addon-api/commit/40c7926342)] - **build**: ensure paths with spaces can be used (Lovell Fuller) [#757](https://github.com/nodejs/node-addon-api/pull/757)
* [[`ef16dfb4a2`](https://github.com/nodejs/node-addon-api/commit/ef16dfb4a2)] - **doc**: update ObjectWrap example (Gabriel Schulhof) [#754](https://github.com/nodejs/node-addon-api/pull/754)
* [[`48f6762bf6`](https://github.com/nodejs/node-addon-api/commit/48f6762bf6)] - **src**: add \_\_wasm32\_\_ guards (Gus Caplan)
* [[`bd2c5ec502`](https://github.com/nodejs/node-addon-api/commit/bd2c5ec502)] - Fixes issue 745. (#748) (Nicola Del Gobbo)
* [[`4c01af2d87`](https://github.com/nodejs/node-addon-api/commit/4c01af2d87)] - Fix typo in CHANGELOG (#715) (Kasumi Hanazuki)
* [[`36e1af96d5`](https://github.com/nodejs/node-addon-api/commit/36e1af96d5)] - **src**: fix use of Reference with typed arrays (Michael Dawson) [#726](https://github.com/nodejs/node-addon-api/pull/726)
* [[`d463f02bc7`](https://github.com/nodejs/node-addon-api/commit/d463f02bc7)] - **src**: fix testEnumerables on ObjectWrap (Ferdinand Holzer) [#736](https://github.com/nodejs/node-addon-api/pull/736)
* [[`ba7ad37d44`](https://github.com/nodejs/node-addon-api/commit/ba7ad37d44)] - **src**: fix ObjectWrap inheritance (David Halls) [#732](https://github.com/nodejs/node-addon-api/pull/732)
* [[`31504c862b`](https://github.com/nodejs/node-addon-api/commit/31504c862b)] - **doc**: fix minor typo in object\_wrap.md (#741) (Daniel Bevenius) [#741](https://github.com/nodejs/node-addon-api/pull/741)
* [[`beccf2145d`](https://github.com/nodejs/node-addon-api/commit/beccf2145d)] - **test**: fix up delays for array buffer test (Michael Dawson) [#737](https://github.com/nodejs/node-addon-api/pull/737)
* [[`45cb1d9748`](https://github.com/nodejs/node-addon-api/commit/45cb1d9748)] - Correct AsyncProgressWorker link in README (#716) (Jeroen Janssen)
* [[`381c0da60c`](https://github.com/nodejs/node-addon-api/commit/381c0da60c)] - **doc**: add instance data APIs (Gabriel Schulhof) [#708](https://github.com/nodejs/node-addon-api/pull/708)
## 2020-04-30 Version 3.0.0, @NickNaso
### Notable changes:
@@ -14,14 +49,14 @@
- Added `Env::RunScript` method to run JavaScript code contained in a string.
- Added templated version of `Napi::Function`.
- Added benchmarking framework.
- Added support for natove addon instance data.
- Added support for native addon instance data.
- Added `Napi::AsyncProgressQueueWorker` api.
- Changed the guards to `NAPI_VERSION > 5`.
- Removed N-API implementation (v6.x and v8.x support).
- `Napi::AsyncWorker::OnWorkComplete` and `Napi::AsyncWorker::OnExecute` methods
are override-able.
- Removed erroneous finalizer cleanup in `Napi::ThreadSafeFunction`.
- Disabled cahcing in `Napi::ArrayBuffer`.
- Disabled caching in `Napi::ArrayBuffer`.
- Explicitly disallow assign and copy operator.
- Some minor corrections and improvements.

View File

@@ -47,7 +47,7 @@ to ideas specified in the **ECMA262 Language Specification**.
- **[Contributors](#contributors)**
- **[License](#license)**
## **Current version: 3.0.0**
## **Current version: 3.0.1**
(See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
@@ -55,8 +55,8 @@ to ideas specified in the **ECMA262 Language Specification**.
<a name="setup"></a>
node-addon-api is based on [N-API](https://nodejs.org/api/n-api.html) and supports using different N-API versions.
This allows addons built with it to run with Node.js versions which support the targeted N-API version.
node-addon-api is based on [N-API](https://nodejs.org/api/n-api.html) and supports using different N-API versions.
This allows addons built with it to run with Node.js versions which support the targeted N-API version.
**However** the node-addon-api support model is to support only the active LTS Node.js versions. This means that
every year there will be a new major which drops support for the Node.js LTS version which has gone out of service.
@@ -115,7 +115,7 @@ The following is the documentation for node-addon-api.
- [Async Operations](doc/async_operations.md)
- [AsyncWorker](doc/async_worker.md)
- [AsyncContext](doc/async_context.md)
- [AsyncProgressWorker](doc/async_progress_worker.md)
- [AsyncWorker Variants](doc/async_worker_variants.md)
- [Thread-safe Functions](doc/threadsafe_function.md)
- [Promises](doc/promises.md)
- [Version management](doc/version_management.md)

View File

@@ -79,7 +79,7 @@ Returns the number of words needed to store this `BigInt` value.
### ToWords
```cpp
void Napi::BigInt::ToWords(size_t* word_count, int* sign_bit, uint64_t* words);
void Napi::BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
```
- `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive

View File

@@ -75,3 +75,58 @@ The `script` can be any of the following types:
- [`Napi::String`](string.md)
- `const char *`
- `const std::string &`
### GetInstanceData
```cpp
template <typename T> T* GetInstanceData();
```
Returns the instance data that was previously associated with the environment,
or `nullptr` if none was associated.
### SetInstanceData
```cpp
template <typename T> using Finalizer = void (*)(Env, T*);
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
void SetInstanceData(T* data);
```
- `[template] fini`: A function to call when the instance data is to be deleted.
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
not given, the default finalizer will be used, which simply uses the `delete`
operator to destroy `T*` when the addon instance is unloaded.
- `[in] data`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle.
Associates a data item stored at `T* data` with the current instance of the
addon. The item will be passed to the function `fini` which gets called when an
instance of the addon is unloaded.
### SetInstanceData
```cpp
template <typename DataType, typename HintType>
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
template <typename DataType,
typename HintType,
FinalizerWithHint<DataType, HintType> fini =
Env::DefaultFiniWithHint<DataType, HintType>>
void SetInstanceData(DataType* data, HintType* hint);
```
- `[template] fini`: A function to call when the instance data is to be deleted.
Accepts a function of the form
`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
the default finalizer will be used, which simply uses the `delete` operator to
destroy `T*` when the addon instance is unloaded.
- `[in] data`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle.
- `[in] hint`: A pointer to data that will be associated with the instance of
the addon for the duration of its lifecycle and will be passed as a hint to
`fini` when the addon instance is unloaded.
Associates a data item stored at `T* data` with the current instance of the
addon. The item will be passed to the function `fini` which gets called when an
instance of the addon is unloaded. This overload accepts an additional hint to
be passed to `fini`.

View File

@@ -22,49 +22,57 @@ your C++ class methods.
class Example : public Napi::ObjectWrap<Example> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
Example(const Napi::CallbackInfo &info);
Example(const Napi::CallbackInfo& info);
static Napi::Value CreateNewItem(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::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<&Example::GetValue>("GetValue"),
InstanceMethod<&Example::SetValue>("SetValue")
InstanceMethod<&Example::SetValue>("SetValue"),
StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
// 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();
*constructor = Napi::Persistent(func);
exports.Set("Example", func);
// Store the constructor as the add-on instance data. This will allow this
// add-on to support multiple instances of itself running on multiple worker
// threads, as well as multiple instances of itself running in different
// contexts on the same thread.
//
// By default, the value set on the environment here will be destroyed when
// the add-on is unloaded using the `delete` operator, but it is also
// possible to supply a custom deleter.
env.SetInstanceData<Napi::FunctionReference>(constructor);
return exports;
}
Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) {
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::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::Value Example::SetValue(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
// ...
Napi::Number value = info[0].As<Napi::Number>();
@@ -78,6 +86,16 @@ Napi::Object Init (Napi::Env env, Napi::Object exports) {
return exports;
}
// Create a new item using the constructor stored during Init.
Napi::Value Example::CreateNewItem(const Napi::CallbackInfo& info) {
// Retrieve the instance data we stored during `Init()`. We only stored the
// constructor there, so we retrieve it here to create a new instance of the
// JS class the constructor represents.
Napi::FunctionReference* constructor =
info.Env().GetInstanceData<Napi::FunctionReference>();
return constructor->New({ Napi::Number::New(info.Env(), 42) });
}
// Register and initialize native add-on
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
```
@@ -137,7 +155,7 @@ 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
Returns a native instance 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

View File

@@ -1,8 +1,10 @@
const path = require('path');
const include = path.relative('.', __dirname);
module.exports = {
include: `"${__dirname}"`,
gyp: path.join(__dirname, 'node_api.gyp:nothing'),
include: include,
gyp: path.join(include, 'node_api.gyp:nothing'),
isNodeApiBuiltin: true,
needsFlag: false
};

View File

@@ -137,7 +137,7 @@ struct FinalizeData {
Hint* hint;
};
#if (NAPI_VERSION > 3)
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
template <typename ContextType=void,
typename Finalizer=std::function<void(Env, void*, ContextType*)>,
typename FinalizerDataType=void>
@@ -196,7 +196,7 @@ struct ThreadSafeFinalize {
FinalizerDataType* data;
Finalizer callback;
};
#endif
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
template <typename Getter, typename Setter>
struct AccessorCallbackData {
@@ -1740,8 +1740,14 @@ inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {
template <typename T>
inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
: TypedArray(env, value), _data(nullptr) {
napi_status status = napi_get_typedarray_info(
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
napi_status status = napi_ok;
if (value != nullptr) {
status = napi_get_typedarray_info(
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
} else {
_type = TypedArrayTypeForPrimitiveType<T>();
_length = 0;
}
NAPI_THROW_IF_FAILED_VOID(_env, status);
}
@@ -3146,10 +3152,11 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
napi_value wrapper = callbackInfo.This();
napi_status status;
napi_ref ref;
status = napi_wrap(env, wrapper, this, FinalizeCallback, nullptr, &ref);
T* instance = static_cast<T*>(this);
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
NAPI_THROW_IF_FAILED_VOID(env, status);
Reference<Object>* instanceRef = this;
Reference<Object>* instanceRef = instance;
*instanceRef = Reference<Object>(env, ref);
}
@@ -3872,7 +3879,7 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
template <typename T>
inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* /*hint*/) {
ObjectWrap<T>* instance = static_cast<ObjectWrap<T>*>(data);
T* instance = static_cast<T*>(data);
instance->Finalize(Napi::Env(env));
delete instance;
}
@@ -4295,7 +4302,7 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
}
}
#if (NAPI_VERSION > 3)
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
////////////////////////////////////////////////////////////////////////////////
// ThreadSafeFunction class
////////////////////////////////////////////////////////////////////////////////
@@ -4962,7 +4969,7 @@ template<class T>
inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(const T* data, size_t count) const {
_worker->SendProgress_(data, count);
}
#endif
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
////////////////////////////////////////////////////////////////////////////////
// Memory Management class

4
node_modules/node-addon-api/napi.h generated vendored
View File

@@ -2042,7 +2042,7 @@ namespace Napi {
bool _suppress_destruct;
};
#if (NAPI_VERSION > 3)
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
class ThreadSafeFunction {
public:
// This API may only be called from the main thread.
@@ -2405,7 +2405,7 @@ namespace Napi {
void Signal() const;
void SendProgress_(const T* data, size_t count);
};
#endif
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
// Memory management.
class MemoryManagement {

View File

@@ -59,6 +59,10 @@
"name": "Cory Mickelson",
"url": "https://github.com/corymickelson"
},
{
"name": "Daniel Bevenius",
"url": "https://github.com/danbev"
},
{
"name": "David Halls",
"url": "https://github.com/davedoesdev"
@@ -107,6 +111,10 @@
"name": "Jason Ginchereau",
"url": "https://github.com/jasongin"
},
{
"name": "Jeroen Janssen",
"url": "https://github.com/japj"
},
{
"name": "Jim Schlight",
"url": "https://github.com/jschlight"
@@ -119,6 +127,10 @@
"name": "joshgarde",
"url": "https://github.com/joshgarde"
},
{
"name": "Kasumi Hanazuki",
"url": "https://github.com/hanazuki"
},
{
"name": "Kelvin",
"url": "https://github.com/kelvinhammond"
@@ -139,6 +151,10 @@
"name": "legendecas",
"url": "https://github.com/legendecas"
},
{
"name": "Lovell Fuller",
"url": "https://github.com/lovell"
},
{
"name": "Luciano Martorella",
"url": "https://github.com/lmartorella"
@@ -272,5 +288,9 @@
"dev:incremental": "node test",
"doc": "doxygen doc/Doxyfile"
},
"version": "3.0.0"
}
"version": "3.0.1"
,"_resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.0.1.tgz"
,"_integrity": "sha512-YUpjl57P55u2yUaKX5Bgy4t5s6SCNYMg+62XNg+k41aYbBL1NgWrZfcgljR5MxDxHDjzl0qHDNtH6SkW4DXNCA=="
,"_from": "node-addon-api@3.0.1"
}