Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Vec<(std::string::String, u16)> and some other small change #1320

Merged
merged 2 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
297 changes: 297 additions & 0 deletions crates/napi/src/bindgen_runtime/js_values/array.rs
Expand Up @@ -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<Self> {
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<T0, T1> FromNapiValue for (T0, T1)
where
T0: FromNapiValue,
T1: FromNapiValue,
{
tuple_from_napi_value!(2, 0, 1);
}

impl<T0, T1, T2> FromNapiValue for (T0, T1, T2)
where
T0: FromNapiValue,
T1: FromNapiValue,
T2: FromNapiValue,
{
tuple_from_napi_value!(3, 0, 1, 2);
}

impl<T0, T1, T2, T3> 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<T0, T1, T2, T3, T4> 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<T0, T1, T2, T3, T4, T5> 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<T0, T1, T2, T3, T4, T5, T6> 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<T0, T1, T2, T3, T4, T5, T6, T7> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> 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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> 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);
}
15 changes: 15 additions & 0 deletions crates/napi/src/bindgen_runtime/js_values/either.rs
Expand Up @@ -10,6 +10,21 @@ pub enum Either<A, B> {
B(B),
}

unsafe impl<A, B> Send for Either<A, B> {}
usrtax marked this conversation as resolved.
Show resolved Hide resolved
unsafe impl<A, B> Sync for Either<A, B> {}
usrtax marked this conversation as resolved.
Show resolved Hide resolved

impl<A: AsRef<T>, B: AsRef<T>, T> AsRef<T> for Either<A, B>
where
T: ?Sized,
{
fn as_ref(&self) -> &T {
match &self {
Self::A(a) => a.as_ref(),
Self::B(b) => b.as_ref(),
}
}
}

impl<A: NapiRaw, B: NapiRaw> Either<A, B> {
/// # Safety
/// Backward compatible with `Either` in **v1**
Expand Down
9 changes: 7 additions & 2 deletions crates/napi/src/bindgen_runtime/js_values/external.rs
Expand Up @@ -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<T: 'static> {
obj: *mut TaggedObject<T>,
Expand All @@ -23,6 +22,12 @@ impl<T: 'static> TypeName for External<T> {
}
}

impl<T: 'static> From<T> for External<T> {
fn from(t: T) -> Self {
External::new(t)
}
}

impl<T: 'static> ValidateNapiValue for External<T> {}

impl<T: 'static> External<T> {
Expand Down
4 changes: 2 additions & 2 deletions crates/napi/src/js_values/de.rs
Expand Up @@ -126,7 +126,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
))
} else {
let key = properties.get_element::<JsString>(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),
Expand Down Expand Up @@ -354,7 +354,7 @@ impl<'de, 'env> MapAccess<'de> for JsObjectAccess<'env> {
));
}
let prop_name = self.properties.get_element::<JsString>(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);
Expand Down