summaryrefslogtreecommitdiff
path: root/crates/parser/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/parser/src')
-rw-r--r--crates/parser/src/error.rs25
-rw-r--r--crates/parser/src/lib.rs15
2 files changed, 36 insertions, 4 deletions
diff --git a/crates/parser/src/error.rs b/crates/parser/src/error.rs
index ee217c7..6fa9e25 100644
--- a/crates/parser/src/error.rs
+++ b/crates/parser/src/error.rs
@@ -1,3 +1,5 @@
+use core::fmt::Debug;
+
use alloc::string::{String, ToString};
use wasmparser::Encoding;
@@ -14,6 +16,29 @@ pub enum ParseError {
Other(String),
}
+impl Debug for ParseError {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ match self {
+ Self::InvalidType => write!(f, "invalid type"),
+ Self::UnsupportedSection(section) => write!(f, "unsupported section: {}", section),
+ Self::DuplicateSection(section) => write!(f, "duplicate section: {}", section),
+ Self::EmptySection(section) => write!(f, "empty section: {}", section),
+ Self::UnsupportedOperator(operator) => write!(f, "unsupported operator: {}", operator),
+ Self::ParseError { message, offset } => {
+ write!(f, "error parsing module: {} at offset {}", message, offset)
+ }
+ Self::InvalidEncoding(encoding) => write!(f, "invalid encoding: {:?}", encoding),
+ Self::InvalidLocalCount { expected, actual } => write!(
+ f,
+ "invalid local count: expected {}, actual {}",
+ expected, actual
+ ),
+ Self::EndNotReached => write!(f, "end of module not reached"),
+ Self::Other(message) => write!(f, "unknown error: {}", message),
+ }
+ }
+}
+
impl From<wasmparser::BinaryReaderError> for ParseError {
fn from(value: wasmparser::BinaryReaderError) -> Self {
Self::ParseError {
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 5b17fb8..6c49af0 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -18,7 +18,11 @@ use wasmparser::Validator;
pub struct Parser {}
impl Parser {
- pub fn parse_module_bytes(wasm: &[u8]) -> Result<TinyWasmModule> {
+ pub fn new() -> Self {
+ Self {}
+ }
+
+ pub fn parse_module_bytes(&self, wasm: &[u8]) -> Result<TinyWasmModule> {
let mut validator = Validator::new();
let mut reader = ModuleReader::new();
@@ -34,18 +38,21 @@ impl Parser {
}
#[cfg(feature = "std")]
- pub fn parse_module_file(path: impl AsRef<crate::std::path::Path>) -> Result<TinyWasmModule> {
+ pub fn parse_module_file(
+ &self,
+ path: impl AsRef<crate::std::path::Path>,
+ ) -> Result<TinyWasmModule> {
use alloc::format;
let f = crate::std::fs::File::open("log.txt").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)
+ self.parse_module_stream(&mut reader)
}
#[cfg(feature = "std")]
- pub fn parse_module_stream(mut stream: impl std::io::Read) -> Result<TinyWasmModule> {
+ pub fn parse_module_stream(&self, mut stream: impl std::io::Read) -> Result<TinyWasmModule> {
use alloc::format;
let mut validator = Validator::new();