From 02cb00f14be333077468db80da0483dae4878701 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 6 Jul 2018 06:58:16 +0800 Subject: [PATCH] Accept func and args when creating the Interpreter --- src/func.rs | 4 ++-- src/runner.rs | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/func.rs b/src/func.rs index beeacea..4ab3162 100644 --- a/src/func.rs +++ b/src/func.rs @@ -144,8 +144,8 @@ impl FuncInstance { check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?; match *func.as_internal() { FuncInstanceInternal::Internal { .. } => { - let mut interpreter = Interpreter::new(externals); - interpreter.start_execution(func, args) + let mut interpreter = Interpreter::new(func, args, externals)?; + interpreter.start_execution() } FuncInstanceInternal::Host { ref host_func_index, diff --git a/src/runner.rs b/src/runner.rs index 0ef2a4c..684e742 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -14,6 +14,7 @@ use value::{ }; use host::Externals; use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX}; +use types::ValueType; use memory_units::Pages; use nan_preserving_float::{F32, F64}; use isa; @@ -49,22 +50,14 @@ pub struct Interpreter<'a, E: Externals + 'a> { externals: &'a mut E, value_stack: ValueStack, call_stack: Vec, + return_type: Option, } impl<'a, E: Externals> Interpreter<'a, E> { - pub fn new(externals: &'a mut E) -> Interpreter<'a, E> { - let value_stack = ValueStack::with_limit(DEFAULT_VALUE_STACK_LIMIT); - let call_stack = Vec::new(); - Interpreter { - externals, - value_stack, - call_stack, - } - } - - pub fn start_execution(&mut self, func: &FuncRef, args: &[RuntimeValue]) -> Result, Trap> { + pub fn new(func: &FuncRef, args: &[RuntimeValue], externals: &'a mut E) -> Result, Trap> { + let mut value_stack = ValueStack::with_limit(DEFAULT_VALUE_STACK_LIMIT); for arg in args { - self.value_stack + value_stack .push(*arg) .map_err( // There is not enough space for pushing initial arguments. @@ -73,12 +66,24 @@ impl<'a, E: Externals> Interpreter<'a, E> { )?; } + let mut call_stack = Vec::new(); let initial_frame = FunctionContext::new(func.clone()); + call_stack.push(initial_frame); - self.call_stack.push(initial_frame); + let return_type = func.signature().return_type(); + + Ok(Interpreter { + externals, + value_stack, + call_stack, + return_type, + }) + } + + pub fn start_execution(&mut self) -> Result, Trap> { self.run_interpreter_loop()?; - let opt_return_value = func.signature().return_type().map(|_vt| { + let opt_return_value = self.return_type.map(|_vt| { self.value_stack.pop() });