Skip to main content

RuntimeValue

Struct RuntimeValue 

Source
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

Source

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).

Source

pub fn value(&self) -> &JsValue

Get a reference to the value

Source

pub fn is_undefined(&self) -> bool

Check if this is undefined

Source

pub fn is_null(&self) -> bool

Check if this is null

Source

pub fn is_nullish(&self) -> bool

Check if this is null or undefined

Source

pub fn is_boolean(&self) -> bool

Check if this is a boolean

Source

pub fn is_number(&self) -> bool

Check if this is a number

Source

pub fn is_string(&self) -> bool

Check if this is a string

Source

pub fn is_object(&self) -> bool

Check if this is an object

Source

pub fn as_bool(&self) -> Option<bool>

Returns the boolean value if this is a Boolean, otherwise None

Source

pub fn as_number(&self) -> Option<f64>

Returns the numeric value if this is a Number, otherwise None

Source

pub fn as_str(&self) -> Option<&str>

Returns the string slice if this is a String, otherwise None

Source

pub fn type_name(&self) -> &'static str

Returns a string describing the type of this value

Source

pub fn len(&self) -> Option<usize>

Get the length of an array.

Returns None if this is not an array.

§Example
let arr = runtime.create_from_json(&json!([1, 2, 3, 4, 5]))?;
assert_eq!(arr.len(), Some(5));
Source

pub fn is_empty(&self) -> Option<bool>

Check if the array is empty.

Returns None if this is not an array.

Source

pub fn is_array(&self) -> bool

Check if this value is an array.

§Example
let arr = runtime.create_from_json(&json!([1, 2, 3]))?;
let obj = runtime.create_from_json(&json!({"x": 1}))?;
assert!(arr.is_array());
assert!(!obj.is_array());
Source

pub fn keys(&self) -> Vec<String>

Get all property keys of an object.

Returns an empty vector if this is not an object.

§Example
let obj = runtime.create_from_json(&json!({"a": 1, "b": 2}))?;
let keys = obj.keys();
assert!(keys.contains(&"a".to_string()));
assert!(keys.contains(&"b".to_string()));

Methods from Deref<Target = JsValue>§

Source

pub fn is_null_or_undefined(&self) -> bool

Check if this value is null or undefined

Source

pub fn is_callable(&self) -> bool

Check if this value is callable (a function)

Source

pub fn is_string(&self) -> bool

Check if this is a string value

Source

pub fn is_undefined(&self) -> bool

Check if this is undefined

Source

pub fn is_null(&self) -> bool

Check if this is null

Source

pub fn is_nullish(&self) -> bool

Check if this is null or undefined (alias for is_null_or_undefined)

Source

pub fn is_boolean(&self) -> bool

Check if this is a boolean

Source

pub fn is_number(&self) -> bool

Check if this is a number

Source

pub fn is_object(&self) -> bool

Check if this is an object

Source

pub fn is_symbol(&self) -> bool

Check if this is a symbol

Source

pub fn as_bool(&self) -> Option<bool>

Returns the boolean value if this is a Boolean, otherwise None

Source

pub fn as_number(&self) -> Option<f64>

Returns the numeric value if this is a Number, otherwise None

Source

pub fn as_str(&self) -> Option<&str>

Returns the string slice if this is a String, otherwise None

Source

pub fn as_js_string(&self) -> Option<&JsString>

Returns a reference to the JsString if this is a String, otherwise None

Source

pub fn as_object(&self) -> Option<&Gc<JsObject>>

Returns a reference to the object if this is an Object, otherwise None

Source

pub fn as_symbol(&self) -> Option<&JsSymbol>

Returns a reference to the symbol if this is a Symbol, otherwise None

Source

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.

Source

pub fn to_boolean(&self) -> bool

Convert to boolean (ToBoolean)

Source

pub fn to_number(&self) -> f64

Convert to number (ToNumber)

Source

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.

Source

pub fn strict_equals(&self, other: &JsValue) -> bool

Strict equality (===)

Source

pub fn type_name(&self) -> &'static str

Returns a string describing the type of this value

Trait Implementations§

Source§

impl Debug for RuntimeValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for RuntimeValue

Source§

type Target = JsValue

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for RuntimeValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq<JsValue> for RuntimeValue

Source§

fn eq(&self, other: &JsValue) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<RuntimeValue> for JsValue

Source§

fn eq(&self, other: &RuntimeValue) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.