fix(napi-derive-backend): generate the same code if source was not changed

This commit is contained in:
LongYinan 2021-10-31 13:13:21 +08:00
parent 88c4175c2d
commit 29277308d7
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 56 additions and 43 deletions

View file

@ -83,9 +83,12 @@ impl NapiStruct {
quote! {} quote! {}
}; };
let getters_setters = self.gen_default_getters_setters(); let mut getters_setters = self.gen_default_getters_setters();
getters_setters.sort_by(|a, b| a.0.cmp(&b.0));
let register = self.gen_register(); let register = self.gen_register();
let getters_setters_token = getters_setters.into_iter().map(|(_, token)| token);
quote! { quote! {
#[allow(clippy::all)] #[allow(clippy::all)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -94,7 +97,7 @@ impl NapiStruct {
use super::*; use super::*;
#ctor #ctor
#(#getters_setters)* #(#getters_setters_token)*
#register #register
} }
} }
@ -281,7 +284,7 @@ impl NapiStruct {
} }
} }
fn gen_default_getters_setters(&self) -> Vec<TokenStream> { fn gen_default_getters_setters(&self) -> Vec<(String, TokenStream)> {
let mut getters_setters = vec![]; let mut getters_setters = vec![];
let struct_name = &self.name; let struct_name = &self.name;
@ -297,48 +300,54 @@ impl NapiStruct {
let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site()); let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site());
if field.getter { if field.getter {
getters_setters.push(quote! { getters_setters.push((
extern "C" fn #getter_name( field.js_name.clone(),
env: sys::napi_env, quote! {
cb: sys::napi_callback_info extern "C" fn #getter_name(
) -> sys::napi_value { env: sys::napi_env,
CallbackInfo::<0>::new(env, cb, Some(0)) cb: sys::napi_callback_info
.and_then(|mut cb| unsafe { cb.unwrap_borrow::<#struct_name>() }) ) -> sys::napi_value {
.and_then(|obj| { CallbackInfo::<0>::new(env, cb, Some(0))
let val = obj.#field_ident.to_owned(); .and_then(|mut cb| unsafe { cb.unwrap_borrow::<#struct_name>() })
unsafe { <#ty as ToNapiValue>::to_napi_value(env, val) } .and_then(|obj| {
}) let val = obj.#field_ident.to_owned();
.unwrap_or_else(|e| { unsafe { <#ty as ToNapiValue>::to_napi_value(env, val) }
unsafe { JsError::from(e).throw_into(env) }; })
std::ptr::null_mut::<sys::napi_value__>() .unwrap_or_else(|e| {
}) unsafe { JsError::from(e).throw_into(env) };
} std::ptr::null_mut::<sys::napi_value__>()
}); })
}
},
));
} }
if field.setter { if field.setter {
getters_setters.push(quote! { getters_setters.push((
extern "C" fn #setter_name( field.js_name.clone(),
env: sys::napi_env, quote! {
cb: sys::napi_callback_info extern "C" fn #setter_name(
) -> sys::napi_value { env: sys::napi_env,
CallbackInfo::<1>::new(env, cb, Some(1)) cb: sys::napi_callback_info
.and_then(|mut cb_info| unsafe { ) -> sys::napi_value {
cb_info.unwrap_borrow_mut::<#struct_name>() CallbackInfo::<1>::new(env, cb, Some(1))
.and_then(|obj| { .and_then(|mut cb_info| unsafe {
<#ty as FromNapiValue>::from_napi_value(env, cb_info.get_arg(0)) cb_info.unwrap_borrow_mut::<#struct_name>()
.and_then(move |val| { .and_then(|obj| {
obj.#field_ident = val; <#ty as FromNapiValue>::from_napi_value(env, cb_info.get_arg(0))
<() as ToNapiValue>::to_napi_value(env, ()) .and_then(move |val| {
}) obj.#field_ident = val;
}) <() as ToNapiValue>::to_napi_value(env, ())
}) })
.unwrap_or_else(|e| { })
unsafe { JsError::from(e).throw_into(env) }; })
std::ptr::null_mut::<sys::napi_value__>() .unwrap_or_else(|e| {
}) unsafe { JsError::from(e).throw_into(env) };
} std::ptr::null_mut::<sys::napi_value__>()
}); })
}
},
));
} }
} }
@ -445,7 +454,9 @@ impl NapiImpl {
appendix.to_tokens(prop); appendix.to_tokens(prop);
} }
let props: Vec<_> = props.values().collect(); let mut props: Vec<_> = props.into_iter().collect();
props.sort_by_key(|(_, prop)| prop.to_string());
let props = props.into_iter().map(|(_, prop)| prop);
Ok(quote! { Ok(quote! {
#[allow(non_snake_case)] #[allow(non_snake_case)]

View file

@ -2,6 +2,8 @@ mod macos;
mod windows; mod windows;
pub fn setup() { pub fn setup() {
println!("cargo:rerun-if-changed=TYPE_DEF_TMP_PATH");
println!("cargo:rerun-if-changed=DEBUG_GENERATED_CODE");
match std::env::var("CARGO_CFG_TARGET_OS").as_deref() { match std::env::var("CARGO_CFG_TARGET_OS").as_deref() {
Ok("macos") => macos::setup(), Ok("macos") => macos::setup(),
Ok("windows") => windows::setup(), Ok("windows") => windows::setup(),