-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlib.rs
158 lines (137 loc) · 4.92 KB
/
lib.rs
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
#![no_std]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables))
))]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
#![forbid(unsafe_code)]
//! See [`tinywasm`](https://docs.rs/tinywasm) for documentation.
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
// log for logging (optional).
#[cfg(feature = "logging")]
#[allow(clippy::single_component_path_imports, unused_imports)]
use log;
// noop fallback if logging is disabled.
#[cfg(not(feature = "logging"))]
#[allow(unused_imports, unused_macros)]
pub(crate) mod log {
macro_rules! debug ( ($($tt:tt)*) => {{}} );
macro_rules! info ( ($($tt:tt)*) => {{}} );
macro_rules! error ( ($($tt:tt)*) => {{}} );
pub(crate) use debug;
pub(crate) use error;
pub(crate) use info;
}
mod conversion;
mod error;
mod module;
mod visit;
pub use error::*;
use module::ModuleReader;
use wasmparser::{Validator, WasmFeaturesInflated};
pub use tinywasm_types::TinyWasmModule;
/// A WebAssembly parser
#[derive(Default, Debug)]
pub struct Parser {}
impl Parser {
/// Create a new parser instance
pub fn new() -> Self {
Self {}
}
fn create_validator() -> Validator {
let features = WasmFeaturesInflated {
bulk_memory: true,
floats: true,
multi_value: true,
mutable_global: true,
reference_types: true,
sign_extension: true,
saturating_float_to_int: true,
function_references: true,
tail_call: true,
multi_memory: true,
simd: true,
memory64: true,
custom_page_sizes: true,
extended_const: false,
wide_arithmetic: false,
gc_types: true,
stack_switching: false,
component_model: false,
exceptions: false,
gc: false,
memory_control: false,
relaxed_simd: false,
threads: false,
shared_everything_threads: false,
legacy_exceptions: false,
cm_async: false,
cm_async_builtins: false,
cm_async_stackful: false,
cm_nested_names: false,
cm_values: false,
cm_error_context: false,
};
Validator::new_with_features(features.into())
}
/// Parse a [`TinyWasmModule`] from bytes
pub fn parse_module_bytes(&self, wasm: impl AsRef<[u8]>) -> Result<TinyWasmModule> {
let wasm = wasm.as_ref();
let mut validator = Self::create_validator();
let mut reader = ModuleReader::new();
for payload in wasmparser::Parser::new(0).parse_all(wasm) {
reader.process_payload(payload?, &mut validator)?;
}
if !reader.end_reached {
return Err(ParseError::EndNotReached);
}
reader.into_module()
}
#[cfg(feature = "std")]
/// Parse a [`TinyWasmModule`] from a file. Requires `std` feature.
pub fn parse_module_file(&self, path: impl AsRef<crate::std::path::Path> + Clone) -> Result<TinyWasmModule> {
use alloc::format;
let f = crate::std::fs::File::open(path.clone())
.map_err(|e| ParseError::Other(format!("Error opening file {:?}: {}", path.as_ref(), e)))?;
let mut reader = crate::std::io::BufReader::new(f);
self.parse_module_stream(&mut reader)
}
#[cfg(feature = "std")]
/// Parse a [`TinyWasmModule`] from a stream. Requires `std` feature.
pub fn parse_module_stream(&self, mut stream: impl std::io::Read) -> Result<TinyWasmModule> {
use alloc::format;
let mut validator = Self::create_validator();
let mut reader = ModuleReader::new();
let mut buffer = alloc::vec::Vec::new();
let mut parser = wasmparser::Parser::new(0);
let mut eof = false;
loop {
match parser.parse(&buffer, eof)? {
wasmparser::Chunk::NeedMoreData(hint) => {
let len = buffer.len();
buffer.extend((0..hint).map(|_| 0u8));
let read_bytes = stream
.read(&mut buffer[len..])
.map_err(|e| ParseError::Other(format!("Error reading from stream: {e}")))?;
buffer.truncate(len + read_bytes);
eof = read_bytes == 0;
}
wasmparser::Chunk::Parsed { consumed, payload } => {
reader.process_payload(payload, &mut validator)?;
buffer.drain(..consumed);
if eof || reader.end_reached {
return reader.into_module();
}
}
};
}
}
}
impl TryFrom<ModuleReader> for TinyWasmModule {
type Error = ParseError;
fn try_from(reader: ModuleReader) -> Result<Self> {
reader.into_module()
}
}