1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
use std::convert::TryFrom; use super::Value; use crate::check_status; use crate::{sys, Error, Result}; #[derive(Clone, Copy)] 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 { type Error = Error; fn try_from(value: JsNumber) -> Result<u32> { let mut result = 0; check_status!(unsafe { sys::napi_get_value_uint32(value.0.env, value.0.value, &mut result) })?; Ok(result) } } impl TryFrom<JsNumber> for i32 { type Error = Error; fn try_from(value: JsNumber) -> Result<i32> { let mut result = 0; check_status!(unsafe { sys::napi_get_value_int32(value.0.env, value.0.value, &mut result) })?; Ok(result) } } impl TryFrom<JsNumber> for i64 { type Error = Error; fn try_from(value: JsNumber) -> Result<i64> { let mut result = 0; check_status!(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?; Ok(result) } } impl TryFrom<JsNumber> for f64 { type Error = Error; fn try_from(value: JsNumber) -> Result<f64> { let mut result = 0_f64; check_status!(unsafe { sys::napi_get_value_double(value.0.env, value.0.value, &mut result) })?; Ok(result) } }