Runtime

Struct Runtime 

Source
pub struct Runtime { /* private fields */ }
Expand description

The main runtime for executing TypeScript code

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Create a new runtime instance

Source

pub fn with_config(config: RuntimeConfig) -> Self

Create a runtime with configuration

Source

pub fn register_internal_module(&mut self, module: InternalModule)

Register an internal module for import

Source

pub fn provide_module( &mut self, resolved_path: impl Into<ModulePath>, source: &str, ) -> Result<(), JsError>

Provide a module source for a pending import.

The resolved_path should be the ImportRequest.resolved_path from the NeedImports result. This ensures proper deduplication of modules even when they are imported with different relative paths.

After providing all required modules, call step() to continue execution.

Source

pub fn fulfill_orders(&mut self, responses: Vec<OrderResponse>)

Fulfill orders with responses from the host.

Stores the responses for when execution resumes. After calling this, call step() to continue execution.

Source

pub fn set_gc_threshold(&self, threshold: usize)

Set the GC threshold (0 = disable automatic collection)

Lower values reduce peak memory but increase GC overhead. Higher values improve throughput but may use more memory.

Source

pub fn step(&mut self) -> Result<StepResult, JsError>

Execute a single bytecode instruction.

This method provides step-by-step execution for host-controlled interruption. Call prepare() first to compile the code, then call step() repeatedly until a terminal result is returned.

§Returns
  • StepResult::Continue - One instruction executed, more to go
  • StepResult::Complete(value) - Execution finished
  • StepResult::NeedImports(...) - Need modules before continuing
  • StepResult::Suspended(...) - Waiting for order fulfillment
  • StepResult::Done - No active execution (call prepare() first)
§Example
let mut runtime = Runtime::new();
runtime.prepare("let x = 0; while (true) { x++; }", None)?;

// Run for at most 1000 steps
for _ in 0..1000 {
    match runtime.step()? {
        StepResult::Continue => continue,
        StepResult::Complete(value) => {
            println!("Completed: {:?}", value);
            break;
        }
        StepResult::Done => break,
        _ => break,
    }
}
Source

pub fn prepare( &mut self, source: &str, path: Option<&str>, ) -> Result<StepResult, JsError>

Prepare code for step-based execution without running it.

After calling this, use step() to execute one instruction at a time, or run_to_completion() to run until a terminal result.

§Returns
  • StepResult::Continue - Ready to step
  • StepResult::NeedImports(...) - Need modules before continuing
§Example
let mut runtime = Runtime::new();

// Prepare and run with step limit
runtime.prepare("let x = 0; while (true) { x++; }", None)?;
for _ in 0..1000 {
    match runtime.step()? {
        StepResult::Continue => continue,
        StepResult::Complete(value) => {
            println!("Completed: {:?}", value);
            break;
        }
        _ => break,
    }
}
Source

pub fn call_depth(&self) -> usize

Get the current call stack depth.

Returns the depth of the JavaScript call stack (trampoline stack + interpreter call stack). This can be used by the host to implement stack depth limits.

Returns 0 if no execution is active.

Source

pub fn collect(&self)

Force a garbage collection cycle

Source

pub fn gc_stats(&self) -> GcStats

Get GC statistics

Trait Implementations§

Source§

impl Default for Runtime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Runtime

§

impl !RefUnwindSafe for Runtime

§

impl !Send for Runtime

§

impl !Sync for Runtime

§

impl Unpin for Runtime

§

impl !UnwindSafe for Runtime

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