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
193
//! Conversions from JavaScript values into Rust values, and the other way around.

use crate::js_string;

use super::{JsBigInt, JsObject, JsString, JsSymbol, JsValue, Profiler};

mod serde_json;
pub(super) mod try_from_js;

impl From<JsString> for JsValue {
    fn from(value: JsString) -> Self {
        let _timer = Profiler::global().start_event("From<JsString>", "value");

        Self::String(value)
    }
}

impl From<char> for JsValue {
    #[inline]
    fn from(value: char) -> Self {
        let _timer = Profiler::global().start_event("From<char>", "value");

        let mut buf: [u16; 2] = [0; 2];

        let out = value.encode_utf16(&mut buf);

        Self::from(js_string!(&*out))
    }
}

impl From<JsSymbol> for JsValue {
    #[inline]
    fn from(value: JsSymbol) -> Self {
        let _timer = Profiler::global().start_event("From<JsSymbol>", "value");

        Self::Symbol(value)
    }
}

impl From<f32> for JsValue {
    #[inline]
    fn from(value: f32) -> Self {
        let _timer = Profiler::global().start_event("From<f32>", "value");

        Self::Rational(value.into())
    }
}

impl From<f64> for JsValue {
    #[inline]
    fn from(value: f64) -> Self {
        let _timer = Profiler::global().start_event("From<f64>", "value");

        Self::Rational(value)
    }
}

impl From<u8> for JsValue {
    #[inline]
    fn from(value: u8) -> Self {
        let _timer = Profiler::global().start_event("From<u8>", "value");

        Self::Integer(value.into())
    }
}

impl From<i8> for JsValue {
    #[inline]
    fn from(value: i8) -> Self {
        let _timer = Profiler::global().start_event("From<i8>", "value");

        Self::Integer(value.into())
    }
}

impl From<u16> for JsValue {
    #[inline]
    fn from(value: u16) -> Self {
        let _timer = Profiler::global().start_event("From<u16>", "value");

        Self::Integer(value.into())
    }
}

impl From<i16> for JsValue {
    #[inline]
    fn from(value: i16) -> Self {
        let _timer = Profiler::global().start_event("From<i16>", "value");

        Self::Integer(value.into())
    }
}

impl From<u32> for JsValue {
    #[inline]
    fn from(value: u32) -> Self {
        let _timer = Profiler::global().start_event("From<u32>", "value");

        i32::try_from(value).map_or_else(|_| Self::Rational(value.into()), Self::Integer)
    }
}

impl From<i32> for JsValue {
    #[inline]
    fn from(value: i32) -> Self {
        let _timer = Profiler::global().start_event("From<i32>", "value");

        Self::Integer(value)
    }
}

impl From<JsBigInt> for JsValue {
    #[inline]
    fn from(value: JsBigInt) -> Self {
        let _timer = Profiler::global().start_event("From<JsBigInt>", "value");

        Self::BigInt(value)
    }
}

impl From<usize> for JsValue {
    #[inline]
    fn from(value: usize) -> Self {
        let _timer = Profiler::global().start_event("From<usize>", "value");

        i32::try_from(value).map_or(Self::Rational(value as f64), Self::Integer)
    }
}

impl From<u64> for JsValue {
    #[inline]
    fn from(value: u64) -> Self {
        let _timer = Profiler::global().start_event("From<u64>", "value");

        i32::try_from(value).map_or(Self::Rational(value as f64), Self::Integer)
    }
}

impl From<i64> for JsValue {
    #[inline]
    fn from(value: i64) -> Self {
        let _timer = Profiler::global().start_event("From<i64>", "value");

        i32::try_from(value).map_or(Self::Rational(value as f64), Self::Integer)
    }
}

impl From<bool> for JsValue {
    #[inline]
    fn from(value: bool) -> Self {
        let _timer = Profiler::global().start_event("From<bool>", "value");

        Self::Boolean(value)
    }
}

impl From<JsObject> for JsValue {
    #[inline]
    fn from(object: JsObject) -> Self {
        let _timer = Profiler::global().start_event("From<JsObject>", "value");

        Self::Object(object)
    }
}

impl From<()> for JsValue {
    #[inline]
    #[allow(clippy::pedantic)] // didn't want to increase our MSRV for just a lint.
    fn from(_: ()) -> Self {
        let _timer = Profiler::global().start_event("From<()>", "value");

        Self::null()
    }
}

/// Converts an `Option<T>` into a `JsValue`.
///
/// It will convert the `None` variant to `JsValue::undefined()`, and the `Some()` variant into a
/// `JsValue` using the `Into` trait.
pub(crate) trait IntoOrUndefined {
    /// Converts an `Option<T>` into a `JsValue`.
    fn into_or_undefined(self) -> JsValue;
}

impl<T> IntoOrUndefined for Option<T>
where
    T: Into<JsValue>,
{
    #[inline]
    fn into_or_undefined(self) -> JsValue {
        self.map_or_else(JsValue::undefined, Into::into)
    }
}