1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use deno_core::v8;
use std::mem::transmute;
use std::ops::Deref;
use std::os::raw::c_void;
use std::ptr::NonNull;
#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct NapiValue<'s>(
Option<NonNull<v8::Value>>,
std::marker::PhantomData<&'s ()>,
);
pub type napi_value<'s> = NapiValue<'s>;
impl<'s> Deref for napi_value<'s> {
type Target = Option<v8::Local<'s, v8::Value>>;
fn deref(&self) -> &Self::Target {
unsafe { transmute::<&Self, &Self::Target>(self) }
}
}
impl<'s, T> From<v8::Local<'s, T>> for napi_value<'s>
where
v8::Local<'s, T>: Into<v8::Local<'s, v8::Value>>,
{
fn from(v: v8::Local<'s, T>) -> Self {
Self(unsafe { transmute(v) }, std::marker::PhantomData)
}
}
const _: () = {
assert!(
std::mem::size_of::<napi_value>() == std::mem::size_of::<*mut c_void>()
);
unsafe {
type Src<'a> = napi_value<'a>;
type Dst = usize;
assert!(std::mem::size_of::<Src>() == std::mem::size_of::<Dst>());
union Transmute<'a> {
src: Src<'a>,
dst: Dst,
}
Transmute {
src: NapiValue(None, std::marker::PhantomData),
}
.dst
};
};