commit
80c97b2660
6 changed files with 78 additions and 5 deletions
|
@ -148,6 +148,7 @@ export class BuildCommand extends Command {
|
||||||
this.disableWindowsX32Optimize
|
this.disableWindowsX32Optimize
|
||||||
) {
|
) {
|
||||||
Object.assign(additionalEnv, {
|
Object.assign(additionalEnv, {
|
||||||
|
CARGO_PROFILE_DEBUG_CODEGEN_UNITS: 256,
|
||||||
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 256,
|
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 256,
|
||||||
CARGO_PROFILE_RELEASE_LTO: false,
|
CARGO_PROFILE_RELEASE_LTO: false,
|
||||||
})
|
})
|
||||||
|
|
|
@ -255,7 +255,15 @@ impl NapiStruct {
|
||||||
match &field.name {
|
match &field.name {
|
||||||
syn::Member::Named(ident) => {
|
syn::Member::Named(ident) => {
|
||||||
field_destructions.push(quote! { #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 {
|
if is_optional_field {
|
||||||
obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?; });
|
obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?; });
|
||||||
} else {
|
} else {
|
||||||
|
@ -264,7 +272,15 @@ impl NapiStruct {
|
||||||
}
|
}
|
||||||
syn::Member::Unnamed(i) => {
|
syn::Member::Unnamed(i) => {
|
||||||
field_destructions.push(quote! { arg#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 {
|
if is_optional_field {
|
||||||
obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?; });
|
obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?; });
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -145,7 +145,6 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
||||||
("Date", "Date"),
|
("Date", "Date"),
|
||||||
("JsBuffer", "Buffer"),
|
("JsBuffer", "Buffer"),
|
||||||
("Buffer", "Buffer"),
|
("Buffer", "Buffer"),
|
||||||
// TODO: Vec<u8> should be Buffer, now is Array<number>
|
|
||||||
("Vec", "Array<{}>"),
|
("Vec", "Array<{}>"),
|
||||||
("Result", "Error | {}"),
|
("Result", "Error | {}"),
|
||||||
("Either", "{} | {}"),
|
("Either", "{} | {}"),
|
||||||
|
|
|
@ -118,6 +118,7 @@ impl FromNapiValue for Array {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Array {
|
impl Array {
|
||||||
|
/// Create `Array` from `Vec<T>`
|
||||||
pub fn from_vec<T>(env: &Env, value: Vec<T>) -> Result<Self>
|
pub fn from_vec<T>(env: &Env, value: Vec<T>) -> Result<Self>
|
||||||
where
|
where
|
||||||
T: ToNapiValue,
|
T: ToNapiValue,
|
||||||
|
@ -129,6 +130,29 @@ impl Array {
|
||||||
})?;
|
})?;
|
||||||
Ok(arr)
|
Ok(arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create `Array` from `&Vec<String>`
|
||||||
|
pub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result<Self> {
|
||||||
|
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<T: Copy + ToNapiValue>`
|
||||||
|
pub fn from_ref_vec<T>(env: &Env, value: &[T]) -> Result<Self>
|
||||||
|
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 {
|
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<sys::napi_value> {
|
||||||
|
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<String> {
|
||||||
|
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||||
|
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<T> FromNapiValue for Vec<T>
|
impl<T> FromNapiValue for Vec<T>
|
||||||
where
|
where
|
||||||
T: FromNapiValue,
|
T: FromNapiValue,
|
||||||
|
|
|
@ -230,7 +230,6 @@ pub mod latin1_string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "latin1")]
|
|
||||||
impl FromNapiValue for Latin1String {
|
impl FromNapiValue for Latin1String {
|
||||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
|
|
|
@ -224,7 +224,7 @@ test('serde-json', (t) => {
|
||||||
const packageJson = readPackageJson()
|
const packageJson = readPackageJson()
|
||||||
t.is(packageJson.name, 'napi-rs')
|
t.is(packageJson.name, 'napi-rs')
|
||||||
t.is(packageJson.version, '0.0.0')
|
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.snapshot(Object.keys(packageJson.devDependencies!).sort())
|
||||||
|
|
||||||
t.is(getPackageJsonName(packageJson), 'napi-rs')
|
t.is(getPackageJsonName(packageJson), 'napi-rs')
|
||||||
|
|
Loading…
Reference in a new issue