Skip to content

Commit

Permalink
[wast] Add support for final types. (#1043)
Browse files Browse the repository at this point in the history
All types are final by default unless the sub keyword
is used. A type marked `(sub final)` will be final.
  • Loading branch information
jpages committed May 18, 2023
1 parent 286b880 commit 64ed105
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/wast/src/component/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ impl<'a> Expander<'a> {
name: None,
def: key.to_def(item.span),
parent: None,
final_type: None,
}));
let idx = Index::Id(id);
t.index = Some(idx);
Expand Down
23 changes: 19 additions & 4 deletions crates/wast/src/core/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,25 @@ impl Encode for RecOrType<'_> {

impl Encode for Type<'_> {
fn encode(&self, e: &mut Vec<u8>) {
if let Some(parent) = &self.parent {
e.push(0x50);
(1 as usize).encode(e);
parent.encode(e);
match (&self.parent, self.final_type) {
(Some(parent), Some(true)) => {
// Type is final with a supertype
e.push(0x4e);
e.push(0x01);
parent.encode(e);
}
(Some(parent), Some(false) | None) => {
// Type is not final and has a declared supertype
e.push(0x50);
e.push(0x01);
parent.encode(e);
}
(None, Some(false)) => {
// Sub was used without any declared supertype
e.push(0x50);
e.push(0x00);
}
(None, _) => {} // No supertype, sub wasn't used
}
match &self.def {
TypeDef::Func(func) => {
Expand Down
1 change: 1 addition & 0 deletions crates/wast/src/core/resolve/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl<'a> Expander<'a> {
name: None,
def: key.to_def(span),
parent: None,
final_type: None,
}));
let idx = Index::Id(id);
key.insert(self, idx);
Expand Down
18 changes: 15 additions & 3 deletions crates/wast/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ pub struct Type<'a> {
pub def: TypeDef<'a>,
/// The declared parent type of this definition.
pub parent: Option<Index<'a>>,
/// Whether this type is final or not. By default types are final.
pub final_type: Option<bool>,
}

impl<'a> Peek for Type<'a> {
Expand All @@ -741,19 +743,28 @@ impl<'a> Parse<'a> for Type<'a> {
let id = parser.parse()?;
let name = parser.parse()?;

let (parent, def) = if parser.peek2::<kw::sub>() {
let (parent, def, final_type) = if parser.peek2::<kw::sub>() {
parser.parens(|parser| {
parser.parse::<kw::sub>()?;

let final_type: Option<bool> =
if parser.peek::<kw::r#final>() {
parser.parse::<kw::r#final>()?;
Some(true)
} else {
Some(false)
};

let parent = if parser.peek::<Index<'a>>() {
parser.parse()?
} else {
None
};
let def = parser.parens(|parser| parser.parse())?;
Ok((parent, def))
Ok((parent, def, final_type))
})?
} else {
(None, parser.parens(|parser| parser.parse())?)
(None, parser.parens(|parser| parser.parse())?, None)
};

Ok(Type {
Expand All @@ -762,6 +773,7 @@ impl<'a> Parse<'a> for Type<'a> {
name,
def,
parent,
final_type,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/wast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ pub mod kw {
custom_keyword!(shared);
custom_keyword!(start);
custom_keyword!(sub);
custom_keyword!(r#final = "final");
custom_keyword!(table);
custom_keyword!(then);
custom_keyword!(r#try = "try");
Expand Down
3 changes: 3 additions & 0 deletions tests/local/gc/gc-rec-sub.wat
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
(rec
(type (sub $a (func)))
)
(rec
(type (sub final $a (func)))
)
(type (sub $a (func)))
)

0 comments on commit 64ed105

Please sign in to comment.