Another round of cleaning.

This commit is contained in:
Sergey Pepyakin 2018-06-14 17:06:52 +03:00
parent 0110ea2d57
commit 4802635c95
1 changed files with 10 additions and 20 deletions

View File

@ -3,7 +3,6 @@ use std::ops;
use std::{u32, usize}; use std::{u32, usize};
use std::fmt; use std::fmt;
use std::iter::repeat; use std::iter::repeat;
use std::collections::VecDeque;
use parity_wasm::elements::Local; use parity_wasm::elements::Local;
use {Error, Trap, TrapKind, Signature}; use {Error, Trap, TrapKind, Signature};
use module::ModuleRef; use module::ModuleRef;
@ -71,12 +70,10 @@ impl<'a, E: Externals> Interpreter<'a, E> {
)?; )?;
} }
let context = FunctionContext::new( let initial_frame = FunctionContext::new(func.clone());
func.clone(),
);
let mut call_stack = VecDeque::new(); let mut call_stack = Vec::new();
call_stack.push_back(context); call_stack.push(initial_frame);
self.run_interpreter_loop(&mut call_stack)?; self.run_interpreter_loop(&mut call_stack)?;
@ -88,10 +85,10 @@ impl<'a, E: Externals> Interpreter<'a, E> {
})) }))
} }
fn run_interpreter_loop(&mut self, call_stack: &mut VecDeque<FunctionContext>) -> Result<(), Trap> { fn run_interpreter_loop(&mut self, call_stack: &mut Vec<FunctionContext>) -> Result<(), Trap> {
loop { loop {
let mut function_context = call_stack let mut function_context = call_stack
.pop_back() .pop()
.expect("on loop entry - not empty; on loop continue - checking for emptiness; qed"); .expect("on loop entry - not empty; on loop continue - checking for emptiness; qed");
let function_ref = function_context.function.clone(); let function_ref = function_context.function.clone();
let function_body = function_ref let function_body = function_ref
@ -113,7 +110,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
match function_return { match function_return {
RunResult::Return => { RunResult::Return => {
if call_stack.back().is_none() { if call_stack.last().is_none() {
// This was the last frame in the call stack. This means we // This was the last frame in the call stack. This means we
// are done executing. // are done executing.
return Ok(()); return Ok(());
@ -127,8 +124,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
match *nested_func.as_internal() { match *nested_func.as_internal() {
FuncInstanceInternal::Internal { .. } => { FuncInstanceInternal::Internal { .. } => {
let nested_context = function_context.nested(nested_func.clone()).map_err(Trap::new)?; let nested_context = function_context.nested(nested_func.clone()).map_err(Trap::new)?;
call_stack.push_back(function_context); call_stack.push(function_context);
call_stack.push_back(nested_context); call_stack.push(nested_context);
}, },
FuncInstanceInternal::Host { ref signature, .. } => { FuncInstanceInternal::Host { ref signature, .. } => {
let args = prepare_function_args(signature, &mut self.value_stack); let args = prepare_function_args(signature, &mut self.value_stack);
@ -144,7 +141,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
if let Some(return_val) = return_val { if let Some(return_val) = return_val {
self.value_stack.push(return_val).map_err(Trap::new)?; self.value_stack.push(return_val).map_err(Trap::new)?;
} }
call_stack.push_back(function_context); call_stack.push(function_context);
} }
} }
}, },
@ -154,16 +151,14 @@ impl<'a, E: Externals> Interpreter<'a, E> {
fn drop_keep(&mut self, drop: u32, keep: u8) -> Result<(), TrapKind> { fn drop_keep(&mut self, drop: u32, keep: u8) -> Result<(), TrapKind> {
assert!(keep <= 1); assert!(keep <= 1);
// println!("drop: {}, keep: {}, stack: {:?}", drop, keep, self.value_stack);
if keep == 1 { if keep == 1 {
let top = *self.value_stack.top(); let top = *self.value_stack.top();
*self.value_stack.pick_mut(drop as usize) = top; *self.value_stack.pick_mut(drop as usize) = top;
} }
// println!("drop: {}, keep: {}, stack: {:?}", drop, keep, self.value_stack);
let cur_stack_len = self.value_stack.len(); let cur_stack_len = self.value_stack.len();
self.value_stack.resize(cur_stack_len - drop as usize); self.value_stack.resize(cur_stack_len - drop as usize);
// println!("drop: {}, keep: {}, stack: {:?}", drop, keep, self.value_stack);
Ok(()) Ok(())
} }
@ -171,9 +166,6 @@ impl<'a, E: Externals> Interpreter<'a, E> {
loop { loop {
let instruction = &instructions[function_context.position]; let instruction = &instructions[function_context.position];
// println!("{:?}", instruction);
// println!(" before = {:?}", self.value_stack);
match self.run_instruction(function_context, instruction)? { match self.run_instruction(function_context, instruction)? {
InstructionOutcome::RunNextInstruction => function_context.position += 1, InstructionOutcome::RunNextInstruction => function_context.position += 1,
InstructionOutcome::Branch(target) => { InstructionOutcome::Branch(target) => {
@ -189,8 +181,6 @@ impl<'a, E: Externals> Interpreter<'a, E> {
break; break;
}, },
} }
// println!(" after = {:?}", self.value_stack);
} }
Ok(RunResult::Return) Ok(RunResult::Return)