added more enviroment functions and fixed some argument bugs

This commit is contained in:
afonya2 2025-06-11 17:57:52 +02:00
parent 12d3250bc4
commit cca26d9fc3
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
5 changed files with 107 additions and 25 deletions

View file

@ -50,7 +50,7 @@ pub struct TableValue {
}
#[derive(Debug, Clone)]
pub struct VMMemoryNativeFunction {
pub func: fn(&mut Machine, Vec<VMMemory>) -> VMMemory,
pub func: fn(&mut Machine, &DecompiledOperation, Vec<VMMemory>) -> VMMemory,
pub variable_id: u32,
}
@ -62,12 +62,12 @@ struct DecompiledFunction {
functions: HashMap<u32, u32>
}
#[derive(Debug, Clone)]
struct DecompiledOperation {
opcode: u8,
arg1: u8,
arg2: i64,
arg3: u8,
pos: usize,
pub struct DecompiledOperation {
pub opcode: u8,
pub arg1: u8,
pub arg2: i64,
pub arg3: u8,
pub pos: usize,
}
#[derive(Debug, Clone)]
@ -85,9 +85,9 @@ struct Variable {
}
pub struct CallStack {
func: usize,
return_reg: usize,
pc: usize,
pub func: usize,
pub return_reg: usize,
pub pc: usize,
}
pub struct Machine {
@ -110,11 +110,7 @@ fn read_be_num(input: &[u8]) -> usize {
}
fn read_str(input: &[u8]) -> String {
let mut result = String::new();
for &byte in input {
result.push(byte as char);
}
return result;
String::from_utf8(input.to_vec()).expect("Invalid UTF-8 sequence")
}
fn read_bin(input: &[u8]) -> String {
@ -802,7 +798,23 @@ impl Machine {
executed_stack = self.call_stack.len() - 1;
},
VMMemory::NativeFunction(nfunc) => {
(nfunc.func)(self, self.stack.clone());
let mut result = (nfunc.func)(self, &operation, self.stack.clone());
let ret_reg = get_register_by_id(&self.registers, operation.arg2 as u8);
if ret_reg.is_none() {
let err = create_error(&format!("Register `{}` not found", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound);
print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone());
process::exit(1);
}
let mut ret_pointer = ret_reg.unwrap().pointer;
if ret_pointer >= self.memory.len() || ret_pointer < 1 {
self.memory.push(VMMemory::Null(VMMemoryNull { variable_id: 0 }));
ret_pointer = self.memory.len() - 1;
set_register(&mut self.registers, Register { id: operation.arg2 as u8, pointer: ret_pointer });
}
let var_id = get_var_id(&self.memory[ret_pointer]);
set_var_id(&mut result, var_id);
self.memory[ret_pointer] = result;
self.stack.clear();
},
_ => {