File tree Expand file tree Collapse file tree 5 files changed +57
-1
lines changed Expand file tree Collapse file tree 5 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,17 @@ property of the `Napi::CallbackInfo`.
190
190
191
191
Returns a ` Napi::Function ` representing the constructor function for the class.
192
192
193
+ ### Finalize
194
+
195
+ Provides an opportunity to run cleanup code that requires access to the ` Napi::Env `
196
+ before the wrapped native object instance is freed. Override to implement.
197
+
198
+ ``` cpp
199
+ virtual void Finalize (Napi::Env env);
200
+ ```
201
+
202
+ - `[in] env`: `Napi::Env`.
203
+
193
204
### StaticMethod
194
205
195
206
Creates property descriptor that represents a static method of a JavaScript class.
Original file line number Diff line number Diff line change @@ -2888,6 +2888,9 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
2888
2888
*instanceRef = Reference<Object>(env, ref);
2889
2889
}
2890
2890
2891
+ template <typename T>
2892
+ inline ObjectWrap<T>::~ObjectWrap () {}
2893
+
2891
2894
template <typename T>
2892
2895
inline T* ObjectWrap<T>::Unwrap(Object wrapper) {
2893
2896
T* unwrapped;
@@ -3261,6 +3264,9 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
3261
3264
return desc;
3262
3265
}
3263
3266
3267
+ template <typename T>
3268
+ inline void ObjectWrap<T>::Finalize(Napi::Env /* env*/ ) {}
3269
+
3264
3270
template <typename T>
3265
3271
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
3266
3272
napi_env env,
@@ -3402,8 +3408,9 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
3402
3408
}
3403
3409
3404
3410
template <typename T>
3405
- inline void ObjectWrap<T>::FinalizeCallback(napi_env /* env*/ , void * data, void * /* hint*/ ) {
3411
+ inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void * data, void * /* hint*/ ) {
3406
3412
T* instance = reinterpret_cast <T*>(data);
3413
+ instance->Finalize (Napi::Env (env));
3407
3414
delete instance;
3408
3415
}
3409
3416
Original file line number Diff line number Diff line change @@ -1570,6 +1570,7 @@ namespace Napi {
1570
1570
class ObjectWrap : public Reference <Object> {
1571
1571
public:
1572
1572
ObjectWrap (const CallbackInfo& callbackInfo);
1573
+ virtual ~ObjectWrap ();
1573
1574
1574
1575
static T* Unwrap (Object wrapper);
1575
1576
@@ -1657,6 +1658,7 @@ namespace Napi {
1657
1658
static PropertyDescriptor InstanceValue (Symbol name,
1658
1659
Napi::Value value,
1659
1660
napi_property_attributes attributes = napi_default);
1661
+ virtual void Finalize (Napi::Env env);
1660
1662
1661
1663
private:
1662
1664
static napi_value ConstructorCallbackWrapper (napi_env env, napi_callback_info info);
Original file line number Diff line number Diff line change @@ -24,6 +24,11 @@ class Test : public Napi::ObjectWrap<Test> {
24
24
public:
25
25
Test (const Napi::CallbackInfo& info) :
26
26
Napi::ObjectWrap<Test>(info) {
27
+
28
+ if (info.Length () > 0 ) {
29
+ finalizeCb_ = Napi::Persistent (info[0 ].As <Napi::Function>());
30
+ }
31
+
27
32
}
28
33
29
34
void Setter (const Napi::CallbackInfo& /* info*/ , const Napi::Value& value) {
@@ -105,8 +110,20 @@ class Test : public Napi::ObjectWrap<Test> {
105
110
}));
106
111
}
107
112
113
+ void Finalize (Napi::Env env) {
114
+
115
+ if (finalizeCb_.IsEmpty ()) {
116
+ return ;
117
+ }
118
+
119
+ finalizeCb_.Call (env.Global (), {Napi::Boolean::New (env, true )});
120
+ finalizeCb_.Unref ();
121
+
122
+ }
123
+
108
124
private:
109
125
std::string value_;
126
+ Napi::FunctionReference finalizeCb_;
110
127
};
111
128
112
129
Napi::Object InitObjectWrap (Napi::Env env) {
Original file line number Diff line number Diff line change @@ -182,6 +182,24 @@ const test = (binding) => {
182
182
}
183
183
} ;
184
184
185
+ const testFinalize = ( clazz ) => {
186
+
187
+ let finalizeCalled = false ;
188
+ const finalizeCb = function ( called ) {
189
+ finalizeCalled = called ;
190
+ } ;
191
+
192
+ //Scope Test instance so that it can be gc'd.
193
+ ( function ( ) {
194
+ new Test ( finalizeCb ) ;
195
+ } ) ( ) ;
196
+
197
+ global . gc ( ) ;
198
+
199
+ assert . strictEqual ( finalizeCalled , true ) ;
200
+
201
+ } ;
202
+
185
203
const testObj = ( obj , clazz ) => {
186
204
testValue ( obj , clazz ) ;
187
205
testAccessor ( obj , clazz ) ;
@@ -198,6 +216,7 @@ const test = (binding) => {
198
216
testStaticMethod ( clazz ) ;
199
217
200
218
testStaticEnumerables ( clazz ) ;
219
+ testFinalize ( clazz ) ;
201
220
} ;
202
221
203
222
// `Test` is needed for accessing exposed symbols
You can’t perform that action at this time.
0 commit comments