From 50ce9c656985cc60ffb49f44ee7360bc74a38b07 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 29 Dec 2020 16:36:38 +0800 Subject: [PATCH] feat(napi): implement value getter for JsNumber --- napi/src/js_values/number.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/napi/src/js_values/number.rs b/napi/src/js_values/number.rs index a81f45d5..b0cd3624 100644 --- a/napi/src/js_values/number.rs +++ b/napi/src/js_values/number.rs @@ -6,6 +6,36 @@ use crate::{sys, Error, Result}; pub struct JsNumber(pub(crate) Value); +impl JsNumber { + #[inline] + pub fn get_uint32(&self) -> Result { + 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 { + 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 { + 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 { + 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 for u32 { type Error = Error;