Skip to content

Commit

Permalink
chore: code optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Nov 17, 2024
1 parent 7df6d91 commit 8b4a9db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
16 changes: 13 additions & 3 deletions src/define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@ pub struct FFICALLPARAMS {
pub errno: Option<bool>,
pub free_result_memory: bool,
pub params_type_rs: Rc<Vec<RsArgsValue>>,
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 {}
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,8 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
let (mut arg_types, arg_values) = get_arg_types_values(Rc::clone(&params_type_rs), params_value)?;
let mut arg_values_c_void = get_value_pointer(&env, Rc::clone(&params_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,
Expand Down Expand Up @@ -275,11 +273,12 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
}

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,
Expand All @@ -302,7 +301,7 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
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;
Expand All @@ -319,7 +318,6 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
let FFICALLPARAMS {
arg_values_c_void, ..
} = &mut self.data;

unsafe {
let result = libc::malloc(std::mem::size_of::<*mut c_void>());
ffi_call(
Expand Down Expand Up @@ -372,8 +370,7 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
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())
Expand Down
3 changes: 2 additions & 1 deletion src/utils/dataprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8b4a9db

Please sign in to comment.