Refactored emscripten usage to allow future ABIs

This commit is contained in:
Syrus
2018-12-10 21:19:39 -08:00
parent eefea5ebee
commit 9a028abfe5
5 changed files with 36 additions and 18 deletions

View File

@ -1 +1 @@
12824 96248

View File

@ -70,24 +70,29 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
debug!("webassembly - creating module"); debug!("webassembly - creating module");
let module = webassembly::compile(wasm_binary).map_err(|err| format!("Can't create the WebAssembly module: {}", err))?; let module = webassembly::compile(wasm_binary).map_err(|err| format!("Can't create the WebAssembly module: {}", err))?;
let is_emscripten = apis::is_emscripten_module(&module); let abi = if apis::is_emscripten_module(&module) {
webassembly::InstanceABI::Emscripten
} else {
webassembly::InstanceABI::None
};
let import_object = if abi == webassembly::InstanceABI::Emscripten {
apis::generate_emscripten_env()
}
else {
webassembly::ImportObject::new()
};
let instance_options = webassembly::InstanceOptions { let instance_options = webassembly::InstanceOptions {
mock_missing_imports: true, mock_missing_imports: true,
mock_missing_globals: true, mock_missing_globals: true,
mock_missing_tables: true, mock_missing_tables: true,
use_emscripten: is_emscripten, abi: abi,
show_progressbar: true, show_progressbar: true,
isa: isa, isa: isa,
}; };
let import_object = if is_emscripten {
apis::generate_emscripten_env()
}
else {
webassembly::ImportObject::new()
};
debug!("webassembly - creating instance"); debug!("webassembly - creating instance");
let mut instance = webassembly::Instance::new( let mut instance = webassembly::Instance::new(
&module, &module,

View File

@ -2,7 +2,7 @@
macro_rules! assert_emscripten_output { macro_rules! assert_emscripten_output {
($file:expr, $name:expr, $args:expr, $expected:expr) => {{ ($file:expr, $name:expr, $args:expr, $expected:expr) => {{
use crate::apis::generate_emscripten_env; use crate::apis::generate_emscripten_env;
use crate::webassembly::{instantiate, start_instance, InstanceOptions, get_isa}; use crate::webassembly::{instantiate, start_instance, InstanceOptions, get_isa, InstanceABI};
use crate::common::stdio::StdioCapturer; use crate::common::stdio::StdioCapturer;
let wasm_bytes = include_bytes!($file); let wasm_bytes = include_bytes!($file);
@ -11,7 +11,7 @@ macro_rules! assert_emscripten_output {
mock_missing_imports: true, mock_missing_imports: true,
mock_missing_globals: true, mock_missing_globals: true,
mock_missing_tables: true, mock_missing_tables: true,
use_emscripten: true, abi: InstanceABI::Emscripten,
show_progressbar: false, show_progressbar: false,
isa: get_isa(), isa: get_isa(),
}); });

View File

@ -85,6 +85,12 @@ impl fmt::Debug for EmscriptenData {
} }
} }
#[derive(PartialEq)]
pub enum InstanceABI {
Emscripten,
None
}
/// An Instance of a WebAssembly module /// An Instance of a WebAssembly module
/// NOTE: There is an assumption that data_pointers is always the /// NOTE: There is an assumption that data_pointers is always the
/// first field /// first field
@ -139,7 +145,7 @@ pub struct InstanceOptions {
pub mock_missing_imports: bool, pub mock_missing_imports: bool,
pub mock_missing_globals: bool, pub mock_missing_globals: bool,
pub mock_missing_tables: bool, pub mock_missing_tables: bool,
pub use_emscripten: bool, pub abi: InstanceABI,
pub show_progressbar: bool, pub show_progressbar: bool,
pub isa: Box<TargetIsa>, pub isa: Box<TargetIsa>,
} }
@ -495,7 +501,7 @@ impl Instance {
let memory = memory.entity; let memory = memory.entity;
// If we use emscripten, we set a fixed initial and maximum // If we use emscripten, we set a fixed initial and maximum
debug!("Instance - init memory ({}, {:?})", memory.pages_count, memory.maximum); debug!("Instance - init memory ({}, {:?})", memory.pages_count, memory.maximum);
let memory = if options.use_emscripten { let memory = if options.abi == InstanceABI::Emscripten {
// We use MAX_PAGES, so at the end the result is: // We use MAX_PAGES, so at the end the result is:
// (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE // (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE
// However, it should be: (initial * LinearMemory::PAGE_SIZE) == 16777216 // However, it should be: (initial * LinearMemory::PAGE_SIZE) == 16777216
@ -520,7 +526,7 @@ impl Instance {
let to_init = &mut mem[offset..offset + init.data.len()]; let to_init = &mut mem[offset..offset + init.data.len()];
to_init.copy_from_slice(&init.data); to_init.copy_from_slice(&init.data);
} }
if options.use_emscripten { if options.abi == InstanceABI::Emscripten {
debug!("emscripten::setup memory"); debug!("emscripten::setup memory");
crate::apis::emscripten::emscripten_set_up_memory(&mut memories[0]); crate::apis::emscripten::emscripten_set_up_memory(&mut memories[0]);
debug!("emscripten::finish setup memory"); debug!("emscripten::finish setup memory");
@ -550,7 +556,7 @@ impl Instance {
tables: tables_pointer[..].into(), tables: tables_pointer[..].into(),
}; };
let emscripten_data = if options.use_emscripten { let emscripten_data = if options.abi == InstanceABI::Emscripten {
unsafe { unsafe {
debug!("emscripten::initiating data"); debug!("emscripten::initiating data");
let malloc_export = module.info.exports.get("_malloc"); let malloc_export = module.info.exports.get("_malloc");

View File

@ -19,7 +19,7 @@ use wasmparser::WasmDecoder;
pub use self::errors::{Error, ErrorKind}; pub use self::errors::{Error, ErrorKind};
pub use self::import_object::{ImportObject, ImportValue}; pub use self::import_object::{ImportObject, ImportValue};
pub use self::instance::{Instance, InstanceOptions}; pub use self::instance::{Instance, InstanceOptions, InstanceABI};
pub use self::memory::LinearMemory; pub use self::memory::LinearMemory;
pub use self::module::{Export, Module, ModuleInfo}; pub use self::module::{Export, Module, ModuleInfo};
@ -56,11 +56,18 @@ pub fn instantiate(
let isa = get_isa(); let isa = get_isa();
let module = compile(buffer_source)?; let module = compile(buffer_source)?;
let abi = if is_emscripten_module(&module) {
InstanceABI::Emscripten
}
else {
InstanceABI::None
};
let options = options.unwrap_or_else(|| InstanceOptions { let options = options.unwrap_or_else(|| InstanceOptions {
mock_missing_imports: false, mock_missing_imports: false,
mock_missing_globals: false, mock_missing_globals: false,
mock_missing_tables: false, mock_missing_tables: false,
use_emscripten: is_emscripten_module(&module), abi: abi,
show_progressbar: false, show_progressbar: false,
isa: isa, isa: isa,
}); });