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,7 +300,9 @@ 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((
field.js_name.clone(),
quote! {
extern "C" fn #getter_name( extern "C" fn #getter_name(
env: sys::napi_env, env: sys::napi_env,
cb: sys::napi_callback_info cb: sys::napi_callback_info
@ -313,11 +318,14 @@ impl NapiStruct {
std::ptr::null_mut::<sys::napi_value__>() std::ptr::null_mut::<sys::napi_value__>()
}) })
} }
}); },
));
} }
if field.setter { if field.setter {
getters_setters.push(quote! { getters_setters.push((
field.js_name.clone(),
quote! {
extern "C" fn #setter_name( extern "C" fn #setter_name(
env: sys::napi_env, env: sys::napi_env,
cb: sys::napi_callback_info cb: sys::napi_callback_info
@ -338,7 +346,8 @@ impl NapiStruct {
std::ptr::null_mut::<sys::napi_value__>() 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(),