boa_engine/value/
variant.rs1#[cfg(feature = "annex-b")]
2use crate::builtins::is_html_dda::IsHTMLDDA;
3use crate::{JsBigInt, JsObject, JsSymbol, JsValue};
4use boa_engine::js_string;
5use boa_string::JsString;
6
7#[derive(Debug, PartialEq)]
11pub enum JsVariant {
12 Null,
14 Undefined,
16 Boolean(bool),
18 String(JsString),
20 Float64(f64),
24 Integer32(i32),
26 BigInt(JsBigInt),
28 Object(JsObject),
30 Symbol(JsSymbol),
32}
33
34impl From<JsVariant> for JsValue {
35 fn from(value: JsVariant) -> Self {
36 match value {
37 JsVariant::Null => JsValue::null(),
38 JsVariant::Undefined => JsValue::undefined(),
39 JsVariant::Boolean(b) => JsValue::new(b),
40 JsVariant::String(s) => JsValue::new(s),
41 JsVariant::Float64(f) => JsValue::new(f),
42 JsVariant::Integer32(i) => JsValue::new(i),
43 JsVariant::BigInt(b) => JsValue::new(b),
44 JsVariant::Object(o) => JsValue::new(o),
45 JsVariant::Symbol(s) => JsValue::new(s),
46 }
47 }
48}
49
50impl JsVariant {
51 #[inline]
53 #[must_use]
54 pub fn is_undefined(&self) -> bool {
55 matches!(self, JsVariant::Undefined)
56 }
57
58 #[must_use]
66 pub fn type_of(&self) -> &'static str {
67 match self {
68 JsVariant::Float64(_) | JsVariant::Integer32(_) => "number",
69 JsVariant::String(_) => "string",
70 JsVariant::Boolean(_) => "boolean",
71 JsVariant::Symbol(_) => "symbol",
72 JsVariant::Null => "object",
73 JsVariant::Undefined => "undefined",
74 JsVariant::BigInt(_) => "bigint",
75 JsVariant::Object(object) => {
76 #[cfg(feature = "annex-b")]
77 if object.is::<IsHTMLDDA>() {
78 return "undefined";
79 }
80 if object.is_callable() {
81 "function"
82 } else {
83 "object"
84 }
85 }
86 }
87 }
88
89 #[must_use]
91 pub fn js_type_of(&self) -> JsString {
92 match self {
93 JsVariant::Float64(_) | JsVariant::Integer32(_) => js_string!("number"),
94 JsVariant::String(_) => js_string!("string"),
95 JsVariant::Boolean(_) => js_string!("boolean"),
96 JsVariant::Symbol(_) => js_string!("symbol"),
97 JsVariant::Null => js_string!("object"),
98 JsVariant::Undefined => js_string!("undefined"),
99 JsVariant::BigInt(_) => js_string!("bigint"),
100 JsVariant::Object(object) => {
101 #[cfg(feature = "annex-b")]
102 if object.is::<IsHTMLDDA>() {
103 return js_string!("undefined");
104 }
105 if object.is_callable() {
106 js_string!("function")
107 } else {
108 js_string!("object")
109 }
110 }
111 }
112 }
113}