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
use crate::custom::IT_SECTION_NAME;
use crate::errors::WITParserError;
use crate::Result;
use walrus::IdsToIndices;
use wasmer_wit::ast::Interfaces;
use wasmer_core::Module as WasmerModule;
use std::borrow::Cow;
use std::path::Path;
pub fn extract_text_wit<P>(wasm_file_path: P) -> Result<String>
where
P: AsRef<Path>,
{
let module = walrus::ModuleConfig::new()
.parse_file(wasm_file_path)
.map_err(WITParserError::CorruptedWasmFile)?;
let raw_custom_section = extract_custom_section(&module)?;
let wit_section_bytes = raw_custom_section.as_ref();
let wit = extract_wit_from_bytes(wit_section_bytes)?;
Ok((&wit).to_string())
}
pub fn extract_wit_from_module(wasmer_module: &WasmerModule) -> Result<Interfaces<'_>> {
let wit_sections = wasmer_module
.custom_sections(IT_SECTION_NAME)
.ok_or(WITParserError::NoITSection)?;
if wit_sections.len() > 1 {
return Err(WITParserError::MultipleITSections);
}
extract_wit_from_bytes(&wit_sections[0])
}
pub fn extract_version_from_module(module: &walrus::Module) -> Result<semver::Version> {
let raw_custom_section = extract_custom_section(&module)?;
let wit_section_bytes = raw_custom_section.as_ref();
let wit = extract_wit_from_bytes(wit_section_bytes)?;
Ok(wit.version)
}
pub(crate) fn extract_wit_from_bytes(wit_section_bytes: &[u8]) -> Result<Interfaces<'_>> {
match wasmer_wit::decoders::binary::parse::<(&[u8], nom::error::ErrorKind)>(wit_section_bytes) {
Ok((remainder, wit)) if remainder.is_empty() => Ok(wit),
Ok(_) => Err(WITParserError::ITRemainderNotEmpty),
Err(e) => Err(WITParserError::CorruptedITSection(e.to_owned())),
}
}
pub(crate) fn extract_custom_section(module: &walrus::Module) -> Result<Cow<'_, [u8]>> {
let sections = module
.customs
.iter()
.filter(|(_, section)| section.name() == IT_SECTION_NAME)
.collect::<Vec<_>>();
if sections.is_empty() {
return Err(WITParserError::NoITSection);
}
if sections.len() > 1 {
return Err(WITParserError::MultipleITSections);
}
let default_ids = IdsToIndices::default();
Ok(sections[0].1.data(&default_ids))
}