wasmer/lib/wasi/src/utils.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2019-03-28 13:46:30 -07:00
use wasmer_runtime_core::module::Module;
2019-03-28 12:19:23 -07:00
#[allow(dead_code)]
2019-03-28 13:46:30 -07:00
/// Check if a provided module is compiled with WASI support
2019-03-28 12:19:23 -07:00
pub fn is_wasi_module(module: &Module) -> bool {
get_wasi_version(module) == Some(WasiVersion::Snapshot1)
}
/// The version of WASI. This is determined by the namespace string
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WasiVersion {
/// "wasi_unstable"
Snapshot0,
/// "wasi_snapshot_preview1"
Snapshot1,
}
pub fn get_wasi_version(module: &Module) -> Option<WasiVersion> {
let mut import_iter = module
.info()
.imported_functions
.iter()
.map(|(_, import_name)| import_name.namespace_index);
// returns None if empty
let first = import_iter.next()?;
if import_iter.all(|idx| idx == first) {
match module.info().namespace_table.get(first) {
"wasi_unstable" => Some(WasiVersion::Snapshot0),
"wasi_snapshot_preview1" => Some(WasiVersion::Snapshot1),
_ => None,
2019-03-28 13:46:30 -07:00
}
} else {
// not all funcs have the same namespace: therefore it's not WASI
None
2019-03-28 13:46:30 -07:00
}
2019-03-28 12:19:23 -07:00
}