From a7067d6732f893123c115d9d6b09bac090cc6568 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 29 Nov 2021 12:52:42 +0800 Subject: [PATCH] feat(sys): remove rust enum in sys --- crates/napi/src/js_values/arraybuffer.rs | 22 ++-- crates/napi/src/js_values/object.rs | 54 +++++---- crates/napi/src/js_values/object_property.rs | 22 ++-- crates/napi/src/promise.rs | 7 +- crates/napi/src/threadsafe_function.rs | 16 +-- crates/sys/Cargo.toml | 1 + crates/sys/src/lib.rs | 119 +++++++++++-------- 7 files changed, 133 insertions(+), 108 deletions(-) diff --git a/crates/napi/src/js_values/arraybuffer.rs b/crates/napi/src/js_values/arraybuffer.rs index 3fe70a1f..7e4ab9d5 100644 --- a/crates/napi/src/js_values/arraybuffer.rs +++ b/crates/napi/src/js_values/arraybuffer.rs @@ -88,19 +88,19 @@ pub enum TypedArrayType { impl From for TypedArrayType { fn from(value: sys::napi_typedarray_type) -> Self { match value { - sys::TypedarrayType::napi_int8_array => Self::Int8, - sys::TypedarrayType::napi_uint8_array => Self::Uint8, - sys::TypedarrayType::napi_uint8_clamped_array => Self::Uint8Clamped, - sys::TypedarrayType::napi_int16_array => Self::Int16, - sys::TypedarrayType::napi_uint16_array => Self::Uint16, - sys::TypedarrayType::napi_int32_array => Self::Int32, - sys::TypedarrayType::napi_uint32_array => Self::Uint32, - sys::TypedarrayType::napi_float32_array => Self::Float32, - sys::TypedarrayType::napi_float64_array => Self::Float64, + sys::TypedarrayType::int8_array => Self::Int8, + sys::TypedarrayType::uint8_array => Self::Uint8, + sys::TypedarrayType::uint8_clamped_array => Self::Uint8Clamped, + sys::TypedarrayType::int16_array => Self::Int16, + sys::TypedarrayType::uint16_array => Self::Uint16, + sys::TypedarrayType::int32_array => Self::Int32, + sys::TypedarrayType::uint32_array => Self::Uint32, + sys::TypedarrayType::float32_array => Self::Float32, + sys::TypedarrayType::float64_array => Self::Float64, #[cfg(feature = "napi6")] - sys::TypedarrayType::napi_bigint64_array => Self::BigInt64, + sys::TypedarrayType::bigint64_array => Self::BigInt64, #[cfg(feature = "napi6")] - sys::TypedarrayType::napi_biguint64_array => Self::BigUint64, + sys::TypedarrayType::biguint64_array => Self::BigUint64, _ => Self::Unknown, } } diff --git a/crates/napi/src/js_values/object.rs b/crates/napi/src/js_values/object.rs index 59fbab1a..ac45df40 100644 --- a/crates/napi/src/js_values/object.rs +++ b/crates/napi/src/js_values/object.rs @@ -98,8 +98,12 @@ impl TryFrom for KeyCollectionMode { fn try_from(value: sys::napi_key_collection_mode) -> Result { match value { - sys::napi_key_collection_mode::napi_key_include_prototypes => Ok(Self::IncludePrototypes), - sys::napi_key_collection_mode::napi_key_own_only => Ok(Self::OwnOnly), + sys::KeyCollectionMode::include_prototypes => Ok(Self::IncludePrototypes), + sys::KeyCollectionMode::own_only => Ok(Self::OwnOnly), + _ => Err(Error::new( + crate::Status::InvalidArg, + format!("Invalid key collection mode: {}", value), + )), } } } @@ -108,10 +112,8 @@ impl TryFrom for KeyCollectionMode { impl From for sys::napi_key_collection_mode { fn from(value: KeyCollectionMode) -> Self { match value { - KeyCollectionMode::IncludePrototypes => { - sys::napi_key_collection_mode::napi_key_include_prototypes - } - KeyCollectionMode::OwnOnly => sys::napi_key_collection_mode::napi_key_own_only, + KeyCollectionMode::IncludePrototypes => sys::KeyCollectionMode::include_prototypes, + KeyCollectionMode::OwnOnly => sys::KeyCollectionMode::own_only, } } } @@ -132,12 +134,16 @@ impl TryFrom for KeyFilter { fn try_from(value: sys::napi_key_filter) -> Result { match value { - sys::napi_key_filter::napi_key_all_properties => Ok(Self::AllProperties), - sys::napi_key_filter::napi_key_writable => Ok(Self::Writable), - sys::napi_key_filter::napi_key_enumerable => Ok(Self::Enumerable), - sys::napi_key_filter::napi_key_configurable => Ok(Self::Configurable), - sys::napi_key_filter::napi_key_skip_strings => Ok(Self::SkipStrings), - sys::napi_key_filter::napi_key_skip_symbols => Ok(Self::SkipSymbols), + sys::KeyFilter::all_properties => Ok(Self::AllProperties), + sys::KeyFilter::writable => Ok(Self::Writable), + sys::KeyFilter::enumerable => Ok(Self::Enumerable), + sys::KeyFilter::configurable => Ok(Self::Configurable), + sys::KeyFilter::skip_strings => Ok(Self::SkipStrings), + sys::KeyFilter::skip_symbols => Ok(Self::SkipSymbols), + _ => Err(Error::new( + crate::Status::InvalidArg, + format!("Invalid key filter [{}]", value), + )), } } } @@ -146,12 +152,12 @@ impl TryFrom for KeyFilter { impl From for sys::napi_key_filter { fn from(value: KeyFilter) -> Self { match value { - KeyFilter::AllProperties => Self::napi_key_all_properties, - KeyFilter::Writable => Self::napi_key_writable, - KeyFilter::Enumerable => Self::napi_key_enumerable, - KeyFilter::Configurable => Self::napi_key_configurable, - KeyFilter::SkipStrings => Self::napi_key_skip_strings, - KeyFilter::SkipSymbols => Self::napi_key_skip_symbols, + KeyFilter::AllProperties => sys::KeyFilter::all_properties, + KeyFilter::Writable => sys::KeyFilter::writable, + KeyFilter::Enumerable => sys::KeyFilter::enumerable, + KeyFilter::Configurable => sys::KeyFilter::configurable, + KeyFilter::SkipStrings => sys::KeyFilter::skip_strings, + KeyFilter::SkipSymbols => sys::KeyFilter::skip_symbols, } } } @@ -168,8 +174,12 @@ impl TryFrom for KeyConversion { fn try_from(value: sys::napi_key_conversion) -> Result { match value { - sys::napi_key_conversion::napi_key_keep_numbers => Ok(Self::KeepNumbers), - sys::napi_key_conversion::napi_key_numbers_to_strings => Ok(Self::NumbersToStrings), + sys::KeyConversion::keep_numbers => Ok(Self::KeepNumbers), + sys::KeyConversion::numbers_to_strings => Ok(Self::NumbersToStrings), + _ => Err(Error::new( + crate::Status::InvalidArg, + format!("Invalid key conversion [{}]", value), + )), } } } @@ -178,8 +188,8 @@ impl TryFrom for KeyConversion { impl From for sys::napi_key_conversion { fn from(value: KeyConversion) -> Self { match value { - KeyConversion::KeepNumbers => Self::napi_key_keep_numbers, - KeyConversion::NumbersToStrings => Self::napi_key_numbers_to_strings, + KeyConversion::KeepNumbers => sys::KeyConversion::keep_numbers, + KeyConversion::NumbersToStrings => sys::KeyConversion::numbers_to_strings, } } } diff --git a/crates/napi/src/js_values/object_property.rs b/crates/napi/src/js_values/object_property.rs index 0b824738..a14d8dfc 100644 --- a/crates/napi/src/js_values/object_property.rs +++ b/crates/napi/src/js_values/object_property.rs @@ -14,14 +14,14 @@ pub struct Property { pub(crate) is_ctor: bool, } -#[repr(u32)] +#[repr(i32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum PropertyAttributes { - Default = sys::napi_property_attributes::napi_default as _, - Writable = sys::napi_property_attributes::napi_writable as _, - Enumerable = sys::napi_property_attributes::napi_enumerable as _, - Configurable = sys::napi_property_attributes::napi_configurable as _, - Static = sys::napi_property_attributes::napi_static as _, + Default = sys::PropertyAttributes::default, + Writable = sys::PropertyAttributes::writable, + Enumerable = sys::PropertyAttributes::enumerable, + Configurable = sys::PropertyAttributes::configurable, + Static = sys::PropertyAttributes::static_, } impl Default for PropertyAttributes { @@ -33,11 +33,11 @@ impl Default for PropertyAttributes { impl From for sys::napi_property_attributes { fn from(value: PropertyAttributes) -> Self { match value { - PropertyAttributes::Default => sys::napi_property_attributes::napi_default, - PropertyAttributes::Writable => sys::napi_property_attributes::napi_writable, - PropertyAttributes::Enumerable => sys::napi_property_attributes::napi_enumerable, - PropertyAttributes::Configurable => sys::napi_property_attributes::napi_configurable, - PropertyAttributes::Static => sys::napi_property_attributes::napi_static, + PropertyAttributes::Default => sys::PropertyAttributes::default, + PropertyAttributes::Writable => sys::PropertyAttributes::writable, + PropertyAttributes::Enumerable => sys::PropertyAttributes::enumerable, + PropertyAttributes::Configurable => sys::PropertyAttributes::configurable, + PropertyAttributes::Static => sys::PropertyAttributes::static_, } } } diff --git a/crates/napi/src/promise.rs b/crates/napi/src/promise.rs index e8d33395..afe11bf7 100644 --- a/crates/napi/src/promise.rs +++ b/crates/napi/src/promise.rs @@ -78,15 +78,12 @@ pub(crate) async fn resolve_from_future { @@ -25,15 +23,11 @@ pub enum ThreadsafeFunctionCallMode { Blocking, } -impl From for napi_threadsafe_function_call_mode { +impl From for sys::napi_threadsafe_function_call_mode { fn from(value: ThreadsafeFunctionCallMode) -> Self { match value { - ThreadsafeFunctionCallMode::Blocking => { - napi_threadsafe_function_call_mode::napi_tsfn_blocking - } - ThreadsafeFunctionCallMode::NonBlocking => { - napi_threadsafe_function_call_mode::napi_tsfn_nonblocking - } + ThreadsafeFunctionCallMode::Blocking => sys::ThreadsafeFunctionCallMode::blocking, + ThreadsafeFunctionCallMode::NonBlocking => sys::ThreadsafeFunctionCallMode::nonblocking, } } } @@ -261,7 +255,7 @@ impl ThreadsafeFunction { check_status!(unsafe { sys::napi_release_threadsafe_function( self.raw_tsfn, - sys::napi_threadsafe_function_release_mode::napi_tsfn_abort, + sys::ThreadsafeFunctionReleaseMode::abort, ) })?; self.aborted.store(true, Ordering::Release); @@ -316,7 +310,7 @@ impl Drop for ThreadsafeFunction { let release_status = unsafe { sys::napi_release_threadsafe_function( self.raw_tsfn, - sys::napi_threadsafe_function_release_mode::napi_tsfn_release, + sys::ThreadsafeFunctionReleaseMode::release, ) }; assert!( diff --git a/crates/sys/Cargo.toml b/crates/sys/Cargo.toml index 90f6a49c..38dbef4d 100644 --- a/crates/sys/Cargo.toml +++ b/crates/sys/Cargo.toml @@ -19,6 +19,7 @@ napi5 = ["napi4"] napi6 = ["napi5"] napi7 = ["napi6"] napi8 = ["napi7"] +experimental = [] [package.metadata.workspaces] independent = true diff --git a/crates/sys/src/lib.rs b/crates/sys/src/lib.rs index d45c8535..e6481f1b 100644 --- a/crates/sys/src/lib.rs +++ b/crates/sys/src/lib.rs @@ -57,17 +57,19 @@ pub struct uv_loop_s { } pub type napi_deferred = *mut napi_deferred__; -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] -pub enum napi_property_attributes { - napi_default = 0, - napi_writable = 1 << 0, - napi_enumerable = 1 << 1, - napi_configurable = 1 << 2, +pub type napi_property_attributes = i32; + +pub mod PropertyAttributes { + use super::napi_property_attributes; + + pub const default: napi_property_attributes = 0; + pub const writable: napi_property_attributes = 1 << 0; + pub const enumerable: napi_property_attributes = 1 << 1; + pub const configurable: napi_property_attributes = 1 << 2; // Used with napi_define_class to distinguish static properties // from instance properties. Ignored by napi_define_properties. - napi_static = 1 << 10, + pub const static_: napi_property_attributes = 1 << 10; } pub type napi_valuetype = i32; @@ -89,19 +91,19 @@ pub mod ValueType { pub type napi_typedarray_type = i32; pub mod TypedarrayType { - pub const napi_int8_array: i32 = 0; - pub const napi_uint8_array: i32 = 1; - pub const napi_uint8_clamped_array: i32 = 2; - pub const napi_int16_array: i32 = 3; - pub const napi_uint16_array: i32 = 4; - pub const napi_int32_array: i32 = 5; - pub const napi_uint32_array: i32 = 6; - pub const napi_float32_array: i32 = 7; - pub const napi_float64_array: i32 = 8; + pub const int8_array: i32 = 0; + pub const uint8_array: i32 = 1; + pub const uint8_clamped_array: i32 = 2; + pub const int16_array: i32 = 3; + pub const uint16_array: i32 = 4; + pub const int32_array: i32 = 5; + pub const uint32_array: i32 = 6; + pub const float32_array: i32 = 7; + pub const float64_array: i32 = 8; #[cfg(feature = "napi6")] - pub const napi_bigint64_array: i32 = 9; + pub const bigint64_array: i32 = 9; #[cfg(feature = "napi6")] - pub const napi_biguint64_array: i32 = 10; + pub const biguint64_array: i32 = 10; } pub type napi_status = i32; @@ -158,29 +160,40 @@ pub struct napi_extended_error_info { pub error_code: napi_status, } -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_key_collection_mode { - napi_key_include_prototypes, - napi_key_own_only, +#[cfg(feature = "napi6")] +pub type napi_key_collection_mode = i32; + +#[cfg(feature = "napi6")] +pub mod KeyCollectionMode { + pub use super::napi_key_collection_mode; + pub const include_prototypes: napi_key_collection_mode = 0; + pub const own_only: napi_key_collection_mode = 1; } -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_key_filter { - napi_key_all_properties = 0, - napi_key_writable = 1, - napi_key_enumerable = 1 << 1, - napi_key_configurable = 1 << 2, - napi_key_skip_strings = 1 << 3, - napi_key_skip_symbols = 1 << 4, +#[cfg(feature = "napi6")] +pub type napi_key_filter = i32; + +#[cfg(feature = "napi6")] +pub mod KeyFilter { + use super::napi_key_filter; + + pub const all_properties: napi_key_filter = 0; + pub const writable: napi_key_filter = 1; + pub const enumerable: napi_key_filter = 1 << 1; + pub const configurable: napi_key_filter = 1 << 2; + pub const skip_strings: napi_key_filter = 1 << 3; + pub const skip_symbols: napi_key_filter = 1 << 4; } -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_key_conversion { - napi_key_keep_numbers, - napi_key_numbers_to_strings, +#[cfg(feature = "napi6")] +pub type napi_key_conversion = i32; + +#[cfg(feature = "napi6")] +pub mod KeyConversion { + use super::napi_key_conversion; + + pub const keep_numbers: napi_key_conversion = 0; + pub const numbers_to_strings: napi_key_conversion = 1; } #[cfg(feature = "napi8")] @@ -834,6 +847,11 @@ extern "C" { pub fn napi_object_seal(env: napi_env, object: napi_value) -> napi_status; } +#[cfg(feature = "experimental")] +extern "C" { + pub fn node_api_get_module_file_name(env: napi_env, result: *mut *const c_char) -> napi_status; +} + #[repr(C)] #[derive(Copy, Clone)] pub struct napi_callback_scope__ { @@ -864,19 +882,24 @@ pub struct napi_threadsafe_function__ { pub type napi_threadsafe_function = *mut napi_threadsafe_function__; #[cfg(feature = "napi4")] -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_threadsafe_function_release_mode { - napi_tsfn_release, - napi_tsfn_abort, +pub type napi_threadsafe_function_release_mode = i32; + +#[cfg(feature = "napi4")] +pub mod ThreadsafeFunctionReleaseMode { + use super::napi_threadsafe_function_release_mode; + pub const release: napi_threadsafe_function_release_mode = 0; + pub const abort: napi_threadsafe_function_release_mode = 1; } #[cfg(feature = "napi4")] -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub enum napi_threadsafe_function_call_mode { - napi_tsfn_nonblocking, - napi_tsfn_blocking, +pub type napi_threadsafe_function_call_mode = i32; + +#[cfg(feature = "napi4")] +pub mod ThreadsafeFunctionCallMode { + use super::napi_threadsafe_function_call_mode; + + pub const nonblocking: napi_threadsafe_function_call_mode = 0; + pub const blocking: napi_threadsafe_function_call_mode = 1; } pub type napi_async_execute_callback =