| 1 |
use crate::annotation; |
| 2 |
use crate::component::*; |
| 3 |
use crate::core::{self, Producers}; |
| 4 |
use crate::kw; |
| 5 |
use crate::parser::{Parse, Parser, Result}; |
| 6 |
use crate::token::Index; |
| 7 |
use crate::token::{Id, NameAnnotation, Span}; |
| 8 |
|
| 9 |
/// A parsed WebAssembly component module. |
| 10 |
#[derive(Debug)] |
| 11 |
#[non_exhaustive] |
| 12 |
pub struct Component<'a> { |
| 13 |
/// Where this `component` was defined |
| 14 |
pub span: Span, |
| 15 |
/// An optional identifier this component is known by |
| 16 |
pub id: Option<Id<'a>>, |
| 17 |
/// An optional `@name` annotation for this component |
| 18 |
pub name: Option<NameAnnotation<'a>>, |
| 19 |
/// What kind of component this was parsed as. |
| 20 |
pub kind: ComponentKind<'a>, |
| 21 |
} |
| 22 |
|
| 23 |
/// The different kinds of ways to define a component. |
| 24 |
#[derive(Debug)] |
| 25 |
pub enum ComponentKind<'a> { |
| 26 |
/// A component defined in the textual s-expression format. |
| 27 |
Text(Vec<ComponentField<'a>>), |
| 28 |
/// A component that had its raw binary bytes defined via the `binary` |
| 29 |
/// directive. |
| 30 |
Binary(Vec<&'a [u8]>), |
| 31 |
} |
| 32 |
|
| 33 |
impl<'a> Component<'a> { |
| 34 |
/// Performs a name resolution pass on this [`Component`], resolving all |
| 35 |
/// symbolic names to indices. |
| 36 |
/// |
| 37 |
/// The WAT format contains a number of shorthands to make it easier to |
| 38 |
/// write, such as inline exports, inline imports, inline type definitions, |
| 39 |
/// etc. Additionally it allows using symbolic names such as `$foo` instead |
| 40 |
/// of using indices. This module will postprocess an AST to remove all of |
| 41 |
/// this syntactic sugar, preparing the AST for binary emission. This is |
| 42 |
/// where expansion and name resolution happens. |
| 43 |
/// |
| 44 |
/// This function will mutate the AST of this [`Component`] and replace all |
| 45 |
/// [`Index`](crate::token::Index) arguments with `Index::Num`. This will |
| 46 |
/// also expand inline exports/imports listed on fields and handle various |
| 47 |
/// other shorthands of the text format. |
| 48 |
/// |
| 49 |
/// If successful the AST was modified to be ready for binary encoding. |
| 50 |
/// |
| 51 |
/// # Errors |
| 52 |
/// |
| 53 |
/// If an error happens during resolution, such a name resolution error or |
| 54 |
/// items are found in the wrong order, then an error is returned. |
| 55 |
pub fn resolve(&mut self) -> std::result::Result<(), crate::Error> { |
| 56 |
match &mut self.kind { |
| 57 |
ComponentKind::Text(fields) => { |
| 58 |
crate::component::expand::expand(fields); |
| 59 |
} |
| 60 |
ComponentKind::Binary(_) => {} |
| 61 |
} |
| 62 |
crate::component::resolve::resolve(self) |
| 63 |
} |
| 64 |
|
| 65 |
/// Encodes this [`Component`] to its binary form. |
| 66 |
/// |
| 67 |
/// This function will take the textual representation in [`Component`] and |
| 68 |
/// perform all steps necessary to convert it to a binary WebAssembly |
| 69 |
/// component, suitable for writing to a `*.wasm` file. This function may |
| 70 |
/// internally modify the [`Component`], for example: |
| 71 |
/// |
| 72 |
/// * Name resolution is performed to ensure that `Index::Id` isn't present |
| 73 |
/// anywhere in the AST. |
| 74 |
/// |
| 75 |
/// * Inline shorthands such as imports/exports/types are all expanded to be |
| 76 |
/// dedicated fields of the component. |
| 77 |
/// |
| 78 |
/// * Component fields may be shuffled around to preserve index ordering from |
| 79 |
/// expansions. |
| 80 |
/// |
| 81 |
/// After all of this expansion has happened the component will be converted to |
| 82 |
/// its binary form and returned as a `Vec<u8>`. This is then suitable to |
| 83 |
/// hand off to other wasm runtimes and such. |
| 84 |
/// |
| 85 |
/// # Errors |
| 86 |
/// |
| 87 |
/// This function can return an error for name resolution errors and other |
| 88 |
/// expansion-related errors. |
| 89 |
pub fn encode(&mut self) -> std::result::Result<Vec<u8>, crate::Error> { |
| 90 |
crate::core::EncodeOptions::default().encode_component(self) |
| 91 |
} |
| 92 |
|
| 93 |
pub(crate) fn parse_without_component_keyword( |
| 94 |
component_keyword_span: Span, |
| 95 |
parser: Parser<'a>, |
| 96 |
) -> Result<Self> { |
| 97 |
let id = parser.parse()?; |
| 98 |
let name = parser.parse()?; |
| 99 |
|
| 100 |
let kind = if parser.peek::<kw::binary>()? { |
| 101 |
parser.parse::<kw::binary>()?; |
| 102 |
let mut data = Vec::new(); |
| 103 |
while !parser.is_empty() { |
| 104 |
data.push(parser.parse()?); |
| 105 |
} |
| 106 |
ComponentKind::Binary(data) |
| 107 |
} else { |
| 108 |
ComponentKind::Text(ComponentField::parse_remaining(parser)?) |
| 109 |
}; |
| 110 |
Ok(Component { |
| 111 |
span: component_keyword_span, |
| 112 |
id, |
| 113 |
name, |
| 114 |
kind, |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl<'a> Parse<'a> for Component<'a> { |
| 120 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 121 |
parser.with_standard_annotations_registered(|parser| { |
| 122 |
let span = parser.parse::<kw::component>()?.0; |
| 123 |
Component::parse_without_component_keyword(span, parser) |
| 124 |
}) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/// A listing of all possible fields that can make up a WebAssembly component. |
| 129 |
#[allow(missing_docs)] |
| 130 |
#[derive(Debug)] |
| 131 |
pub enum ComponentField<'a> { |
| 132 |
CoreModule(CoreModule<'a>), |
| 133 |
CoreInstance(CoreInstance<'a>), |
| 134 |
CoreType(CoreType<'a>), |
| 135 |
CoreRec(core::Rec<'a>), |
| 136 |
Component(NestedComponent<'a>), |
| 137 |
Instance(Instance<'a>), |
| 138 |
Alias(Alias<'a>), |
| 139 |
Type(Type<'a>), |
| 140 |
CanonicalFunc(CanonicalFunc<'a>), |
| 141 |
CoreFunc(CoreFunc<'a>), // Supports inverted forms of other items |
| 142 |
Func(Func<'a>), // Supports inverted forms of other items |
| 143 |
Start(Start<'a>), |
| 144 |
Import(ComponentImport<'a>), |
| 145 |
Export(ComponentExport<'a>), |
| 146 |
Custom(Custom<'a>), |
| 147 |
Producers(Producers<'a>), |
| 148 |
} |
| 149 |
|
| 150 |
impl<'a> ComponentField<'a> { |
| 151 |
fn parse_remaining(parser: Parser<'a>) -> Result<Vec<ComponentField<'a>>> { |
| 152 |
let mut fields = Vec::new(); |
| 153 |
while !parser.is_empty() { |
| 154 |
fields.push(parser.parens(ComponentField::parse)?); |
| 155 |
} |
| 156 |
Ok(fields) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<'a> Parse<'a> for ComponentField<'a> { |
| 161 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 162 |
if parser.peek::<kw::core>()? { |
| 163 |
if parser.peek2::<kw::module>()? { |
| 164 |
return Ok(Self::CoreModule(parser.parse()?)); |
| 165 |
} |
| 166 |
if parser.peek2::<kw::instance>()? { |
| 167 |
return Ok(Self::CoreInstance(parser.parse()?)); |
| 168 |
} |
| 169 |
if parser.peek2::<kw::r#type>()? { |
| 170 |
return Ok(Self::CoreType(parser.parse()?)); |
| 171 |
} |
| 172 |
if parser.peek2::<kw::func>()? { |
| 173 |
return Ok(Self::CoreFunc(parser.parse()?)); |
| 174 |
} |
| 175 |
if parser.peek2::<kw::rec>()? { |
| 176 |
parser.parse::<kw::core>()?; |
| 177 |
return Ok(Self::CoreRec(parser.parse()?)); |
| 178 |
} |
| 179 |
} else { |
| 180 |
if parser.peek::<kw::component>()? { |
| 181 |
return Ok(Self::Component(parser.parse()?)); |
| 182 |
} |
| 183 |
if parser.peek::<kw::instance>()? { |
| 184 |
return Ok(Self::Instance(parser.parse()?)); |
| 185 |
} |
| 186 |
if parser.peek::<kw::alias>()? { |
| 187 |
return Ok(Self::Alias(parser.parse()?)); |
| 188 |
} |
| 189 |
if parser.peek::<kw::r#type>()? { |
| 190 |
return Ok(Self::Type(Type::parse_maybe_with_inline_exports(parser)?)); |
| 191 |
} |
| 192 |
if parser.peek::<kw::import>()? { |
| 193 |
return Ok(Self::Import(parser.parse()?)); |
| 194 |
} |
| 195 |
if parser.peek::<kw::func>()? { |
| 196 |
return Ok(Self::Func(parser.parse()?)); |
| 197 |
} |
| 198 |
if parser.peek::<kw::export>()? { |
| 199 |
return Ok(Self::Export(parser.parse()?)); |
| 200 |
} |
| 201 |
if parser.peek::<kw::start>()? { |
| 202 |
return Ok(Self::Start(parser.parse()?)); |
| 203 |
} |
| 204 |
if parser.peek::<kw::canon>()? { |
| 205 |
return Ok(Self::CanonicalFunc(parser.parse()?)); |
| 206 |
} |
| 207 |
if parser.peek::<annotation::custom>()? { |
| 208 |
return Ok(Self::Custom(parser.parse()?)); |
| 209 |
} |
| 210 |
if parser.peek::<annotation::producers>()? { |
| 211 |
return Ok(Self::Producers(parser.parse()?)); |
| 212 |
} |
| 213 |
} |
| 214 |
Err(parser.error("expected valid component field")) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/// A function to call at instantiation time. |
| 219 |
#[derive(Debug)] |
| 220 |
pub struct Start<'a> { |
| 221 |
/// The function to call. |
| 222 |
pub func: Index<'a>, |
| 223 |
/// The arguments to pass to the function. |
| 224 |
pub args: Vec<ItemRef<'a, kw::value>>, |
| 225 |
/// Names of the result values. |
| 226 |
pub results: Vec<Option<Id<'a>>>, |
| 227 |
} |
| 228 |
|
| 229 |
impl<'a> Parse<'a> for Start<'a> { |
| 230 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 231 |
parser.parse::<kw::start>()?; |
| 232 |
let func = parser.parse()?; |
| 233 |
let mut args = Vec::new(); |
| 234 |
while !parser.is_empty() && !parser.peek2::<kw::result>()? { |
| 235 |
args.push(parser.parens(|parser| parser.parse())?); |
| 236 |
} |
| 237 |
|
| 238 |
let mut results = Vec::new(); |
| 239 |
while !parser.is_empty() && parser.peek2::<kw::result>()? { |
| 240 |
results.push(parser.parens(|parser| { |
| 241 |
parser.parse::<kw::result>()?; |
| 242 |
parser.parens(|parser| { |
| 243 |
parser.parse::<kw::value>()?; |
| 244 |
parser.parse() |
| 245 |
}) |
| 246 |
})?); |
| 247 |
} |
| 248 |
|
| 249 |
Ok(Start { |
| 250 |
func, |
| 251 |
args, |
| 252 |
results, |
| 253 |
}) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/// A nested WebAssembly component. |
| 258 |
#[derive(Debug)] |
| 259 |
pub struct NestedComponent<'a> { |
| 260 |
/// Where this `component` was defined |
| 261 |
pub span: Span, |
| 262 |
/// An optional identifier this component is known by |
| 263 |
pub id: Option<Id<'a>>, |
| 264 |
/// An optional `@name` annotation for this component |
| 265 |
pub name: Option<NameAnnotation<'a>>, |
| 266 |
/// If present, inline export annotations which indicate names this |
| 267 |
/// definition should be exported under. |
| 268 |
pub exports: InlineExport<'a>, |
| 269 |
/// What kind of component this was parsed as. |
| 270 |
pub kind: NestedComponentKind<'a>, |
| 271 |
} |
| 272 |
|
| 273 |
/// The different kinds of ways to define a nested component. |
| 274 |
#[derive(Debug)] |
| 275 |
pub enum NestedComponentKind<'a> { |
| 276 |
/// This is actually an inline import of a component |
| 277 |
Import { |
| 278 |
/// The information about where this is being imported from. |
| 279 |
import: InlineImport<'a>, |
| 280 |
/// The type of component being imported. |
| 281 |
ty: ComponentTypeUse<'a, ComponentType<'a>>, |
| 282 |
}, |
| 283 |
/// The component is defined inline as a local definition with its fields |
| 284 |
/// listed here. |
| 285 |
Inline(Vec<ComponentField<'a>>), |
| 286 |
} |
| 287 |
|
| 288 |
impl<'a> Parse<'a> for NestedComponent<'a> { |
| 289 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 290 |
parser.depth_check()?; |
| 291 |
|
| 292 |
let span = parser.parse::<kw::component>()?.0; |
| 293 |
let id = parser.parse()?; |
| 294 |
let name = parser.parse()?; |
| 295 |
let exports = parser.parse()?; |
| 296 |
|
| 297 |
let kind = if let Some(import) = parser.parse()? { |
| 298 |
NestedComponentKind::Import { |
| 299 |
import, |
| 300 |
ty: parser.parse()?, |
| 301 |
} |
| 302 |
} else { |
| 303 |
let mut fields = Vec::new(); |
| 304 |
while !parser.is_empty() { |
| 305 |
fields.push(parser.parens(|p| p.parse())?); |
| 306 |
} |
| 307 |
NestedComponentKind::Inline(fields) |
| 308 |
}; |
| 309 |
|
| 310 |
Ok(NestedComponent { |
| 311 |
span, |
| 312 |
id, |
| 313 |
name, |
| 314 |
exports, |
| 315 |
kind, |
| 316 |
}) |
| 317 |
} |
| 318 |
} |