Most tests yay
This commit is contained in:
33
src/ops.rs
33
src/ops.rs
@@ -2,8 +2,7 @@ use binread::BinRead;
|
||||
use machine::Machine;
|
||||
use frame::Frame;
|
||||
use Result;
|
||||
use std::io::Write;
|
||||
use std::io::Read;
|
||||
use std::io::{Read};
|
||||
|
||||
pub type OpFunc = fn(&mut Machine) -> Result<()>;
|
||||
|
||||
@@ -79,8 +78,10 @@ fn dup(machine: &mut Machine) -> Result<()> {
|
||||
|
||||
fn out(machine: &mut Machine) -> Result<()> {
|
||||
let val = machine.cur_stack().pop()?;
|
||||
print!("{}", val as u8 as char);
|
||||
::std::io::stdout().flush().unwrap();
|
||||
let buffer = [val as u8];
|
||||
let mut out = machine.stream_out.lock().unwrap();
|
||||
out.write_all(&buffer).unwrap();
|
||||
out.flush().unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -104,14 +105,10 @@ fn isub(machine: &mut Machine) -> Result<()> {
|
||||
}
|
||||
|
||||
fn _in(machine: &mut Machine) -> Result<()> {
|
||||
let char: Option<u8> =
|
||||
::std::io::stdin()
|
||||
.bytes()
|
||||
.next()
|
||||
.and_then(|r| r.ok());
|
||||
let val = match char {
|
||||
None => 0i32,
|
||||
Some(i) => i32::from(i)
|
||||
let mut buffer = [0; 1];
|
||||
let val = match machine.stream_in.read_exact(&mut buffer) {
|
||||
Err(_) => 0i32,
|
||||
Ok(_) => i32::from(buffer[0]),
|
||||
};
|
||||
machine.cur_stack().push(val);
|
||||
Ok(())
|
||||
@@ -122,8 +119,9 @@ fn goto(machine: &mut Machine) -> Result<()> {
|
||||
machine.block.jump(offset)
|
||||
}
|
||||
|
||||
fn halt(_: &mut Machine) -> Result<()> {
|
||||
Err("Halt")
|
||||
fn halt(machine: &mut Machine) -> Result<()> {
|
||||
machine.halted = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ifeq(machine: &mut Machine) -> Result<()> {
|
||||
@@ -181,7 +179,7 @@ fn swap(machine: &mut Machine) -> Result<()> {
|
||||
|
||||
fn wide(machine: &mut Machine) -> Result<()> {
|
||||
machine.wide = true;
|
||||
Ok(())
|
||||
machine.step()
|
||||
}
|
||||
|
||||
fn iload(machine: &mut Machine) -> Result<()> {
|
||||
@@ -208,7 +206,7 @@ fn invokevirtual(machine: &mut Machine) -> Result<()> {
|
||||
let method_index = machine.block.read_u16()?;
|
||||
let invoke_addr = machine.pool.get(method_index as usize)?;
|
||||
let return_addr = machine.get_program_counter();
|
||||
// println!("METHOD {}", method_index);
|
||||
|
||||
machine.block.seek(invoke_addr as usize)?;
|
||||
let arg_count = machine.block.read_u16()?;
|
||||
let var_count = machine.block.read_u16()?;
|
||||
@@ -224,7 +222,6 @@ fn invokevirtual(machine: &mut Machine) -> Result<()> {
|
||||
cur_stack.pop()?; // Nuke the OBJREF
|
||||
}
|
||||
|
||||
// println!("retaddr set {}" ,return_addr);
|
||||
newframe.set(0, return_addr as i32)?;
|
||||
|
||||
machine.frame.push(newframe);
|
||||
@@ -237,10 +234,8 @@ fn ireturn(machine: &mut Machine) -> Result<()> {
|
||||
Some(a) => a,
|
||||
None => return Err("Got no frame... somehow...")
|
||||
};
|
||||
// println!("stack: {:?}", prev_frame.stack);
|
||||
let result = prev_frame.stack.pop()?;
|
||||
let return_addr = prev_frame.get(0)?;
|
||||
// println!("result: {}\nretaddr: {}", result, return_addr);
|
||||
machine.cur_stack().push(result);
|
||||
machine.block.seek(return_addr as usize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user