feat(interface-types) Introduce the record type.

This patch updates the `Type` type to be an enum with 2 variants:
`Function` and `Record`, resp. to represent:

1. `(@interface type (func (param i32 i32) (result string)))`
2. `(@interface type (record string i32))`

This patch updates the binary encoder and decoder, along with the WAT
encoder and decoder.
This commit is contained in:
Ivan Enderlin
2020-03-24 16:29:29 +01:00
parent 63f824a240
commit c3c6fcbfdd
5 changed files with 143 additions and 32 deletions

View File

@ -112,6 +112,19 @@ where
}
}
/// Encode a `TypeKind` into bytes.
impl<W> ToBytes<W> for TypeKind
where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
match self {
TypeKind::Function => 0x00_u8.to_bytes(writer),
TypeKind::Record => 0x01_u8.to_bytes(writer),
}
}
}
/// Encode an `InterfaceKind` into bytes.
impl<W> ToBytes<W> for InterfaceKind
where
@ -136,8 +149,18 @@ where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
self.inputs.to_bytes(writer)?;
self.outputs.to_bytes(writer)?;
match self {
Type::Function { inputs, outputs } => {
TypeKind::Function.to_bytes(writer)?;
inputs.to_bytes(writer)?;
outputs.to_bytes(writer)?;
}
Type::Record { fields } => {
TypeKind::Record.to_bytes(writer)?;
fields.to_bytes(writer)?;
}
}
Ok(())
}