Merge branch 'feature/vm_refactor' of github.com:wasmerio/wasmer into feature/vm_refactor

This commit is contained in:
Syrus 2019-01-18 14:28:13 -08:00
commit 62d06043b2
6 changed files with 125 additions and 60 deletions

View File

@ -351,9 +351,7 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
let mut func = ir::Function::with_name_signature(name, sig);
println!("translating function");
func_translator.translate(body_bytes, &mut func, &mut func_env)?;
println!("done translating function");
func
};

View File

@ -4,7 +4,7 @@ use wasmer_runtime::{import::Imports, Instance};
fn main() {
let mut instance = create_module_1();
let result = instance.call("get-0", &[]);
let result = instance.call("call-overwritten-element", &[]);
println!("result: {:?}", result);
}
@ -20,14 +20,17 @@ fn main() {
fn create_module_1() -> Instance {
let module_str = r#"(module
(type (;0;) (func (result i32)))
(import "spectest" "global_i32" (global (;0;) i32))
(import "spectest" "table" (table (;0;) 10 anyfunc))
(func (;0;) (type 0) (result i32)
get_global 0)
i32.const 65)
(func (;1;) (type 0) (result i32)
get_global 1)
(global (;1;) i32 (get_global 0))
(export "get-0" (func 0))
(export "get-0-ref" (func 1)))
i32.const 66)
(func (;2;) (type 0) (result i32)
i32.const 9
call_indirect (type 0))
(export "call-overwritten-element" (func 2))
(elem (;0;) (i32.const 9) 0)
(elem (;1;) (i32.const 9) 1))
"#;
let wasm_binary = wat2wasm(module_str.as_bytes()).expect("WAST not valid or malformed");
let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new())

View File

@ -307,12 +307,43 @@ impl ImportBacking {
imports: &mut Imports,
vmctx: *mut vm::Ctx,
) -> LinkResult<Self> {
Ok(ImportBacking {
functions: import_functions(module, imports, vmctx)?,
memories: import_memories(module, imports, vmctx)?,
tables: import_tables(module, imports, vmctx)?,
globals: import_globals(module, imports)?,
})
let mut failed = false;
let mut link_errors = vec![];
let functions = import_functions(module, imports, vmctx).unwrap_or_else(|le| {
failed = true;
link_errors.extend(le);
Map::new().into_boxed_map()
});
let memories = import_memories(module, imports, vmctx).unwrap_or_else(|le| {
failed = true;
link_errors.extend(le);
Map::new().into_boxed_map()
});
let tables = import_tables(module, imports, vmctx).unwrap_or_else(|le| {
failed = true;
link_errors.extend(le);
Map::new().into_boxed_map()
});
let globals = import_globals(module, imports).unwrap_or_else(|le| {
failed = true;
link_errors.extend(le);
Map::new().into_boxed_map()
});
if failed {
Err(link_errors)
} else {
Ok(ImportBacking {
functions,
memories,
tables,
globals,
})
}
}
pub fn imported_memory(&self, memory_index: ImportedMemoryIndex) -> vm::ImportedMemory {
@ -325,6 +356,7 @@ fn import_functions(
imports: &mut Imports,
vmctx: *mut vm::Ctx,
) -> LinkResult<BoxedMap<ImportedFuncIndex, vm::ImportedFunc>> {
let mut link_errors = vec![];
let mut functions = Map::with_capacity(module.imported_functions.len());
for (index, ImportName { namespace, name }) in &module.imported_functions {
let sig_index = module.func_assoc[index.convert_up(module)];
@ -347,12 +379,12 @@ fn import_functions(
},
});
} else {
Err(LinkError::IncorrectImportSignature {
link_errors.push(LinkError::IncorrectImportSignature {
namespace: namespace.clone(),
name: name.clone(),
expected: expected_sig.clone(),
found: signature.clone(),
})?
});
}
}
Some(export_type) => {
@ -363,20 +395,27 @@ fn import_functions(
Export::Global { .. } => "global",
}
.to_string();
Err(LinkError::IncorrectImportType {
link_errors.push(LinkError::IncorrectImportType {
namespace: namespace.clone(),
name: name.clone(),
expected: "function".to_string(),
found: export_type_name,
})?
});
}
None => {
link_errors.push(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
});
}
None => Err(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
})?,
}
}
Ok(functions.into_boxed_map())
if link_errors.len() > 0 {
Err(link_errors)
} else {
Ok(functions.into_boxed_map())
}
}
fn import_memories(
@ -384,6 +423,7 @@ fn import_memories(
imports: &mut Imports,
vmctx: *mut vm::Ctx,
) -> LinkResult<BoxedMap<ImportedMemoryIndex, vm::ImportedMemory>> {
let mut link_errors = vec![];
let mut memories = Map::with_capacity(module.imported_memories.len());
for (_index, (ImportName { namespace, name }, expected_memory_desc)) in
&module.imported_memories
@ -406,12 +446,12 @@ fn import_memories(
},
});
} else {
Err(LinkError::IncorrectMemoryDescription {
link_errors.push(LinkError::IncorrectMemoryDescription {
namespace: namespace.clone(),
name: name.clone(),
expected: expected_memory_desc.clone(),
found: memory_desc.clone(),
})?
});
}
}
Some(export_type) => {
@ -422,20 +462,27 @@ fn import_memories(
Export::Global { .. } => "global",
}
.to_string();
Err(LinkError::IncorrectImportType {
link_errors.push(LinkError::IncorrectImportType {
namespace: namespace.clone(),
name: name.clone(),
expected: "memory".to_string(),
found: export_type_name,
})?
});
}
None => {
link_errors.push(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
});
}
None => Err(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
})?,
}
}
Ok(memories.into_boxed_map())
if link_errors.len() > 0 {
Err(link_errors)
} else {
Ok(memories.into_boxed_map())
}
}
fn import_tables(
@ -443,6 +490,7 @@ fn import_tables(
imports: &mut Imports,
vmctx: *mut vm::Ctx,
) -> LinkResult<BoxedMap<ImportedTableIndex, vm::ImportedTable>> {
let mut link_errors = vec![];
let mut tables = Map::with_capacity(module.imported_tables.len());
for (_index, (ImportName { namespace, name }, expected_table_desc)) in &module.imported_tables {
let table_import = imports
@ -463,12 +511,12 @@ fn import_tables(
},
});
} else {
Err(LinkError::IncorrectTableDescription {
link_errors.push(LinkError::IncorrectTableDescription {
namespace: namespace.clone(),
name: name.clone(),
expected: expected_table_desc.clone(),
found: table_desc.clone(),
})?
});
}
}
Some(export_type) => {
@ -479,26 +527,34 @@ fn import_tables(
Export::Global { .. } => "global",
}
.to_string();
Err(LinkError::IncorrectImportType {
link_errors.push(LinkError::IncorrectImportType {
namespace: namespace.clone(),
name: name.clone(),
expected: "table".to_string(),
found: export_type_name,
})?
});
}
None => {
link_errors.push(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
});
}
None => Err(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
})?,
}
}
Ok(tables.into_boxed_map())
if link_errors.len() > 0 {
Err(link_errors)
} else {
Ok(tables.into_boxed_map())
}
}
fn import_globals(
module: &ModuleInner,
imports: &mut Imports,
) -> LinkResult<BoxedMap<ImportedGlobalIndex, vm::ImportedGlobal>> {
let mut link_errors = vec![];
let mut globals = Map::with_capacity(module.imported_globals.len());
for (_, (ImportName { namespace, name }, imported_global_desc)) in &module.imported_globals {
let import = imports
@ -511,12 +567,12 @@ fn import_globals(
global: local.inner(),
});
} else {
Err(LinkError::IncorrectGlobalDescription {
link_errors.push(LinkError::IncorrectGlobalDescription {
namespace: namespace.clone(),
name: name.clone(),
expected: imported_global_desc.clone(),
found: global.clone(),
})?
});
}
}
Some(export_type) => {
@ -527,18 +583,25 @@ fn import_globals(
Export::Global { .. } => "global",
}
.to_string();
Err(LinkError::IncorrectImportType {
link_errors.push(LinkError::IncorrectImportType {
namespace: namespace.clone(),
name: name.clone(),
expected: "global".to_string(),
found: export_type_name,
})?
});
}
None => {
link_errors.push(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
});
}
None => Err(LinkError::ImportNotFound {
namespace: namespace.clone(),
name: name.clone(),
})?,
}
}
Ok(globals.into_boxed_map())
if link_errors.len() > 0 {
Err(link_errors)
} else {
Ok(globals.into_boxed_map())
}
}

