Skip to content

Commit 917bd60

Browse files
Gabriel Schulhofmhdawson
Gabriel Schulhof
authored andcommitted
src: remove TODOs by fixing memory leaks
PR-URL: #343 Fixes: #333 Reviewed-By: Michael Dawson <[email protected]>
1 parent dfcb939 commit 917bd60

15 files changed

+895
-125
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ npm install
133133
npm test
134134
```
135135

136+
To avoid testing the deprecated portions of the API run
137+
```
138+
npm install
139+
npm test --disable-deprecated
140+
```
141+
136142
Take a look and get inspired by our **[test suite](https://github.com/nodejs/node-addon-api/tree/master/test)**
137143

138144
<a name="resources"></a>

doc/property_descriptor.md

+102-12
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,31 @@ Value TestFunction(const CallbackInfo& info) {
2222
}
2323

2424
Void Init(Env env) {
25-
// Accessor
26-
PropertyDescriptor pd1 = PropertyDescriptor::Accessor("pd1", TestGetter);
27-
PropertyDescriptor pd2 = PropertyDescriptor::Accessor("pd2", TestGetter, TestSetter);
28-
29-
// Function
30-
PropertyDescriptor pd3 = PropertyDescriptor::Function("function", TestFunction);
31-
32-
// Value
33-
Boolean true_bool = Boolean::New(env, true);
34-
PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", TestFunction, napi_writable);
35-
36-
// Assign to an Object
25+
// Create an object.
3726
Object obj = Object::New(env);
27+
28+
// Accessor
29+
PropertyDescriptor pd1 = PropertyDescriptor::Accessor(env,
30+
obj,
31+
"pd1",
32+
TestGetter);
33+
PropertyDescriptor pd2 = PropertyDescriptor::Accessor(env,
34+
obj,
35+
"pd2",
36+
TestGetter,
37+
TestSetter);
38+
// Function
39+
PropertyDescriptor pd3 = PropertyDescriptor::Function(env,
40+
"function",
41+
TestFunction);
42+
// Value
43+
Boolean true_bool = Boolean::New(env, true);
44+
PropertyDescriptor pd4 =
45+
PropertyDescriptor::Value("boolean value",
46+
Napi::Boolean::New(env, true),
47+
napi_writable);
48+
49+
// Assign properties to the object.
3850
obj.DefineProperties({pd1, pd2, pd3, pd4});
3951
}
4052
```
@@ -71,6 +83,32 @@ The name of the property can be any of the following types:
7183
- `napi_value value`
7284
- `Napi::Name`
7385
86+
**This signature is deprecated. It will result in a memory leak if used.**
87+
88+
```cpp
89+
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (
90+
Napi::Env env,
91+
Napi::Object object,
92+
___ name,
93+
Getter getter,
94+
napi_property_attributes attributes = napi_default,
95+
void *data = nullptr);
96+
```
97+
98+
* `[in] env`: The environemnt in which to create this accessor.
99+
* `[in] object`: The object on which the accessor will be defined.
100+
* `[in] name`: The name used for the getter function.
101+
* `[in] getter`: A getter function.
102+
* `[in] attributes`: Potential attributes for the getter function.
103+
* `[in] data`: A pointer to data of any type, default is a null pointer.
104+
105+
Returns a `Napi::PropertyDescriptor` that contains a `Getter` accessor.
106+
107+
The name of the property can be any of the following types:
108+
- `const char*`
109+
- `const std::string &`
110+
- `Napi::Name`
111+
74112
```cpp
75113
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name,
76114
Getter getter,
@@ -93,6 +131,34 @@ The name of the property can be any of the following types:
93131
- `napi_value value`
94132
- `Napi::Name`
95133
134+
**This signature is deprecated. It will result in a memory leak if used.**
135+
136+
```cpp
137+
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Accessor (
138+
Napi::Env env,
139+
Napi::Object object,
140+
___ name,
141+
Getter getter,
142+
Setter setter,
143+
napi_property_attributes attributes = napi_default,
144+
void *data = nullptr);
145+
```
146+
147+
* `[in] env`: The environemnt in which to create this accessor.
148+
* `[in] object`: The object on which the accessor will be defined.
149+
* `[in] name`: The name of the getter and setter function.
150+
* `[in] getter`: The getter function.
151+
* `[in] setter`: The setter function.
152+
* `[in] attributes`: Potential attributes for the getter function.
153+
* `[in] data`: A pointer to data of any type, default is a null pointer.
154+
155+
Returns a `Napi::PropertyDescriptor` that contains a `Getter` and `Setter` function.
156+
157+
The name of the property can be any of the following types:
158+
- `const char*`
159+
- `const std::string &`
160+
- `Napi::Name`
161+
96162
### Function
97163

98164
```cpp
@@ -115,6 +181,30 @@ The name of the property can be any of the following types:
115181
- `napi_value value`
116182
- `Napi::Name`
117183
184+
**This signature is deprecated. It will result in a memory leak if used.**
185+
186+
```cpp
187+
static Napi::PropertyDescriptor Napi::PropertyDescriptor::Function (
188+
Napi::Env env,
189+
___ name,
190+
Callable cb,
191+
napi_property_attributes attributes = napi_default,
192+
void *data = nullptr);
193+
```
194+
195+
* `[in] env`: The environemnt in which to create this accessor.
196+
* `[in] name`: The name of the Callable function.
197+
* `[in] cb`: The function
198+
* `[in] attributes`: Potential attributes for the getter function.
199+
* `[in] data`: A pointer to data of any type, default is a null pointer.
200+
201+
Returns a `Napi::PropertyDescriptor` that contains a callable `Napi::Function`.
202+
203+
The name of the property can be any of the following types:
204+
- `const char*`
205+
- `const std::string &`
206+
- `Napi::Name`
207+
118208
### Value
119209

