Skip to content

Commit 8410af4

Browse files
add basic component implementation
1 parent c9d86d9 commit 8410af4

8 files changed

Lines changed: 467 additions & 207 deletions

File tree

gabagool/src/binary_grammar.rs

Lines changed: 108 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,88 @@
1-
use crate::{parse_err, Error, Result};
2-
use std::result::Result as StdResult;
1+
use crate::{
2+
compiler::{self, ModuleCode},
3+
parse_err,
4+
parser::Parser,
5+
Error, Result,
6+
};
7+
use std::sync::Arc;
8+
9+
#[derive(Debug)]
10+
pub struct Component {
11+
pub(crate) parsed: ParsedComponent,
12+
}
13+
14+
impl Component {
15+
pub fn new(bytes: &[u8]) -> Result<Self> {
16+
let parsed = Parser::new(bytes).parse()?.try_as_component()?;
17+
18+
Ok(Self { parsed })
19+
}
20+
}
21+
22+
/// A parsed and compiled WASM module ready to be instantiated
23+
#[derive(Debug)]
24+
pub struct Module {
25+
pub(crate) code: Arc<ModuleCode>,
26+
27+
pub(crate) functions: Vec<Function>,
28+
pub(crate) tables: Vec<TableDef>,
29+
pub(crate) mems: Vec<MemoryType>,
30+
pub(crate) element_segments: Vec<ElementSegment>,
31+
pub(crate) globals: Vec<Global>,
32+
pub(crate) data_segments: Vec<DataSegment>,
33+
pub(crate) start: Option<u32>,
34+
pub(crate) import_declarations: Vec<ImportDeclaration>,
35+
pub(crate) exports: Vec<Export>,
36+
pub(crate) tags: Vec<Tag>,
37+
}
38+
39+
impl Module {
40+
pub fn new(bytes: &[u8]) -> Result<Self> {
41+
let module = Parser::new(bytes).parse()?.try_as_module()?;
42+
43+
let code = compiler::compile(&module);
44+
45+
Ok(Self {
46+
code: Arc::new(code),
47+
48+
functions: module.functions,
49+
tables: module.tables,
50+
mems: module.mems,
51+
element_segments: module.element_segments,
52+
globals: module.globals,
53+
data_segments: module.data_segments,
54+
start: module.start,
55+
import_declarations: module.import_declarations,
56+
exports: module.exports,
57+
tags: module.tags,
58+
})
59+
}
60+
61+
pub fn from_parsed(parsed: ParsedModule) -> Result<Self> {
62+
let code = compiler::compile(&parsed);
63+
Ok(Self {
64+
code: Arc::new(code),
65+
functions: parsed.functions,
66+
tables: parsed.tables,
67+
mems: parsed.mems,
68+
element_segments: parsed.element_segments,
69+
globals: parsed.globals,
70+
data_segments: parsed.data_segments,
71+
start: parsed.start,
72+
import_declarations: parsed.import_declarations,
73+
exports: parsed.exports,
74+
tags: parsed.tags,
75+
})
76+
}
77+
78+
pub fn import_declarations(&self) -> &[ImportDeclaration] {
79+
&self.import_declarations
80+
}
81+
82+
pub fn types(&self) -> &[SubType] {
83+
&self.code.types
84+
}
85+
}
386

487
// todo: should we box ParsedModule?
588
#[allow(clippy::large_enum_variant)]
@@ -9,19 +92,38 @@ pub enum Parsed {
992
Component(ParsedComponent),
1093
}
1194

95+
impl Parsed {
96+
pub fn try_as_module(self) -> Result<ParsedModule> {
97+
let out = match self {
98+
Self::Module(m) => m,
99+
Self::Component(_) => parse_err!("expected module"),
100+
};
101+
102+
Ok(out)
103+
}
104+
105+
pub fn try_as_component(self) -> Result<ParsedComponent> {
106+
let out = match self {
107+
Self::Module(_) => parse_err!("expected component"),
108+
Self::Component(c) => c,
109+
};
110+
111+
Ok(out)
112+
}
113+
}
114+
12115
#[derive(Debug, Clone)]
13116
pub struct ParsedComponent {
14117
pub sections: Vec<ComponentSection>,
15118
}
16119

