pinar/objects/
jsref.rs

1
2use std::marker::PhantomData;
3use napi_sys::*;
4use crate::Result;
5use crate::prelude::*;
6
7#[allow(dead_code)]
8pub struct JsRef<T: JsValue> {
9    pub(crate) env: Env,
10    pub(crate) js_ref: napi_ref,
11    phantom: PhantomData<T>
12}
13
14pub trait AsJsRef<T: JsValue> {
15    fn as_js_ref(&self) -> Result<JsRef<T>>;
16}
17
18impl<T: JsValue> AsJsRef<T> for T {
19    fn as_js_ref(&self) -> Result<JsRef<T>> {
20        let env = self.get_value().env;
21        let mut js_ref: napi_ref = std::ptr::null_mut();
22        unsafe {
23            Status::result(napi_create_reference(
24                env.env(),
25                self.get_value().value,
26                1,
27                &mut js_ref as *mut napi_ref
28            ))?;
29        }
30        Ok(JsRef {
31            env,
32            js_ref,
33            phantom: PhantomData
34        })
35    }
36}