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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
pub(crate) mod internal;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::error::Error;
use std::fmt::{self, Debug, Display};
use neon_runtime;
use neon_runtime::raw;
use types::Value;
use context::Context;
#[cfg(feature = "legacy-runtime")]
use context::internal::Env;
use result::{JsResult, JsResultExt};
use self::internal::SuperType;
pub trait Managed: Copy {
fn to_raw(self) -> raw::Local;
fn from_raw(h: raw::Local) -> Self;
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Handle<'a, T: Managed + 'a> {
value: T,
phantom: PhantomData<&'a T>
}
impl<'a, T: Managed + 'a> PartialEq for Handle<'a, T> {
fn eq(&self, other: &Self) -> bool {
unsafe { neon_runtime::mem::same_handle(self.to_raw(), other.to_raw()) }
}
}
impl<'a, T: Managed + 'a> Eq for Handle<'a, T> { }
impl<'a, T: Managed + 'a> Handle<'a, T> {
pub(crate) fn new_internal(value: T) -> Handle<'a, T> {
Handle {
value: value,
phantom: PhantomData
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct DowncastError<F: Value, T: Value> {
phantom_from: PhantomData<F>,
phantom_to: PhantomData<T>,
}
impl<F: Value, T: Value> Debug for DowncastError<F, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "DowncastError")
}
}
impl<F: Value, T: Value> DowncastError<F, T> {
fn new() -> Self {
DowncastError {
phantom_from: PhantomData,
phantom_to: PhantomData,
}
}
}
impl<F: Value, T: Value> Display for DowncastError<F, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "failed to downcast {} to {}", F::name(), T::name())
}
}
impl<F: Value, T: Value> Error for DowncastError<F, T> {}
pub type DowncastResult<'a, F, T> = Result<Handle<'a, T>, DowncastError<F, T>>;
impl<'a, F: Value, T: Value> JsResultExt<'a, T> for DowncastResult<'a, F, T> {
fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, T> {
match self {
Ok(v) => Ok(v),
Err(e) => cx.throw_type_error(&e.to_string())
}
}
}
impl<'a, T: Value> Handle<'a, T> {
pub fn upcast<U: Value + SuperType<T>>(&self) -> Handle<'a, U> {
Handle::new_internal(SuperType::upcast_internal(self.value))
}
#[cfg(feature = "legacy-runtime")]
pub fn is_a<U: Value>(&self) -> bool {
U::is_typeof(Env::current(), self.value)
}
#[cfg(feature = "napi-runtime")]
pub fn is_a<'b, U: Value, C: Context<'b>>(&self, cx: &mut C) -> bool {
U::is_typeof(cx.env(), self.value)
}
#[cfg(feature = "legacy-runtime")]
pub fn downcast<U: Value>(&self) -> DowncastResult<'a, T, U> {
match U::downcast(Env::current(), self.value) {
Some(v) => Ok(Handle::new_internal(v)),
None => Err(DowncastError::new())
}
}
#[cfg(feature = "napi-runtime")]
pub fn downcast<'b, U: Value, C: Context<'b>>(&self, cx: &mut C) -> DowncastResult<'a, T, U> {
match U::downcast(cx.env(), self.value) {
Some(v) => Ok(Handle::new_internal(v)),
None => Err(DowncastError::new())
}
}
#[cfg(feature = "legacy-runtime")]
pub fn downcast_or_throw<'b, U: Value, C: Context<'b>>(&self, cx: &mut C) -> JsResult<'a, U> {
self.downcast().or_throw(cx)
}
#[cfg(feature = "napi-runtime")]
pub fn downcast_or_throw<'b, U: Value, C: Context<'b>>(&self, cx: &mut C) -> JsResult<'a, U> {
self.downcast(cx).or_throw(cx)
}
}
impl<'a, T: Managed> Deref for Handle<'a, T> {
type Target = T;
fn deref<'b>(&'b self) -> &'b T {
&self.value
}
}
impl<'a, T: Managed> DerefMut for Handle<'a, T> {
fn deref_mut<'b>(&'b mut self) -> &'b mut T {
&mut self.value
}
}