pub struct ControlFlowHandler;Expand description
Handles control flow operations for the VM
Implementations§
Source§impl ControlFlowHandler
impl ControlFlowHandler
Sourcepub fn jump<S, V>(
_stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn jump<S, V>(
_stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registers containing the program countertarget_ip- The target instruction pointer to jump to
§Returns
Ok(usize)with the new instruction pointer on successErr(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);Sourcepub fn jump_if_true<S, V>(
stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn jump_if_true<S, V>(
stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registerstarget_ip- The target instruction pointer to jump to
§Returns
Ok(usize)with the new instruction pointer if jump occursOk(usize)with current IP + 1 if no jump occursErr(ExecutionError)on failure
Sourcepub fn jump_if_false<S, V>(
stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn jump_if_false<S, V>(
stack: &mut S,
_registers: &mut Registers,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registerstarget_ip- The target instruction pointer to jump to
§Returns
Ok(usize)with the new instruction pointer if jump occursOk(usize)with current IP + 1 if no jump occursErr(ExecutionError)on failure
Sourcepub fn call<S, V>(
stack: &mut S,
_registers: &mut Registers,
frame: &mut Frame,
arg_count: ArgIndex,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn call<S, V>(
stack: &mut S,
_registers: &mut Registers,
frame: &mut Frame,
arg_count: ArgIndex,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registersframe- The current execution framearg_count- The number of arguments to pop from stack
§Returns
Ok(())on successErr(ExecutionError)on failure
Sourcepub fn return_from_function<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn return_from_function<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
Returns from the current function
Restores the previous execution context and returns control to the calling function.
§Arguments
stack- The stack to operate onregisters- The VM registersframe- The current execution frame
§Returns
Ok(usize)with the return address on successErr(ExecutionError)on failure
Sourcepub fn create_scope<S, V>(
_stack: &mut S,
_frame: &mut Frame,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn create_scope<S, V>(
_stack: &mut S,
_frame: &mut Frame,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
Sourcepub fn exit_scope<S, V>(
_stack: &mut S,
_frame: &mut Frame,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn exit_scope<S, V>(
_stack: &mut S,
_frame: &mut Frame,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
Sourcepub fn throw<S, V>(
stack: &mut S,
_registers: &mut Registers,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn throw<S, V>(
stack: &mut S,
_registers: &mut Registers,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
Sourcepub 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>where
S: StackOperations,
V: VariableManager,
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>where
S: StackOperations,
V: VariableManager,
Sets up a try-catch block
Prepares the VM for exception handling with try and catch blocks.
§Arguments
stack- The stack to operate onregisters- The VM registersframe- The current execution frametry_block_size- Size of the try blockcatch_block_size- Size of the catch block
§Returns
Ok(usize)with the new instruction pointer on successErr(ExecutionError)on failure
Sourcepub fn finally<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn finally<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
Sourcepub fn break_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_label: String,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn break_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_label: String,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registersframe- The current execution frametarget_label- The target label to jump to
§Returns
Ok(usize)with the target instruction pointer on successErr(ExecutionError)on failure
Sourcepub fn continue_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_label: String,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn continue_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_label: String,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registersframe- The current execution frametarget_label- The target label to jump to
§Returns
Ok(usize)with the target instruction pointer on successErr(ExecutionError)on failure
Sourcepub fn switch_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
_case_count: CodeAddress,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn switch_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
_case_count: CodeAddress,
) -> Result<(), ExecutionError>where
S: StackOperations,
V: VariableManager,
Sourcepub fn case_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
_case_value: Value,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn case_statement<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
_case_value: Value,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registersframe- The current execution framecase_value- The value to compare againsttarget_ip- The target instruction pointer to jump to
§Returns
Ok(usize)with the target instruction pointer on successErr(ExecutionError)on failure
Sourcepub fn default_case<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
pub fn default_case<S, V>(
_stack: &mut S,
_registers: &mut Registers,
_frame: &mut Frame,
target_ip: CodeAddress,
) -> Result<usize, ExecutionError>where
S: StackOperations,
V: VariableManager,
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 onregisters- The VM registersframe- The current execution frametarget_ip- The target instruction pointer to jump to
§Returns
Ok(usize)with the target instruction pointer on successErr(ExecutionError)on failure