Skip to main content

nanvm_lib/vm/
unpacked.rs

1use crate::vm::{
2    Array, BigInt, Function, IVm, Number, Object, String, dispatch::Dispatch, nullish::Nullish,
3};
4
5#[derive(Clone)]
6pub enum Unpacked<A: IVm> {
7    Nullish(Nullish),
8    Boolean(bool),
9    Number(Number),
10    String(String<A>),
11    BigInt(BigInt<A>),
12    Object(Object<A>),
13    Array(Array<A>),
14    Function(Function<A>),
15}
16
17impl<A: IVm> Unpacked<A> {
18    /// Whether this is `undefined` or `null`: the one place the match is
19    /// written. A match, never `Nullish::try_from`, which builds and discards
20    /// an error value for every value that is not nullish, the common answer.
21    pub fn is_nullish(&self) -> bool {
22        matches!(self, Unpacked::Nullish(_))
23    }
24
25    /// Whether this is `undefined`, and not `null`: the absent value an
26    /// optional argument takes its default on. A match, as `is_nullish` is.
27    pub fn is_undefined(&self) -> bool {
28        matches!(self, Unpacked::Nullish(Nullish::Undefined))
29    }
30
31    pub fn dispatch<T: Dispatch<A>>(self, o: T) -> T::Result {
32        match self {
33            Unpacked::Nullish(v) => o.nullish(v),
34            Unpacked::Boolean(v) => o.bool(v),
35            Unpacked::Number(v) => o.number(v),
36            Unpacked::String(v) => o.string(v),
37            Unpacked::BigInt(v) => o.bigint(v),
38            Unpacked::Object(v) => o.object(v),
39            Unpacked::Array(v) => o.array(v),
40            Unpacked::Function(v) => o.function(v),
41        }
42    }
43}