mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-14 06:35:40 +00:00
Merge #300
300: hook up wasi to wasmer r=lachlansneff a=MarkMcCaskey Co-authored-by: Mark McCaskey <mark@wasmer.io> Co-authored-by: Mark McCaskey <markmccaskey@users.noreply.github.com>
This commit is contained in:
commit
3323c3c60a
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1339,6 +1339,7 @@ dependencies = [
|
|||||||
"wasmer-llvm-backend 0.1.0",
|
"wasmer-llvm-backend 0.1.0",
|
||||||
"wasmer-runtime 0.2.1",
|
"wasmer-runtime 0.2.1",
|
||||||
"wasmer-runtime-core 0.2.1",
|
"wasmer-runtime-core 0.2.1",
|
||||||
|
"wasmer-wasi 0.1.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -28,6 +28,7 @@ wasmer-runtime-core = { path = "lib/runtime-core" }
|
|||||||
wasmer-emscripten = { path = "lib/emscripten" }
|
wasmer-emscripten = { path = "lib/emscripten" }
|
||||||
wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true }
|
wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true }
|
||||||
wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true }
|
wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true }
|
||||||
|
wasmer-wasi = { path = "lib/wasi", optional = true }
|
||||||
|
|
||||||
[workspace]
|
[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"]
|
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"]
|
||||||
@ -42,4 +43,5 @@ default = ["fast-tests"]
|
|||||||
# This feature will allow cargo test to run much faster
|
# This feature will allow cargo test to run much faster
|
||||||
fast-tests = []
|
fast-tests = []
|
||||||
llvm = ["wasmer-llvm-backend"]
|
llvm = ["wasmer-llvm-backend"]
|
||||||
dynasm = ["wasmer-dynasm-backend"]
|
dynasm = ["wasmer-dynasm-backend"]
|
||||||
|
wasi = ["wasmer-wasi"]
|
@ -1,10 +1,14 @@
|
|||||||
mod state;
|
mod state;
|
||||||
mod syscalls;
|
mod syscalls;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use self::state::WasiState;
|
use self::state::WasiState;
|
||||||
use self::syscalls::*;
|
use self::syscalls::*;
|
||||||
|
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
|
|
||||||
|
pub use self::utils::is_wasi_module;
|
||||||
|
|
||||||
use wasmer_runtime_core::{func, import::ImportObject, imports};
|
use wasmer_runtime_core::{func, import::ImportObject, imports};
|
||||||
|
|
||||||
pub fn generate_import_object(args: Vec<Vec<u8>>, envs: Vec<Vec<u8>>) -> ImportObject {
|
pub fn generate_import_object(args: Vec<Vec<u8>>, envs: Vec<Vec<u8>>) -> ImportObject {
|
||||||
|
15
lib/wasi/src/utils.rs
Normal file
15
lib/wasi/src/utils.rs
Normal file
@ -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
|
||||||
|
}
|
@ -15,6 +15,8 @@ use wasmer::*;
|
|||||||
use wasmer_emscripten;
|
use wasmer_emscripten;
|
||||||
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
|
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
|
||||||
use wasmer_runtime_core::backend::CompilerConfig;
|
use wasmer_runtime_core::backend::CompilerConfig;
|
||||||
|
#[cfg(feature = "wasi")]
|
||||||
|
use wasmer_wasi;
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
|
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
|
||||||
@ -200,6 +202,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
|
|||||||
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
.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 (_abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) {
|
||||||
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
|
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
|
||||||
(
|
(
|
||||||
@ -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
|
let mut instance = module
|
||||||
.instantiate(&import_object)
|
.instantiate(&import_object)
|
||||||
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
|
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
|
||||||
|
@ -21,6 +21,7 @@ pub struct ResultObject {
|
|||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum InstanceABI {
|
pub enum InstanceABI {
|
||||||
Emscripten,
|
Emscripten,
|
||||||
|
WASI,
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user