From 11a5a35485853c722d55dca32a6c3175ecdea8fb Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 7 Dec 2021 21:45:01 +0800 Subject: [PATCH 1/4] feat(cli): workaround for Windows i686 ICE in dev mode --- cli/src/build.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/src/build.ts b/cli/src/build.ts index a9a95c01..c83f6bfb 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -148,6 +148,7 @@ export class BuildCommand extends Command { this.disableWindowsX32Optimize ) { Object.assign(additionalEnv, { + CARGO_PROFILE_DEBUG_CODEGEN_UNITS: 256, CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 256, CARGO_PROFILE_RELEASE_LTO: false, }) From f7a101db01c20c207d836c39c63afb4567655be7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 7 Dec 2021 21:45:40 +0800 Subject: [PATCH 2/4] chore: cleanup stale codes --- crates/backend/src/typegen.rs | 1 - crates/napi/src/bindgen_runtime/js_values/string.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 8076e4b9..86aa6092 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -145,7 +145,6 @@ static KNOWN_TYPES: Lazy> = Lazy::new(|| { ("Date", "Date"), ("JsBuffer", "Buffer"), ("Buffer", "Buffer"), - // TODO: Vec should be Buffer, now is Array ("Vec", "Array<{}>"), ("Result", "Error | {}"), ("Either", "{} | {}"), diff --git a/crates/napi/src/bindgen_runtime/js_values/string.rs b/crates/napi/src/bindgen_runtime/js_values/string.rs index 6a7d618d..c60c1bc1 100644 --- a/crates/napi/src/bindgen_runtime/js_values/string.rs +++ b/crates/napi/src/bindgen_runtime/js_values/string.rs @@ -230,7 +230,6 @@ pub mod latin1_string { } } - #[cfg(feature = "latin1")] impl FromNapiValue for Latin1String { unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { let mut len = 0; From ff717df34f9df1494c312c999c3d16d17d6c748f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 7 Dec 2021 21:49:45 +0800 Subject: [PATCH 3/4] fix(napi-derive-backend): skip set object property if it is None --- crates/backend/src/codegen/struct.rs | 20 ++++++++++++++++++-- examples/napi/__test__/values.spec.ts | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index e09af144..d9aa1c75 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -255,7 +255,15 @@ impl NapiStruct { match &field.name { syn::Member::Named(ident) => { field_destructions.push(quote! { #ident }); - obj_field_setters.push(quote! { obj.set(#field_js_name, #ident)?; }); + if is_optional_field { + obj_field_setters.push(quote! { + if #ident.is_some() { + obj.set(#field_js_name, #ident)?; + } + }); + } else { + obj_field_setters.push(quote! { obj.set(#field_js_name, #ident)?; }); + } if is_optional_field { obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?; }); } else { @@ -264,7 +272,15 @@ impl NapiStruct { } syn::Member::Unnamed(i) => { field_destructions.push(quote! { arg#i }); - obj_field_setters.push(quote! { obj.set(#field_js_name, arg#1)?; }); + if is_optional_field { + obj_field_setters.push(quote! { + if arg#1.is_some() { + obj.set(#field_js_name, arg#i)?; + } + }); + } else { + obj_field_setters.push(quote! { obj.set(#field_js_name, arg#1)?; }); + } if is_optional_field { obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?; }); } else { diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index a66153cb..e2a30783 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -224,7 +224,7 @@ test('serde-json', (t) => { const packageJson = readPackageJson() t.is(packageJson.name, 'napi-rs') t.is(packageJson.version, '0.0.0') - t.is(packageJson.dependencies, null) + t.is(packageJson.dependencies, undefined) t.snapshot(Object.keys(packageJson.devDependencies!).sort()) t.is(getPackageJsonName(packageJson), 'napi-rs') From 831e050877ea64c9a7c26041ab8678c432aaf3ba Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 7 Dec 2021 21:50:15 +0800 Subject: [PATCH 4/4] feat(napi): create Array from &Vec --- .../src/bindgen_runtime/js_values/array.rs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index e8c21dbe..1e4b057d 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -118,6 +118,7 @@ impl FromNapiValue for Array { } impl Array { + /// Create `Array` from `Vec` pub fn from_vec(env: &Env, value: Vec) -> Result where T: ToNapiValue, @@ -129,6 +130,29 @@ impl Array { })?; Ok(arr) } + + /// Create `Array` from `&Vec` + pub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result { + let mut arr = Array::new(env.0, value.len() as u32)?; + value.iter().enumerate().try_for_each(|(index, val)| { + arr.set(index as u32, val.as_str())?; + Ok::<(), Error>(()) + })?; + Ok(arr) + } + + /// Create `Array` from `&Vec` + pub fn from_ref_vec(env: &Env, value: &[T]) -> Result + where + T: ToNapiValue + Copy, + { + let mut arr = Array::new(env.0, value.len() as u32)?; + value.iter().enumerate().try_for_each(|(index, val)| { + arr.set(index as u32, *val)?; + Ok::<(), Error>(()) + })?; + Ok(arr) + } } impl ValidateNapiValue for Array { @@ -162,6 +186,40 @@ where } } +macro_rules! impl_for_primitive_type { + ($primitive_type:ident) => { + impl ToNapiValue for &Vec<$primitive_type> { + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + let mut arr = Array::new(env, val.len() as u32)?; + + for (i, v) in val.iter().enumerate() { + arr.set(i as u32, *v)?; + } + + Array::to_napi_value(env, arr) + } + } + }; +} + +impl_for_primitive_type!(u32); +impl_for_primitive_type!(i32); +impl_for_primitive_type!(i64); +impl_for_primitive_type!(f64); +impl_for_primitive_type!(bool); + +impl ToNapiValue for &Vec { + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + let mut arr = Array::new(env, val.len() as u32)?; + + for (i, v) in val.iter().enumerate() { + arr.set(i as u32, v.as_str())?; + } + + Array::to_napi_value(env, arr) + } +} + impl FromNapiValue for Vec where T: FromNapiValue,