Skip to content

Commit

Permalink
Merge #300
Browse files Browse the repository at this point in the history
300: hook up wasi to wasmer r=lachlansneff a=MarkMcCaskey



Co-authored-by: Mark McCaskey <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
3 people committed Mar 28, 2019
2 parents e227b89 + ce22818 commit 3323c3c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ wasmer-runtime-core = { path = "lib/runtime-core" }
wasmer-emscripten = { path = "lib/emscripten" }
wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true }
wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true }
wasmer-wasi = { path = "lib/wasi", optional = true }

[workspace]
members = ["lib/clif-backend", "lib/dynasm-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi"]
Expand All @@ -42,4 +43,5 @@ default = ["fast-tests"]
# This feature will allow cargo test to run much faster
fast-tests = []
llvm = ["wasmer-llvm-backend"]
dynasm = ["wasmer-dynasm-backend"]
dynasm = ["wasmer-dynasm-backend"]
wasi = ["wasmer-wasi"]
4 changes: 4 additions & 0 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod state;
mod syscalls;
mod utils;

use self::state::WasiState;
use self::syscalls::*;

use std::ffi::c_void;

pub use self::utils::is_wasi_module;

use wasmer_runtime_core::{func, import::ImportObject, imports};

pub fn generate_import_object(args: Vec<Vec<u8>>, envs: Vec<Vec<u8>>) -> ImportObject {
Expand Down
15 changes: 15 additions & 0 deletions lib/wasi/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use wasmer_runtime_core::module::Module;

/// Check if a provided module is compiled with WASI support
pub fn is_wasi_module(module: &Module) -> bool {
for (_, import_name) in &module.info().imported_functions {
let namespace = module
.info()
.namespace_table
.get(import_name.namespace_index);
if namespace == "wasi_unstable" {
return true;
}
}
false
}
23 changes: 23 additions & 0 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use wasmer::*;
use wasmer_emscripten;
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
use wasmer_runtime_core::backend::CompilerConfig;
#[cfg(feature = "wasi")]
use wasmer_wasi;

#[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
Expand Down Expand Up @@ -200,6 +202,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("Can't compile module: {:?}", e))?
};

// TODO: refactor this
#[cfg(not(features = "wasi"))]
let (_abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) {
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
(
Expand All @@ -215,6 +219,25 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
)
};

#[cfg(features = "wasi")]
let (_abi, import_object) = if wasmer_wasi::is_wasi_module(&module) {
(
InstanceABI::WASI,
wasmer_wasi::generate_import_object(
options.args.iter().map(|arg| arg.into_bytes()).collect(),
env::vars()
.iter()
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
.collect(),
),
)
} else {
(
InstanceABI::None,
wasmer_runtime_core::import::ImportObject::new(),
)
};

let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
Expand Down
1 change: 1 addition & 0 deletions src/webassembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct ResultObject {
#[derive(PartialEq)]
pub enum InstanceABI {
Emscripten,
WASI,
None,
}

Expand Down

0 comments on commit 3323c3c

Please sign in to comment.