pub struct RuntimeValue { /* private fields */ }Expand description
A JS value with an attached guard that keeps it alive until dropped.
This struct ensures that GC-managed objects remain valid for as long as
the RuntimeValue exists. The guard is private to prevent accidental
extraction of the value without the guard.
§Creating RuntimeValues
For primitives (no GC needed):
use tsrun::{RuntimeValue, JsValue};
let num = RuntimeValue::unguarded(JsValue::from(42.0));
assert_eq!(num.as_number(), Some(42.0));
let text = RuntimeValue::unguarded(JsValue::from("hello"));
assert_eq!(text.as_str(), Some("hello"));For objects returned from execution:
use tsrun::{Interpreter, StepResult};
let mut interp = Interpreter::new();
interp.prepare("({ x: 1, y: 2 })", None).unwrap();
loop {
match interp.step().unwrap() {
StepResult::Continue => continue,
StepResult::Complete(value) => {
// value is a RuntimeValue keeping the object alive
assert!(value.is_object());
break;
}
_ => break,
}
}Implementations§
Source§impl RuntimeValue
impl RuntimeValue
Sourcepub fn unguarded(value: JsValue) -> Self
pub fn unguarded(value: JsValue) -> Self
Create an unguarded RuntimeValue (for primitives). Use this for values that don’t need GC protection (strings, numbers, booleans, null, undefined).
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
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 is_empty(&self) -> Option<bool>
pub fn is_empty(&self) -> Option<bool>
Check if the array is empty.
Returns None if this is not an array.
Methods from Deref<Target = 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 (===)