Cache memory index.

This commit is contained in:
Sergey Pepyakin 2018-06-14 18:47:23 +03:00
parent ea1e15bc3f
commit 03f378a000
2 changed files with 21 additions and 36 deletions

View File

@ -56,14 +56,6 @@ impl<T> StackWithLimit<T> where T: Clone {
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn pick_mut(&mut self, depth: usize) -> Result<&mut T, Error> {
let len = self.values.len();
// TODO:
self.values
.get_mut(len - 1 - depth)
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn get(&self, index: usize) -> Result<&T, Error> {
if index >= self.values.len() {
return Err(Error(format!("trying to get value at position {} on stack of size {}", index, self.values.len())));

View File

@ -6,6 +6,7 @@ use std::iter::repeat;
use parity_wasm::elements::Local;
use {Error, Trap, TrapKind, Signature};
use module::ModuleRef;
use memory::MemoryRef;
use func::{FuncRef, FuncInstance, FuncInstanceInternal};
use value::{
RuntimeValue, FromRuntimeValue, WrapInto, TryTruncateInto, ExtendInto,
@ -13,7 +14,6 @@ use value::{
};
use host::Externals;
use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
use common::stack::StackWithLimit;
use memory_units::Pages;
use nan_preserving_float::{F32, F64};
use isa;
@ -123,7 +123,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
match *nested_func.as_internal() {
FuncInstanceInternal::Internal { .. } => {
let nested_context = function_context.nested(nested_func.clone()).map_err(Trap::new)?;
let nested_context = FunctionContext::new(nested_func.clone());
call_stack.push(function_context);
call_stack.push(nested_context);
},
@ -534,8 +534,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
offset,
raw_address,
)?;
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
let b = m.get(address, mem::size_of::<T>())
.map_err(|_| TrapKind::MemoryAccessOutOfBounds)?;
@ -555,8 +555,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
offset,
raw_address,
)?;
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
let b = m.get(address, mem::size_of::<T>())
.map_err(|_| TrapKind::MemoryAccessOutOfBounds)?;
@ -585,8 +585,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
raw_address,
)?;
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
m.set(address, &stack_value)
.map_err(|_| TrapKind::MemoryAccessOutOfBounds)?;
@ -617,8 +617,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
offset,
raw_address,
)?;
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
m.set(address, &stack_value)
.map_err(|_| TrapKind::MemoryAccessOutOfBounds)?;
@ -626,8 +626,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
}
fn run_current_memory(&mut self, context: &mut FunctionContext) -> Result<InstructionOutcome, TrapKind> {
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
let s = m.current_size().0;
self
@ -640,8 +640,8 @@ impl<'a, E: Externals> Interpreter<'a, E> {
let pages: u32 = self
.value_stack
.pop_as();
let m = context.module()
.memory_by_index(DEFAULT_MEMORY_INDEX)
let m = context
.memory()
.expect("Due to validation memory should exists");
let m = match m.grow(Pages(pages as usize)) {
Ok(Pages(new_size)) => new_size as u32,
@ -1003,6 +1003,7 @@ struct FunctionContext {
/// Internal function reference.
pub function: FuncRef,
pub module: ModuleRef,
pub memory: Option<MemoryRef>,
/// Current instruction position.
pub position: usize,
}
@ -1013,28 +1014,16 @@ impl FunctionContext {
FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"),
FuncInstanceInternal::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
};
let memory = module.memory_by_index(DEFAULT_MEMORY_INDEX);
FunctionContext {
is_initialized: false,
function: function,
module: ModuleRef(module),
memory: memory,
position: 0,
}
}
pub fn nested(&mut self, function: FuncRef) -> Result<Self, TrapKind> {
let module = match *function.as_internal() {
FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"),
FuncInstanceInternal::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
};
Ok(FunctionContext {
is_initialized: false,
function: function,
module: ModuleRef(module),
position: 0,
})
}
pub fn is_initialized(&self) -> bool {
self.is_initialized
}
@ -1061,6 +1050,10 @@ impl FunctionContext {
pub fn module(&self) -> ModuleRef {
self.module.clone()
}
pub fn memory(&self) -> Option<&MemoryRef> {
self.memory.as_ref()
}
}
impl fmt::Debug for FunctionContext {