feat(interface-types) Forwarded adapters have been removed.

This commit is contained in:
Ivan Enderlin 2020-02-24 15:56:11 +01:00
parent 1f2a5640a6
commit 165a8ca585
6 changed files with 6 additions and 158 deletions

View File

@ -175,13 +175,6 @@ pub enum Adapter<'input> {
},
}
/// Represented a forwarded export.
#[derive(PartialEq, Debug)]
pub struct Forward<'input> {
/// The forwarded export name.
pub name: &'input str,
}
/// Represents a set of interfaces, i.e. it entirely describes a WIT
/// definition.
#[derive(PartialEq, Default, Debug)]
@ -197,7 +190,4 @@ pub struct Interfaces<'input> {
/// All the adapters.
pub adapters: Vec<Adapter<'input>>,
/// All the forwarded functions.
pub forwards: Vec<Forward<'input>>,
}

View File

@ -379,23 +379,6 @@ fn adapters<'input, E: ParseError<&'input [u8]>>(
Ok((input, adapters))
}
/// Parse a list of forwarded exports.
fn forwards<'input, E: ParseError<&'input [u8]>>(
mut input: &'input [u8],
) -> IResult<&'input [u8], Vec<Forward>, E> {
consume!((input, number_of_forwards) = uleb(input)?);
let mut forwards = Vec::with_capacity(number_of_forwards as usize);
for _ in 0..number_of_forwards {
consume!((input, forward_name) = string(input)?);
forwards.push(Forward { name: forward_name });
}
Ok((input, forwards))
}
/// Parse complete interfaces.
fn interfaces<'input, E: ParseError<&'input [u8]>>(
bytes: &'input [u8],
@ -406,7 +389,6 @@ fn interfaces<'input, E: ParseError<&'input [u8]>>(
consume!((input, types) = types(input)?);
consume!((input, imports) = imports(input)?);
consume!((input, adapters) = adapters(input)?);
consume!((input, forwards) = forwards(input)?);
Ok((
input,
@ -415,7 +397,6 @@ fn interfaces<'input, E: ParseError<&'input [u8]>>(
types,
imports,
adapters,
forwards,
},
))
}
@ -474,9 +455,6 @@ fn interfaces<'input, E: ParseError<&'input [u8]>>(
/// 0x02, // S32
/// 0x01, // list of 1 item
/// 0x00, 0x01, // ArgumentGet { index: 1 }
/// 0x01, // 1 adapter
/// 0x01, // string of 1 byte
/// 0x61, // "a"
/// ];
/// let output = Ok((
/// &[] as &[u8],
@ -504,7 +482,6 @@ fn interfaces<'input, E: ParseError<&'input [u8]>>(
/// output_types: vec![InterfaceType::S32],
/// instructions: vec![Instruction::ArgumentGet { index: 1 }],
/// }],
/// forwards: vec![Forward { name: "a" }],
/// },
/// ));
///
@ -875,23 +852,6 @@ mod tests {
assert_eq!(adapters::<()>(input), output);
}
#[test]
fn test_forwards() {
let input = &[
0x02, // 2 adapters
0x01, // string of 1 byte
0x61, // "a"
0x02, // string of 2 bytes
0x62, 0x63, // "b", "c"
];
let output = Ok((
&[] as &[u8],
vec![Forward { name: "a" }, Forward { name: "bc" }],
));
assert_eq!(forwards::<()>(input), output);
}
#[test]
fn test_parse() {
let input = &[
@ -934,9 +894,6 @@ mod tests {
0x0c, // I32
0x01, // list of 1 item
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, // 1 adapter
0x01, // string of 1 byte
0x61, // "a"
];
let output = Ok((
&[] as &[u8],
@ -964,7 +921,6 @@ mod tests {
output_types: vec![InterfaceType::I32],
instructions: vec![Instruction::ArgumentGet { index: 1 }],
}],
forwards: vec![Forward { name: "a" }],
},
));

View File

@ -15,7 +15,6 @@ mod keyword {
// New keywords.
custom_keyword!(adapt);
custom_keyword!(forward);
// New types.
custom_keyword!(s8);
@ -288,7 +287,6 @@ enum Interface<'a> {
Type(Type<'a>),
Import(Import<'a>),
Adapter(Adapter<'a>),
Forward(Forward<'a>),
}
impl<'a> Parse<'a> for Interface<'a> {
@ -307,8 +305,6 @@ impl<'a> Parse<'a> for Interface<'a> {
Ok(Interface::Import(parser.parse()?))
} else if lookahead.peek::<keyword::adapt>() {
Ok(Interface::Adapter(parser.parse()?))
} else if lookahead.peek::<keyword::forward>() {
Ok(Interface::Forward(parser.parse()?))
} else {
Err(lookahead.error())
}
@ -430,20 +426,6 @@ impl<'a> Parse<'a> for Adapter<'a> {
}
}
impl<'a> Parse<'a> for Forward<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<keyword::forward>()?;
let name = parser.parens(|parser| {
parser.parse::<keyword::export>()?;
Ok(parser.parse()?)
})?;
Ok(Forward { name })
}
}
impl<'a> Parse<'a> for Interfaces<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut interfaces: Interfaces = Default::default();
@ -456,7 +438,6 @@ impl<'a> Parse<'a> for Interfaces<'a> {
Interface::Type(ty) => interfaces.types.push(ty),
Interface::Import(import) => interfaces.imports.push(import),
Interface::Adapter(adapter) => interfaces.adapters.push(adapter),
Interface::Forward(forward) => interfaces.forwards.push(forward),
}
}
@ -494,9 +475,7 @@ impl<'a> Parse<'a> for Interfaces<'a> {
/// arg.get 42)
///
/// (@interface adapt (export "bar")
/// arg.get 42)
///
/// (@interface forward (export "main"))"#,
/// arg.get 42)"#,
/// )
/// .unwrap();
/// let output = Interfaces {
@ -542,7 +521,6 @@ impl<'a> Parse<'a> for Interfaces<'a> {
/// instructions: vec![Instruction::ArgumentGet { index: 42 }],
/// },
/// ],
/// forwards: vec![Forward { name: "main" }],
/// };
///
/// assert_eq!(parse(&input).unwrap(), output);
@ -826,13 +804,6 @@ mod tests {
assert_eq!(parser::parse::<Interface>(&input).unwrap(), output);
}
#[test]
fn test_forward() {
let input = buffer(r#"(@interface forward (export "foo"))"#);
let output = Interface::Forward(Forward { name: "foo" });
assert_eq!(parser::parse::<Interface>(&input).unwrap(), output);
}
#[test]
fn test_interfaces() {
@ -852,9 +823,7 @@ mod tests {
arg.get 42)
(@interface adapt (export "bar")
arg.get 42)
(@interface forward (export "main"))"#,
arg.get 42)"#,
);
let output = Interfaces {
exports: vec![
@ -899,7 +868,6 @@ mod tests {
instructions: vec![Instruction::ArgumentGet { index: 42 }],
},
],
forwards: vec![Forward { name: "main" }],
};
assert_eq!(parser::parse::<Interfaces>(&input).unwrap(), output);

