diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs index a58ad5b007..a35104127a 100644 --- a/crates/wast/src/component/expand.rs +++ b/crates/wast/src/component/expand.rs @@ -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); diff --git a/crates/wast/src/core/binary.rs b/crates/wast/src/core/binary.rs index 5ae4752349..80f0080c5a 100644 --- a/crates/wast/src/core/binary.rs +++ b/crates/wast/src/core/binary.rs @@ -173,10 +173,25 @@ impl Encode for RecOrType<'_> { impl Encode for Type<'_> { fn encode(&self, e: &mut Vec) { - 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) => { diff --git a/crates/wast/src/core/resolve/types.rs b/crates/wast/src/core/resolve/types.rs index b3cc76d1f9..aaf2988c18 100644 --- a/crates/wast/src/core/resolve/types.rs +++ b/crates/wast/src/core/resolve/types.rs @@ -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); diff --git a/crates/wast/src/core/types.rs b/crates/wast/src/core/types.rs index 9abb906806..5cf7beccaa 100644 --- a/crates/wast/src/core/types.rs +++ b/crates/wast/src/core/types.rs @@ -724,6 +724,8 @@ pub struct Type<'a> { pub def: TypeDef<'a>, /// The declared parent type of this definition. pub parent: Option>, + /// Whether this type is final or not. By default types are final. + pub final_type: Option, } impl<'a> Peek for Type<'a> { @@ -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::() { + let (parent, def, final_type) = if parser.peek2::() { parser.parens(|parser| { parser.parse::()?; + + let final_type: Option = + if parser.peek::() { + parser.parse::()?; + Some(true) + } else { + Some(false) + }; + let parent = if parser.peek::>() { 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 { @@ -762,6 +773,7 @@ impl<'a> Parse<'a> for Type<'a> { name, def, parent, + final_type, }) } } diff --git a/crates/wast/src/lib.rs b/crates/wast/src/lib.rs index 90142d8a49..2952ecd6fa 100644 --- a/crates/wast/src/lib.rs +++ b/crates/wast/src/lib.rs @@ -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"); diff --git a/tests/local/gc/gc-rec-sub.wat b/tests/local/gc/gc-rec-sub.wat index 527f3abfe2..cfea4f732a 100644 --- a/tests/local/gc/gc-rec-sub.wat +++ b/tests/local/gc/gc-rec-sub.wat @@ -35,5 +35,8 @@ (rec (type (sub $a (func))) ) + (rec + (type (sub final $a (func))) + ) (type (sub $a (func))) )