From 03e80361d9cf66fbcaee387bdbfe62715f9018b4 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 23 Aug 2022 16:17:33 +0800 Subject: [PATCH 1/2] feat(napi): implement ToNapiValue for f32 --- .../src/bindgen_runtime/js_values/number.rs | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/number.rs b/crates/napi/src/bindgen_runtime/js_values/number.rs index 80421706..199622d6 100644 --- a/crates/napi/src/bindgen_runtime/js_values/number.rs +++ b/crates/napi/src/bindgen_runtime/js_values/number.rs @@ -1,11 +1,10 @@ use super::{check_status, sys}; -use crate::{type_of, Error, Result}; +use crate::{bindgen_prelude::ToNapiValue, type_of, Error, Result}; macro_rules! impl_number_conversions { ( $( ($name:literal, $t:ty as $st:ty, $get:ident, $create:ident) ,)* ) => { $( impl $crate::bindgen_prelude::TypeName for $t { - #[inline(always)] fn type_name() -> &'static str { $name } @@ -17,8 +16,7 @@ macro_rules! impl_number_conversions { impl $crate::bindgen_prelude::ValidateNapiValue for $t { } - impl $crate::bindgen_prelude::ToNapiValue for $t { - #[inline(always)] + impl ToNapiValue for $t { unsafe fn to_napi_value(env: $crate::sys::napi_env, val: $t) -> Result<$crate::sys::napi_value> { let mut ptr = std::ptr::null_mut(); let val: $st = val.into(); @@ -34,7 +32,6 @@ macro_rules! impl_number_conversions { } impl $crate::bindgen_prelude::FromNapiValue for $t { - #[inline(always)] unsafe fn from_napi_value(env: $crate::sys::napi_env, napi_val: $crate::sys::napi_value) -> Result { let mut ret = 0 as $st; @@ -62,3 +59,16 @@ impl_number_conversions!( ("i64", i64 as i64, napi_get_value_int64, napi_create_int64), ("f64", f64 as f64, napi_get_value_double, napi_create_double), ); + +impl ToNapiValue for f32 { + unsafe fn to_napi_value(env: crate::sys::napi_env, val: f32) -> Result { + let mut ptr = std::ptr::null_mut(); + + check_status!( + unsafe { sys::napi_create_double(env, val.into(), &mut ptr) }, + "Failed to convert rust type `f32` into napi value", + )?; + + Ok(ptr) + } +} From 36275438ade17be9aef2e66e7aa4eb21e86dd446 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 23 Aug 2022 16:17:53 +0800 Subject: [PATCH 2/2] chore(napi): misc cleanup --- crates/macro/src/parser/mod.rs | 6 ++++++ crates/napi/src/bindgen_runtime/js_values/object.rs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index a38db36a..2c068d1b 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -724,6 +724,12 @@ impl ParseNapi for syn::ItemFn { "#[napi] can't be applied to a function with #[napi(ts_type)]" ); } + if opts.return_if_invalid().is_some() && opts.strict().is_some() { + bail_span!( + self, + "#[napi(return_if_invalid)] can't be used with #[napi(strict)]" + ); + } let napi = self.convert_to_ast(opts); self.to_tokens(tokens); diff --git a/crates/napi/src/bindgen_runtime/js_values/object.rs b/crates/napi/src/bindgen_runtime/js_values/object.rs index 04870e59..9396e6e7 100644 --- a/crates/napi/src/bindgen_runtime/js_values/object.rs +++ b/crates/napi/src/bindgen_runtime/js_values/object.rs @@ -30,7 +30,7 @@ impl Object { check_status!( sys::napi_get_named_property(self.0.env, self.0.value, c_field.as_ptr(), &mut ret), "Failed to get property with field `{}`", - c_field.to_string_lossy(), + field.as_ref(), )?; let ty = type_of!(self.0.env, ret)?;