Merge pull request #1286 from napi-rs/f32-to-napi-value

feat(napi): implement ToNapiValue for f32
This commit is contained in:
LongYinan 2022-08-23 17:03:01 +08:00 committed by GitHub
commit 4153c03a9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View file

@ -724,6 +724,12 @@ impl ParseNapi for syn::ItemFn {
"#[napi] can't be applied to a function with #[napi(ts_type)]" "#[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); let napi = self.convert_to_ast(opts);
self.to_tokens(tokens); self.to_tokens(tokens);

View file

@ -1,11 +1,10 @@
use super::{check_status, sys}; 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 { macro_rules! impl_number_conversions {
( $( ($name:literal, $t:ty as $st:ty, $get:ident, $create:ident) ,)* ) => { ( $( ($name:literal, $t:ty as $st:ty, $get:ident, $create:ident) ,)* ) => {
$( $(
impl $crate::bindgen_prelude::TypeName for $t { impl $crate::bindgen_prelude::TypeName for $t {
#[inline(always)]
fn type_name() -> &'static str { fn type_name() -> &'static str {
$name $name
} }
@ -17,8 +16,7 @@ macro_rules! impl_number_conversions {
impl $crate::bindgen_prelude::ValidateNapiValue for $t { } impl $crate::bindgen_prelude::ValidateNapiValue for $t { }
impl $crate::bindgen_prelude::ToNapiValue for $t { impl ToNapiValue for $t {
#[inline(always)]
unsafe fn to_napi_value(env: $crate::sys::napi_env, val: $t) -> Result<$crate::sys::napi_value> { 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 mut ptr = std::ptr::null_mut();
let val: $st = val.into(); let val: $st = val.into();
@ -34,7 +32,6 @@ macro_rules! impl_number_conversions {
} }
impl $crate::bindgen_prelude::FromNapiValue for $t { 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<Self> { unsafe fn from_napi_value(env: $crate::sys::napi_env, napi_val: $crate::sys::napi_value) -> Result<Self> {
let mut ret = 0 as $st; let mut ret = 0 as $st;
@ -62,3 +59,16 @@ impl_number_conversions!(
("i64", i64 as i64, napi_get_value_int64, napi_create_int64), ("i64", i64 as i64, napi_get_value_int64, napi_create_int64),
("f64", f64 as f64, napi_get_value_double, napi_create_double), ("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<crate::sys::napi_value> {
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)
}
}

View file

@ -30,7 +30,7 @@ impl Object {
check_status!( check_status!(
sys::napi_get_named_property(self.0.env, self.0.value, c_field.as_ptr(), &mut ret), sys::napi_get_named_property(self.0.env, self.0.value, c_field.as_ptr(), &mut ret),
"Failed to get property with field `{}`", "Failed to get property with field `{}`",
c_field.to_string_lossy(), field.as_ref(),
)?; )?;
let ty = type_of!(self.0.env, ret)?; let ty = type_of!(self.0.env, ret)?;