fix(napi,napi-derive): ffi lifetime and pointer sound issues (#2216)

* The ownership of the CString will be taken in map function

* for empty struct like `#[napi] struct A;`, the `value_ref` will be `0x1`, and it will be overwritten by the others instance of the same class
This commit is contained in:
sjh 2024-08-18 18:21:13 +08:00 committed by GitHub
parent 2e5bf773e3
commit 09a79edfb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -281,7 +281,10 @@ impl NapiStruct {
val: #name
) -> napi::Result<napi::bindgen_prelude::sys::napi_value> {
if let Some(ctor_ref) = napi::__private::get_class_constructor(#js_name_str) {
let wrapped_value = Box::into_raw(Box::new(val));
let mut wrapped_value = Box::into_raw(Box::new(val));
if wrapped_value as usize == 0x1 {
wrapped_value = Box::into_raw(Box::new(0u8)).cast();
}
let instance_value = #name::new_instance(env, wrapped_value.cast(), ctor_ref)?;
#iterator_implementation
Ok(instance_value)
@ -299,7 +302,10 @@ impl NapiStruct {
pub fn into_reference(val: #name, env: napi::Env) -> napi::Result<napi::bindgen_prelude::Reference<#name>> {
if let Some(ctor_ref) = napi::bindgen_prelude::get_class_constructor(#js_name_str) {
unsafe {
let wrapped_value = Box::into_raw(Box::new(val));
let mut wrapped_value = Box::into_raw(Box::new(val));
if wrapped_value as usize == 0x1 {
wrapped_value = Box::into_raw(Box::new(0u8)).cast();
}
let instance_value = #name::new_instance(env.raw(), wrapped_value.cast(), ctor_ref)?;
{
let env = env.raw();

View File

@ -680,7 +680,7 @@ impl Env {
check_status!(unsafe {
sys::napi_throw_error(
self.0,
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
code.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
msg.as_ptr(),
)
})
@ -693,7 +693,7 @@ impl Env {
check_status!(unsafe {
sys::napi_throw_range_error(
self.0,
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
code.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
msg.as_ptr(),
)
})
@ -706,7 +706,7 @@ impl Env {
check_status!(unsafe {
sys::napi_throw_type_error(
self.0,
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
code.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()),
msg.as_ptr(),
)
})