Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TODOs to address memory leaks #343

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ npm install
npm test
```

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)**

<a name="resources"></a>
Expand Down
3 changes: 3 additions & 0 deletions doc/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ To use **N-API** in a native module:

At build time, the N-API back-compat library code will be used only when the
targeted node version *does not* have N-API built-in.

The preprocessor directive `NODE_ADDON_API_DISABLE_DEPRECATED` can be defined at
compile time to skip the definition of deprecated APIs.
9 changes: 5 additions & 4 deletions test/binding.gyp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
'variables': {
'NAPI_VERSION%': ""
'NAPI_VERSION%': "",
'disable_deprecated': "<!(node -p \"process.env['npm_config_disable_deprecated']\")"
},
'target_defaults': {
'sources': [
Expand Down Expand Up @@ -34,13 +35,13 @@
'thunking_manual.cc',
],
'conditions': [
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ]
['NAPI_VERSION!=""', { 'defines': ['NAPI_VERSION=<@(NAPI_VERSION)'] } ],
['disable_deprecated=="true"', { 'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED'] }]
],
'include_dirs': ["<!@(node -p \"require('../').include\")"],
'dependencies': ["<!(node -p \"require('../').gyp\")"],
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
'defines': ['NODE_ADDON_API_DISABLE_DEPRECATED']
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
},
'targets': [
{
Expand Down
30 changes: 30 additions & 0 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,22 @@ void DefineProperties(const CallbackInfo& info) {

if (nameType.Utf8Value() == "literal") {
obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor("readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor("readwriteAccessor", TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj, "readonlyAccessor", TestGetter),
PropertyDescriptor::Accessor(env, obj, "readwriteAccessor", TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value("readonlyValue", trueValue),
PropertyDescriptor::Value("readwriteValue", trueValue, napi_writable),
PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable),
PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function("function", TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env, "function", TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
Expand All @@ -83,20 +92,36 @@ void DefineProperties(const CallbackInfo& info) {
std::string str7("function");

obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(str1, TestGetter),
PropertyDescriptor::Accessor(str2, TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj, str1, TestGetter),
PropertyDescriptor::Accessor(env, obj, str2, TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value(str3, trueValue),
PropertyDescriptor::Value(str4, trueValue, napi_writable),
PropertyDescriptor::Value(str5, trueValue, napi_enumerable),
PropertyDescriptor::Value(str6, trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(str7, TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env, str7, TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to test both the deprecated and non-deprecated so this should just be #ifndef, #endif as opposed to using #else ? Same goes to all of the other places as well.

});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(
Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Accessor(env, obj,
Napi::String::New(env, "readonlyAccessor"), TestGetter),
PropertyDescriptor::Accessor(env, obj,
Napi::String::New(env, "readwriteAccessor"), TestGetter, TestSetter),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Value(
Napi::String::New(env, "readonlyValue"), trueValue),
PropertyDescriptor::Value(
Expand All @@ -105,8 +130,13 @@ void DefineProperties(const CallbackInfo& info) {
Napi::String::New(env, "enumerableValue"), trueValue, napi_enumerable),
PropertyDescriptor::Value(
Napi::String::New(env, "configurableValue"), trueValue, napi_configurable),
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(
Napi::String::New(env, "function"), TestFunction),
#else // NODE_ADDON_API_DISABLE_DEPRECATED
PropertyDescriptor::Function(env,
Napi::String::New(env, "function"), TestFunction),
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
});
}
}
Expand Down