Merge pull request #377 from napi-rs/size-hint-in-external

Size hint in external
This commit is contained in:
LongYinan 2020-12-22 18:47:01 +08:00 committed by GitHub
commit 28b20b1894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

View file

@ -297,10 +297,6 @@ impl Env {
&mut raw_value,
)
})?;
let mut changed = 0;
check_status!(unsafe {
sys::napi_adjust_external_memory(self.0, length as i64, &mut changed)
})?;
Ok(JsBufferValue::new(
JsBuffer(Value {
env: self.0,
@ -416,10 +412,6 @@ impl Env {
&mut raw_value,
)
})?;
let mut changed = 0;
check_status!(unsafe {
sys::napi_adjust_external_memory(self.0, length as i64, &mut changed)
})?;
Ok(JsArrayBufferValue::new(
JsArrayBuffer(Value {
@ -685,7 +677,14 @@ impl Env {
}
#[inline]
pub fn create_external<T: 'static>(&self, native_object: T) -> Result<JsExternal> {
/// If `size_hint` provided, `Env::adjust_external_memory` will be called under the hood.
/// If no `size_hint` provided, global garbage collections will be triggered less times than expected.
/// If getting the exact `native_object` size is difficult, you can provide an approximate value, it's only effect to the GC.
pub fn create_external<T: 'static>(
&self,
native_object: T,
size_hint: Option<i64>,
) -> Result<JsExternal> {
let mut object_value = ptr::null_mut();
check_status!(unsafe {
sys::napi_create_external(
@ -696,6 +695,11 @@ impl Env {
&mut object_value,
)
})?;
if let Some(size_hint) = size_hint {
check_status!(unsafe {
sys::napi_adjust_external_memory(self.0, size_hint, ptr::null_mut())
})?;
};
Ok(unsafe { JsExternal::from_raw_unchecked(self.0, object_value) })
}
@ -1024,17 +1028,13 @@ impl Env {
}
unsafe extern "C" fn drop_buffer(
env: sys::napi_env,
_env: sys::napi_env,
finalize_data: *mut c_void,
hint: *mut c_void,
) {
let length_ptr = hint as *mut (usize, usize);
let (length, cap) = *Box::from_raw(length_ptr);
mem::drop(Vec::from_raw_parts(finalize_data as *mut u8, length, cap));
let mut changed = 0;
let adjust_external_memory_status =
sys::napi_adjust_external_memory(env, -(length as i64), &mut changed);
debug_assert!(Status::from(adjust_external_memory_status) == Status::Ok);
}
unsafe extern "C" fn raw_finalize<T>(

View file

@ -7,7 +7,7 @@ fn add_cleanup_hook(mut env: Env) -> ContextlessResult<JsExternal> {
let hook = env.add_env_cleanup_hook((), |_| {
println!("cleanup hook executed");
})?;
env.create_external(hook).map(Some)
env.create_external(hook, None).map(Some)
}
#[js_function(1)]

View file

@ -10,7 +10,7 @@ struct NativeObject {
pub fn create_external(ctx: CallContext) -> Result<JsExternal> {
let count = ctx.get::<JsNumber>(0)?.try_into()?;
let native = NativeObject { count };
ctx.env.create_external(native)
ctx.env.create_external(native, None)
}
#[js_function(1)]