use stoffel_vm::core_vm::VirtualMachine;
use stoffel_vm_types::{
compiled_binary::utils::{from_vm_functions, load_from_file, save_to_file, try_to_vm_functions},
core_types::Value,
functions::VMFunction,
instructions::Instruction,
};
#[test]
fn test_execute_program_from_stoffel_bytecode_mul_clear() {
let func = VMFunction::new(
"main".to_string(),
vec![],
vec![],
None,
3,
vec![
Instruction::LDI(0, Value::I64(42)),
Instruction::LDI(1, Value::I64(37)),
Instruction::MUL(2, 0, 1),
Instruction::RET(2),
],
Default::default(),
);
let binary = from_vm_functions(&[func]);
let mut path = std::env::temp_dir();
path.push("bytecode_mul_clear.stfl");
save_to_file(&binary, &path).expect("failed to save compiled bytecode");
let loaded = load_from_file(&path).expect("failed to load compiled bytecode");
let vm_functions = try_to_vm_functions(&loaded).expect("compiled bytecode should be valid");
assert!(!vm_functions.is_empty());
let mut vm = VirtualMachine::new();
for f in vm_functions {
vm.register_function(f);
}
let result = vm.execute("main").expect("VM execution failed");
assert_eq!(result, Value::I64(42 * 37));
}