[fine] The VM lives! We can test it a little!
This commit is contained in:
parent
866830b485
commit
1eb7da77fc
8 changed files with 117 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use crate::compiler::{Function, Instruction, Module};
|
||||
use crate::compiler::{Export, Function, Instruction, Module};
|
||||
use crate::semantics::Type;
|
||||
use thiserror::Error;
|
||||
|
||||
|
|
@ -175,6 +175,13 @@ impl Context {
|
|||
Context { module, globals }
|
||||
}
|
||||
|
||||
pub fn init(&mut self) -> std::result::Result<(), VMError> {
|
||||
let init_index = self.module.init;
|
||||
let init_fn = self.module.functions[init_index].clone();
|
||||
eval(self, init_fn, vec![])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_global(&self, i: usize) -> Result<StackValue> {
|
||||
self.globals
|
||||
.get(i)
|
||||
|
|
@ -321,8 +328,10 @@ fn eval_one(
|
|||
Instruction::Return => match stack.pop() {
|
||||
Some(mut frame) => {
|
||||
// The return value is at the top of the stack already.
|
||||
let retval = f.pop_value()?;
|
||||
std::mem::swap(&mut frame, f);
|
||||
*index = f.pc;
|
||||
f.push_value(retval);
|
||||
}
|
||||
None => return Ok(Flow::Break),
|
||||
},
|
||||
|
|
@ -343,6 +352,18 @@ pub fn eval(
|
|||
loop {
|
||||
let instructions = f.func.instructions();
|
||||
let instruction = instructions[index];
|
||||
|
||||
// {
|
||||
// eprint!("{index}: {instruction:?} [");
|
||||
// for val in f.stack.iter().rev().take(3) {
|
||||
// eprint!("{val:?} ");
|
||||
// }
|
||||
// if f.stack.len() > 3 {
|
||||
// eprint!("...");
|
||||
// }
|
||||
// eprintln!("]");
|
||||
// }
|
||||
|
||||
index += 1;
|
||||
|
||||
match eval_one(instruction, &mut index, c, &mut f, &mut stack) {
|
||||
|
|
@ -369,3 +390,19 @@ pub fn eval(
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_export_fn(
|
||||
c: &mut Context,
|
||||
name: &str,
|
||||
args: &[StackValue],
|
||||
) -> std::result::Result<StackValue, VMError> {
|
||||
let export = match c.module.exports.get(name) {
|
||||
Some(Export::Function(id)) => id,
|
||||
Some(_) => todo!(),
|
||||
None => todo!(),
|
||||
};
|
||||
|
||||
let function = c.module.functions[*export].clone();
|
||||
let args = args.iter().map(|a| a.clone()).collect();
|
||||
eval(c, function, args)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue