feat(napi): allow create reference with refcount

This commit is contained in:
LongYinan 2021-11-30 17:14:01 +08:00
parent 1a2c692a1a
commit fa23769e9d
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7

View file

@ -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<T>(&self, value: T) -> Result<Ref<()>>
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<T>(&self, value: T, ref_count: u32) -> Result<Ref<()>>
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`