diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index 96100b31..3fc7baba 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -302,3 +302,300 @@ where Ok(ptr::null_mut()) } } + +macro_rules! arr_get { + ($arr:expr, $n:expr) => { + if let Some(e) = $arr.get($n)? { + e + } else { + return Err(Error::new( + Status::InvalidArg, + format!( + "Found inconsistent data type in Array[{}] when converting to Rust T", + $n + ) + .to_owned(), + )); + } + }; +} + +macro_rules! tuple_from_napi_value { + ($total:expr, $($n:expr),+) => { + unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { + let arr = unsafe { Array::from_napi_value(env, napi_val)? }; + if arr.len() < $total { + return Err(Error::new( + Status::InvalidArg, + format!("Array length < {}",$total).to_owned(), + )); + } + Ok(($(arr_get!(arr,$n)),+)) + } + } +} + +impl FromNapiValue for (T0, T1) +where + T0: FromNapiValue, + T1: FromNapiValue, +{ + tuple_from_napi_value!(2, 0, 1); +} + +impl FromNapiValue for (T0, T1, T2) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, +{ + tuple_from_napi_value!(3, 0, 1, 2); +} + +impl FromNapiValue for (T0, T1, T2, T3) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, +{ + tuple_from_napi_value!(4, 0, 1, 2, 3); +} + +impl FromNapiValue for (T0, T1, T2, T3, T4) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, +{ + tuple_from_napi_value!(5, 0, 1, 2, 3, 4); +} + +impl FromNapiValue for (T0, T1, T2, T3, T4, T5) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, +{ + tuple_from_napi_value!(6, 0, 1, 2, 3, 4, 5); +} + +impl FromNapiValue for (T0, T1, T2, T3, T4, T5, T6) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, +{ + tuple_from_napi_value!(7, 0, 1, 2, 3, 4, 5, 6); +} + +impl FromNapiValue for (T0, T1, T2, T3, T4, T5, T6, T7) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, +{ + tuple_from_napi_value!(8, 0, 1, 2, 3, 4, 5, 6, 7); +} + +impl FromNapiValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, +{ + tuple_from_napi_value!(9, 0, 1, 2, 3, 4, 5, 6, 7, 8); +} + +impl FromNapiValue + for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, +{ + tuple_from_napi_value!(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); +} + +impl FromNapiValue + for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, +{ + tuple_from_napi_value!(11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +} + +impl FromNapiValue + for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, + T11: FromNapiValue, +{ + tuple_from_napi_value!(12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); +} + +impl FromNapiValue + for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, + T11: FromNapiValue, + T12: FromNapiValue, +{ + tuple_from_napi_value!(13, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); +} + +impl FromNapiValue + for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, + T11: FromNapiValue, + T12: FromNapiValue, + T13: FromNapiValue, +{ + tuple_from_napi_value!(14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); +} + +impl FromNapiValue + for ( + T0, + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + ) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, + T11: FromNapiValue, + T12: FromNapiValue, + T13: FromNapiValue, + T14: FromNapiValue, +{ + tuple_from_napi_value!(15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); +} + +impl FromNapiValue + for ( + T0, + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + ) +where + T0: FromNapiValue, + T1: FromNapiValue, + T2: FromNapiValue, + T3: FromNapiValue, + T4: FromNapiValue, + T5: FromNapiValue, + T6: FromNapiValue, + T7: FromNapiValue, + T8: FromNapiValue, + T9: FromNapiValue, + T10: FromNapiValue, + T11: FromNapiValue, + T12: FromNapiValue, + T13: FromNapiValue, + T14: FromNapiValue, + T15: FromNapiValue, +{ + tuple_from_napi_value!(16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); +} diff --git a/crates/napi/src/bindgen_runtime/js_values/either.rs b/crates/napi/src/bindgen_runtime/js_values/either.rs index 38fa5064..840f1526 100644 --- a/crates/napi/src/bindgen_runtime/js_values/either.rs +++ b/crates/napi/src/bindgen_runtime/js_values/either.rs @@ -10,6 +10,21 @@ pub enum Either { B(B), } +unsafe impl Send for Either {} +unsafe impl Sync for Either {} + +impl, B: AsRef, T> AsRef for Either +where + T: ?Sized, +{ + fn as_ref(&self) -> &T { + match &self { + Self::A(a) => a.as_ref(), + Self::B(b) => b.as_ref(), + } + } +} + impl Either { /// # Safety /// Backward compatible with `Either` in **v1** diff --git a/crates/napi/src/bindgen_runtime/js_values/external.rs b/crates/napi/src/bindgen_runtime/js_values/external.rs index 3ce29a13..6243c3c5 100644 --- a/crates/napi/src/bindgen_runtime/js_values/external.rs +++ b/crates/napi/src/bindgen_runtime/js_values/external.rs @@ -3,9 +3,8 @@ use std::{ ops::{Deref, DerefMut}, }; -use crate::{check_status, sys, Error, Status, TaggedObject}; - use super::{FromNapiValue, ToNapiValue, TypeName, ValidateNapiValue}; +use crate::{check_status, sys, Error, Status, TaggedObject}; pub struct External { obj: *mut TaggedObject, @@ -23,6 +22,12 @@ impl TypeName for External { } } +impl From for External { + fn from(t: T) -> Self { + External::new(t) + } +} + impl ValidateNapiValue for External {} impl External { diff --git a/crates/napi/src/js_values/de.rs b/crates/napi/src/js_values/de.rs index b0b69057..657f7fa7 100644 --- a/crates/napi/src/js_values/de.rs +++ b/crates/napi/src/js_values/de.rs @@ -126,7 +126,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> { )) } else { let key = properties.get_element::(0)?; - let value: JsUnknown = js_object.get_property(&key)?; + let value: JsUnknown = js_object.get_property(key)?; visitor.visit_enum(JsEnumAccess::new( key.into_utf8()?.into_owned()?, Some(&value.0), @@ -354,7 +354,7 @@ impl<'de, 'env> MapAccess<'de> for JsObjectAccess<'env> { )); } let prop_name = self.properties.get_element::(self.idx)?; - let value: JsUnknown = self.value.get_property(&prop_name)?; + let value: JsUnknown = self.value.get_property(prop_name)?; self.idx += 1; let mut de = De(&value.0);