Skip to content

Commit 717b602

Browse files
committed
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-api-gcc-4
2 parents 75dd422 + 61c9846 commit 717b602

16 files changed

+359
-189
lines changed

CHANGELOG.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
# node-addon-api Changelog
22

3+
## 2020-07-13 Version 3.0.1, @NickNaso
4+
5+
### Notable changes:
6+
7+
#### API
8+
9+
- Fixed the usage of `Napi::Reference` with `Napi::TypedArray`.
10+
- Fixed `Napi::ObjectWrap` inheritance.
11+
12+
#### Documentation
13+
14+
- Updated the example for `Napi::ObjectWrap`.
15+
- Added documentation for instance data APIs.
16+
- Some minor corrections all over the documentation.
17+
18+
#### TEST
19+
20+
- Fixed test for `Napi::ArrayBuffer` and `Napi::Buffer`.
21+
- Some minor corrections all over the test suite.
22+
23+
### Commits
24+
25+
* [[`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)
26+
* [[`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)
27+
* [[`48f6762bf6`](https://github.com/nodejs/node-addon-api/commit/48f6762bf6)] - **src**: add \_\_wasm32\_\_ guards (Gus Caplan)
28+
* [[`bd2c5ec502`](https://github.com/nodejs/node-addon-api/commit/bd2c5ec502)] - Fixes issue 745. (#748) (Nicola Del Gobbo)
29+
* [[`4c01af2d87`](https://github.com/nodejs/node-addon-api/commit/4c01af2d87)] - Fix typo in CHANGELOG (#715) (Kasumi Hanazuki)
30+
* [[`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)
31+
* [[`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)
32+
* [[`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)
33+
* [[`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)
34+
* [[`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)
35+
* [[`45cb1d9748`](https://github.com/nodejs/node-addon-api/commit/45cb1d9748)] - Correct AsyncProgressWorker link in README (#716) (Jeroen Janssen)
36+
* [[`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)
37+
338
## 2020-04-30 Version 3.0.0, @NickNaso
439

540
### Notable changes:
@@ -14,14 +49,14 @@
1449
- Added `Env::RunScript` method to run JavaScript code contained in a string.
1550
- Added templated version of `Napi::Function`.
1651
- Added benchmarking framework.
17-
- Added support for natove addon instance data.
52+
- Added support for native addon instance data.
1853
- Added `Napi::AsyncProgressQueueWorker` api.
1954
- Changed the guards to `NAPI_VERSION > 5`.
2055
- Removed N-API implementation (v6.x and v8.x support).
2156
- `Napi::AsyncWorker::OnWorkComplete` and `Napi::AsyncWorker::OnExecute` methods
2257
are override-able.
2358
- Removed erroneous finalizer cleanup in `Napi::ThreadSafeFunction`.
24-
- Disabled cahcing in `Napi::ArrayBuffer`.
59+
- Disabled caching in `Napi::ArrayBuffer`.
2560
- Explicitly disallow assign and copy operator.
2661
- Some minor corrections and improvements.
2762

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ to ideas specified in the **ECMA262 Language Specification**.
4747
- **[Contributors](#contributors)**
4848
- **[License](#license)**
4949

50-
## **Current version: 3.0.0**
50+
## **Current version: 3.0.1**
5151

5252
(See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
5353

doc/bigint.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Returns the number of words needed to store this `BigInt` value.
7979
### ToWords
8080

8181
```cpp
82-
void Napi::BigInt::ToWords(size_t* word_count, int* sign_bit, uint64_t* words);
82+
void Napi::BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
8383
```
8484
8585
- `[out] sign_bit`: Integer representing if the JavaScript `BigInt` is positive

doc/object_wrap.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,57 @@ your C++ class methods.
2222
class Example : public Napi::ObjectWrap<Example> {
2323
public:
2424
static Napi::Object Init(Napi::Env env, Napi::Object exports);
25-
Example(const Napi::CallbackInfo &info);
25+
Example(const Napi::CallbackInfo& info);
26+
static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
2627

2728
private:
28-
static Napi::FunctionReference constructor;
2929
double _value;
30-
Napi::Value GetValue(const Napi::CallbackInfo &info);
31-
Napi::Value SetValue(const Napi::CallbackInfo &info);
30+
Napi::Value GetValue(const Napi::CallbackInfo& info);
31+
Napi::Value SetValue(const Napi::CallbackInfo& info);
3232
};
3333

3434
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
3535
// This method is used to hook the accessor and method callbacks
3636
Napi::Function func = DefineClass(env, "Example", {
3737
InstanceMethod<&Example::GetValue>("GetValue"),
38-
InstanceMethod<&Example::SetValue>("SetValue")
38+
InstanceMethod<&Example::SetValue>("SetValue"),
39+
StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
3940
});
4041

42+
Napi::FunctionReference* constructor = new Napi::FunctionReference();
43+
4144
// Create a peristent reference to the class constructor. This will allow
4245
// a function called on a class prototype and a function
4346
// called on instance of a class to be distinguished from each other.
44-
constructor = Napi::Persistent(func);
45-
// Call the SuppressDestruct() method on the static data prevent the calling
46-
// to this destructor to reset the reference when the environment is no longer
47-
// available.
48-
constructor.SuppressDestruct();
47+
*constructor = Napi::Persistent(func);
4948
exports.Set("Example", func);
49+
50+
// Store the constructor as the add-on instance data. This will allow this
51+
// add-on to support multiple instances of itself running on multiple worker
52+
// threads, as well as multiple instances of itself running in different
53+
// contexts on the same thread.
54+
//
55+
// By default, the value set on the environment here will be destroyed when
56+
// the add-on is unloaded using the `delete` operator, but it is also
57+
// possible to supply a custom deleter.
58+
env.SetInstanceData<Napi::FunctionReference>(constructor);
59+
5060
return exports;
5161
}
5262

53-
Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) {
63+
Example::Example(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Example>(info) {
5464
Napi::Env env = info.Env();
5565
// ...
5666
Napi::Number value = info[0].As<Napi::Number>();
5767
this->_value = value.DoubleValue();
5868
}
5969

60-
Napi::FunctionReference Example::constructor;
61-
62-
Napi::Value Example::GetValue(const Napi::CallbackInfo &info){
70+
Napi::Value Example::GetValue(const Napi::CallbackInfo& info){
6371
Napi::Env env = info.Env();
6472
return Napi::Number::New(env, this->_value);
6573
}
6674

67-
Napi::Value Example::SetValue(const Napi::CallbackInfo &info){
75+
Napi::Value Example::SetValue(const Napi::CallbackInfo& info){
6876
Napi::Env env = info.Env();
6977
// ...
7078
Napi::Number value = info[0].As<Napi::Number>();
@@ -78,6 +86,16 @@ Napi::Object Init (Napi::Env env, Napi::Object exports) {
7886
return exports;
7987
}
8088

89+
// Create a new item using the constructor stored during Init.
90+
Napi::Value Example::CreateNewItem(const Napi::CallbackInfo& info) {
91+
// Retrieve the instance data we stored during `Init()`. We only stored the
92+
// constructor there, so we retrieve it here to create a new instance of the
93+
// JS class the constructor represents.
94+
Napi::FunctionReference* constructor =
95+
info.Env().GetInstanceData<Napi::FunctionReference>();
96+
return constructor->New({ Napi::Number::New(info.Env(), 42) });
97+
}
98+
8199
// Register and initialize native add-on
82100
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
83101
```
@@ -137,7 +155,7 @@ static T* Napi::ObjectWrap::Unwrap(Napi::Object wrapper);
137155

138156
* `[in] wrapper`: The JavaScript object that wraps the native instance.
139157

140-
Returns a native instace wrapped in a JavaScript object. Given the
158+
Returns a native instance wrapped in a JavaScript object. Given the
141159
Napi:Object, this allows a method to get a pointer to the wrapped
142160
C++ object and then reference fields, call methods, etc. within that class.
143161
In many cases calling Unwrap is not required, as methods can

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const path = require('path');
22

3+
const include = path.relative('.', __dirname);
4+
35
module.exports = {
4-
include: `"${__dirname}"`,
5-
gyp: path.join(__dirname, 'node_api.gyp:nothing'),
6+
include: include,
7+
gyp: path.join(include, 'node_api.gyp:nothing'),
68
isNodeApiBuiltin: true,
79
needsFlag: false
810
};

napi-inl.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct FinalizeData {
138138
Hint* hint;
139139
};
140140

141-
#if (NAPI_VERSION > 3)
141+
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
142142
template <typename ContextType=void,
143143
typename Finalizer=std::function<void(Env, void*, ContextType*)>,
144144
typename FinalizerDataType=void>
@@ -234,8 +234,8 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
234234
}
235235
return cb;
236236
}
237-
#endif
238-
#endif
237+
#endif // NAPI_VERSION > 4
238+
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
239239

240240
template <typename Getter, typename Setter>
241241
struct AccessorCallbackData {
@@ -1779,8 +1779,14 @@ inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {
17791779
template <typename T>
17801780
inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
17811781
: TypedArray(env, value), _data(nullptr) {
1782-
napi_status status = napi_get_typedarray_info(
1783-
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
1782+
napi_status status = napi_ok;
1783+
if (value != nullptr) {
1784+
status = napi_get_typedarray_info(
1785+
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
1786+
} else {
1787+
_type = TypedArrayTypeForPrimitiveType<T>();
1788+
_length = 0;
1789+
}
17841790
NAPI_THROW_IF_FAILED_VOID(_env, status);
17851791
}
17861792

@@ -3185,10 +3191,11 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
31853191
napi_value wrapper = callbackInfo.This();
31863192
napi_status status;
31873193
napi_ref ref;
3188-
status = napi_wrap(env, wrapper, this, FinalizeCallback, nullptr, &ref);
3194+
T* instance = static_cast<T*>(this);
3195+
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
31893196
NAPI_THROW_IF_FAILED_VOID(env, status);
31903197

3191-
Reference<Object>* instanceRef = this;
3198+
Reference<Object>* instanceRef = instance;
31923199
*instanceRef = Reference<Object>(env, ref);
31933200
}
31943201

@@ -3911,7 +3918,7 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
39113918

39123919
template <typename T>
39133920
inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* /*hint*/) {
3914-
ObjectWrap<T>* instance = static_cast<ObjectWrap<T>*>(data);
3921+
T* instance = static_cast<T*>(data);
39153922
instance->Finalize(Napi::Env(env));
39163923
delete instance;
39173924
}
@@ -4334,7 +4341,7 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
43344341
}
43354342
}
43364343

4337-
#if (NAPI_VERSION > 3)
4344+
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
43384345
////////////////////////////////////////////////////////////////////////////////
43394346
// ThreadSafeFunctionEx<ContextType,DataType,CallJs> class
43404347
////////////////////////////////////////////////////////////////////////////////
@@ -5358,7 +5365,7 @@ template<class T>
53585365
inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(const T* data, size_t count) const {
53595366
_worker->SendProgress_(data, count);
53605367
}
5361-
#endif
5368+
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
53625369

53635370
////////////////////////////////////////////////////////////////////////////////
53645371
// Memory Management class

0 commit comments

Comments
 (0)