Accept func and args when creating the Interpreter

This commit is contained in:
Wei Tang 2018-07-06 06:58:16 +08:00
parent d39dd4c773
commit 02cb00f14b
2 changed files with 21 additions and 16 deletions

View File

@ -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,

View File

@ -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<FunctionContext>,
return_type: Option<ValueType>,
}
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<Option<RuntimeValue>, Trap> {
pub fn new(func: &FuncRef, args: &[RuntimeValue], externals: &'a mut E) -> Result<Interpreter<'a, E>, 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<Option<RuntimeValue>, 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()
});