feat(interface-types) Re-implement Type.

The semantics of “types” have changed since the previous draft. Now, a
type is like a regular WebAssembly type but with Interface Types.
This commit is contained in:
Ivan Enderlin
2020-02-24 16:23:31 +01:00
parent 165a8ca585
commit 410d8d4476
6 changed files with 137 additions and 130 deletions

View File

@ -60,6 +60,16 @@ pub(crate) enum AdapterKind {
Export,
}
/// Represents a type signature.
#[derive(PartialEq, Debug)]
pub struct Type {
/// Types for the parameters.
pub inputs: Vec<InterfaceType>,
/// Types for the results.
pub outputs: Vec<InterfaceType>,
}
/// Represents an exported function signature.
#[derive(PartialEq, Debug)]
pub struct Export<'input> {
@ -89,55 +99,6 @@ pub struct Import<'input> {
pub output_types: Vec<InterfaceType>,
}
/// Represents a structural type.
#[derive(PartialEq, Debug)]
pub struct Type<'input> {
/// The type name.
pub name: &'input str,
/// The field names.
field_names: Vec<&'input str>,
/// The field types.
field_types: Vec<InterfaceType>,
}
impl<'input> Type<'input> {
/// Creates a new `Type`.
///
/// The constructor panics if there is the length of `names` is
/// different than the length of `types`.
pub fn new(type_name: &'input str, names: Vec<&'input str>, types: Vec<InterfaceType>) -> Self {
assert_eq!(
names.len(),
types.len(),
"There must be the same number of field names than field types."
);
Self {
name: type_name,
field_names: names,
field_types: types,
}
}
/// Adds a new field to the type.
pub fn add_field(&mut self, name: &'input str, ty: InterfaceType) {
self.field_names.push(name);
self.field_types.push(ty);
}
/// Returns the field names.
pub fn field_names(&self) -> &Vec<&'input str> {
&self.field_names
}
/// Returns the field types.
pub fn field_types(&self) -> &Vec<InterfaceType> {
&self.field_types
}
}
/// Represents an adapter.
#[derive(PartialEq, Debug)]
pub enum Adapter<'input> {
@ -179,12 +140,12 @@ pub enum Adapter<'input> {
/// definition.
#[derive(PartialEq, Default, Debug)]
pub struct Interfaces<'input> {
/// All the types.
pub types: Vec<Type>,
/// All the exported functions.
pub exports: Vec<Export<'input>>,
/// All the types.
pub types: Vec<Type<'input>>,
/// All the imported functions.
pub imports: Vec<Import<'input>>,