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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::collections::BTreeSet;
use wasmer::Module;
#[allow(dead_code)]
pub fn is_wasi_module(module: &Module) -> bool {
get_wasi_version(module, false).is_some()
}
#[derive(Debug, Clone, Copy, Eq)]
pub enum WasiVersion {
Snapshot0,
Snapshot1,
Latest,
}
impl WasiVersion {
pub const fn get_namespace_str(&self) -> &'static str {
match *self {
WasiVersion::Snapshot0 => SNAPSHOT0_NAMESPACE,
WasiVersion::Snapshot1 => SNAPSHOT1_NAMESPACE,
WasiVersion::Latest => SNAPSHOT1_NAMESPACE,
}
}
}
impl PartialEq<WasiVersion> for WasiVersion {
fn eq(&self, other: &Self) -> bool {
match (*self, *other) {
(Self::Snapshot1, Self::Latest)
| (Self::Latest, Self::Snapshot1)
| (Self::Latest, Self::Latest)
| (Self::Snapshot0, Self::Snapshot0)
| (Self::Snapshot1, Self::Snapshot1) => true,
_ => false,
}
}
}
impl PartialOrd for WasiVersion {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for WasiVersion {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
if self == other {
return std::cmp::Ordering::Equal;
}
match (*self, *other) {
(Self::Snapshot0, _) => std::cmp::Ordering::Less,
(Self::Snapshot1, Self::Snapshot0) | (Self::Latest, Self::Snapshot0) => {
std::cmp::Ordering::Greater
}
_ => unreachable!("Missing info about ordering of WasiVerison"),
}
}
}
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().functions().map(|f| f.module().to_owned());
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,
})
}
}
pub fn get_wasi_versions(module: &Module, strict: bool) -> Option<BTreeSet<WasiVersion>> {
let mut out = BTreeSet::new();
let imports = module.imports().functions().map(|f| f.module().to_owned());
let mut non_wasi_seen = false;
for ns in imports {
match ns.as_str() {
SNAPSHOT0_NAMESPACE => {
out.insert(WasiVersion::Snapshot0);
}
SNAPSHOT1_NAMESPACE => {
out.insert(WasiVersion::Snapshot1);
}
_ => {
non_wasi_seen = true;
}
}
}
if strict && non_wasi_seen {
None
} else {
Some(out)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn wasi_version_equality() {
assert_eq!(WasiVersion::Snapshot0, WasiVersion::Snapshot0);
assert_eq!(WasiVersion::Snapshot1, WasiVersion::Snapshot1);
assert_eq!(WasiVersion::Snapshot1, WasiVersion::Latest);
assert_eq!(WasiVersion::Latest, WasiVersion::Snapshot1);
assert_eq!(WasiVersion::Latest, WasiVersion::Latest);
assert!(WasiVersion::Snapshot0 != WasiVersion::Snapshot1);
assert!(WasiVersion::Snapshot1 != WasiVersion::Snapshot0);
assert!(WasiVersion::Snapshot0 != WasiVersion::Latest);
assert!(WasiVersion::Latest != WasiVersion::Snapshot0);
}
#[test]
fn wasi_version_ordering() {
assert!(WasiVersion::Snapshot0 <= WasiVersion::Snapshot0);
assert!(WasiVersion::Snapshot1 <= WasiVersion::Snapshot1);
assert!(WasiVersion::Latest <= WasiVersion::Latest);
assert!(WasiVersion::Snapshot0 >= WasiVersion::Snapshot0);
assert!(WasiVersion::Snapshot1 >= WasiVersion::Snapshot1);
assert!(WasiVersion::Latest >= WasiVersion::Latest);
assert!(WasiVersion::Snapshot0 < WasiVersion::Snapshot1);
assert!(WasiVersion::Snapshot1 > WasiVersion::Snapshot0);
assert!(WasiVersion::Snapshot0 < WasiVersion::Latest);
assert!(WasiVersion::Latest > WasiVersion::Snapshot0);
assert!(!(WasiVersion::Snapshot1 < WasiVersion::Latest));
assert!(!(WasiVersion::Latest > WasiVersion::Snapshot1));
}
}