2020-02-10 15:27:04 +01:00
|
|
|
//! Represents the WIT language as a tree. This is the central
|
|
|
|
//! representation of the language.
|
|
|
|
|
2019-09-26 14:14:46 +02:00
|
|
|
use crate::interpreter::Instruction;
|
2019-09-13 14:50:17 +02:00
|
|
|
use std::str;
|
2019-09-12 22:44:20 +02:00
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents the types supported by WIT.
|
2020-02-19 16:12:06 +01:00
|
|
|
#[derive(PartialEq, Clone, Copy, Debug)]
|
2019-09-12 22:44:20 +02:00
|
|
|
pub enum InterfaceType {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// An integer.
|
2019-09-12 22:44:20 +02:00
|
|
|
Int,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A float.
|
2019-09-12 22:44:20 +02:00
|
|
|
Float,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// Opaque reference.
|
2019-09-12 22:44:20 +02:00
|
|
|
Any,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A string.
|
2019-09-12 22:44:20 +02:00
|
|
|
String,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A sequence.
|
2019-09-12 22:44:20 +02:00
|
|
|
Seq,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A 32-bits integer.
|
2019-09-12 22:44:20 +02:00
|
|
|
I32,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A 64-bits integer.
|
2019-09-12 22:44:20 +02:00
|
|
|
I64,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A 32-bits float.
|
2019-09-12 22:44:20 +02:00
|
|
|
F32,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A 64-bits float.
|
2019-09-12 22:44:20 +02:00
|
|
|
F64,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// An `any` reference.
|
2019-09-12 22:44:20 +02:00
|
|
|
AnyRef,
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents the kind of adapter.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
2019-09-13 14:26:49 +02:00
|
|
|
pub(crate) enum AdapterKind {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// An adapter defined for an imported function of a WebAssembly instance.
|
2019-09-12 22:44:20 +02:00
|
|
|
Import,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// An adapter defined for an exported function of a WebAssembly instance.
|
2019-09-12 22:44:20 +02:00
|
|
|
Export,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// A helper function.
|
2019-09-12 22:44:20 +02:00
|
|
|
HelperFunction,
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents an exported function signature.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub struct Export<'input> {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// The function name.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub name: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function input types.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub input_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function output types.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub output_types: Vec<InterfaceType>,
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents an imported function signature.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
2020-02-10 15:41:41 +01:00
|
|
|
pub struct Import<'input> {
|
|
|
|
/// The function namespace.
|
|
|
|
pub namespace: &'input str,
|
|
|
|
|
|
|
|
/// The function name.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub name: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function input types.
|
|
|
|
pub input_types: Vec<InterfaceType>,
|
|
|
|
|
|
|
|
/// The function output types.
|
|
|
|
pub output_types: Vec<InterfaceType>,
|
2019-09-12 22:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:44:28 +01:00
|
|
|
/// Represents a structural type.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
2020-02-10 15:41:41 +01:00
|
|
|
pub struct Type<'input> {
|
2020-02-10 15:44:28 +01:00
|
|
|
/// The type name.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub name: &'input str,
|
2020-02-10 15:44:28 +01:00
|
|
|
|
|
|
|
/// The field names.
|
2020-02-13 11:24:29 +01:00
|
|
|
field_names: Vec<&'input str>,
|
2020-02-10 15:44:28 +01:00
|
|
|
|
|
|
|
/// The field types.
|
2020-02-13 11:24:29 +01:00
|
|
|
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
|
|
|
|
}
|
2019-09-12 22:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents an adapter.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub enum Adapter<'input> {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// An adapter for an imported function.
|
2019-09-12 22:44:20 +02:00
|
|
|
Import {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// The function namespace.
|
2019-09-12 22:44:20 +02:00
|
|
|
namespace: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function name.
|
2019-09-12 22:44:20 +02:00
|
|
|
name: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function input types.
|
2019-09-12 22:44:20 +02:00
|
|
|
input_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function output types.
|
2019-09-12 22:44:20 +02:00
|
|
|
output_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The instructions of the adapter.
|
2019-09-12 22:44:20 +02:00
|
|
|
instructions: Vec<Instruction<'input>>,
|
|
|
|
},
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// An adapter for an exported function.
|
2019-09-12 22:44:20 +02:00
|
|
|
Export {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// The function name.
|
2019-09-12 22:44:20 +02:00
|
|
|
name: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function input types.
|
2019-09-12 22:44:20 +02:00
|
|
|
input_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The function output types.
|
2019-09-12 22:44:20 +02:00
|
|
|
output_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The instructions of the adapter.
|
2019-09-12 22:44:20 +02:00
|
|
|
instructions: Vec<Instruction<'input>>,
|
|
|
|
},
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// An adapter for a helper function.
|
2019-09-12 22:44:20 +02:00
|
|
|
HelperFunction {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// The helper name.
|
2019-09-12 22:44:20 +02:00
|
|
|
name: &'input str,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The helper input types.
|
2019-09-12 22:44:20 +02:00
|
|
|
input_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The helper output types.
|
2019-09-12 22:44:20 +02:00
|
|
|
output_types: Vec<InterfaceType>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// The instructions of the adapter.
|
2019-09-12 22:44:20 +02:00
|
|
|
instructions: Vec<Instruction<'input>>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represented a forwarded export.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub struct Forward<'input> {
|
2020-02-10 15:41:41 +01:00
|
|
|
/// The forwarded export name.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub name: &'input str,
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:41:41 +01:00
|
|
|
/// Represents a set of interfaces, i.e. it entirely describes a WIT
|
|
|
|
/// definition.
|
2019-09-12 22:44:20 +02:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub struct Interfaces<'input> {
|
2020-02-10 15:44:28 +01:00
|
|
|
/// All the exported functions.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub exports: Vec<Export<'input>>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// All the types.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub types: Vec<Type<'input>>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// All the imported functions.
|
|
|
|
pub imports: Vec<Import<'input>>,
|
|
|
|
|
2020-02-10 15:44:28 +01:00
|
|
|
/// All the adapters.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub adapters: Vec<Adapter<'input>>,
|
2020-02-10 15:41:41 +01:00
|
|
|
|
|
|
|
/// All the forwarded functions.
|
2019-09-12 22:44:20 +02:00
|
|
|
pub forwards: Vec<Forward<'input>>,
|
|
|
|
}
|