1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use wasmer::{ExternType, Module};
#[allow(dead_code)]
pub fn is_wasi_module(module: &Module) -> bool {
get_wasi_version(module, false).is_some()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WasiVersion {
Snapshot0,
Snapshot1,
Latest,
}
const SNAPSHOT0_NAMESPACE: &str = "wasi_unstable";
const SNAPSHOT1_NAMESPACE: &str = "wasi_snapshot_preview1";
pub fn get_wasi_version(module: &Module, strict: bool) -> Option<WasiVersion> {
let mut imports = module.imports().filter_map(|extern_| match extern_.ty() {
ExternType::Function(_f) => Some(extern_.module().to_owned()),
_ => None,
});
if strict {
let first_module = imports.next()?;
if imports.all(|module| module == first_module) {
match first_module.as_str() {
SNAPSHOT0_NAMESPACE => Some(WasiVersion::Snapshot0),
SNAPSHOT1_NAMESPACE => Some(WasiVersion::Snapshot1),
_ => None,
}
} else {
None
}
} else {
imports.find_map(|module| match module.as_str() {
SNAPSHOT0_NAMESPACE => Some(WasiVersion::Snapshot0),
SNAPSHOT1_NAMESPACE => Some(WasiVersion::Snapshot1),
_ => None,
})
}
}