From fa23769e9d0738afb133925f30a057330679d37f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 30 Nov 2021 17:14:01 +0800 Subject: [PATCH] feat(napi): allow create reference with refcount --- crates/napi/src/env.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index 8cbc723d..9d5e7502 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -809,7 +809,7 @@ impl Env { } } - /// This API create a new reference with the specified reference count to the Object passed in. + /// This API create a new reference with the initial 1 ref count to the Object passed in. pub fn create_reference(&self, value: T) -> Result> where T: NapiRaw, @@ -828,6 +828,24 @@ impl Env { }) } + /// This API create a new reference with the specified reference count to the Object passed in. + pub fn create_reference_with_refcount(&self, value: T, ref_count: u32) -> Result> + where + T: NapiRaw, + { + let mut raw_ref = ptr::null_mut(); + let raw_value = unsafe { value.raw() }; + check_status!(unsafe { + sys::napi_create_reference(self.0, raw_value, ref_count, &mut raw_ref) + })?; + Ok(Ref { + raw_ref, + count: ref_count, + inner: (), + raw_value, + }) + } + /// Get reference value from `Ref` with type check /// /// Return error if the type of `reference` provided is mismatched with `T`