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