Skip to main content

jsi_rs/
runtime.rs

1use std::pin::Pin;
2
3use crate::sys::ffi;
4
5pub trait IntoJSIBigInt {
6    fn create_jsi_bigint(self, runtime: &mut JSRuntime) -> crate::JSBigInt;
7}
8
9impl IntoJSIBigInt for i64 {
10    fn create_jsi_bigint(self, runtime: &mut JSRuntime) -> crate::JSBigInt {
11        crate::JSBigInt::from_i64(runtime, self)
12    }
13}
14
15impl IntoJSIBigInt for u64 {
16    fn create_jsi_bigint(self, runtime: &mut JSRuntime) -> crate::JSBigInt {
17        crate::JSBigInt::from_u64(runtime, self)
18    }
19}
20
21/// Wrapper around facebook::jsi::Runtime providing a safe Rust API
22pub struct JSRuntime {
23    pub(crate) ptr: *mut ffi::JSIRuntime,
24}
25
26impl JSRuntime {
27    pub unsafe fn from_raw(ptr: *mut ffi::JSIRuntime) -> Self {
28        Self { ptr }
29    }
30
31    pub(crate) fn pin_mut(&mut self) -> Pin<&mut ffi::JSIRuntime> {
32        unsafe { Pin::new_unchecked(&mut *self.ptr) }
33    }
34
35    /// Access the inner raw pointer for advanced usage
36    #[cfg(feature = "unsafe")]
37    pub fn inner(&self) -> *mut ffi::JSIRuntime {
38        self.ptr
39    }
40
41    pub fn create_undefined() -> crate::JSValue {
42        crate::JSValue::undefined()
43    }
44
45    pub fn create_null() -> crate::JSValue {
46        crate::JSValue::null()
47    }
48
49    pub fn create_bool(value: bool) -> crate::JSValue {
50        crate::JSValue::bool(value)
51    }
52
53    pub fn create_number(value: f64) -> crate::JSValue {
54        crate::JSValue::number(value)
55    }
56
57    pub fn create_string(&mut self, data: &str) -> crate::JSString {
58        crate::JSString::new(self, data)
59    }
60
61    pub fn create_object(&mut self) -> crate::JSObject {
62        crate::JSObject::new(self)
63    }
64
65    pub fn create_array_empty(&mut self) -> crate::JSArray {
66        crate::JSArray::new(self, 0)
67    }
68
69    pub fn create_array(&mut self, length: usize) -> crate::JSArray {
70        crate::JSArray::new(self, length)
71    }
72
73    pub fn create_prop_name_id(&mut self, name: &str) -> crate::JSPropNameID {
74        crate::JSPropNameID::new(self, name)
75    }
76
77    pub fn create_bigint<T: IntoJSIBigInt>(&mut self, value: T) -> crate::JSBigInt {
78        value.create_jsi_bigint(self)
79    }
80}
81
82// JSRuntime is Send but not Sync
83// JavaScript runtimes are typically single-threaded
84unsafe impl Send for JSRuntime {}
85
86#[cfg(test)]
87mod tests {}