Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard authored and Richard committed Apr 20, 2020
1 parent b2effc8 commit 8e41735
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
52 changes: 25 additions & 27 deletions examples/simulator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate alloc;
use alloc::string::{String,ToString};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use watson::*;

Expand All @@ -21,7 +21,6 @@ fn malloc(size: usize) -> *mut u8 {
ptr
}


#[no_mangle]
fn load(ptr: *mut u8, len: usize) {
log("starting interpreter");
Expand All @@ -37,43 +36,42 @@ fn load(ptr: *mut u8, len: usize) {
Ok(executor) => {
log("called main function");
s.execution = Some(executor);
},
}
Err(_) => {
log("could not call main function");
}
}

},
}
Err(e) => {
log(e);
}
}
}

struct Simulator {
execution:Option<WasmExecution<Program>>,
program_string:String,
interpreter_string:String,
}
execution: Option<WasmExecution<Program>>,
program_string: String,
interpreter_string: String,
}

impl Default for Simulator {
fn default() -> Self {
Simulator {
execution:None,
execution: None,
program_string: "".to_string(),
interpreter_string: "".to_string(),
}
}
}

#[no_mangle]
fn get_program() -> *const u8 {
fn get_program() -> *const u8 {
let s = globals::get::<Simulator>();
return s.program_string.as_ptr();
}

#[no_mangle]
fn get_interpreter() -> *const u8 {
fn get_interpreter() -> *const u8 {
let mut s = globals::get::<Simulator>();
s.interpreter_string = serde_json::to_string(&s.execution).unwrap();
s.interpreter_string.push_str("\0");
Expand All @@ -84,38 +82,38 @@ fn get_interpreter() -> *const u8 {
fn next_instruction() {
let mut s = globals::get::<Simulator>();
let execution_unit = match s.execution.as_mut().unwrap().next_unit() {
Ok(r)=>r,
Err(e)=>{
Ok(r) => r,
Err(e) => {
log(e);
return;
}
};
};
let response = match execution_unit {
// if an import is called, figure out what to do
ExecutionUnit::CallImport(x) => {
let s = format!("import was called: {}",&x.name);
let s = format!("import was called: {}", &x.name);
log(&s);
ExecutionResponse::DoNothing
}
// if there's nothing left to do, break out of loop
ExecutionUnit::Complete(_) => {
log("PROGRAM COMPLETE!");
return;
},
}
// handle other execution units with default behavior
mut x @ _ => match x.evaluate() {
Ok(r)=>r,
Err(e)=>{
log(e);
panic!("");
}
Ok(r) => r,
Err(e) => {
log(e);
panic!("");
}
},
};
match s.execution.as_mut().unwrap().execute(response) {
Ok(r)=>r,
Err(e)=>{
Ok(r) => r,
Err(e) => {
log(e);
return;
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::core::*;
use alloc::sync::Arc;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::convert::TryInto;
Expand Down

0 comments on commit 8e41735

Please sign in to comment.