boa_engine/value/type.rs
1use super::JsValue;
2use crate::JsVariant;
3
4/// Possible types of values as defined at <https://tc39.es/ecma262/#sec-typeof-operator>.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub enum Type {
7 /// The "undefined" type.
8 Undefined,
9
10 /// The "null" type.
11 Null,
12
13 /// The "boolean" type.
14 Boolean,
15
16 /// The "number" type.
17 Number,
18
19 /// The "string" type.
20 String,
21
22 /// The "symbol" type.
23 Symbol,
24
25 /// The "bigint" type.
26 BigInt,
27
28 /// The "object" type.
29 Object,
30}
31
32impl JsValue {
33 /// Get the type of a value
34 ///
35 /// This is the abstract operation Type(v), as described in
36 /// <https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-language-types>.
37 ///
38 /// Check [`JsValue::type_of`] if you need to call the `typeof` operator.
39 #[must_use]
40 pub fn get_type(&self) -> Type {
41 match self.variant() {
42 JsVariant::Float64(_) | JsVariant::Integer32(_) => Type::Number,
43 JsVariant::String(_) => Type::String,
44 JsVariant::Boolean(_) => Type::Boolean,
45 JsVariant::Symbol(_) => Type::Symbol,
46 JsVariant::Null => Type::Null,
47 JsVariant::Undefined => Type::Undefined,
48 JsVariant::BigInt(_) => Type::BigInt,
49 JsVariant::Object(_) => Type::Object,
50 }
51 }
52}