From 29277308d7017c2e80c209e8656dc8c4659e7a12 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 31 Oct 2021 13:13:21 +0800 Subject: [PATCH] fix(napi-derive-backend): generate the same code if source was not changed --- crates/backend/src/codegen/struct.rs | 97 ++++++++++++++++------------ crates/build/src/lib.rs | 2 + 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 886feb8a..e29cd9ba 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -83,9 +83,12 @@ impl NapiStruct { 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 getters_setters_token = getters_setters.into_iter().map(|(_, token)| token); + quote! { #[allow(clippy::all)] #[allow(non_snake_case)] @@ -94,7 +97,7 @@ impl NapiStruct { use super::*; #ctor - #(#getters_setters)* + #(#getters_setters_token)* #register } } @@ -281,7 +284,7 @@ impl NapiStruct { } } - fn gen_default_getters_setters(&self) -> Vec { + fn gen_default_getters_setters(&self) -> Vec<(String, TokenStream)> { let mut getters_setters = vec![]; let struct_name = &self.name; @@ -297,48 +300,54 @@ impl NapiStruct { let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site()); if field.getter { - getters_setters.push(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)) - .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) } - }) - .unwrap_or_else(|e| { - unsafe { JsError::from(e).throw_into(env) }; - std::ptr::null_mut::() - }) - } - }); + getters_setters.push(( + 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)) + .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) } + }) + .unwrap_or_else(|e| { + unsafe { JsError::from(e).throw_into(env) }; + std::ptr::null_mut::() + }) + } + }, + )); } if field.setter { - getters_setters.push(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)) - .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)) - .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::() - }) - } - }); + getters_setters.push(( + 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)) + .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)) + .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::() + }) + } + }, + )); } } @@ -445,7 +454,9 @@ impl NapiImpl { 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! { #[allow(non_snake_case)] diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 72046f9d..0d8c1bb6 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -2,6 +2,8 @@ mod macos; mod windows; 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() { Ok("macos") => macos::setup(), Ok("windows") => windows::setup(),