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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
use crate::core::*;
use crate::parser::{Parse, Parser, Result};
use crate::token::{Id, Index, NameAnnotation, Span};
use crate::{annotation, kw};
pub use crate::core::resolve::Names;
/// A parsed WebAssembly core module.
#[derive(Debug)]
pub struct Module<'a> {
/// Where this `module` was defined
pub span: Span,
/// An optional identifier this module is known by
pub id: Option<Id<'a>>,
/// An optional `@name` annotation for this module
pub name: Option<NameAnnotation<'a>>,
/// What kind of module this was parsed as.
pub kind: ModuleKind<'a>,
}
/// The different kinds of ways to define a module.
#[derive(Debug)]
pub enum ModuleKind<'a> {
/// A module defined in the textual s-expression format.
Text(Vec<ModuleField<'a>>),
/// A module that had its raw binary bytes defined via the `binary`
/// directive.
Binary(Vec<&'a [u8]>),
}
impl<'a> Module<'a> {
/// Performs a name resolution pass on this [`Module`], resolving all
/// symbolic names to indices.
///
/// The WAT format contains a number of shorthands to make it easier to
/// write, such as inline exports, inline imports, inline type definitions,
/// etc. Additionally it allows using symbolic names such as `$foo` instead
/// of using indices. This module will postprocess an AST to remove all of
/// this syntactic sugar, preparing the AST for binary emission. This is
/// where expansion and name resolution happens.
///
/// This function will mutate the AST of this [`Module`] and replace all
/// [`Index`](crate::token::Index) arguments with `Index::Num`. This will
/// also expand inline exports/imports listed on fields and handle various
/// other shorthands of the text format.
///
/// If successful the AST was modified to be ready for binary encoding. A
/// [`Names`] structure is also returned so if you'd like to do your own
/// name lookups on the result you can do so as well.
///
/// # Errors
///
/// If an error happens during resolution, such a name resolution error or
/// items are found in the wrong order, then an error is returned.
pub fn resolve(&mut self) -> std::result::Result<Names<'a>, crate::Error> {
let names = match &mut self.kind {
ModuleKind::Text(fields) => crate::core::resolve::resolve(fields)?,
ModuleKind::Binary(_blobs) => Default::default(),
};
Ok(names)
}
/// Encodes this [`Module`] to its binary form.
///
/// This function will take the textual representation in [`Module`] and
/// perform all steps necessary to convert it to a binary WebAssembly
/// module, suitable for writing to a `*.wasm` file. This function may
/// internally modify the [`Module`], for example:
///
/// * Name resolution is performed to ensure that `Index::Id` isn't present
/// anywhere in the AST.
///
/// * Inline shorthands such as imports/exports/types are all expanded to be
/// dedicated fields of the module.
///
/// * Module fields may be shuffled around to preserve index ordering from
/// expansions.
///
/// After all of this expansion has happened the module will be converted to
/// its binary form and returned as a `Vec<u8>`. This is then suitable to
/// hand off to other wasm runtimes and such.
///
/// # Errors
///
/// This function can return an error for name resolution errors and other
/// expansion-related errors.
pub fn encode(&mut self) -> std::result::Result<Vec<u8>, crate::Error> {
self.resolve()?;
Ok(match &self.kind {
ModuleKind::Text(fields) => crate::core::binary::encode(&self.id, &self.name, fields),
ModuleKind::Binary(blobs) => blobs.iter().flat_map(|b| b.iter().cloned()).collect(),
})
}
pub(crate) fn validate(&self, parser: Parser<'_>) -> Result<()> {
let mut starts = 0;
if let ModuleKind::Text(fields) = &self.kind {
for item in fields.iter() {
if let ModuleField::Start(_) = item {
starts += 1;
}
}
}
if starts > 1 {
return Err(parser.error("multiple start sections found"));
}
Ok(())
}
}
impl<'a> Parse<'a> for Module<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let _r = parser.register_annotation("custom");
let _r = parser.register_annotation("producers");
let _r = parser.register_annotation("name");
let _r = parser.register_annotation("dylink.0");
let _r = parser.register_annotation("metadata.code.branch_hint");
let span = parser.parse::<kw::module>()?.0;
let id = parser.parse()?;
let name = parser.parse()?;
let kind = if parser.peek::<kw::binary>()? {
parser.parse::<kw::binary>()?;
let mut data = Vec::new();
while !parser.is_empty() {
data.push(parser.parse()?);
}
ModuleKind::Binary(data)
} else {
ModuleKind::Text(ModuleField::parse_remaining(parser)?)
};
Ok(Module {
span,
id,
name,
kind,
})
}
}
/// A listing of all possible fields that can make up a WebAssembly module.
#[allow(missing_docs)]
#[derive(Debug)]
pub enum ModuleField<'a> {
Type(Type<'a>),
Rec(Rec<'a>),
Import(Import<'a>),
Func(Func<'a>),
Table(Table<'a>),
Memory(Memory<'a>),
Global(Global<'a>),
Export(Export<'a>),
Start(Index<'a>),
Elem(Elem<'a>),
Data(Data<'a>),
Tag(Tag<'a>),
Custom(Custom<'a>),
}
impl<'a> ModuleField<'a> {
pub(crate) fn parse_remaining(parser: Parser<'a>) -> Result<Vec<ModuleField>> {
let mut fields = Vec::new();
while !parser.is_empty() {
fields.push(parser.parens(ModuleField::parse)?);
}
Ok(fields)
}
}
impl<'a> Parse<'a> for ModuleField<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.peek::<Type<'a>>()? {
return Ok(ModuleField::Type(parser.parse()?));
}
if parser.peek::<kw::rec>()? {
return Ok(ModuleField::Rec(parser.parse()?));
}
if parser.peek::<kw::import>()? {
return Ok(ModuleField::Import(parser.parse()?));
}
if parser.peek::<kw::func>()? {
return Ok(ModuleField::Func(parser.parse()?));
}
if parser.peek::<kw::table>()? {
return Ok(ModuleField::Table(parser.parse()?));
}
if parser.peek::<kw::memory>()? {
return Ok(ModuleField::Memory(parser.parse()?));
}
if parser.peek::<kw::global>()? {
return Ok(ModuleField::Global(parser.parse()?));
}
if parser.peek::<kw::export>()? {
return Ok(ModuleField::Export(parser.parse()?));
}
if parser.peek::<kw::start>()? {
parser.parse::<kw::start>()?;
return Ok(ModuleField::Start(parser.parse()?));
}
if parser.peek::<kw::elem>()? {
return Ok(ModuleField::Elem(parser.parse()?));
}
if parser.peek::<kw::data>()? {
return Ok(ModuleField::Data(parser.parse()?));
}
if parser.peek::<kw::tag>()? {
return Ok(ModuleField::Tag(parser.parse()?));
}
if parser.peek::<annotation::custom>()?
|| parser.peek::<annotation::producers>()?
|| parser.peek::<annotation::dylink_0>()?
{
return Ok(ModuleField::Custom(parser.parse()?));
}
Err(parser.error("expected valid module field"))
}
}