diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index b016b577..6e1916d5 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -34,7 +34,7 @@ impl NapiEnum { }); quote! { - impl TypeName for #name { + impl napi::bindgen_prelude::TypeName for #name { fn type_name() -> &'static str { #name_str } @@ -44,16 +44,22 @@ impl NapiEnum { } } - impl ValidateNapiValue for #name { - unsafe fn validate(env: sys::napi_env, napi_val: sys::napi_value) -> Result<()> { - assert_type_of!(env, napi_val, ValueType::Number) + impl napi::bindgen_prelude::ValidateNapiValue for #name { + unsafe fn validate( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result<()> { + napi::bindgen_prelude::assert_type_of!(env, napi_val, napi::bindgen_prelude::ValueType::Number) } } - impl FromNapiValue for #name { - unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { + impl napi::bindgen_prelude::FromNapiValue for #name { + unsafe fn from_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result { let val = i32::from_napi_value(env, napi_val).map_err(|e| { - error!( + napi::bindgen_prelude::error!( e.status, "Failed to convert napi value into enum `{}`. {}", #name_str, @@ -64,8 +70,8 @@ impl NapiEnum { match val { #(#from_napi_branches,)* _ => { - Err(error!( - Status::InvalidArg, + Err(napi::bindgen_prelude::error!( + napi::bindgen_prelude::Status::InvalidArg, "value `{}` does not match any variant of enum `{}`", val, #name_str @@ -75,8 +81,11 @@ impl NapiEnum { } } - impl ToNapiValue for #name { - unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + impl napi::bindgen_prelude::ToNapiValue for #name { + unsafe fn to_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + val: Self + ) -> napi::bindgen_prelude::Result { let val = match val { #(#to_napi_branches,)* }; @@ -101,8 +110,8 @@ impl NapiEnum { define_properties.push(quote! { { let name = CString::new(#name_lit).unwrap(); - check_status!( - sys::napi_set_named_property(env, obj_ptr, name.as_ptr(), i32::to_napi_value(env, #val_lit)?), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_set_named_property(env, obj_ptr, name.as_ptr(), i32::to_napi_value(env, #val_lit)?), "Failed to defined enum `{}`", #js_name_lit )?; @@ -113,16 +122,16 @@ impl NapiEnum { quote! { #[allow(non_snake_case)] #[allow(clippy::all)] - #[ctor] + #[napi::bindgen_prelude::ctor] fn #register_name() { use std::ffi::CString; use std::ptr; - unsafe fn cb(env: sys::napi_env) -> Result { + unsafe fn cb(env: napi::bindgen_prelude::sys::napi_env) -> napi::bindgen_prelude::Result { let mut obj_ptr = ptr::null_mut(); - check_status!( - sys::napi_create_object(env, &mut obj_ptr), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_create_object(env, &mut obj_ptr), "Failed to create napi object" )?; @@ -131,7 +140,7 @@ impl NapiEnum { Ok(obj_ptr) } - register_module_export(#js_name_lit, cb); + napi::bindgen_prelude::register_module_export(#js_name_lit, cb); } } } diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 07acba9c..7ed24761 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -33,7 +33,7 @@ impl TryToTokens for NapiFn { quote! { Ok(#receiver(#(#arg_names),*).await) } }; quote! { - execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| { + napi::bindgen_prelude::execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| { #ret }) } @@ -47,20 +47,20 @@ impl TryToTokens for NapiFn { quote! { #native_call } } else if self.kind == FnKind::Constructor { quote! { - let call_from_factory = ___CALL_FROM_FACTORY.load(std::sync::atomic::Ordering::Relaxed); + let call_from_factory = napi::bindgen_prelude::___CALL_FROM_FACTORY.load(std::sync::atomic::Ordering::Relaxed); // constructor function is called from class `factory` // so we should skip the original `constructor` logic if call_from_factory { return std::ptr::null_mut(); } - CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| { + napi::bindgen_prelude::CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| { #(#arg_conversions)* #native_call }) } } else { quote! { - CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| { + napi::bindgen_prelude::CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| { #(#arg_conversions)* #native_call }) @@ -73,13 +73,13 @@ impl TryToTokens for NapiFn { #[allow(non_snake_case)] #[allow(clippy::all)] extern "C" fn #intermediate_ident( - env: sys::napi_env, - cb: sys::napi_callback_info - ) -> sys::napi_value { + env: napi::bindgen_prelude::sys::napi_env, + cb: napi::bindgen_prelude::sys::napi_callback_info + ) -> napi::bindgen_prelude::sys::napi_value { unsafe { #function_call.unwrap_or_else(|e| { - JsError::from(e).throw_into(env); - std::ptr::null_mut::() + napi::bindgen_prelude::JsError::from(e).throw_into(env); + std::ptr::null_mut::() }) } } @@ -118,7 +118,7 @@ impl NapiFn { match arg { NapiFnArgKind::PatType(path) => { if &path.ty.to_token_stream().to_string() == "Env" { - args.push(quote! { Env::from(env) }); + args.push(quote! { napi::bindgen_prelude::Env::from(env) }); skipped_arg_count += 1; } else { arg_conversions.push(self.gen_ty_arg_conversion(&ident, i, path)); @@ -149,18 +149,18 @@ impl NapiFn { .. }) => { quote! { - let #arg_name = <#elem as FromNapiMutRef>::from_napi_mut_ref(env, cb.get_arg(#index))?; + let #arg_name = <#elem as napi::bindgen_prelude::FromNapiMutRef>::from_napi_mut_ref(env, cb.get_arg(#index))?; } } syn::Type::Reference(syn::TypeReference { elem, .. }) => { quote! { - let #arg_name = <#elem as FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?; + let #arg_name = <#elem as napi::bindgen_prelude::FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?; } } _ => { let type_check = if self.strict { quote! { - <#ty as ValidateNapiValue>::validate(env, cb.get_arg(#index))?; + <#ty as napi::bindgen_prelude::ValidateNapiValue>::validate(env, cb.get_arg(#index))?; } } else { quote! {} @@ -169,7 +169,7 @@ impl NapiFn { quote! { let #arg_name = { #type_check - <#ty as FromNapiValue>::from_napi_value(env, cb.get_arg(#index))? + <#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, cb.get_arg(#index))? }; } } @@ -183,13 +183,15 @@ impl NapiFn { for (i, ty) in cb.args.iter().enumerate() { let cb_arg_ident = Ident::new(&format!("callback_arg_{}", i), Span::call_site()); inputs.push(quote! { #cb_arg_ident: #ty }); - arg_conversions.push(quote! { <#ty as ToNapiValue>::to_napi_value(env, #cb_arg_ident)? }); + arg_conversions.push( + quote! { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #cb_arg_ident)? }, + ); } let ret = match &cb.ret { Some(ty) => { quote! { - let ret = <#ty as FromNapiValue>::from_napi_value(env, ret_ptr)?; + let ret = <#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, ret_ptr)?; Ok(ret) } @@ -198,7 +200,7 @@ impl NapiFn { }; quote! { - assert_type_of!(env, cb.get_arg(#index), ValueType::Function)?; + napi::bindgen_prelude::assert_type_of!(env, cb.get_arg(#index), napi::bindgen_prelude::ValueType::Function)?; let #arg_name = |#(#inputs),*| { let args = vec![ #(#arg_conversions),* @@ -206,8 +208,8 @@ impl NapiFn { let mut ret_ptr = std::ptr::null_mut(); - check_status!( - sys::napi_call_function( + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_call_function( env, cb.this(), cb.get_arg(#index), @@ -258,14 +260,14 @@ impl NapiFn { } else if self.is_ret_result { if self.is_async { quote! { - <#ty as ToNapiValue>::to_napi_value(env, #ret) + <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #ret) } } else { quote! { match #ret { - Ok(value) => ToNapiValue::to_napi_value(env, value), + Ok(value) => napi::bindgen_prelude::ToNapiValue::to_napi_value(env, value), Err(err) => { - JsError::from(err).throw_into(env); + napi::bindgen_prelude::JsError::from(err).throw_into(env); Ok(std::ptr::null_mut()) }, } @@ -273,12 +275,12 @@ impl NapiFn { } } else { quote! { - <#ty as ToNapiValue>::to_napi_value(env, #ret) + <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #ret) } } } else { quote! { - <() as ToNapiValue>::to_napi_value(env, ()) + <() as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, ()) } } } @@ -296,13 +298,13 @@ impl NapiFn { quote! { #[allow(clippy::all)] #[allow(non_snake_case)] - #[ctor] + #[napi::bindgen_prelude::ctor] fn #module_register_name() { - unsafe fn cb(env: sys::napi_env) -> Result { + unsafe fn cb(env: napi::bindgen_prelude::sys::napi_env) -> napi::bindgen_prelude::Result { let mut fn_ptr = std::ptr::null_mut(); - check_status!( - sys::napi_create_function( + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_create_function( env, #js_name.as_ptr() as *const _, #name_len, @@ -317,7 +319,7 @@ impl NapiFn { Ok(fn_ptr) } - register_module_export(#js_name, cb); + napi::bindgen_prelude::register_module_export(#js_name, cb); } } } diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index c85d9dc7..4e8ebef8 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -12,7 +12,7 @@ use crate::{ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> TokenStream { let name_str = name.to_string(); quote! { - impl TypeName for #name { + impl napi::bindgen_prelude::TypeName for #name { fn type_name() -> &'static str { #name_str } @@ -24,12 +24,15 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token #to_napi_val_impl - impl FromNapiRef for #name { - unsafe fn from_napi_ref(env: sys::napi_env, napi_val: sys::napi_value) -> Result<&'static Self> { + impl napi::bindgen_prelude::FromNapiRef for #name { + unsafe fn from_napi_ref( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result<&'static Self> { let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut(); - check_status!( - sys::napi_unwrap(env, napi_val, &mut wrapped_val), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_unwrap(env, napi_val, &mut wrapped_val), "Failed to recover `{}` type from napi value", #name_str, )?; @@ -38,12 +41,15 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token } } - impl FromNapiMutRef for #name { - unsafe fn from_napi_mut_ref(env: sys::napi_env, napi_val: sys::napi_value) -> Result<&'static mut Self> { + impl napi::bindgen_prelude::FromNapiMutRef for #name { + unsafe fn from_napi_mut_ref( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result<&'static mut Self> { let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut(); - check_status!( - sys::napi_unwrap(env, napi_val, &mut wrapped_val), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_unwrap(env, napi_val, &mut wrapped_val), "Failed to recover `{}` type from napi value", #name_str, )?; @@ -132,14 +138,14 @@ impl NapiStruct { quote! { extern "C" fn constructor( - env: sys::napi_env, - cb: sys::napi_callback_info - ) -> sys::napi_value { - CallbackInfo::<#fields_len>::new(env, cb, None) + env: napi::bindgen_prelude::sys::napi_env, + cb: napi::bindgen_prelude::sys::napi_callback_info + ) -> napi::bindgen_prelude::sys::napi_value { + napi::bindgen_prelude::CallbackInfo::<#fields_len>::new(env, cb, None) .and_then(|cb| unsafe { cb.construct(#js_name_str, #construct) }) .unwrap_or_else(|e| { - unsafe { JsError::from(e).throw_into(env) }; - std::ptr::null_mut::() + unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) }; + std::ptr::null_mut::() }) } } @@ -188,13 +194,15 @@ impl NapiStruct { }; quote! { - impl ToNapiValue for #name { - unsafe fn to_napi_value(env: sys::napi_env, val: #name) -> Result { - if let Some(ctor_ref) = get_class_constructor(#js_name_str) { + impl napi::bindgen_prelude::ToNapiValue for #name { + unsafe fn to_napi_value( + env: napi::bindgen_prelude::sys::napi_env, val: #name + ) -> napi::bindgen_prelude::Result { + if let Some(ctor_ref) = napi::bindgen_prelude::get_class_constructor(#js_name_str) { let mut ctor = std::ptr::null_mut(); - check_status!( - sys::napi_get_reference_value(env, ctor_ref, &mut ctor), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_get_reference_value(env, ctor_ref, &mut ctor), "Failed to get constructor of class `{}`", #js_name_str )?; @@ -203,15 +211,17 @@ impl NapiStruct { let #destructed_fields = val; let args = vec![#(#field_conversions),*]; - check_status!( - sys::napi_new_instance(env, ctor, args.len(), args.as_ptr(), &mut result), + napi::bindgen_prelude::check_status!( + napi::bindgen_prelude::sys::napi_new_instance(env, ctor, args.len(), args.as_ptr(), &mut result), "Failed to construct class `{}`", #js_name_str )?; Ok(result) } else { - Err(Error::new(Status::InvalidArg, format!("Failed to get constructor of class `{}`", #js_name_str))) + Err(napi::bindgen_prelude::Error::new( + napi::bindgen_prelude::Status::InvalidArg, format!("Failed to get constructor of class `{}`", #js_name_str)) + ) } } } @@ -255,7 +265,7 @@ impl NapiStruct { }; quote! { - impl TypeName for #name { + impl napi::bindgen_prelude::TypeName for #name { fn type_name() -> &'static str { #name_str } @@ -265,22 +275,25 @@ impl NapiStruct { } } - impl ToNapiValue for #name { - unsafe fn to_napi_value(env: sys::napi_env, val: #name) -> Result { - let env_wrapper = Env::from(env); + impl napi::bindgen_prelude::ToNapiValue for #name { + unsafe fn to_napi_value(env: napi::bindgen_prelude::sys::napi_env, val: #name) -> napi::bindgen_prelude::Result { + let env_wrapper = napi::bindgen_prelude::Env::from(env); let mut obj = env_wrapper.create_object()?; let #destructed_fields = val; #(#obj_field_setters)* - Object::to_napi_value(env, obj) + napi::bindgen_prelude::Object::to_napi_value(env, obj) } } - impl FromNapiValue for #name { - unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { - let env_wrapper = Env::from(env); - let mut obj = Object::from_napi_value(env, napi_val)?; + impl napi::bindgen_prelude::FromNapiValue for #name { + unsafe fn from_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result { + let env_wrapper = napi::bindgen_prelude::Env::from(env); + let mut obj = napi::bindgen_prelude::Object::from_napi_value(env, napi_val)?; #(#obj_field_getters)* @@ -312,18 +325,18 @@ impl NapiStruct { field.js_name.clone(), quote! { extern "C" fn #getter_name( - env: sys::napi_env, - cb: sys::napi_callback_info - ) -> sys::napi_value { - CallbackInfo::<0>::new(env, cb, Some(0)) + env: napi::bindgen_prelude::sys::napi_env, + cb: napi::bindgen_prelude::sys::napi_callback_info + ) -> napi::bindgen_prelude::sys::napi_value { + napi::bindgen_prelude::CallbackInfo::<0>::new(env, cb, Some(0)) .and_then(|mut cb| unsafe { cb.unwrap_borrow::<#struct_name>() }) .and_then(|obj| { let val = obj.#field_ident.to_owned(); - unsafe { <#ty as ToNapiValue>::to_napi_value(env, val) } + unsafe { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, val) } }) .unwrap_or_else(|e| { - unsafe { JsError::from(e).throw_into(env) }; - std::ptr::null_mut::() + unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) }; + std::ptr::null_mut::() }) } }, @@ -335,23 +348,23 @@ impl NapiStruct { field.js_name.clone(), quote! { extern "C" fn #setter_name( - env: sys::napi_env, - cb: sys::napi_callback_info - ) -> sys::napi_value { - CallbackInfo::<1>::new(env, cb, Some(1)) + env: napi::bindgen_prelude::sys::napi_env, + cb: napi::bindgen_prelude::sys::napi_callback_info + ) -> napi::bindgen_prelude::sys::napi_value { + napi::bindgen_prelude::CallbackInfo::<1>::new(env, cb, Some(1)) .and_then(|mut cb_info| unsafe { cb_info.unwrap_borrow_mut::<#struct_name>() .and_then(|obj| { - <#ty as FromNapiValue>::from_napi_value(env, cb_info.get_arg(0)) + <#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, cb_info.get_arg(0)) .and_then(move |val| { obj.#field_ident = val; - <() as ToNapiValue>::to_napi_value(env, ()) + <() as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, ()) }) }) }) .unwrap_or_else(|e| { - unsafe { JsError::from(e).throw_into(env) }; - std::ptr::null_mut::() + unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) }; + std::ptr::null_mut::() }) } }, @@ -369,7 +382,7 @@ impl NapiStruct { let mut props = vec![]; if self.kind == NapiStructKind::Constructor { - props.push(quote! { Property::new("constructor").unwrap().with_ctor(constructor) }); + props.push(quote! { napi::bindgen_prelude::Property::new("constructor").unwrap().with_ctor(constructor) }); } for field in self.fields.iter() { @@ -384,7 +397,7 @@ impl NapiStruct { let js_name = &field.js_name; let mut prop = quote! { - Property::new(#js_name) + napi::bindgen_prelude::Property::new(#js_name) .unwrap() }; @@ -404,9 +417,9 @@ impl NapiStruct { quote! { #[allow(non_snake_case)] #[allow(clippy::all)] - #[ctor] + #[napi::bindgen_prelude::ctor] fn #struct_register_name() { - register_class(#name_str, #js_name, vec![#(#props),*]); + napi::bindgen_prelude::register_class(#name_str, #js_name, vec![#(#props),*]); } } } @@ -442,7 +455,7 @@ impl NapiImpl { let prop = props.entry(&item.js_name).or_insert_with(|| { quote! { - Property::new(#js_name).unwrap() + napi::bindgen_prelude::Property::new(#js_name).unwrap() } }); @@ -454,7 +467,7 @@ impl NapiImpl { if item.fn_self.is_some() { quote! { .with_method(#intermediate_name) } } else { - quote! { .with_method(#intermediate_name).with_property_attributes(PropertyAttributes::Static) } + quote! { .with_method(#intermediate_name).with_property_attributes(napi::bindgen_prelude::PropertyAttributes::Static) } } } }; @@ -473,9 +486,9 @@ impl NapiImpl { use super::*; #(#methods)* - #[ctor] + #[napi::bindgen_prelude::ctor] fn #register_name() { - register_class(#name_str, #js_name, vec![#(#props),*]); + napi::bindgen_prelude::register_class(#name_str, #js_name, vec![#(#props),*]); } } }) diff --git a/examples/napi/src/array.rs b/examples/napi/src/array.rs index 7315fd42..04b55d31 100644 --- a/examples/napi/src/array.rs +++ b/examples/napi/src/array.rs @@ -1,5 +1,3 @@ -use napi::bindgen_prelude::*; - #[napi] fn get_words() -> Vec<&'static str> { vec!["foo", "bar"] diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 154d0117..73b03119 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -1,4 +1,3 @@ -use napi::bindgen_prelude::*; use napi::Result; use crate::r#enum::Kind; diff --git a/examples/napi/src/class_factory.rs b/examples/napi/src/class_factory.rs index d5c7076f..116f9772 100644 --- a/examples/napi/src/class_factory.rs +++ b/examples/napi/src/class_factory.rs @@ -1,5 +1,3 @@ -use napi::bindgen_prelude::*; - #[napi] pub struct ClassWithFactory { pub name: String, diff --git a/examples/napi/src/nullable.rs b/examples/napi/src/nullable.rs index 2f4745c5..eb52c12c 100644 --- a/examples/napi/src/nullable.rs +++ b/examples/napi/src/nullable.rs @@ -1,5 +1,3 @@ -use napi::bindgen_prelude::*; - #[napi] fn map_option(val: Option) -> Option { val.map(|v| v + 1) diff --git a/examples/napi/src/number.rs b/examples/napi/src/number.rs index 557ffe90..7eeef2c6 100644 --- a/examples/napi/src/number.rs +++ b/examples/napi/src/number.rs @@ -1,5 +1,3 @@ -use napi::bindgen_prelude::*; - #[napi] fn add(a: u32, b: u32) -> u32 { a + b