napi/js_values/
global.rs

1use super::*;
2use crate::bindgen_runtime::{FnArgs, FromNapiValue, Function, Unknown};
3
4pub struct JsGlobal<'env>(
5  pub(crate) Value,
6  pub(crate) std::marker::PhantomData<&'env ()>,
7);
8
9impl FromNapiValue for JsGlobal<'_> {
10  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
11    Ok(JsGlobal(
12      Value {
13        env,
14        value: napi_val,
15        value_type: ValueType::Object,
16      },
17      std::marker::PhantomData,
18    ))
19  }
20}
21
22impl<'env> JsValue<'env> for JsGlobal<'env> {
23  fn value(&self) -> Value {
24    self.0
25  }
26}
27
28impl<'env> JsObjectValue<'env> for JsGlobal<'env> {}
29
30pub struct JsTimeout<'env>(
31  pub(crate) Value,
32  pub(crate) std::marker::PhantomData<&'env ()>,
33);
34
35impl<'env> JsValue<'env> for JsTimeout<'env> {
36  fn value(&self) -> Value {
37    self.0
38  }
39}
40
41impl<'env> JsObjectValue<'env> for JsTimeout<'env> {}
42
43impl FromNapiValue for JsTimeout<'_> {
44  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
45    Ok(JsTimeout(
46      Value {
47        env,
48        value: napi_val,
49        value_type: ValueType::Object,
50      },
51      std::marker::PhantomData,
52    ))
53  }
54}
55pub struct JSON<'env>(
56  pub(crate) Value,
57  pub(crate) std::marker::PhantomData<&'env ()>,
58);
59
60impl<'env> JsValue<'env> for JSON<'env> {
61  fn value(&self) -> Value {
62    self.0
63  }
64}
65
66impl<'env> JsObjectValue<'env> for JSON<'env> {}
67
68impl FromNapiValue for JSON<'_> {
69  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
70    Ok(JSON(
71      Value {
72        env,
73        value: napi_val,
74        value_type: ValueType::Object,
75      },
76      std::marker::PhantomData,
77    ))
78  }
79}
80
81impl JSON<'_> {
82  pub fn stringify<V: ToNapiValue>(&self, value: V) -> Result<std::string::String> {
83    let func: Function<V, std::string::String> = self.get_named_property_unchecked("stringify")?;
84    func.call(value)
85  }
86}
87
88type SupportType<'a> = Function<'a, FnArgs<(Function<'a, (), Unknown<'a>>, f64)>, JsTimeout<'a>>;
89
90impl<'env> JsGlobal<'env> {
91  pub fn set_interval(
92    &self,
93    handler: Function<(), Unknown>,
94    interval: f64,
95  ) -> Result<JsTimeout<'env>> {
96    let func: SupportType = self.get_named_property_unchecked("setInterval")?;
97    func.call(FnArgs {
98      data: (handler, interval),
99    })
100  }
101
102  pub fn clear_interval(&self, timer: JsTimeout) -> Result<()> {
103    let func: Function<JsTimeout, ()> = self.get_named_property_unchecked("clearInterval")?;
104    func.call(timer)
105  }
106
107  pub fn set_timeout(
108    &self,
109    handler: Function<(), Unknown>,
110    interval: f64,
111  ) -> Result<JsTimeout<'env>> {
112    let func: SupportType = self.get_named_property_unchecked("setTimeout")?;
113    func.call(FnArgs {
114      data: (handler, interval),
115    })
116  }
117
118  pub fn clear_timeout(&self, timer: JsTimeout) -> Result<()> {
119    let func: Function<JsTimeout, ()> = self.get_named_property_unchecked("clearTimeout")?;
120    func.call(timer)
121  }
122}