Struct ControlFlowHandler

Source
pub struct ControlFlowHandler;
Expand description

Handles control flow operations for the VM

Implementations§

Source§

impl ControlFlowHandler

Source

pub fn jump<S, V>( _stack: &mut S, _registers: &mut Registers, target_ip: CodeAddress, ) -> Result<usize, ExecutionError>

Performs an unconditional jump to a target address

Sets the program counter to the target address, effectively changing the execution flow.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers containing the program counter
  • target_ip - The target instruction pointer to jump to
§Returns
  • Ok(usize) with the new instruction pointer on success
  • Err(ExecutionError) on failure
§Examples
let mut stack = MyStack::new();
let mut registers = MyRegisters::new();
let new_ip = ControlFlowHandler::jump(&mut stack, &mut registers, 100)?;
assert_eq!(new_ip, 100);
Source

pub fn jump_if_true<S, V>( stack: &mut S, _registers: &mut Registers, target_ip: CodeAddress, ) -> Result<usize, ExecutionError>

Performs a conditional jump if the top stack value is true

Pops a value from the stack and jumps to the target address if the value is truthy.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • target_ip - The target instruction pointer to jump to
§Returns
  • Ok(usize) with the new instruction pointer if jump occurs
  • Ok(usize) with current IP + 1 if no jump occurs
  • Err(ExecutionError) on failure
Source

pub fn jump_if_false<S, V>( stack: &mut S, _registers: &mut Registers, target_ip: CodeAddress, ) -> Result<usize, ExecutionError>

Performs a conditional jump if the top stack value is false

Pops a value from the stack and jumps to the target address if the value is falsy.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • target_ip - The target instruction pointer to jump to
§Returns
  • Ok(usize) with the new instruction pointer if jump occurs
  • Ok(usize) with current IP + 1 if no jump occurs
  • Err(ExecutionError) on failure
Source

pub fn call<S, V>( stack: &mut S, _registers: &mut Registers, frame: &mut Frame, arg_count: ArgIndex, ) -> Result<(), ExecutionError>

Sets up a function call with arguments

Pops the function and arguments from the stack and sets up the frame for function execution.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • arg_count - The number of arguments to pop from stack
§Returns
  • Ok(()) on success
  • Err(ExecutionError) on failure
Source

pub fn return_from_function<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, ) -> Result<usize, ExecutionError>

Returns from the current function

Restores the previous execution context and returns control to the calling function.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
§Returns
  • Ok(usize) with the return address on success
  • Err(ExecutionError) on failure
Source

pub fn create_scope<S, V>( _stack: &mut S, _frame: &mut Frame, ) -> Result<(), ExecutionError>

Creates a new scope for variable declarations

Sets up a new variable scope for block execution.

§Arguments
  • stack - The stack to operate on
  • frame - The current execution frame
§Returns
  • Ok(()) on success
  • Err(ExecutionError) on failure
Source

pub fn exit_scope<S, V>( _stack: &mut S, _frame: &mut Frame, ) -> Result<(), ExecutionError>

Exits the current scope

Cleans up the current variable scope.

§Arguments
  • stack - The stack to operate on
  • frame - The current execution frame
§Returns
  • Ok(()) on success
  • Err(ExecutionError) on failure
Source

pub fn throw<S, V>( stack: &mut S, _registers: &mut Registers, ) -> Result<(), ExecutionError>

Throws an exception

Pops a value from the stack and throws it as an exception, interrupting normal execution flow.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
§Returns
  • Ok(()) on success
  • Err(ExecutionError) on failure
Source

pub fn try_catch<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, try_block_size: CodeAddress, catch_block_size: CodeAddress, ) -> Result<usize, ExecutionError>

Sets up a try-catch block

Prepares the VM for exception handling with try and catch blocks.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • try_block_size - Size of the try block
  • catch_block_size - Size of the catch block
§Returns
  • Ok(usize) with the new instruction pointer on success
  • Err(ExecutionError) on failure
Source

pub fn finally<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, ) -> Result<usize, ExecutionError>

Executes the finally block

Handles cleanup code that should run regardless of exception handling.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
§Returns
  • Ok(usize) with the new instruction pointer on success
  • Err(ExecutionError) on failure
Source

pub fn break_statement<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, target_label: String, ) -> Result<usize, ExecutionError>

Breaks out of a loop or switch statement

Exits the current loop or switch statement and continues execution at the specified target.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • target_label - The target label to jump to
§Returns
  • Ok(usize) with the target instruction pointer on success
  • Err(ExecutionError) on failure
Source

pub fn continue_statement<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, target_label: String, ) -> Result<usize, ExecutionError>

Continues to the next iteration of a loop

Skips the rest of the current loop iteration and continues with the next iteration.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • target_label - The target label to jump to
§Returns
  • Ok(usize) with the target instruction pointer on success
  • Err(ExecutionError) on failure
Source

pub fn switch_statement<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, _case_count: CodeAddress, ) -> Result<(), ExecutionError>

Sets up a switch statement

Prepares the VM for switch statement execution.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • case_count - The number of cases in the switch
§Returns
  • Ok(()) on success
  • Err(ExecutionError) on failure
Source

pub fn case_statement<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, _case_value: Value, target_ip: CodeAddress, ) -> Result<usize, ExecutionError>

Handles a case in a switch statement

Compares the switch value with a case value and jumps to the target if they match.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • case_value - The value to compare against
  • target_ip - The target instruction pointer to jump to
§Returns
  • Ok(usize) with the target instruction pointer on success
  • Err(ExecutionError) on failure
Source

pub fn default_case<S, V>( _stack: &mut S, _registers: &mut Registers, _frame: &mut Frame, target_ip: CodeAddress, ) -> Result<usize, ExecutionError>

Handles the default case in a switch statement

Jumps to the default case target when no other cases match.

§Arguments
  • stack - The stack to operate on
  • registers - The VM registers
  • frame - The current execution frame
  • target_ip - The target instruction pointer to jump to
§Returns
  • Ok(usize) with the target instruction pointer on success
  • Err(ExecutionError) on failure

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