wasper/loader/
types.rs

1use super::{error::Error, parser::Parser};
2use crate::binary::*;
3#[cfg(not(feature = "std"))]
4use crate::lib::*;
5
6impl<'a> Parser<'a> {
7    pub fn reftype(&mut self) -> Result<RefType, Error> {
8        if let Some(byte) = self.byte() {
9            FromByte::from_byte(byte).ok_or(Error::Expected(format!("reftype")))
10        } else {
11            Err(Error::UnexpectedEof(format!("reftype")))
12        }
13    }
14
15    pub fn valtype(&mut self) -> Result<ValType, Error> {
16        if let Some(byte) = self.byte() {
17            FromByte::from_byte(byte).ok_or(Error::Expected(format!("valtype")))
18        } else {
19            Err(Error::UnexpectedEof(format!("valtype")))
20        }
21    }
22
23    pub fn is_valtype(&self, byte: u8) -> bool {
24        byte == 0x7F
25            || byte == 0x7E
26            || byte == 0x7D
27            || byte == 0x7c
28            || byte == 0x7B
29            || byte == 0x70
30            || byte == 0x6F
31    }
32
33    pub fn result_types(&mut self) -> Result<ResultType, Error> {
34        Ok(ResultType(self.vec(Self::valtype)?))
35    }
36
37    pub fn functype(&mut self) -> Result<FuncType, Error> {
38        if let Some(byte) = self.byte() {
39            if byte != 0x60 {
40                return Err(Error::Expected(format!("0x60")));
41            }
42        }
43
44        Ok(FuncType(self.result_types()?, self.result_types()?))
45    }
46
47    pub fn limits(&mut self) -> Result<Limits, Error> {
48        match self.byte() {
49            Some(0x00) => Ok(Limits::Min(self.u32()?)),
50            Some(0x01) => Ok(Limits::MinMax(self.u32()?, self.u32()?)),
51            Some(_) => Err(Error::Expected(format!("limits"))),
52            None => Err(Error::UnexpectedEof(format!("limits"))),
53        }
54    }
55
56    pub fn memory(&mut self) -> Result<Memory, Error> {
57        Ok(Memory(self.limits()?))
58    }
59
60    pub fn table(&mut self) -> Result<Table, Error> {
61        Ok(Table {
62            reftype: self.reftype()?,
63            limits: self.limits()?,
64        })
65    }
66
67    pub fn mut_(&mut self) -> Result<Mut, Error> {
68        match self.byte() {
69            Some(0x00) => Ok(Mut::Const),
70            Some(0x01) => Ok(Mut::Var),
71            _ => Err(Error::Expected(format!("0x00 or 0x01"))),
72        }
73    }
74
75    pub fn globaltype(&mut self) -> Result<GlobalType, Error> {
76        Ok(GlobalType {
77            valtype: self.valtype()?,
78            mut_: self.mut_()?,
79        })
80    }
81}