jsbind/
undefined.rs

1use crate::utils::*;
2
3/// The struct merely carries the underlying `emlite::Val` so it can be
4/// passed through JS APIs; there is only one meaningful instance,
5/// exposed as [`Undefined::VALUE`].
6#[derive(Clone, Debug)]
7#[repr(transparent)]
8pub struct Undefined {
9    inner: emlite::Val,
10}
11
12impl Undefined {
13    /// The canonical `undefined` value.
14    ///
15    /// Because `undefined` has no runtime state, storing a single
16    /// constant is sufficient and avoids repeated `Val::undefined()`
17    /// calls.
18    pub const VALUE: Undefined = Undefined {
19        inner: emlite::Val::undefined(),
20    };
21
22    /// Always returns `false` — `undefined` is never `null`.
23    #[inline]
24    pub fn is_null(&self) -> bool {
25        false
26    }
27
28    /// Always returns `true`.
29    #[inline]
30    pub fn is_undefined(&self) -> bool {
31        true
32    }
33}
34
35bind!(Undefined);
36
37impl crate::prelude::DynCast for Undefined {
38    #[inline]
39    fn instanceof(_val: &emlite::Val) -> bool {
40        false
41    }
42    #[inline]
43    fn unchecked_from_val(v: emlite::Val) -> Self {
44        v.as_::<Self>() // zero-cost new-type cast
45    }
46    #[inline]
47    fn unchecked_from_val_ref(v: &emlite::Val) -> &Self {
48        unsafe { &*(v as *const emlite::Val as *const Self) }
49    }
50    #[inline]
51    fn unchecked_from_val_mut(v: &mut emlite::Val) -> &mut Self {
52        unsafe { &mut *(v as *mut emlite::Val as *mut Self) }
53    }
54}