17-
#[allow(clippy::large_enum_variant)]
18120
#[derive(Debug, Clone)]
19121
pub enum ComponentSection {
20-
CoreModule(ParsedModule),
122+
CoreModule(Box<ParsedModule>),
21123
CoreInstance(Vec<CoreInstance>),
22124
CoreType(Vec<CoreType>),
23125
Component(ParsedComponent),
24-
Instance(Vec<ComponentInstance>),
126+
Instance(Vec<ParsedComponentInstance>),
25127
Alias(Vec<Alias>),
26128
ComponentType(Vec<ComponentTypeDef>),
27129
Canonical(Vec<CanonicalDef>),
@@ -283,7 +385,7 @@ pub enum Alias {
283385
}
284386

285387
#[derive(Debug, Clone)]
286-
pub enum ComponentInstance {
388+
pub enum ParsedComponentInstance {
287389
Instantiate {
288390
component_idx: u32,
289391
args: Vec<ComponentInstantiateArg>,
@@ -445,115 +547,6 @@ pub struct ComponentExportDecl {
445547
pub desc: ExternDesc,
446548
}
447549

448-
#[derive(Debug, Clone, PartialEq)]
449-
pub enum ComponentValue {
450-
Bool(bool),
451-
S8(i8),
452-
U8(u8),
453-
S16(i16),
454-
U16(u16),
455-
S32(i32),
456-
U32(u32),
457-
S64(i64),
458-
U64(u64),
459-
F32(f32),
460-
F64(f64),
461-
Char(char),
462-
String(String),
463-
List(Vec<Self>),
464-
Record(Vec<(String, Self)>),
465-
Tuple(Vec<Self>),
466-
Variant(String, Option<Box<Self>>),
467-
Enum(String),
468-
Option(Option<Box<Self>>),
469-
Result(StdResult<Option<Box<Self>>, Option<Box<Self>>>),
470-
Flags(Vec<String>),
471-
}
472-
473-
impl From<bool> for ComponentValue {
474-
fn from(v: bool) -> Self {
475-
Self::Bool(v)
476-
}
477-
}
478-
479-
impl From<i8> for ComponentValue {
480-
fn from(v: i8) -> Self {
481-
Self::S8(v)
482-
}
483-
}
484-
485-
impl From<u8> for ComponentValue {
486-
fn from(v: u8) -> Self {
487-
Self::U8(v)
488-
}
489-
}
490-
491-
impl From<i16> for ComponentValue {
492-
fn from(v: i16) -> Self {
493-
Self::S16(v)
494-
}
495-
}
496-
497-
impl From<u16> for ComponentValue {
498-
fn from(v: u16) -> Self {
499-
Self::U16(v)
500-
}
501-
}
502-
503-
impl From<i32> for ComponentValue {
504-
fn from(v: i32) -> Self {
505-
Self::S32(v)
506-
}
507-
}
508-
509-
impl From<u32> for ComponentValue {
510-
fn from(v: u32) -> Self {
511-
Self::U32(v)
512-
}
513-
}
514-
515-
impl From<i64> for ComponentValue {
516-
fn from(v: i64) -> Self {
517-
Self::S64(v)
518-
}
519-
}
520-
521-
impl From<u64> for ComponentValue {
522-
fn from(v: u64) -> Self {
523-
Self::U64(v)
524-
}
525-
}
526-
527-
impl From<f32> for ComponentValue {
528-
fn from(v: f32) -> Self {
529-
Self::F32(v)
530-
}
531-
}
532-
533-
impl From<f64> for ComponentValue {
534-
fn from(v: f64) -> Self {
535-
Self::F64(v)
536-
}
537-
}
538-
539-
impl From<char> for ComponentValue {
540-
fn from(v: char) -> Self {
541-
Self::Char(v)
542-
}
543-
}
544-
545-
impl From<String> for ComponentValue {
546-
fn from(v: String) -> Self {
547-
Self::String(v)
548-
}
549-
}
550-
551-
impl From<&str> for ComponentValue {
552-
fn from(v: &str) -> Self {
553-
Self::String(v.to_string())
554-
}
555-
}
556-
557550
#[derive(Debug, Default, Clone)]
558551
pub struct ParsedModule {
559552
pub types: Vec<SubType>,

gabagool/src/execution_grammar.rs

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::fmt::Debug;
2-
use std::rc::Rc;
3-
41
use crate::binary_grammar::{Function, FunctionType, GlobalType, MemoryType, RefType, TableType};
2+
use std::rc::Rc;
3+
use std::result::Result as StdResult;
4+
use std::{collections::HashMap, fmt::Debug};
55

66
#[repr(u8)]
77
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -95,6 +95,115 @@ impl From<f64> for RawValue {
9595
}
9696
}
9797

98+
#[derive(Debug, Clone, PartialEq)]
99+
pub enum ComponentValue {
100+
Bool(bool),
101+
S8(i8),
102+
U8(u8),
103+
S16(i16),
104+
U16(u16),
105+
S32(i32),
106+
U32(u32),
107+
S64(i64),
108+
U64(u64),
109+
F32(f32),
110+
F64(f64),
111+
Char(char),
112+
String(String),
113+
List(Vec<Self>),
114+
Record(Vec<(String, Self)>),
115+
Tuple(Vec<Self>),
116+
Variant(String, Option<Box<Self>>),
117+
Enum(String),
118+
Option(Option<Box<Self>>),
119+
Result(StdResult<Option<Box<Self>>, Option<Box<Self>>>),
120+
Flags(Vec<String>),
121+
}
122+
123+
impl From<bool> for ComponentValue {
124+
fn from(v: bool) -> Self {
125+
Self::Bool(v)
126+
}
127+
}
128+
129+
impl From<i8> for ComponentValue {
130+
fn from(v: i8) -> Self {
131+
Self::S8(v)
132+
}
133+
}
134+
135+
impl From<u8> for ComponentValue {
136+
fn from(v: u8) -> Self {
137+
Self::U8(v)
138+
}
139+
}
140+
141+
impl From<i16> for ComponentValue {
142+
fn from(v: i16) -> Self {
143+
Self::S16(v)
144+
}
145+
}
146+
147+
impl From<u16> for ComponentValue {
148+
fn from(v: u16) -> Self {
149+
Self::U16(v)
150+
}
151+
}
152+
153+
impl From<i32> for ComponentValue {
154+
fn from(v: i32) -> Self {
155+
Self::S32(v)
156+
}
157+
}
158+
159+
impl From<u32> for ComponentValue {
160+
fn from(v: u32) -> Self {
161+
Self::U32(v)
162+
}
163+
}
164+
165+
impl From<i64> for ComponentValue {
166+
fn from(v: i64) -> Self {
167+
Self::S64(v)
168+
}
169+
}
170+
171+
impl From<u64> for ComponentValue {
172+
fn from(v: u64) -> Self {
173+
Self::U64(v)
174+
}
175+
}
176+
177+
impl From<f32> for ComponentValue {
178+
fn from(v: f32) -> Self {
179+
Self::F32(v)
180+
}
181+
}
182+
183+
impl From<f64> for ComponentValue {
184+
fn from(v: f64) -> Self {
185+
Self::F64(v)
186+
}
187+
}
188+
189+
impl From<char> for ComponentValue {
190+
fn from(v: char) -> Self {
191+
Self::Char(v)
192+
}
193+
}
194+
195+
impl From<String> for ComponentValue {
196+
fn from(v: String) -> Self {
197+
Self::String(v)
198+
}
199+
}
200+
201+
impl From<&str> for ComponentValue {
202+
fn from(v: &str) -> Self {
203+
Self::String(v.to_string())
204+
}
205+
}
206+
98207
/// A temporary struct that accumulates address mappings during instantiation
99208
#[derive(Debug, Clone, Default)]
100209
pub struct AddressMap {
@@ -170,3 +279,10 @@ pub struct ExportInstance {
170279
pub name: String,
171280
pub value: ExternalValue,
172281
}
282+
283+
#[derive(Debug)]
284+
pub struct InstantiatedComponent {
285+
// maps from name to func addr
286+
pub exports: HashMap<String, usize>,
287+
pub may_leave: bool,
288+
}

0 commit comments

Comments
 (0)