pub enum JsValue {
Undefined,
Null,
Boolean(bool),
Number(f64),
String(JsString),
Symbol(Box<JsSymbol>),
Object(JsObjectRef),
}Expand description
A JavaScript value.
This is the primary type for representing all JavaScript values in the interpreter. Size-optimized to 16 bytes by boxing the rare Symbol variant.
§Conversions
use tsrun::JsValue;
// Numbers
let n: JsValue = 42.into();
let f: JsValue = 3.14.into();
// Strings
let s: JsValue = "hello".into();
let owned: JsValue = String::from("world").into();
// Booleans
let t: JsValue = true.into();
// Unit converts to undefined
let u: JsValue = ().into();
assert!(u.is_undefined());§Type Coercion
Use to_boolean(), to_number(), to_string_value() for JS coercion:
use tsrun::JsValue;
// Falsy values
assert!(!JsValue::from(0).to_boolean());
assert!(!JsValue::from("").to_boolean());
assert!(!JsValue::Null.to_boolean());
assert!(!JsValue::Undefined.to_boolean());
assert!(!JsValue::from(f64::NAN).to_boolean());
// Truthy values
assert!(JsValue::from(1).to_boolean());
assert!(JsValue::from("x").to_boolean());Variants§
Undefined
Null
Boolean(bool)
Number(f64)
String(JsString)
Symbol(Box<JsSymbol>)
Object(JsObjectRef)
Implementations§
Source§impl JsValue
impl JsValue
Sourcepub fn is_null_or_undefined(&self) -> bool
pub fn is_null_or_undefined(&self) -> bool
Check if this value is null or undefined
Sourcepub fn is_callable(&self) -> bool
pub fn is_callable(&self) -> bool
Check if this value is callable (a function)
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Check if this is undefined
Sourcepub fn is_nullish(&self) -> bool
pub fn is_nullish(&self) -> bool
Check if this is null or undefined (alias for is_null_or_undefined)
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Check if this is a boolean
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the boolean value if this is a Boolean, otherwise None
Sourcepub fn as_number(&self) -> Option<f64>
pub fn as_number(&self) -> Option<f64>
Returns the numeric value if this is a Number, otherwise None
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the string slice if this is a String, otherwise None
Sourcepub fn as_js_string(&self) -> Option<&JsString>
pub fn as_js_string(&self) -> Option<&JsString>
Returns a reference to the JsString if this is a String, otherwise None
Sourcepub fn as_object(&self) -> Option<&Gc<JsObject>>
pub fn as_object(&self) -> Option<&Gc<JsObject>>
Returns a reference to the object if this is an Object, otherwise None
Sourcepub fn as_symbol(&self) -> Option<&JsSymbol>
pub fn as_symbol(&self) -> Option<&JsSymbol>
Returns a reference to the symbol if this is a Symbol, otherwise None
Sourcepub fn guard_by(&self, guard: &Guard<JsObject>)
pub fn guard_by(&self, guard: &Guard<JsObject>)
If this value is an object, add it to the guard. This keeps the object alive as long as the guard exists.
Sourcepub fn to_boolean(&self) -> bool
pub fn to_boolean(&self) -> bool
Convert to boolean (ToBoolean)
Sourcepub fn to_js_string(&self) -> JsString
pub fn to_js_string(&self) -> JsString
Convert to string (ToString)
Note: Prefer using Interpreter::to_js_string() which uses interned strings.
This method is kept for internal use in value.rs, Debug impl, and tests.
Sourcepub fn strict_equals(&self, other: &JsValue) -> bool
pub fn strict_equals(&self, other: &JsValue) -> bool
Strict equality (===)