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
72
73
74
75
76
77
78
79
80
81
use wasmer_runtime_core::module::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 module_info = &module.info();
let mut imports = module_info.imported_functions.iter();
if strict {
let mut imports = imports.map(|(_, import_name)| import_name.namespace_index);
let first = imports.next()?;
if imports.all(|index| index == first) {
match module_info.namespace_table.get(first) {
SNAPSHOT0_NAMESPACE => Some(WasiVersion::Snapshot0),
SNAPSHOT1_NAMESPACE => Some(WasiVersion::Snapshot1),
_ => None,
}
} else {
None
}
} else {
let namespace_table = &module_info.namespace_table;
imports.find_map(|(_, import_name)| {
let namespace_index = import_name.namespace_index;
match namespace_table.get(namespace_index) {
SNAPSHOT0_NAMESPACE => Some(WasiVersion::Snapshot0),
SNAPSHOT1_NAMESPACE => Some(WasiVersion::Snapshot1),
_ => None,
}
})
}
}