clean up implementation

This commit is contained in:
Mark McCaskey
2019-03-27 14:01:27 -07:00
parent f9a29445ca
commit 09068c1a74
11 changed files with 109 additions and 58 deletions

View File

@ -14,6 +14,8 @@ use crate::{
};
use std::{any::Any, ptr::NonNull};
use hashbrown::HashMap;
pub mod sys {
pub use crate::sys::*;
}
@ -38,11 +40,28 @@ impl Token {
}
}
/// Configuration data for the compiler
pub struct CompilerConfig {
/// Symbol information generated from emscripten; used for more detailed debug messages
pub symbol_map: Option<HashMap<u32, String>>,
}
impl Default for CompilerConfig {
fn default() -> CompilerConfig {
CompilerConfig { symbol_map: None }
}
}
pub trait Compiler {
/// Compiles a `Module` from WebAssembly binary format.
/// The `CompileToken` parameter ensures that this can only
/// be called from inside the runtime.
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner>;
fn compile(
&self,
wasm: &[u8],
comp_conf: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner>;
unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
}

View File

@ -13,7 +13,6 @@ use crate::{
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Value},
vm,
};
use hashbrown::HashMap;
use std::{mem, sync::Arc};
pub(crate) struct InstanceInner {
@ -45,11 +44,7 @@ pub struct Instance {
}
impl Instance {
pub(crate) fn new(
module: Arc<ModuleInner>,
imports: &ImportObject,
maybe_symbol_map: Option<HashMap<u32, String>>,
) -> Result<Instance> {
pub(crate) fn new(module: Arc<ModuleInner>, imports: &ImportObject) -> Result<Instance> {
// We need the backing and import_backing to create a vm::Ctx, but we need
// a vm::Ctx to create a backing and an import_backing. The solution is to create an
// uninitialized vm::Ctx and then initialize it in-place.
@ -68,12 +63,7 @@ impl Instance {
// Initialize the vm::Ctx in-place after the backing
// has been boxed.
unsafe {
*inner.vmctx = vm::Ctx::new(
&mut inner.backing,
&mut inner.import_backing,
&module,
maybe_symbol_map,
)
*inner.vmctx = vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module)
};
let instance = Instance {

View File

@ -68,7 +68,19 @@ pub fn compile_with(
) -> CompileResult<module::Module> {
let token = backend::Token::generate();
compiler
.compile(wasm, token)
.compile(wasm, Default::default(), token)
.map(|inner| module::Module::new(Arc::new(inner)))
}
/// The same as `compile_with` but takes a symbol map for use with debugging
pub fn compile_with_config(
wasm: &[u8],
compiler: &dyn backend::Compiler,
compiler_config: backend::CompilerConfig,
) -> CompileResult<module::Module> {
let token = backend::Token::generate();
compiler
.compile(wasm, compiler_config, token)
.map(|inner| module::Module::new(Arc::new(inner)))
}

View File

@ -56,6 +56,8 @@ pub struct ModuleInfo {
pub namespace_table: StringTable<NamespaceIndex>,
pub name_table: StringTable<NameIndex>,
pub em_symbol_map: Option<HashMap<u32, String>>,
}
/// A compiled WebAssembly module.
@ -100,12 +102,8 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn instantiate(
&self,
import_object: &ImportObject,
maybe_symbol_map: Option<HashMap<u32, String>>,
) -> error::Result<Instance> {
Instance::new(Arc::clone(&self.inner), import_object, maybe_symbol_map)
pub fn instantiate(&self, import_object: &ImportObject) -> error::Result<Instance> {
Instance::new(Arc::clone(&self.inner), import_object)
}
pub fn cache(&self) -> Result<Artifact, CacheError> {

View File

@ -5,9 +5,10 @@ use crate::{
structures::TypedIndex,
types::{LocalOrImport, MemoryIndex},
};
use hashbrown::HashMap;
use std::{ffi::c_void, mem, ptr};
use hashbrown::HashMap;
/// The context of the currently running WebAssembly instance.
///
///
@ -25,8 +26,6 @@ pub struct Ctx {
pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
pub maybe_symbol_map: Option<HashMap<u32, String>>,
}
/// The internal context of the currently running WebAssembly instance.
@ -70,7 +69,6 @@ impl Ctx {
local_backing: &mut LocalBacking,
import_backing: &mut ImportBacking,
module: &ModuleInner,
maybe_symbol_map: Option<HashMap<u32, String>>,
) -> Self {
Self {
internal: InternalCtx {
@ -93,7 +91,6 @@ impl Ctx {
data: ptr::null_mut(),
data_finalizer: None,
maybe_symbol_map,
}
}
@ -126,7 +123,6 @@ impl Ctx {
data,
data_finalizer: Some(data_finalizer),
maybe_symbol_map: None,
}
}
@ -162,6 +158,10 @@ impl Ctx {
},
}
}
pub unsafe fn borrow_symbol_map(&self) -> &Option<HashMap<u32, String>> {
&(*self.module).info.em_symbol_map
}
}
#[doc(hidden)]