Most tests yay
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use block::Block;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use block::Block;
|
||||
use ops::Operation;
|
||||
use ijvmreader::IJVMReader;
|
||||
use binread::{BinRead, BinReadable};
|
||||
@@ -8,26 +9,36 @@ use stack::Stack;
|
||||
use pool::Pool;
|
||||
use netstack::NetStack;
|
||||
use Result;
|
||||
use std::sync::Mutex;
|
||||
use std::rc::Rc;
|
||||
|
||||
const MAGIC_HEADER:u32 = 0x1DEA_DFAD;
|
||||
const ANTI_BS_SIZE:usize = 0xFFFF;
|
||||
|
||||
pub struct Machine {
|
||||
pub wide: bool,
|
||||
pub halted: bool,
|
||||
pub pool: Pool,
|
||||
pub block: Block,
|
||||
pub frame: Vec<Frame>,
|
||||
pub net: NetStack
|
||||
pub net: NetStack,
|
||||
|
||||
pub stream_in: Box<Read>,
|
||||
pub stream_out: Rc<Mutex<Write>>,
|
||||
|
||||
}
|
||||
|
||||
impl Machine {
|
||||
pub fn new(pool: Pool, block: Block) -> Machine{
|
||||
Machine {
|
||||
wide: false,
|
||||
halted: false,
|
||||
pool,
|
||||
block,
|
||||
frame: vec![Frame::new(ANTI_BS_SIZE)],
|
||||
net: NetStack::new(),
|
||||
stream_in: Box::new(::std::io::stdin()),
|
||||
stream_out: Rc::new(Mutex::new(::std::io::stdout())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,14 +83,25 @@ impl Machine {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<()> {
|
||||
while self.has_step() {
|
||||
self.step()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_step(&self) -> bool {
|
||||
self.block.has_i8()
|
||||
!self.halted && self.block.has_i8()
|
||||
}
|
||||
|
||||
pub fn get_program_counter(&self) -> usize {
|
||||
self.block.cur()
|
||||
}
|
||||
|
||||
pub fn get_tos(&mut self) -> Result<i32> {
|
||||
self.cur_stack().top()
|
||||
}
|
||||
|
||||
pub fn cur_frame(&mut self) -> &mut Frame {
|
||||
self.frame.last_mut().unwrap()
|
||||
}
|
||||
@@ -88,6 +110,10 @@ impl Machine {
|
||||
&mut self.cur_frame().stack
|
||||
}
|
||||
|
||||
pub fn cur_instruction(&mut self) -> u8 {
|
||||
self.block[self.get_program_counter()]
|
||||
}
|
||||
|
||||
// pub fn get_stack_pointer(&self) -> usize {
|
||||
// return self.frame.last().unwrap().stack.len();
|
||||
// }
|
||||
@@ -99,4 +125,12 @@ impl Machine {
|
||||
}
|
||||
Ok(self.block.read_u8()? as usize)
|
||||
}
|
||||
|
||||
pub fn set_input(&mut self, instream: Box<Read>) {
|
||||
self.stream_in = instream;
|
||||
}
|
||||
|
||||
pub fn set_output(&mut self, outstream: Rc<Mutex<Write>>) {
|
||||
self.stream_out = outstream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user