pub struct Runtime { /* private fields */ }Expand description
The main runtime for executing TypeScript code
Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn with_config(config: RuntimeConfig) -> Self
pub fn with_config(config: RuntimeConfig) -> Self
Create a runtime with configuration
Sourcepub fn register_internal_module(&mut self, module: InternalModule)
pub fn register_internal_module(&mut self, module: InternalModule)
Register an internal module for import
Sourcepub fn provide_module(
&mut self,
resolved_path: impl Into<ModulePath>,
source: &str,
) -> Result<(), JsError>
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.
Sourcepub fn fulfill_orders(&mut self, responses: Vec<OrderResponse>)
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.
Sourcepub fn set_gc_threshold(&self, threshold: usize)
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.
Sourcepub fn step(&mut self) -> Result<StepResult, JsError>
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 goStepResult::Complete(value)- Execution finishedStepResult::NeedImports(...)- Need modules before continuingStepResult::Suspended(...)- Waiting for order fulfillmentStepResult::Done- No active execution (callprepare()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,
}
}Sourcepub fn prepare(
&mut self,
source: &str,
path: Option<&str>,
) -> Result<StepResult, JsError>
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 stepStepResult::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,
}
}Sourcepub fn call_depth(&self) -> usize
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.