Next Generation WASM Microkernel Operating System
1use crate::component::*;
2use crate::core;
3use crate::kw;
4use crate::parser::{Parse, Parser, Result};
5use crate::token::{Id, NameAnnotation, Span};
6use alloc::vec::Vec;
7
8/// A core WebAssembly module to be created as part of a component.
9///
10/// This is a member of the core module section.
11#[derive(Debug)]
12pub struct CoreModule<'a> {
13 /// Where this `core module` was defined.
14 pub span: Span,
15 /// An identifier that this module is resolved with (optionally) for name
16 /// resolution.
17 pub id: Option<Id<'a>>,
18 /// An optional name for this module stored in the custom `name` section.
19 pub name: Option<NameAnnotation<'a>>,
20 /// If present, inline export annotations which indicate names this
21 /// definition should be exported under.
22 pub exports: InlineExport<'a>,
23 /// What kind of module this is, be it an inline-defined or imported one.
24 pub kind: CoreModuleKind<'a>,
25}
26
27/// Possible ways to define a core module in the text format.
28#[derive(Debug)]
29pub enum CoreModuleKind<'a> {
30 /// A core module which is actually defined as an import
31 Import {
32 /// Where this core module is imported from
33 import: InlineImport<'a>,
34 /// The type that this core module will have.
35 ty: CoreTypeUse<'a, ModuleType<'a>>,
36 },
37
38 /// Modules that are defined inline.
39 Inline {
40 /// Fields in the core module.
41 fields: Vec<core::ModuleField<'a>>,
42 },
43}
44
45impl<'a> Parse<'a> for CoreModule<'a> {
46 fn parse(parser: Parser<'a>) -> Result<Self> {
47 parser.depth_check()?;
48
49 let span = parser.parse::<kw::core>()?.0;
50 parser.parse::<kw::module>()?;
51 let id = parser.parse()?;
52 let name = parser.parse()?;
53 let exports = parser.parse()?;
54
55 let kind = if let Some(import) = parser.parse()? {
56 CoreModuleKind::Import {
57 import,
58 ty: parser.parse()?,
59 }
60 } else {
61 let mut fields = Vec::new();
62 while !parser.is_empty() {
63 fields.push(parser.parens(|p| p.parse())?);
64 }
65 CoreModuleKind::Inline { fields }
66 };
67
68 Ok(Self {
69 span,
70 id,
71 name,
72 exports,
73 kind,
74 })
75 }
76}