Skip to content

Commit

Permalink
Default vm.out to stdout. Enable capturing through a ref cell. This i…
Browse files Browse the repository at this point in the history
…s still not ideal, but we're getting there.
  • Loading branch information
Ivorforce committed Jul 18, 2024
1 parent 76efdbc commit d3040a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/interpreter/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn main(module: &Module, runtime: &mut Runtime) -> RResult<()> {
let compiled = runtime.compile_server.compile_deep(&runtime.source, entry_function)?;

unsafe {
runtime.vm.run(compiled, &runtime.compile_server, vec![], &mut std::io::stdout())?;
runtime.vm.run(compiled, &runtime.compile_server, vec![])?;
}

Ok(())
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn transpile(module: &Module, runtime: &mut Runtime) -> RResult<Box<Transpil
let compiled = runtime.compile_server.compile_deep(&runtime.source, entry_function)?;

unsafe {
runtime.vm.run(compiled, &runtime.compile_server, vec![Value { u8: 0 }], &mut std::io::stdout())?;
runtime.vm.run(compiled, &runtime.compile_server, vec![Value { u8: 0 }])?;
}

let exported_artifacts = gather_functions_logic(runtime, &runtime.vm.transpile_functions);
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Runtime {
self.source.fn_logic.insert(Rc::clone(&dummy_head), FunctionLogic::Implementation(implementation));

let compiled = self.compile_server.compile_deep(&self.source, &dummy_head)?;
let result = self.vm.run(compiled, &self.compile_server, vec![], &mut std::io::stdout())?;
let result = self.vm.run(compiled, &self.compile_server, vec![])?;

// We know by now that the expression is supposed to evaluate to something.
return Ok(result.ok_or(RuntimeError::error("").to_array())?)
Expand Down
25 changes: 16 additions & 9 deletions src/interpreter/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use std::rc::{Rc, Weak};

use uuid::Uuid;

Expand Down Expand Up @@ -36,8 +38,7 @@ mod tests {
// stack: [true]
chunk.push(OpCode::RETURN);

let mut out: Vec<u8> = vec![];
let result = runtime.vm.run(Rc::new(chunk), &runtime.compile_server, vec![], &mut out)?;
let result = runtime.vm.run(Rc::new(chunk), &runtime.compile_server, vec![])?;

unsafe { assert_eq!(result.unwrap().bool, true); }

Expand All @@ -55,12 +56,13 @@ mod tests {
// TODO Should gather all used functions and compile them
let compiled = runtime.compile_server.compile_deep(&runtime.source, entry_function)?;

let mut out: Vec<u8> = vec![];
unsafe {
runtime.vm.run(compiled, &runtime.compile_server, vec![], &mut out)?;
}
runtime.vm.out = RefCell::new(Box::new(vec![]));
runtime.vm.run(compiled, &runtime.compile_server, vec![])?;
let out = unsafe {
((runtime.vm.out.borrow_mut().as_mut() as *mut dyn Write) as *mut Vec<u8>).as_ref().unwrap()
};

Ok(std::str::from_utf8(&out).unwrap().to_string())
Ok(std::str::from_utf8(out).unwrap().to_string())
}

/// This tests the transpiler, interpreter and function calls.
Expand Down Expand Up @@ -160,7 +162,12 @@ mod tests {

let mut out: Vec<u8> = vec![];
unsafe {
let result = runtime.vm.run(compiled, &runtime.compile_server, vec![], &mut out);
runtime.vm.out = RefCell::new(Box::new(vec![]));
let result = runtime.vm.run(compiled, &runtime.compile_server, vec![]);
let out = unsafe {
((runtime.vm.out.borrow_mut().as_mut() as *mut dyn Write) as *mut Vec<u8>).as_ref().unwrap()
};

assert_eq!(std::str::from_utf8(&out).unwrap().to_string(), "Assertion failure.\n");

if let Ok(_) = result {
Expand Down
10 changes: 6 additions & 4 deletions src/interpreter/vm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::alloc::{alloc, Layout};
use std::cell::RefCell;
use std::mem::transmute;
use std::ops::Neg;
use std::ptr::{read_unaligned, write_unaligned};
use std::rc::Rc;
use std::rc::{Rc, Weak};

use monoteny_macro::{bin_expr, pop_ip, pop_sp, un_expr};
use uuid::Uuid;
Expand All @@ -12,12 +13,12 @@ use crate::interpreter::chunks::Chunk;
use crate::interpreter::compile::compile_server::CompileServer;
use crate::interpreter::data::{string_to_ptr, Value};
use crate::interpreter::opcode::{OpCode, Primitive};
use crate::interpreter::runtime::Runtime;
use crate::interpreter::vm::call_frame::CallFrame;

pub mod call_frame;

pub struct VM {
pub out: RefCell<Box<dyn std::io::Write>>,
pub stack: Vec<Value>,
pub transpile_functions: Vec<Uuid>,
pub call_frames: Vec<CallFrame>,
Expand All @@ -32,13 +33,14 @@ impl VM {
pub fn new() -> VM {
VM {
// TODO This should dynamically resize probably.
out: RefCell::new(Box::new(std::io::stdout())),
stack: vec![Value::alloc(); 1024 * 1024],
transpile_functions: vec![],
call_frames: Default::default(),
}
}

pub fn run(&mut self, initial_chunk: Rc<Chunk>, compile_server: &CompileServer, parameters: Vec<Value>, pipe_out: &mut dyn std::io::Write) -> RResult<Option<Value>> {
pub fn run(&mut self, initial_chunk: Rc<Chunk>, compile_server: &CompileServer, parameters: Vec<Value>) -> RResult<Option<Value>> {
unsafe {
let mut sp: *mut Value = &mut self.stack[0] as *mut Value;
for parameter in parameters {
Expand Down Expand Up @@ -332,7 +334,7 @@ impl VM {
OpCode::PRINT => {
// TODO Shouldn't need to copy it
let string: String = read_unaligned(pop_sp!().ptr as *mut String);
writeln!(pipe_out, "{}", string)
writeln!(self.out.borrow_mut(), "{}", string)
.map_err(|e| RuntimeError::error(&e.to_string()).to_array())?;
}
OpCode::NEG => {
Expand Down

0 comments on commit d3040a6

Please sign in to comment.