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
use crate::ast::{self, kw};
use wast::parser::{Parse, Parser, Result};

/// A `*.wit` file in its entirety.
///
/// This is typically what you're parsing at the top-level.
pub struct Wit<'a> {
    /// The module that this `*.wit` file contained.
    pub module: Module<'a>,
}

impl<'a> Parse<'a> for Wit<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        let module = parser.parens(|parser| parser.parse())?;
        Ok(Wit { module })
    }
}

/// A WebAssembly interface types-enhanced module.
pub struct Module<'a> {
    /// The core WebAssembly module which doesn't use interface types.
    pub core: wast::Module<'a>,
    /// The various `@interface` adapter directives representing the wasm
    /// interface types of this module.
    pub adapters: Vec<Adapter<'a>>,
}

impl Module<'_> {
    /// Encodes this `Module` into its binary form.
    pub fn encode(&mut self) -> std::result::Result<Vec<u8>, wast::Error> {
        let names = self.core.resolve()?;
        let core = match &self.core.kind {
            wast::ModuleKind::Text(list) => &list[..],
            wast::ModuleKind::Binary(_) => &[],
        };
        crate::resolve::resolve(core, &mut self.adapters, &names)?;
        let mut core = self.core.encode()?;
        crate::binary::append(&self.adapters, &mut core);
        Ok(core)
    }
}

impl<'a> Parse<'a> for Module<'a> {
    fn parse(parser: Parser<'a>) -> Result<Module<'a>> {
        let span = parser.parse::<kw::module>()?.0;
        let name = parser.parse()?;
        let mut fields = Vec::new();
        let mut adapters = Vec::new();
        while !parser.is_empty() {
            parser.parens(|parser| {
                if parser.peek::<ast::AtInterface>() {
                    adapters.push(parser.parse()?);
                } else {
                    fields.push(parser.parse()?);
                }
                Ok(())
            })?;
        }
        Ok(Module {
            core: wast::Module {
                span,
                name,
                kind: wast::ModuleKind::Text(fields),
            },
            adapters,
        })
    }
}

/// List of possible `@interface` adapters that can be listed in a module.
pub enum Adapter<'a> {
    /// An interface type definition (function signature).
    Type(ast::Type<'a>),
    /// An import definition using interface types as a function signature.
    Import(ast::Import<'a>),
    /// An export using wasm interface types as a function signature.
    Export(ast::Export<'a>),
    /// An adapter function using wasm interface types.
    Func(ast::Func<'a>),
    /// A connection between a core wasm import and an inline defined function.
    Implement(ast::Implement<'a>),
}

impl<'a> Parse<'a> for Adapter<'a> {
    fn parse(parser: Parser<'a>) -> Result<Adapter<'a>> {
        parser.parse::<ast::AtInterface>()?;
        let mut l = parser.lookahead1();
        if l.peek::<kw::r#type>() {
            return Ok(Adapter::Type(parser.parse()?));
        }
        if l.peek::<kw::import>() {
            return Ok(Adapter::Import(parser.parse()?));
        }
        if l.peek::<kw::export>() {
            return Ok(Adapter::Export(parser.parse()?));
        }
        if l.peek::<kw::func>() {
            return Ok(Adapter::Func(parser.parse()?));
        }
        if l.peek::<kw::implement>() {
            return Ok(Adapter::Implement(parser.parse()?));
        }
        Err(l.error())
    }
}