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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Implementations in this file are equivalent to a call to `.downcast()` and
// `.value(&mut cx)`. These specialized versions provide a performance benefit
// because they can combine two Node-API calls into a single call that both
// gets the value and checks the type at the same time.

use std::{convert::Infallible, error, fmt, marker::PhantomData, ptr};

use crate::{
    context::Context,
    handle::Handle,
    result::{NeonResult, ResultExt, Throw},
    sys,
    types::{
        buffer::{Binary, TypedArray},
        extract::{private, TryFromJs},
        private::ValueInternal,
        JsArrayBuffer, JsBoolean, JsBuffer, JsNumber, JsString, JsTypedArray, JsValue, Value,
    },
};

#[cfg(feature = "napi-5")]
use crate::types::JsDate;

/// Error returned when a JavaScript value is not the type expected
pub struct TypeExpected<T: Value>(PhantomData<T>);

impl<T: Value> TypeExpected<T> {
    fn new() -> Self {
        Self(PhantomData)
    }
}

impl<T: Value> fmt::Display for TypeExpected<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "expected {}", T::name())
    }
}

impl<T: Value> fmt::Debug for TypeExpected<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("TypeExpected").field(&T::name()).finish()
    }
}

impl<T: Value> error::Error for TypeExpected<T> {}

impl<T, U: Value> ResultExt<T> for Result<T, TypeExpected<U>> {
    fn or_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T> {
        match self {
            Ok(v) => Ok(v),
            Err(_) => cx.throw_type_error(format!("expected {}", U::name())),
        }
    }
}

macro_rules! from_js {
    () => {
        fn from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Self>
        where
            C: Context<'cx>,
        {
            Self::try_from_js(cx, v)?.or_throw(cx)
        }
    };
}

impl<'cx, V> TryFromJs<'cx> for Handle<'cx, V>
where
    V: Value,
{
    type Error = TypeExpected<V>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        Ok(v.downcast(cx).map_err(|_| TypeExpected::new()))
    }

    from_js!();
}

impl<'cx, V: Value> private::Sealed for Handle<'cx, V> {}

impl<'cx, T> TryFromJs<'cx> for Option<T>
where
    T: TryFromJs<'cx>,
{
    type Error = T::Error;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        if is_null_or_undefined(cx, v)? {
            return Ok(Ok(None));
        }

        T::try_from_js(cx, v).map(|v| v.map(Some))
    }

    fn from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Self>
    where
        C: Context<'cx>,
    {
        if is_null_or_undefined(cx, v)? {
            return Ok(None);
        }

        T::from_js(cx, v).map(Some)
    }
}

impl<'cx, T> private::Sealed for Option<T> where T: TryFromJs<'cx> {}

impl<'cx> TryFromJs<'cx> for f64 {
    type Error = TypeExpected<JsNumber>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let mut n = 0f64;

        unsafe {
            match sys::get_value_double(cx.env().to_raw(), v.to_local(), &mut n) {
                sys::Status::NumberExpected => return Ok(Err(TypeExpected::new())),
                sys::Status::PendingException => return Err(Throw::new()),
                status => assert_eq!(status, sys::Status::Ok),
            }
        }

        Ok(Ok(n))
    }

    from_js!();
}

impl private::Sealed for f64 {}

impl<'cx> TryFromJs<'cx> for bool {
    type Error = TypeExpected<JsBoolean>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let mut b = false;

        unsafe {
            match sys::get_value_bool(cx.env().to_raw(), v.to_local(), &mut b) {
                sys::Status::BooleanExpected => return Ok(Err(TypeExpected::new())),
                sys::Status::PendingException => return Err(Throw::new()),
                status => assert_eq!(status, sys::Status::Ok),
            }
        }

        Ok(Ok(b))
    }

    from_js!();
}

impl private::Sealed for bool {}

impl<'cx> TryFromJs<'cx> for String {
    type Error = TypeExpected<JsString>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let env = cx.env().to_raw();
        let v = v.to_local();
        let mut len = 0usize;

        unsafe {
            match sys::get_value_string_utf8(env, v, ptr::null_mut(), 0, &mut len) {
                sys::Status::StringExpected => return Ok(Err(TypeExpected::new())),
                sys::Status::PendingException => return Err(Throw::new()),
                status => assert_eq!(status, sys::Status::Ok),
            }
        }

        // Make room for null terminator to avoid losing a character
        let mut buf = Vec::<u8>::with_capacity(len + 1);
        let mut written = 0usize;

