2020-06-21 20:10:06 +09:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use super::Value;
|
2020-11-25 18:42:14 +09:00
|
|
|
use crate::check_status;
|
2020-06-21 20:10:06 +09:00
|
|
|
use crate::{sys, Error, Result};
|
|
|
|
|
|
|
|
pub struct JsNumber(pub(crate) Value);
|
|
|
|
|
2020-12-29 17:36:38 +09:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
impl TryFrom<JsNumber> for u32 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: JsNumber) -> Result<u32> {
|
|
|
|
let mut result = 0;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_get_value_uint32(value.0.env, value.0.value, &mut result) })?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<JsNumber> for i32 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: JsNumber) -> Result<i32> {
|
|
|
|
let mut result = 0;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_get_value_int32(value.0.env, value.0.value, &mut result) })?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<JsNumber> for i64 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: JsNumber) -> Result<i64> {
|
|
|
|
let mut result = 0;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_get_value_int64(value.0.env, value.0.value, &mut result) })?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<JsNumber> for f64 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: JsNumber) -> Result<f64> {
|
|
|
|
let mut result = 0_f64;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_get_value_double(value.0.env, value.0.value, &mut result) })?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|