Skip to content

Commit

Permalink
Merge pull request #4 from Fantom-foundation/runtime-fmt-fix
Browse files Browse the repository at this point in the history
runtime-fmt and clippy fixes
  • Loading branch information
SamuelMarks committed Aug 16, 2019
2 parents 6b957a4 + 36966a3 commit 61b6c58
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ configure = "0.1.1"
env_logger = "0.6.0"
failure = "0.1.3"
libc = "0.2"
runtime-fmt = "0.3.0"
runtime-fmt = { git = "https://github.com/leenozara/runtime-fmt", branch = "unicode_internal_fix" }
ring = "0.13.4"
serde = "1.0.80"
serde_derive = "1.0.80"
Expand Down
2 changes: 1 addition & 1 deletion src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl FreeChunks {
.free_chunks
.binary_search_by(|(f, t)| (item.1 - item.0).cmp(&(t - f)))
{
Ok(_) => Err(AllocatorError::AddressAlreadyFreed { address: item.0 })?,
Ok(_) => return Err(AllocatorError::AddressAlreadyFreed { address: item.0 }.into()),
Err(pos) => {
self.free_chunks.insert(pos, item);
}
Expand Down
29 changes: 16 additions & 13 deletions src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,17 @@ macro_rules! parse_instruction_with_string_and_register {
}

impl Instruction {
fn parse_fd(stream: &mut Iterator<Item = u8>) -> Result<Instruction, Error> {
fn parse_fd(stream: &mut dyn Iterator<Item = u8>) -> Result<Instruction, Error> {
let name = Instruction::parse_string(stream)?;
let args = Instruction::parse_u64(stream)?;
let skip = Instruction::parse_u64(stream)?;
Ok(Instruction::Fd { name, args, skip })
}

fn parse_call(stream: &mut Iterator<Item = u8>, nargs: usize) -> Result<Instruction, Error> {
fn parse_call(
stream: &mut dyn Iterator<Item = u8>,
nargs: usize,
) -> Result<Instruction, Error> {
let return_register = Instruction::parse_register(stream)?;
let mut arguments = [None; 8];
for i in arguments.iter_mut().take(nargs - 1) {
Expand All @@ -304,64 +307,64 @@ impl Instruction {
})
}

fn parse_string(stream: &mut Iterator<Item = u8>) -> Result<String, Error> {
fn parse_string(stream: &mut dyn Iterator<Item = u8>) -> Result<String, Error> {
let string_length = stream.next().ok_or(ParsingError::StringWithoutLenght)? as usize;
let string_bytes: Vec<u8> = stream.take(string_length).collect();
let name = String::from_utf8(string_bytes)?;
Ok(name)
}

fn parse_u16(stream: &mut Iterator<Item = u8>) -> Result<u16, Error> {
fn parse_u16(stream: &mut dyn Iterator<Item = u8>) -> Result<u16, Error> {
let bytes: Vec<u8> = stream.take(2).collect();
if bytes.len() != 2 {
Err(ParsingError::UnexpectedEndOfStream)?;
return Err(ParsingError::UnexpectedEndOfStream.into());
}
#[allow(clippy::transmute_ptr_to_ptr)]
let byte_pairs: &[u16] =
unsafe { std::slice::from_raw_parts(std::mem::transmute(bytes.as_ptr()), 1) };
Ok(byte_pairs[0])
}

fn parse_u32(stream: &mut Iterator<Item = u8>) -> Result<u32, Error> {
fn parse_u32(stream: &mut dyn Iterator<Item = u8>) -> Result<u32, Error> {
let bytes: Vec<u8> = stream.take(4).collect();
if bytes.len() != 4 {
Err(ParsingError::UnexpectedEndOfStream)?;
return Err(ParsingError::UnexpectedEndOfStream.into());
}
#[allow(clippy::transmute_ptr_to_ptr)]
let byte_pairs: &[u32] =
unsafe { std::slice::from_raw_parts(std::mem::transmute(bytes.as_ptr()), 1) };
Ok(byte_pairs[0])
}

fn parse_i64(stream: &mut Iterator<Item = u8>) -> Result<i64, Error> {
fn parse_i64(stream: &mut dyn Iterator<Item = u8>) -> Result<i64, Error> {
let bytes: Vec<u8> = stream.take(8).collect();
if bytes.len() != 8 {
Err(ParsingError::U64LacksInformation)?;
return Err(ParsingError::U64LacksInformation.into());
}
#[allow(clippy::transmute_ptr_to_ptr)]
let byte_groups: &[i64] =
unsafe { std::slice::from_raw_parts(std::mem::transmute(bytes.as_ptr()), 1) };
Ok(byte_groups[0])
}

fn parse_u64(stream: &mut Iterator<Item = u8>) -> Result<u64, Error> {
fn parse_u64(stream: &mut dyn Iterator<Item = u8>) -> Result<u64, Error> {
let bytes: Vec<u8> = stream.take(8).collect();
if bytes.len() != 8 {
Err(ParsingError::U64LacksInformation)?;
return Err(ParsingError::U64LacksInformation.into());
}
#[allow(clippy::transmute_ptr_to_ptr)]
let byte_groups: &[u64] =
unsafe { std::slice::from_raw_parts(std::mem::transmute(bytes.as_ptr()), 1) };
Ok(byte_groups[0])
}

fn parse_register(stream: &mut Iterator<Item = u8>) -> Result<u8, Error> {
fn parse_register(stream: &mut dyn Iterator<Item = u8>) -> Result<u8, Error> {
Ok(stream
.next()
.ok_or(ParsingError::RegisterExpectedNothingFound)?)
}

fn parse_value(stream: &mut Iterator<Item = u8>) -> Result<Value, Error> {
fn parse_value(stream: &mut dyn Iterator<Item = u8>) -> Result<Value, Error> {
let flag = stream.next().ok_or(ParsingError::ValueWithNoFlag)?;
if flag == 0 {
Instruction::parse_register(stream).map(Value::Register)
Expand Down
22 changes: 13 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub mod instruction;
mod memory;
mod register_set;

type CpuFn = Box<Fn(&NativeFunctions, Vec<u8>) -> Result<u64, Error>>;
type CpuFn = Box<dyn Fn(&NativeFunctions, Vec<u8>) -> Result<u64, Error>>;
#[repr(C)]
enum Function {
Native(CpuFn),
Expand All @@ -114,11 +114,12 @@ struct NativeFunctions {
impl NativeFunctions {
fn puts(&self, args: Vec<u8>) -> Result<u64, Error> {
if args.len() != 1 {
Err(RuntimeError::WrongArgumentsNumber {
return Err(RuntimeError::WrongArgumentsNumber {
name: "puts".to_owned(),
expected: 1,
got: args.len(),
})?;
}
.into());
}
self.register_stack
.borrow()
Expand Down Expand Up @@ -319,11 +320,12 @@ impl NativeFunctions {

fn exit(&self, args: Vec<u8>) -> Result<u64, Error> {
if args.len() != 1 {
Err(RuntimeError::WrongArgumentsNumber {
return Err(RuntimeError::WrongArgumentsNumber {
name: "exit".to_owned(),
expected: 1,
got: args.len(),
})?;
}
.into());
}
Err(RuntimeError::ProgramEnded {
errno: self.register_stack.borrow().last().unwrap().get(0)?,
Expand All @@ -333,23 +335,25 @@ impl NativeFunctions {

fn malloc(&self, args: Vec<u8>) -> Result<u64, Error> {
if args.len() != 1 {
Err(RuntimeError::WrongArgumentsNumber {
return Err(RuntimeError::WrongArgumentsNumber {
name: "malloc".to_owned(),
expected: 1,
got: args.len(),
})?;
}
.into());
}
let size = self.register_stack.borrow().last().unwrap().get(0)? as usize;
self.allocator.borrow_mut().malloc(size).map(|v| v as u64)
}

fn free(&self, args: Vec<u8>) -> Result<u64, Error> {
if args.len() != 1 {
Err(RuntimeError::WrongArgumentsNumber {
return Err(RuntimeError::WrongArgumentsNumber {
name: "free".to_owned(),
expected: 1,
got: args.len(),
})?;
}
.into());
}
let address = self.register_stack.borrow().last().unwrap().get(0)? as usize;
self.allocator.borrow_mut().free(address).map(|_| 0)
Expand Down

0 comments on commit 61b6c58

Please sign in to comment.