nodex_api/value/
bigint.rs

1use crate::{api, prelude::*};
2use std::marker::PhantomData;
3
4#[derive(Copy, Clone, Debug)]
5pub struct JsBigInt<T: Copy>(pub(crate) JsValue, PhantomData<T>);
6
7impl<T: Copy> JsBigInt<T> {
8    pub(crate) fn from_value(value: JsValue) -> JsBigInt<T> {
9        JsBigInt(value, PhantomData)
10    }
11
12    #[cfg(feature = "v6")]
13    /// This API converts the C int64_t type to the JavaScript BigInt type.
14    pub fn new_i64(env: NapiEnv, value: i64) -> NapiResult<JsBigInt<i64>> {
15        let value = napi_call!(=napi_create_bigint_int64, env, value);
16        Ok(JsBigInt::from_raw(env, value))
17    }
18
19    #[cfg(feature = "v6")]
20    /// This API converts the C unt64_t type to the JavaScript BigInt type.
21    pub fn new_u64(env: NapiEnv, value: u64) -> NapiResult<JsBigInt<u64>> {
22        let value = napi_call!(=napi_create_bigint_uint64, env, value);
23        Ok(JsBigInt::from_raw(env, value))
24    }
25}
26
27impl<T: Copy> NapiValueT for JsBigInt<T> {
28    fn from_raw(env: NapiEnv, raw: napi_value) -> JsBigInt<T> {
29        JsBigInt(JsValue(env, raw), PhantomData)
30    }
31
32    fn value(&self) -> JsValue {
33        self.0
34    }
35}
36
37impl<T: Copy> NapiValueCheck for JsBigInt<T> {
38    fn check(&self) -> NapiResult<bool> {
39        Ok(self.kind()? == NapiValuetype::Bigint)
40    }
41}