quickjs_rusty/value/
tag.rs

1use std::fmt::Debug;
2
3use libquickjs_ng_sys as q;
4
5#[repr(i32)]
6#[derive(PartialEq, Eq, Clone, Copy, Debug)]
7pub enum JsTag {
8    // Used by C code as a marker.
9    // Not relevant for bindings.
10    // First = q::JS_TAG_FIRST,
11    #[cfg(feature = "bigint")]
12    BigInt = q::JS_TAG_BIG_INT,
13    Symbol = q::JS_TAG_SYMBOL,
14    String = q::JS_TAG_STRING,
15    Module = q::JS_TAG_MODULE,
16    FunctionBytecode = q::JS_TAG_FUNCTION_BYTECODE,
17    Object = q::JS_TAG_OBJECT,
18
19    Int = q::JS_TAG_INT,
20    Bool = q::JS_TAG_BOOL,
21    Null = q::JS_TAG_NULL,
22    Undefined = q::JS_TAG_UNDEFINED,
23    Uninitialized = q::JS_TAG_UNINITIALIZED,
24    CatchOffset = q::JS_TAG_CATCH_OFFSET,
25    Exception = q::JS_TAG_EXCEPTION,
26    ShortBigInt = q::JS_TAG_SHORT_BIG_INT,
27    Float64 = q::JS_TAG_FLOAT64,
28}
29
30impl JsTag {
31    #[inline]
32    pub(super) fn from_c(value: &q::JSValue) -> JsTag {
33        let inner = unsafe { q::JS_Ext_ValueGetTag(*value) };
34        match inner {
35            q::JS_TAG_INT => JsTag::Int,
36            q::JS_TAG_BOOL => JsTag::Bool,
37            q::JS_TAG_NULL => JsTag::Null,
38            q::JS_TAG_MODULE => JsTag::Module,
39            q::JS_TAG_OBJECT => JsTag::Object,
40            q::JS_TAG_STRING => JsTag::String,
41            q::JS_TAG_SYMBOL => JsTag::Symbol,
42            q::JS_TAG_FLOAT64 => JsTag::Float64,
43            q::JS_TAG_EXCEPTION => JsTag::Exception,
44            q::JS_TAG_UNDEFINED => JsTag::Undefined,
45            q::JS_TAG_CATCH_OFFSET => JsTag::CatchOffset,
46            q::JS_TAG_UNINITIALIZED => JsTag::Uninitialized,
47            q::JS_TAG_FUNCTION_BYTECODE => JsTag::FunctionBytecode,
48            #[cfg(feature = "bigint")]
49            q::JS_TAG_SHORT_BIG_INT => JsTag::ShortBigInt,
50            #[cfg(feature = "bigint")]
51            q::JS_TAG_BIG_INT => JsTag::BigInt,
52            _other => {
53                unreachable!("Unknown js_tag: {}", _other);
54            }
55        }
56    }
57
58    #[allow(dead_code)]
59    pub(super) fn to_c(self) -> i32 {
60        // TODO: figure out why this is needed
61        // Just casting with `as` does not work correctly
62        match self {
63            #[cfg(feature = "bigint")]
64            JsTag::BigInt => q::JS_TAG_FUNCTION_BYTECODE,
65            JsTag::Symbol => q::JS_TAG_SYMBOL,
66            JsTag::String => q::JS_TAG_STRING,
67            JsTag::Module => q::JS_TAG_MODULE,
68            JsTag::FunctionBytecode => q::JS_TAG_FUNCTION_BYTECODE,
69            JsTag::Object => q::JS_TAG_OBJECT,
70
71            JsTag::Int => q::JS_TAG_INT,
72            JsTag::Bool => q::JS_TAG_BOOL,
73            JsTag::Null => q::JS_TAG_NULL,
74            JsTag::Undefined => q::JS_TAG_UNDEFINED,
75            JsTag::Uninitialized => q::JS_TAG_UNINITIALIZED,
76            JsTag::CatchOffset => q::JS_TAG_CATCH_OFFSET,
77            JsTag::Exception => q::JS_TAG_EXCEPTION,
78            #[cfg(feature = "bigint")]
79            JsTag::ShortBigInt => q::JS_TAG_SHORT_BIG_INT,
80            JsTag::Float64 => q::JS_TAG_FLOAT64,
81        }
82    }
83
84    /// Returns `true` if the js_tag is [`Undefined`].
85    #[inline]
86    pub fn is_undefined(&self) -> bool {
87        matches!(self, Self::Undefined)
88    }
89
90    /// Returns `true` if the js_tag is [`Object`].
91    #[inline]
92    pub fn is_object(&self) -> bool {
93        matches!(self, Self::Object)
94    }
95
96    /// Returns `true` if the js_tag is [`Exception`].
97    #[inline]
98    pub fn is_exception(&self) -> bool {
99        matches!(self, Self::Exception)
100    }
101
102    /// Returns `true` if the js_tag is [`Int`].
103    #[inline]
104    pub fn is_int(&self) -> bool {
105        matches!(self, Self::Int)
106    }
107
108    /// Returns `true` if the js_tag is [`Bool`].
109    #[inline]
110    pub fn is_bool(&self) -> bool {
111        matches!(self, Self::Bool)
112    }
113
114    /// Returns `true` if the js_tag is [`Null`].
115    #[inline]
116    pub fn is_null(&self) -> bool {
117        matches!(self, Self::Null)
118    }
119
120    /// Returns `true` if the js_tag is [`Module`].
121    #[inline]
122    pub fn is_module(&self) -> bool {
123        matches!(self, Self::Module)
124    }
125
126    /// Returns `true` if the js_tag is [`String`].
127    #[inline]
128    pub fn is_string(&self) -> bool {
129        matches!(self, Self::String)
130    }
131
132    /// Returns `true` if the js_tag is [`Symbol`].
133    #[inline]
134    pub fn is_symbol(&self) -> bool {
135        matches!(self, Self::Symbol)
136    }
137
138    /// Returns `true` if the js_tag is [`BigInt`].
139    #[cfg(feature = "bigint")]
140    #[inline]
141    pub fn is_big_int(&self) -> bool {
142        matches!(self, Self::BigInt)
143    }
144
145    /// Returns `true` if the js_tag is [`Float64`].
146    #[inline]
147    pub fn is_float64(&self) -> bool {
148        matches!(self, Self::Float64)
149    }
150}