View File

@ -1,7 +1,7 @@
//! Writes the AST into bytes representing WIT with its binary format.
use crate::{
ast::{Adapter, AdapterKind, Export, Forward, Import, InterfaceType, Interfaces, Type},
ast::{Adapter, AdapterKind, Export, Import, InterfaceType, Interfaces, Type},
interpreter::Instruction,
};
use std::io::{self, Write};
@ -219,18 +219,6 @@ where
}
}
/// Encode an `Forward` into bytes.
///
/// Decoder is `decoders::binary::forwards`.
impl<W> ToBytes<W> for Forward<'_>
where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
self.name.to_bytes(writer)
}
}
/// Encode an `Interfaces` into bytes.
///
/// Decoder is `decoders::binary::parse`.
@ -243,7 +231,6 @@ where
self.types.to_bytes(writer)?;
self.imports.to_bytes(writer)?;
self.adapters.to_bytes(writer)?;
self.forwards.to_bytes(writer)?;
Ok(())
}
@ -575,18 +562,6 @@ mod tests {
);
}
#[test]
fn test_forward() {
assert_to_bytes!(
Forward { name: "ab" },
&[
0x02, // string of length 2
0x61, // "a"
0x62, // "b"
]
);
}
#[test]
fn test_interfaces() {
assert_to_bytes!(
@ -614,7 +589,6 @@ mod tests {
output_types: vec![InterfaceType::I32],
instructions: vec![Instruction::ArgumentGet { index: 1 }],
}],
forwards: vec![Forward { name: "a" }],
},
&[
0x01, // 1 export
@ -656,9 +630,6 @@ mod tests {
0x0c, // I32
0x01, // list of 1 item
0x00, 0x01, // ArgumentGet { index: 1 }
0x01, // 1 adapter
0x01, // string of 1 byte
0x61, // "a"
]
);
}

