feat(napi): implement value getter for JsNumber

This commit is contained in:
LongYinan 2020-12-29 16:36:38 +08:00
parent aa3f0932c8
commit 50ce9c6569
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881

View file

@ -6,6 +6,36 @@ use crate::{sys, Error, Result};
pub struct JsNumber(pub(crate) Value); pub struct JsNumber(pub(crate) Value);
impl JsNumber {
#[inline]
pub fn get_uint32(&self) -> Result<u32> {
let mut result = 0;
check_status!(unsafe { sys::napi_get_value_uint32(self.0.env, self.0.value, &mut result) })?;
Ok(result)
}
#[inline]
pub fn get_int32(&self) -> Result<i32> {
let mut result = 0;
check_status!(unsafe { sys::napi_get_value_int32(self.0.env, self.0.value, &mut result) })?;
Ok(result)
}
#[inline]
pub fn get_int64(&self) -> Result<i64> {
let mut result = 0;
check_status!(unsafe { sys::napi_get_value_int64(self.0.env, self.0.value, &mut result) })?;
Ok(result)
}
#[inline]
pub fn get_double(&self) -> Result<f64> {
let mut result = 0_f64;
check_status!(unsafe { sys::napi_get_value_double(self.0.env, self.0.value, &mut result) })?;
Ok(result)
}
}
impl TryFrom<JsNumber> for u32 { impl TryFrom<JsNumber> for u32 {
type Error = Error; type Error = Error;