implements PR-026

This commit is contained in:
2026-03-24 13:40:56 +00:00
parent 67e7acf73b
commit 0ff4909079
11 changed files with 44 additions and 66 deletions
@@ -68,11 +68,7 @@ fn patch_module_hostcalls(
}
impl VirtualMachine {
pub fn initialize(
&mut self,
program_bytes: Vec<u8>,
entrypoint: &str,
) -> Result<(), VmInitError> {
pub fn initialize(&mut self, program_bytes: Vec<u8>) -> Result<(), VmInitError> {
self.program = ProgramImage::default();
self.pc = 0;
self.operand_stack.clear();
@@ -113,23 +109,11 @@ impl VirtualMachine {
return Err(VmInitError::InvalidFormat);
};
let pc = if entrypoint.is_empty() {
program.functions.first().map(|f| f.code_offset as usize).unwrap_or(0)
} else if let Ok(func_idx) = entrypoint.parse::<usize>() {
program
.functions
.get(func_idx)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else if let Some(&func_idx) = program.exports.get(entrypoint) {
program
.functions
.get(func_idx as usize)
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?
} else {
return Err(VmInitError::EntrypointNotFound);
};
let pc = program
.functions
.first()
.map(|f| f.code_offset as usize)
.ok_or(VmInitError::EntrypointNotFound)?;
self.program = program;
self.pc = pc;
@@ -142,6 +126,10 @@ impl VirtualMachine {
self.capabilities = caps;
}
pub fn prepare_boot_call(&mut self) {
self.prepare_call_by_index(0);
}
pub fn prepare_call(&mut self, entrypoint: &str) {
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
idx
@@ -149,6 +137,10 @@ impl VirtualMachine {
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
};
self.prepare_call_by_index(func_idx);
}
fn prepare_call_by_index(&mut self, func_idx: usize) {
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
self.pc = callee.code_offset as usize;
self.halted = false;