        unsafe {
            assert_eq!(
                sys::get_value_string_utf8(
                    env,
                    v,
                    buf.as_mut_ptr().cast(),
                    buf.capacity(),
                    &mut written,
                ),
                sys::Status::Ok,
            );

            debug_assert_eq!(len, written);
            buf.set_len(len);

            Ok(Ok(String::from_utf8_unchecked(buf)))
        }
    }

    from_js!();
}

impl private::Sealed for String {}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-5")))]
#[cfg(feature = "napi-5")]
/// Extract an [`f64`] from a [`JsDate`]
pub struct Date(pub f64);

#[cfg_attr(docsrs, doc(cfg(feature = "napi-5")))]
#[cfg(feature = "napi-5")]
impl<'cx> TryFromJs<'cx> for Date {
    type Error = TypeExpected<JsDate>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let mut d = 0f64;

        unsafe {
            match sys::get_date_value(cx.env().to_raw(), v.to_local(), &mut d) {
                sys::Status::DateExpected => return Ok(Err(TypeExpected::new())),
                sys::Status::PendingException => return Err(Throw::new()),
                status => assert_eq!(status, sys::Status::Ok),
            }
        }

        Ok(Ok(Date(d)))
    }

    from_js!();
}

impl private::Sealed for Date {}

// This implementation primarily exists for macro authors. It is infallible, rather
// than checking a type, to match the JavaScript conventions of ignoring additional
// arguments.
//
// N.B.: There is a blanket impl of `FromArgs` for `T` where `T: TryFromJs` to make
// the common case of `arity == 1` more ergonomic and avoid `(T)` is *not* a tuple
// foot-gun (but, `(T,)` is). This creates ambiguity for `()`. Are we extracting
// unit from the first argument of a function with `arity == 1` or is this a function
// with `arity == 0`? By making extraction of unit infallible, we eliminate any
// impact from the ambiguity.
impl<'cx> TryFromJs<'cx> for () {
    type Error = Infallible;

    fn try_from_js<C>(
        _cx: &mut C,
        _v: Handle<'cx, JsValue>,
    ) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        Ok(Ok(()))
    }

    fn from_js<C>(_cx: &mut C, _v: Handle<'cx, JsValue>) -> NeonResult<Self>
    where
        C: Context<'cx>,
    {
        Ok(())
    }
}

impl private::Sealed for () {}

impl<'cx, T> TryFromJs<'cx> for Vec<T>
where
    JsTypedArray<T>: Value,
    T: Binary,
{
    type Error = TypeExpected<JsTypedArray<T>>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let v = match v.downcast::<JsTypedArray<T>, _>(cx) {
            Ok(v) => v,
            Err(_) => return Ok(Err(Self::Error::new())),
        };

        Ok(Ok(v.as_slice(cx).to_vec()))
    }

    from_js!();
}

impl<T> private::Sealed for Vec<T>
where
    JsTypedArray<T>: Value,
    T: Binary,
{
}

/// Extract a [`Vec<u8>`] from a [`JsBuffer`]
pub struct Buffer(pub Vec<u8>);

impl<'cx> TryFromJs<'cx> for Buffer {
    type Error = TypeExpected<JsBuffer>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let v = match v.downcast::<JsBuffer, _>(cx) {
            Ok(v) => v,
            Err(_) => return Ok(Err(Self::Error::new())),
        };

        Ok(Ok(Buffer(v.as_slice(cx).to_vec())))
    }

    from_js!();
}

impl private::Sealed for Buffer {}

/// Extract a [`Vec<u8>`] from a [`JsArrayBuffer`]
pub struct ArrayBuffer(pub Vec<u8>);

impl<'cx> TryFromJs<'cx> for ArrayBuffer {
    type Error = TypeExpected<JsBuffer>;

    fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
    where
        C: Context<'cx>,
    {
        let v = match v.downcast::<JsArrayBuffer, _>(cx) {
            Ok(v) => v,
            Err(_) => return Ok(Err(Self::Error::new())),
        };

        Ok(Ok(ArrayBuffer(v.as_slice(cx).to_vec())))
    }

    from_js!();
}

impl private::Sealed for ArrayBuffer {}

fn is_null_or_undefined<'cx, C, V>(cx: &mut C, v: Handle<V>) -> NeonResult<bool>
where
    C: Context<'cx>,
    V: Value,
{
    let mut ty = sys::ValueType::Object;

    unsafe {
        match sys::typeof_value(cx.env().to_raw(), v.to_local(), &mut ty) {
            sys::Status::PendingException => return Err(Throw::new()),
            status => assert_eq!(status, sys::Status::Ok),
        }
    }

    Ok(matches!(
        ty,
        sys::ValueType::Undefined | sys::ValueType::Null,
    ))
}