View File

@ -53,7 +53,6 @@
//! instructions: vec![Instruction::ArgumentGet { index: 42 }],
//! },
//! ],
//! forwards: vec![Forward { name: "main" }],
//! })
//! .to_string();
//! let output = r#";; Interfaces
@ -79,17 +78,14 @@
//!
//! ;; Interface, Adapter bar
//! (@interface adapt (export "bar")
//! arg.get 42)
//!
//! ;; Interface, Forward main
//! (@interface forward (export "main"))"#;
//! arg.get 42)"#;
//!
//! assert_eq!(input, output);
//! # }
//! ```
use crate::{
ast::{Adapter, Export, Forward, Import, InterfaceType, Interfaces, Type},
ast::{Adapter, Export, Import, InterfaceType, Interfaces, Type},
interpreter::Instruction,
};
use std::string::ToString;
@ -283,16 +279,6 @@ impl<'input> ToString for &Adapter<'input> {
}
}
/// Encode a `Forward` into a string.
impl<'input> ToString for &Forward<'input> {
fn to_string(&self) -> String {
format!(
r#"(@interface forward (export "{name}"))"#,
name = self.name,
)
}
}
/// Encode an `Interfaces` into a string.
impl<'input> ToString for &Interfaces<'input> {
fn to_string(&self) -> String {
@ -348,20 +334,10 @@ impl<'input> ToString for &Interfaces<'input> {
accumulator
});
let forwards = self
.forwards
.iter()
.fold(String::new(), |mut accumulator, forward| {
accumulator.push_str(&format!("\n\n;; Interface, Forward {}\n", forward.name));
accumulator.push_str(&forward.to_string());
accumulator
});
output.push_str(&exports);
output.push_str(&types);
output.push_str(&imports);
output.push_str(&adapters);
output.push_str(&forwards);
output
}
@ -629,14 +605,6 @@ mod tests {
assert_eq!(inputs, outputs);
}
#[test]
fn test_forward() {
let input: String = (&Forward { name: "main" }).to_string();
let output = r#"(@interface forward (export "main"))"#;
assert_eq!(input, output);
}
#[test]
fn test_interfaces() {
let input: String = (&Interfaces {
@ -682,7 +650,6 @@ mod tests {
instructions: vec![Instruction::ArgumentGet { index: 42 }],
},
],
forwards: vec![Forward { name: "main" }],
})
.to_string();
let output = r#";; Interfaces
@ -708,10 +675,7 @@ mod tests {
;; Interface, Adapter bar
(@interface adapt (export "bar")
arg.get 42)
;; Interface, Forward main
(@interface forward (export "main"))"#;
arg.get 42)"#;
assert_eq!(input, output);
}

View File

@ -29,7 +29,6 @@ fn test_binary_encoding_decoding_roundtrip() {
output_types: vec![InterfaceType::I32],
instructions: vec![Instruction::ArgumentGet { index: 1 }],
}],
forwards: vec![Forward { name: "a" }],
};
let mut binary = vec![];