File tree 2 files changed +14
-2
lines changed
2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -387,7 +387,7 @@ impl JsEnv {
387
387
Ok ( js_constructor)
388
388
}
389
389
390
- pub fn create_reference ( & self , cons : napi_value , count : u32 ) -> Result < napi_ref , NjError > {
390
+ pub fn create_reference ( & self , cons : napi_value , count : u32 ) -> Result < napi_ref , NjError > {
391
391
392
392
let mut result = ptr:: null_mut ( ) ;
393
393
napi_call_result ! (
@@ -402,6 +402,9 @@ impl JsEnv {
402
402
Ok ( result)
403
403
}
404
404
405
+ pub fn delete_reference ( & self , ref_ : napi_ref ) -> Result < ( ) , NjError > {
406
+ Ok ( napi_call_result ! ( crate :: sys:: napi_delete_reference( self . 0 , ref_) ) ?)
407
+ }
405
408
406
409
pub fn get_new_target ( & self , info : napi_callback_info ) -> Result < napi_value , NjError > {
407
410
Original file line number Diff line number Diff line change @@ -47,6 +47,15 @@ impl <T>JSObjectWrapper<T> where T: JSClass {
47
47
rust_ref. wrapper = wrap;
48
48
}
49
49
50
+ // Finally, remove the reference in response to finalize callback
51
+ // See footnote on `napi_wrap` documentation: https://nodejs.org/api/n-api.html#n_api_napi_wrap
52
+ //
53
+ // "Caution: The optional returned reference (if obtained) should be deleted via napi_delete_reference
54
+ // ONLY in response to the finalize callback invocation. If it is deleted before then,
55
+ // then the finalize callback may never be invoked. Therefore, when obtaining a reference a
56
+ // finalize callback is also required in order to enable correct disposal of the reference."
57
+ js_env. delete_reference ( wrap) ;
58
+
50
59
Ok ( js_cb. this_owned ( ) )
51
60
}
52
61
}
@@ -153,7 +162,7 @@ pub trait JSClass: Sized {
153
162
debug ! ( "my object finalize" ) ;
154
163
unsafe {
155
164
let ptr: * mut JSObjectWrapper < Self > = finalize_data as * mut JSObjectWrapper < Self > ;
156
- let _rust = Box :: from_raw ( ptr) ;
165
+ Box :: from_raw ( ptr) ;
157
166
}
158
167
159
168
}
You can’t perform that action at this time.
0 commit comments