feat(napi-derive) Support #[napi(strict)] on &T and &mut T. (#1238)

This commit is contained in:
Ivan Enderlin 2022-08-06 15:55:35 +02:00 committed by GitHub
parent 0f14799776
commit 6a9fdba8b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -188,22 +188,7 @@ impl NapiFn {
path: &syn::PatType,
) -> TokenStream {
let ty = &*path.ty;
match ty {
syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
elem,
..
}) => {
quote! {
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 napi::bindgen_prelude::FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?;
}
}
_ => {
let type_check = if self.return_if_invalid {
quote! {
if let Ok(maybe_promise) = <#ty as napi::bindgen_prelude::ValidateNapiValue>::validate(env, cb.get_arg(#index)) {
@ -225,6 +210,28 @@ impl NapiFn {
quote! {}
};
match ty {
syn::Type::Reference(syn::TypeReference {
mutability: Some(_),
elem,
..
}) => {
quote! {
let #arg_name = {
#type_check
<#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 = {
#type_check
<#elem as napi::bindgen_prelude::FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?
};
}
}
_ => {
quote! {
let #arg_name = {
#type_check