120210
```cpp

doc/setup.md

+3
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ To use **N-API** in a native module:
6666

6767
At build time, the N-API back-compat library code will be used only when the
6868
targeted node version *does not* have N-API built-in.
69+
70+
The preprocessor directive `NODE_ADDON_API_DISABLE_DEPRECATED` can be defined at
71+
compile time before including `napi.h` to skip the definition of deprecated APIs.

napi-inl.deprecated.h

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#ifndef SRC_NAPI_INL_DEPRECATED_H_
2+
#define SRC_NAPI_INL_DEPRECATED_H_
3+
4+
////////////////////////////////////////////////////////////////////////////////
5+
// PropertyDescriptor class
6+
////////////////////////////////////////////////////////////////////////////////
7+
8+
template <typename Getter>
9+
inline PropertyDescriptor
10+
PropertyDescriptor::Accessor(const char* utf8name,
11+
Getter getter,
12+
napi_property_attributes attributes,
13+
void* /*data*/) {
14+
typedef details::CallbackData<Getter, Napi::Value> CbData;
15+
// TODO: Delete when the function is destroyed
16+
auto callbackData = new CbData({ getter, nullptr });
17+
18+
return PropertyDescriptor({
19+
utf8name,
20+
nullptr,
21+
nullptr,
22+
CbData::Wrapper,
23+
nullptr,
24+
nullptr,
25+
attributes,
26+
callbackData
27+
});
28+
}
29+
30+
template <typename Getter>
31+
inline PropertyDescriptor PropertyDescriptor::Accessor(const std::string& utf8name,
32+
Getter getter,
33+
napi_property_attributes attributes,
34+
void* data) {
35+
return Accessor(utf8name.c_str(), getter, attributes, data);
36+
}
37+
38+
template <typename Getter>
39+
inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name,
40+
Getter getter,
41+
napi_property_attributes attributes,
42+
void* /*data*/) {
43+
typedef details::CallbackData<Getter, Napi::Value> CbData;
44+
// TODO: Delete when the function is destroyed
45+
auto callbackData = new CbData({ getter, nullptr });
46+
47+
return PropertyDescriptor({
48+
nullptr,
49+
name,
50+
nullptr,
51+
CbData::Wrapper,
52+
nullptr,
53+
nullptr,
54+
attributes,
55+
callbackData
56+
});
57+
}
58+
59+
template <typename Getter>
60+
inline PropertyDescriptor PropertyDescriptor::Accessor(Name name,
61+
Getter getter,
62+
napi_property_attributes attributes,
63+
void* data) {
64+
napi_value nameValue = name;
65+
return PropertyDescriptor::Accessor(nameValue, getter, attributes, data);
66+
}
67+
68+
template <typename Getter, typename Setter>
69+
inline PropertyDescriptor PropertyDescriptor::Accessor(const char* utf8name,
70+
Getter getter,
71+
Setter setter,
72+
napi_property_attributes attributes,
73+
void* /*data*/) {
74+
typedef details::AccessorCallbackData<Getter, Setter> CbData;
75+
// TODO: Delete when the function is destroyed
76+
auto callbackData = new CbData({ getter, setter });
77+
78+
return PropertyDescriptor({
79+
utf8name,
80+
nullptr,
81+
nullptr,
82+
CbData::GetterWrapper,
83+
CbData::SetterWrapper,
84+
nullptr,
85+
attributes,
86+
callbackData
87+
});
88+
}
89+
90+
template <typename Getter, typename Setter>
91+
inline PropertyDescriptor PropertyDescriptor::Accessor(const std::string& utf8name,
92+
Getter getter,
93+
Setter setter,
94+
napi_property_attributes attributes,
95+
void* data) {
96+
return Accessor(utf8name.c_str(), getter, setter, attributes, data);
97+
}
98+
99+
template <typename Getter, typename Setter>
100+
inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name,
101+
Getter getter,
102+
Setter setter,
103+
napi_property_attributes attributes,
104+
void* /*data*/) {
105+
typedef details::AccessorCallbackData<Getter, Setter> CbData;
106+
// TODO: Delete when the function is destroyed
107+
auto callbackData = new CbData({ getter, setter });
108+
109+
return PropertyDescriptor({
110+
nullptr,
111+
name,
112+
nullptr,
113+
CbData::GetterWrapper,
114+
CbData::SetterWrapper,
115+
nullptr,
116+
attributes,
117+
callbackData
118+
});
119+
}
120+
121+
template <typename Getter, typename Setter>
122+
inline PropertyDescriptor PropertyDescriptor::Accessor(Name name,
123+
Getter getter,
124+
Setter setter,
125+
napi_property_attributes attributes,
126+
void* data) {
127+
napi_value nameValue = name;
128+
return PropertyDescriptor::Accessor(nameValue, getter, setter, attributes, data);
129+
}
130+
131+
template <typename Callable>
132+
inline PropertyDescriptor PropertyDescriptor::Function(const char* utf8name,
133+
Callable cb,
134+
napi_property_attributes attributes,
135+
void* /*data*/) {
136+
typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
137+
typedef details::CallbackData<Callable, ReturnType> CbData;
138+
// TODO: Delete when the function is destroyed
139+
auto callbackData = new CbData({ cb, nullptr });
140+
141+
return PropertyDescriptor({
142+
utf8name,
143+
nullptr,
144+
CbData::Wrapper,
145+
nullptr,
146+
nullptr,
147+
nullptr,
148+
attributes,
149+
callbackData
150+
});
151+
}
152+
153+
template <typename Callable>
154+
inline PropertyDescriptor PropertyDescriptor::Function(const std::string& utf8name,
155+
Callable cb,
156+
napi_property_attributes attributes,
157+
void* data) {
158+
return Function(utf8name.c_str(), cb, attributes, data);
159+
}
160+
161+
template <typename Callable>
162+
inline PropertyDescriptor PropertyDescriptor::Function(napi_value name,
163+
Callable cb,
164+
napi_property_attributes attributes,
165+
void* /*data*/) {
166+
typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
167+
typedef details::CallbackData<Callable, ReturnType> CbData;
168+
// TODO: Delete when the function is destroyed
169+
auto callbackData = new CbData({ cb, nullptr });
170+
171+
return PropertyDescriptor({
172+
nullptr,
173+
name,
174+
CbData::Wrapper,
175+
nullptr,
176+
nullptr,
177+
nullptr,
178+
attributes,
179+
callbackData
180+
});
181+
}
182+
183+
template <typename Callable>
184+
inline PropertyDescriptor PropertyDescriptor::Function(Name name,
185+
Callable cb,
186+
napi_property_attributes attributes,
187+
void* data) {
188+
napi_value nameValue = name;
189+
return PropertyDescriptor::Function(nameValue, cb, attributes, data);
190+
}
191+
192+
#endif // !SRC_NAPI_INL_DEPRECATED_H_

0 commit comments

Comments
 (0)