View File

@ -2,7 +2,7 @@ use crate::types::{FuncSig, GlobalDesc, Memory, MemoryIndex, Table, TableIndex,
pub type Result<T> = std::result::Result<T, Box<Error>>;
pub type CompileResult<T> = std::result::Result<T, Box<CompileError>>;
pub type LinkResult<T> = std::result::Result<T, Box<LinkError>>;
pub type LinkResult<T> = std::result::Result<T, Vec<LinkError>>;
pub type RuntimeResult<T> = std::result::Result<T, Box<RuntimeError>>;
pub type CallResult<T> = std::result::Result<T, Box<CallError>>;
@ -120,7 +120,7 @@ impl PartialEq for CallError {
#[derive(Debug, Clone)]
pub enum Error {
CompileError(CompileError),
LinkError(LinkError),
LinkError(Vec<LinkError>),
RuntimeError(RuntimeError),
CallError(CallError),
}
@ -137,9 +137,9 @@ impl From<Box<CompileError>> for Box<Error> {
}
}
impl From<Box<LinkError>> for Box<Error> {
fn from(link_err: Box<LinkError>) -> Self {
Box::new(Error::LinkError(*link_err))
impl From<Vec<LinkError>> for Box<Error> {
fn from(link_err: Vec<LinkError>) -> Self {
Box::new(Error::LinkError(link_err))
}
}

View File

@ -248,9 +248,9 @@ impl InstanceInner {
) -> (MemoryPointer, Context, Memory) {
match mem_index.local_or_import(module) {
LocalOrImport::Local(local_mem_index) => {
let vm_mem = &mut self.backing.memories[local_mem_index];
let vm_mem = &mut self.backing.vm_memories[local_mem_index];
(
unsafe { MemoryPointer::new(&mut vm_mem.into_vm_memory(local_mem_index)) },
unsafe { MemoryPointer::new(vm_mem) },
Context::Internal,
*module
.memories
@ -313,9 +313,9 @@ impl InstanceInner {
) -> (TablePointer, Context, Table) {
match table_index.local_or_import(module) {
LocalOrImport::Local(local_table_index) => {
let vm_table = &mut self.backing.tables[local_table_index];
let vm_table = &mut self.backing.vm_tables[local_table_index];
(
unsafe { TablePointer::new(&mut vm_table.into_vm_table()) },
unsafe { TablePointer::new(vm_table) },
Context::Internal,
*module
.tables

View File

@ -7,6 +7,7 @@ use std::{
/// This is a dynamically-sized slice
/// that can only be indexed by the
/// correct index type.
#[derive(Debug)]
pub struct SliceMap<K, V>
where
K: TypedIndex,