diff --git a/src/define.rs b/src/define.rs index 3fa0a34..2095ff4 100644 --- a/src/define.rs +++ b/src/define.rs @@ -378,8 +378,7 @@ pub struct FFICALLPARAMS { pub errno: Option, pub free_result_memory: bool, pub params_type_rs: Rc>, - pub r_type: *mut ffi_type, - pub arg_types: Vec<*mut ffi_type>, + pub ffi_type_cleanup: FFITypeCleanup, } pub struct BarePointerWrap(pub *mut c_void); unsafe impl Send for FFICALL {} @@ -429,8 +428,19 @@ pub struct OpenParams { pub struct FFITypeCleanup { pub struct_type_box: Option<*mut ffi_type>, pub elements_box: Option<*mut Vec<*mut ffi_type>>, + pub r_type: Option<*mut ffi_type>, + pub arg_types: Vec<*mut ffi_type>, +} +impl FFITypeCleanup { + pub fn new() -> Self { + Self { + struct_type_box: None, + elements_box: None, + r_type: None, + arg_types: vec![], + } + } } - impl Drop for FFITypeCleanup { fn drop(&mut self) { unsafe { diff --git a/src/lib.rs b/src/lib.rs index b091bc6..b080f79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -223,10 +223,8 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result { let (mut arg_types, arg_values) = get_arg_types_values(Rc::clone(¶ms_type_rs), params_value)?; let mut arg_values_c_void = get_value_pointer(&env, Rc::clone(¶ms_type_rs), arg_values)?; let ret_type_rs = type_define_to_rs_args(&env, ret_type)?; - let mut ffi_type_cleanup = FFITypeCleanup { - struct_type_box: None, - elements_box: None, - }; + let mut ffi_type_cleanup = FFITypeCleanup::new(); + unsafe fn get_ffi_type( ret_type_rs: &RsArgsValue, ffi_type_cleanup: &mut FFITypeCleanup, @@ -275,11 +273,12 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result { } let r_type = get_ffi_type(&ret_type_rs, &mut ffi_type_cleanup); - + ffi_type_cleanup.r_type = Some(r_type); + ffi_type_cleanup.arg_types = arg_types; let mut cif = ffi_cif { abi: ffi_abi_FFI_DEFAULT_ABI, nargs: params_type_len as u32, - arg_types: arg_types.as_mut_ptr(), + arg_types: ffi_type_cleanup.arg_types.as_mut_ptr(), rtype: r_type, bytes: 0, flags: 0, @@ -302,7 +301,7 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result { ffi_abi_FFI_DEFAULT_ABI, params_type_len as u32, r_type, - arg_types.as_mut_ptr(), + ffi_type_cleanup.arg_types.as_mut_ptr(), ); if run_in_new_thread == Some(true) { use napi::Task; @@ -319,7 +318,6 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result { let FFICALLPARAMS { arg_values_c_void, .. } = &mut self.data; - unsafe { let result = libc::malloc(std::mem::size_of::<*mut c_void>()); ffi_call( @@ -372,8 +370,7 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result { errno, free_result_memory, params_type_rs, - r_type, - arg_types, + ffi_type_cleanup, }); let async_work_promise = env.spawn(task)?; Ok(async_work_promise.promise_object().into_unknown()) diff --git a/src/utils/dataprocess.rs b/src/utils/dataprocess.rs index a6bf514..ba80223 100644 --- a/src/utils/dataprocess.rs +++ b/src/utils/dataprocess.rs @@ -328,7 +328,7 @@ pub unsafe fn get_value_pointer( let c_double = Box::new(val); Ok(Box::into_raw(c_double) as *mut c_void) } - RsArgsValue::U8Array(buffer, v) => { + RsArgsValue::U8Array(buffer, _) => { let buffer = buffer.unwrap(); let ptr = buffer.as_ptr(); std::mem::forget(buffer); @@ -385,6 +385,7 @@ pub unsafe fn get_value_pointer( let func_ret_type = if func_desc.get(RET_TYPE).is_some() { func_desc.get(RET_TYPE).unwrap().clone() } else { + // void type RsArgsValue::I32(7) }; let free_c_params_memory = func_desc.get(FREE_FUNCTION_TAG).unwrap().clone();