Skip to content

Commit

Permalink
Refactored emscripten usage to allow future ABIs
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Dec 11, 2018
1 parent eefea5e commit 9a028ab
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/nginx/logs/nginx.pid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12824
96248
23 changes: 14 additions & 9 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,29 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
debug!("webassembly - creating module");
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 {
mock_missing_imports: true,
mock_missing_globals: true,
mock_missing_tables: true,
use_emscripten: is_emscripten,
abi: abi,
show_progressbar: true,
isa: isa,
};

let import_object = if is_emscripten {
apis::generate_emscripten_env()
}
else {
webassembly::ImportObject::new()
};

debug!("webassembly - creating instance");
let mut instance = webassembly::Instance::new(
&module,
Expand Down
4 changes: 2 additions & 2 deletions src/emtests/_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
macro_rules! assert_emscripten_output {
($file:expr, $name:expr, $args:expr, $expected:expr) => {{
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;

let wasm_bytes = include_bytes!($file);
Expand All @@ -11,7 +11,7 @@ macro_rules! assert_emscripten_output {
mock_missing_imports: true,
mock_missing_globals: true,
mock_missing_tables: true,
use_emscripten: true,
abi: InstanceABI::Emscripten,
show_progressbar: false,
isa: get_isa(),
});
Expand Down
14 changes: 10 additions & 4 deletions src/webassembly/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl fmt::Debug for EmscriptenData {
}
}

#[derive(PartialEq)]
pub enum InstanceABI {
Emscripten,
None
}

/// An Instance of a WebAssembly module
/// NOTE: There is an assumption that data_pointers is always the
/// first field
Expand Down Expand Up @@ -139,7 +145,7 @@ pub struct InstanceOptions {
pub mock_missing_imports: bool,
pub mock_missing_globals: bool,
pub mock_missing_tables: bool,
pub use_emscripten: bool,
pub abi: InstanceABI,
pub show_progressbar: bool,
pub isa: Box<TargetIsa>,
}
Expand Down Expand Up @@ -495,7 +501,7 @@ impl Instance {
let memory = memory.entity;
// If we use emscripten, we set a fixed initial and 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:
// (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE
// However, it should be: (initial * LinearMemory::PAGE_SIZE) == 16777216
Expand All @@ -520,7 +526,7 @@ impl Instance {
let to_init = &mut mem[offset..offset + init.data.len()];
to_init.copy_from_slice(&init.data);
}
if options.use_emscripten {
if options.abi == InstanceABI::Emscripten {
debug!("emscripten::setup memory");
crate::apis::emscripten::emscripten_set_up_memory(&mut memories[0]);
debug!("emscripten::finish setup memory");
Expand Down Expand Up @@ -550,7 +556,7 @@ impl Instance {
tables: tables_pointer[..].into(),
};

let emscripten_data = if options.use_emscripten {
let emscripten_data = if options.abi == InstanceABI::Emscripten {
unsafe {
debug!("emscripten::initiating data");
let malloc_export = module.info.exports.get("_malloc");
Expand Down
11 changes: 9 additions & 2 deletions src/webassembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use wasmparser::WasmDecoder;

pub use self::errors::{Error, ErrorKind};
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::module::{Export, Module, ModuleInfo};

Expand Down Expand Up @@ -56,11 +56,18 @@ pub fn instantiate(
let isa = get_isa();
let module = compile(buffer_source)?;

let abi = if is_emscripten_module(&module) {
InstanceABI::Emscripten
}
else {
InstanceABI::None
};

let options = options.unwrap_or_else(|| InstanceOptions {
mock_missing_imports: false,
mock_missing_globals: false,
mock_missing_tables: false,
use_emscripten: is_emscripten_module(&module),
abi: abi,
show_progressbar: false,
isa: isa,
});
Expand Down

0 comments on commit 9a028ab

Please sign in to comment.