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 19, 2024
1 parent b9a1d12 commit a7c4fc8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 88 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ libffi-sys = { version = "^2.3.0" }
libc = "0.2"
indexmap = "2.0"
widestring = "1.1.0"
strum = "0.26"
strum_macros = "0.26"

[build-dependencies]
napi-build = "2.0.1"
Expand Down
2 changes: 1 addition & 1 deletion src/datatype/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub unsafe fn get_rs_value_from_pointer(
) -> RsArgsValue {
match type_desc {
RsArgsValue::I32(number) => {
let data = match number.to_basic_data_type() {
let data = match (*number).try_into().unwrap() {
BasicDataType::U8 => RsArgsValue::U8(*(pointer as *mut u8)),
BasicDataType::I16 => RsArgsValue::I16(*(pointer as *mut i16)),
BasicDataType::I32 => RsArgsValue::I32(*(pointer as *mut i32)),
Expand Down
13 changes: 4 additions & 9 deletions src/datatype/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ unsafe fn free_struct_memory(
continue;
}
if let RsArgsValue::I32(number) = val {
let data_type = number.to_basic_data_type();
let data_type = (*number).try_into().unwrap();
match data_type {
BasicDataType::U8 => {
let (size, align) = get_size_align::<c_uchar>();
Expand Down Expand Up @@ -176,8 +176,6 @@ unsafe fn free_struct_memory(
field_ptr = field_ptr.offset(padding as isize);
if dynamic_array {
free_dynamic_string_array(field_ptr, array_len);
} else {
//
}
offset += size + padding;
field_size = size
Expand All @@ -193,8 +191,6 @@ unsafe fn free_struct_memory(
field_ptr = field_ptr.offset(padding as isize);
if dynamic_array {
free_dynamic_double_array(field_ptr, array_len);
} else {
//
}
offset += size + padding;
field_size = size
Expand All @@ -210,8 +206,6 @@ unsafe fn free_struct_memory(
field_ptr = field_ptr.offset(padding as isize);
if dynamic_array {
free_dynamic_float_array(field_ptr, array_len);
} else {
//
}
offset += size + padding;
field_size = size
Expand Down Expand Up @@ -267,6 +261,7 @@ unsafe fn free_struct_memory(
let (size, align) = calculate_struct_size(&obj);
let padding = (align - (offset % align)) % align;
field_ptr = field_ptr.offset(padding as isize);
free_struct_memory(field_ptr, obj, ptr_type);
offset += size + padding;
field_size = size
} else {
Expand All @@ -290,7 +285,7 @@ pub unsafe fn free_rs_pointer_memory(
) {
match ptr_desc {
RsArgsValue::I32(number) => {
let basic_data_type = number.to_basic_data_type();
let basic_data_type = (*number).try_into().unwrap();
match basic_data_type {
BasicDataType::String => {
let _ = CString::from_raw(*(ptr as *mut *mut c_char));
Expand Down Expand Up @@ -365,7 +360,7 @@ pub unsafe fn free_c_pointer_memory(
) {
match ptr_desc {
RsArgsValue::I32(number) => {
let basic_data_type = number.to_basic_data_type();
let basic_data_type = (*number).try_into().unwrap();
match basic_data_type {
BasicDataType::String => {
free(*(ptr as *mut *mut i8) as *mut c_void);
Expand Down
2 changes: 1 addition & 1 deletion src/datatype/restore_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub unsafe fn create_rs_struct_from_pointer(
}
if let RsArgsValue::I32(number) = val {
let field = field.clone();
match number.to_basic_data_type() {
match (*number).try_into().unwrap() {
BasicDataType::U8 => {
let (size, align) = get_size_align::<c_uchar>();
let padding = (align - (offset % align)) % align;
Expand Down
104 changes: 35 additions & 69 deletions src/define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use napi::{Env, JsExternal, JsObject, JsUnknown};
use std::collections::HashMap;
use std::hash::Hash;
use std::rc::Rc;
use strum_macros::{EnumIter, FromRepr};

type StandardResult<T, E> = std::result::Result<T, E>;

#[derive(Debug)]
pub enum FFIError {
NapiError(Error<NapiStatus>),
UnExpectedError,
Expand Down Expand Up @@ -94,7 +98,7 @@ pub struct FFIFUNCDESC {
}

#[napi]
#[derive(Debug)]
#[derive(Debug, FromRepr)]
pub enum DataType {
String = 0,
I32 = 1,
Expand All @@ -115,8 +119,7 @@ pub enum DataType {
BigInt = 16,
I16 = 17,
}

#[derive(Debug)]
#[derive(Debug, FromRepr)]
pub enum BasicDataType {
String = 0,
I32 = 1,
Expand All @@ -133,7 +136,7 @@ pub enum BasicDataType {
I16 = 17,
}

#[derive(Debug)]
#[derive(Debug, FromRepr)]
pub enum RefDataType {
I32Array = 3,
StringArray = 4,
Expand All @@ -142,67 +145,37 @@ pub enum RefDataType {
FloatArray = 13,
}

pub trait ToDataType {
fn to_data_type(self) -> DataType;
fn to_basic_data_type(self) -> BasicDataType;
fn to_ref_data_type(self) -> RefDataType;
}
impl ToDataType for i32 {
fn to_data_type(self) -> DataType {
match self {
0 => DataType::String,
1 => DataType::I32,
2 => DataType::Double,
3 => DataType::I32Array,
4 => DataType::StringArray,
5 => DataType::DoubleArray,
6 => DataType::Boolean,
7 => DataType::Void,
8 => DataType::I64,
9 => DataType::U8,
10 => DataType::U8Array,
11 => DataType::External,
12 => DataType::U64,
13 => DataType::FloatArray,
14 => DataType::Float,
15 => DataType::WString,
16 => DataType::BigInt,
17 => DataType::I16,
_ => panic!("unknow DataType"),
}
impl TryFrom<i32> for DataType {
type Error = FFIError;
fn try_from(value: i32) -> StandardResult<Self, Self::Error> {
DataType::from_repr(value as usize).ok_or(FFIError::UnsupportedValueType(format!(
"Invalid DataType value: {}",
value
)))
}
fn to_basic_data_type(self) -> BasicDataType {
if is_array_type(&self) {
panic!(
}

impl TryFrom<i32> for BasicDataType {
type Error = FFIError;
fn try_from(value: i32) -> StandardResult<Self, Self::Error> {
if let Ok(_) = value.try_into() as StandardResult<RefDataType, FFIError> {
return Err(FFIError::UnsupportedValueType(format!(
"In the latest ffi-rs version, please use ffi-rs.arrayConstrutor to describe array type"
)
}
match self {
0 => BasicDataType::String,
1 => BasicDataType::I32,
2 => BasicDataType::Double,
6 => BasicDataType::Boolean,
7 => BasicDataType::Void,
8 => BasicDataType::I64,
9 => BasicDataType::U8,
11 => BasicDataType::External,
12 => BasicDataType::U64,
14 => BasicDataType::Float,
15 => BasicDataType::WString,
16 => BasicDataType::BigInt,
17 => BasicDataType::I16,
_ => panic!("unknow DataType"),
)));
}
BasicDataType::from_repr(value as usize).ok_or(FFIError::UnsupportedValueType(format!(
"Invalid BasicDataType value: {}",
value
)))
}
fn to_ref_data_type(self) -> RefDataType {
match self {
3 => RefDataType::I32Array,
4 => RefDataType::StringArray,
5 => RefDataType::DoubleArray,
10 => RefDataType::U8Array,
13 => RefDataType::FloatArray,
_ => panic!("unknow DataType"),
}
}
impl TryFrom<i32> for RefDataType {
type Error = FFIError;
fn try_from(value: i32) -> StandardResult<Self, Self::Error> {
RefDataType::from_repr(value as usize).ok_or(FFIError::UnsupportedValueType(format!(
"Invalid RefDataType value: {}",
value
)))
}
}
use libffi::middle::Type;
Expand All @@ -214,7 +187,7 @@ impl RsArgsTrait for RsArgsValue {
fn to_ffi_type(&self) -> Type {
match self {
RsArgsValue::I32(number) => {
let data_type = number.to_basic_data_type();
let data_type = (*number).try_into().unwrap();
match data_type {
BasicDataType::String => Type::pointer(),
BasicDataType::WString => Type::pointer(),
Expand All @@ -235,13 +208,6 @@ impl RsArgsTrait for RsArgsValue {
}
}

pub fn is_array_type(value: &i32) -> bool {
match value {
3 | 4 | 5 | 10 | 13 => true,
_ => false,
}
}

pub enum RsArgsValue {
String(String),
WString(String),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ unsafe fn load(env: Env, params: FFIParams) -> napi::Result<JsUnknown> {
) -> *mut ffi_type {
match ret_type_rs {
RsArgsValue::I32(number) => {
let ret_data_type = number.to_basic_data_type();
let ret_data_type = (*number).try_into().unwrap();
match ret_data_type {
BasicDataType::U8 => &mut ffi_type_uint8 as *mut ffi_type,
BasicDataType::I32 => &mut ffi_type_sint32 as *mut ffi_type,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/dataprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn get_array_desc(obj: &IndexMap<String, RsArgsValue>) -> FFIARRARYDESC {
if let RsArgsValue::I32(number) = obj.get(ARRAY_TYPE_TAG).unwrap() {
array_type = *number
}
let array_type = array_type.to_ref_data_type();
let array_type = array_type.try_into().unwrap();

FFIARRARYDESC {
array_len,
Expand Down Expand Up @@ -106,7 +106,7 @@ pub unsafe fn get_arg_types_values(
.map(|(param, value)| {
let res = match param {
RsArgsValue::I32(number) => {
let param_data_type = number.to_basic_data_type();
let param_data_type = (*number).try_into()?;
match param_data_type {
BasicDataType::U8 => {
let arg_type = &mut ffi_type_sint32 as *mut ffi_type;
Expand Down Expand Up @@ -527,7 +527,7 @@ pub unsafe fn get_params_value_rs_struct(
let field = field.clone();
match field_type.clone() {
RsArgsValue::I32(data_type_number) => {
let data_type: DataType = data_type_number.to_data_type();
let data_type: DataType = data_type_number.try_into()?;
let val = match data_type {
DataType::String => {
let val: JsString = params_value_object.get_named_property(&field)?;
Expand Down Expand Up @@ -760,7 +760,7 @@ pub unsafe fn get_js_unknown_from_pointer(
) -> Result<JsUnknown> {
match ret_type_rs {
RsArgsValue::I32(number) => {
let ret_data_type = number.to_basic_data_type();
let ret_data_type = (*number).try_into()?;
match ret_data_type {
BasicDataType::String => {
let ptr_str = CStr::from_ptr(*(ptr as *mut *const c_char))
Expand Down Expand Up @@ -848,7 +848,7 @@ pub unsafe fn get_js_unknown_from_pointer(
unsafe fn write_rs_ptr_to_c(ret_type: &RsArgsValue, src: *mut c_void, dst: *mut c_void) {
match &ret_type {
RsArgsValue::I32(number) => {
let ret_data_type = number.to_basic_data_type();
let ret_data_type = (*number).try_into().unwrap();
match ret_data_type {
BasicDataType::U8 => std::ptr::copy(src, dst, std::mem::size_of::<u8>()),
BasicDataType::I16 => std::ptr::copy(src, dst, std::mem::size_of::<i16>()),
Expand Down
4 changes: 2 additions & 2 deletions src/utils/object_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub fn calculate_struct_size(struct_type: &IndexMap<String, RsArgsValue>) -> (us
if field_name == FFI_TAG_FIELD {
return (size, align, offset);
}
if let RsArgsValue::I32(number) = field_type {
return match number.to_basic_data_type() {
if let RsArgsValue::I32(field_type_number) = field_type {
return match (*field_type_number).try_into().unwrap() {
BasicDataType::U8 => calculate_u8(size, align, offset),
BasicDataType::I16 => calculate_i16(size, align, offset),
BasicDataType::I32 => calculate_i32(size, align, offset),
Expand Down

0 comments on commit a7c4fc8

Please sign in to comment.