neon/types_impl/extract/
try_into_js.rs1use crate::{
2 context::{Context, Cx},
3 handle::{Handle, Root},
4 object::Object,
5 result::{JsResult, ResultExt, Throw},
6 types::{
7 extract::{Date, TryIntoJs},
8 JsBoolean, JsDate, JsNumber, JsString, JsUndefined, JsValue, Value,
9 },
10};
11
12impl<'cx, T> TryIntoJs<'cx> for Handle<'cx, T>
13where
14 T: Value,
15{
16 type Value = T;
17
18 fn try_into_js(self, _cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
19 Ok(self)
20 }
21}
22
23impl<'cx, O> TryIntoJs<'cx> for Root<O>
24where
25 O: Object,
26{
27 type Value = O;
28
29 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
30 Ok(self.into_inner(cx))
31 }
32}
33
34impl<'cx, T, E> TryIntoJs<'cx> for Result<T, E>
35where
36 T: TryIntoJs<'cx>,
37 E: TryIntoJs<'cx>,
38{
39 type Value = T::Value;
40
41 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
42 match self {
43 Ok(v) => v.try_into_js(cx),
44 Err(err) => {
45 let err = err.try_into_js(cx)?;
46
47 cx.throw(err)
48 }
49 }
50 }
51}
52
53impl<'cx> TryIntoJs<'cx> for Throw {
54 type Value = JsValue;
55
56 fn try_into_js(self, _cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
57 Err(self)
58 }
59}
60
61impl<'cx, T> TryIntoJs<'cx> for Option<T>
62where
63 T: TryIntoJs<'cx>,
64{
65 type Value = JsValue;
66
67 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
68 if let Some(val) = self {
69 val.try_into_js(cx).map(|v| v.upcast())
70 } else {
71 Ok(cx.undefined().upcast())
72 }
73 }
74}
75
76impl<'cx, T> TryIntoJs<'cx> for Box<T>
77where
78 T: TryIntoJs<'cx>,
79{
80 type Value = T::Value;
81
82 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
83 (*self).try_into_js(cx)
84 }
85}
86
87macro_rules! impl_number {
88 ($ty:ident) => {
89 impl<'cx> TryIntoJs<'cx> for $ty {
90 type Value = JsNumber;
91
92 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
93 Ok(cx.number(self))
94 }
95 }
96 };
97
98 ($($ty:ident),* $(,)?) => {
99 $(
100 impl_number!($ty);
101 )*
102 }
103}
104
105impl_number!(u8, u16, u32, i8, i16, i32, f32, f64);
106
107impl<'cx> TryIntoJs<'cx> for String {
108 type Value = JsString;
109
110 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
111 Ok(cx.string(self))
112 }
113}
114
115impl<'a, 'cx> TryIntoJs<'cx> for &'a str {
116 type Value = JsString;
117
118 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
119 Ok(cx.string(self))
120 }
121}
122
123impl<'a, 'cx> TryIntoJs<'cx> for &'a String {
124 type Value = JsString;
125
126 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
127 Ok(cx.string(self))
128 }
129}
130
131impl<'cx> TryIntoJs<'cx> for bool {
132 type Value = JsBoolean;
133
134 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
135 Ok(cx.boolean(self))
136 }
137}
138
139impl<'cx> TryIntoJs<'cx> for () {
140 type Value = JsUndefined;
141
142 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
143 Ok(cx.undefined())
144 }
145}
146
147impl<'cx> TryIntoJs<'cx> for Date {
148 type Value = JsDate;
149
150 fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
151 cx.date(self.0).or_throw(cx)
152 }
153}