Skip to main content

jsi_rs/
bigint.rs

1use crate::{JSRuntime, JSString};
2
3/// Wrapper around facebook::jsi::BigInt providing a safe Rust API
4pub struct JSBigInt {
5    pub(crate) inner: cxx::UniquePtr<crate::sys::ffi::JSIBigInt>,
6}
7
8impl JSBigInt {
9    /// Create a new BigInt from a signed 64-bit integer
10    pub fn from_i64(runtime: &mut JSRuntime, value: i64) -> Self {
11        let ptr = crate::sys::ffi::create_bigint_from_i64(runtime.pin_mut(), value);
12        Self { inner: ptr }
13    }
14
15    /// Create a new BigInt from an unsigned 64-bit integer
16    pub fn from_u64(runtime: &mut JSRuntime, value: u64) -> Self {
17        let ptr = crate::sys::ffi::create_bigint_from_u64(runtime.pin_mut(), value);
18        Self { inner: ptr }
19    }
20
21    pub fn as_string_opt(
22        &self,
23        runtime: &mut JSRuntime,
24        radix: i32,
25    ) -> Result<JSString, cxx::Exception> {
26        let inner = crate::sys::ffi::bigint_to_string(runtime.pin_mut(), &self.inner, radix)?;
27        Ok(JSString { inner })
28    }
29
30    pub fn as_string(&self, runtime: &mut JSRuntime) -> Result<JSString, cxx::Exception> {
31        self.as_string_opt(runtime, 10)
32    }
33
34    pub fn to_string(&self, runtime: &mut JSRuntime) -> Result<String, cxx::Exception> {
35        let js_string = self.as_string(runtime)?;
36        Ok(js_string.value(runtime))
37    }
38
39    /// Access the inner UniquePtr for advanced usage
40    #[cfg(feature = "unsafe")]
41    pub fn inner(&self) -> &cxx::UniquePtr<crate::sys::ffi::JSIBigInt> {
42        &self.inner
43    }
44}