-
Notifications
You must be signed in to change notification settings - Fork 798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(interface-types) Implement Interface Types (WIT) #787
Changes from all commits
b3af77c
7ca546e
45ba77c
1c1b74b
dc254e0
24ac7a6
5a8a2b9
a7ffffc
4ba9aac
40613d3
6279b3e
bd3a888
480fe0f
6ec35c8
fc9389d
dc3c72e
2f3c37f
c633450
62e1f78
9d4c983
39a8178
2237e62
b7b37d2
56afb4d
8557e83
aea18f6
be5624e
4d9dacb
8d75db9
ef568ca
981692e
49a7587
5ce18fc
ade098b
fce270a
f537b4d
c35395b
a047e68
7e18be1
63b1391
87f61ab
9fdaf0c
d813472
7b3ab38
99c9fc4
207d69f
b562400
c8af1b1
ba16d12
fe14c5c
60a9056
0b0a89b
102ebe8
5ed5ac8
e098934
d63508f
32325c1
36c7dbd
d667cb8
2fc1fbb
9fda6f9
98fb069
55ae0e4
c5b963b
98c7309
b3c102d
6b0e43b
13b9cb3
b7941f4
1ad42d8
370fd6d
3f0c32b
d8f9230
722727b
13cee90
6891517
ae6e261
9559747
eb2f9db
7f8d916
c697e68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "wasmer-interface-types" | ||
version = "0.13.1" | ||
description = "WebAssembly Interface Types library for Wasmer" | ||
license = "MIT" | ||
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"] | ||
repository = "https://github.com/wasmerio/wasmer" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
nom = "5.1" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
//! Represents the WIT language as a tree. This is the central | ||
//! representation of the language. | ||
|
||
use crate::interpreter::Instruction; | ||
use std::str; | ||
|
||
/// Represents the types supported by WIT. | ||
#[derive(PartialEq, Clone, Debug)] | ||
pub enum InterfaceType { | ||
/// An integer. | ||
Int, | ||
|
||
/// A float. | ||
Float, | ||
|
||
/// Opaque reference. | ||
Any, | ||
|
||
/// A string. | ||
String, | ||
|
||
/// A sequence. | ||
Seq, | ||
|
||
/// A 32-bits integer. | ||
I32, | ||
|
||
/// A 64-bits integer. | ||
I64, | ||
|
||
/// A 32-bits float. | ||
F32, | ||
|
||
/// A 64-bits float. | ||
F64, | ||
|
||
/// An `any` reference. | ||
AnyRef, | ||
} | ||
|
||
/// Represents the kind of adapter. | ||
#[derive(PartialEq, Debug)] | ||
pub(crate) enum AdapterKind { | ||
/// An adapter defined for an imported function of a WebAssembly instance. | ||
Import, | ||
|
||
/// An adapter defined for an exported function of a WebAssembly instance. | ||
Export, | ||
|
||
/// A helper function. | ||
HelperFunction, | ||
} | ||
|
||
/// Represents an exported function signature. | ||
#[derive(PartialEq, Debug)] | ||
pub struct Export<'input> { | ||
/// The function name. | ||
pub name: &'input str, | ||
|
||
/// The function input types. | ||
pub input_types: Vec<InterfaceType>, | ||
|
||
/// The function output types. | ||
pub output_types: Vec<InterfaceType>, | ||
} | ||
|
||
/// Represents an imported function signature. | ||
#[derive(PartialEq, Debug)] | ||
pub struct Import<'input> { | ||
/// The function namespace. | ||
pub namespace: &'input str, | ||
|
||
/// The function name. | ||
pub name: &'input str, | ||
|
||
/// The function input types. | ||
pub input_types: Vec<InterfaceType>, | ||
|
||
/// The function output types. | ||
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> { | ||
/// An adapter for an imported function. | ||
Import { | ||
/// The function namespace. | ||
namespace: &'input str, | ||
|
||
/// The function name. | ||
name: &'input str, | ||
|
||
/// The function input types. | ||
input_types: Vec<InterfaceType>, | ||
|
||
/// The function output types. | ||
output_types: Vec<InterfaceType>, | ||
|
||
/// The instructions of the adapter. | ||
instructions: Vec<Instruction<'input>>, | ||
}, | ||
|
||
/// An adapter for an exported function. | ||
Export { | ||
/// The function name. | ||
name: &'input str, | ||
|
||
/// The function input types. | ||
input_types: Vec<InterfaceType>, | ||
|
||
/// The function output types. | ||
output_types: Vec<InterfaceType>, | ||
|
||
/// The instructions of the adapter. | ||
instructions: Vec<Instruction<'input>>, | ||
}, | ||
|
||
/// An adapter for a helper function. | ||
HelperFunction { | ||
/// The helper name. | ||
name: &'input str, | ||
|
||
/// The helper input types. | ||
input_types: Vec<InterfaceType>, | ||
|
||
/// The helper output types. | ||
output_types: Vec<InterfaceType>, | ||
|
||
/// The instructions of the adapter. | ||
instructions: Vec<Instruction<'input>>, | ||
}, | ||
} | ||
|
||
/// Represented a forwarded export. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps the docs should explain what forwarding is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not super defined in the spec yet :-D. It should be used to “rename” an export, like |
||
#[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, Debug)] | ||
pub struct Interfaces<'input> { | ||
/// 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>>, | ||
|
||
/// All the adapters. | ||
pub adapters: Vec<Adapter<'input>>, | ||
|
||
/// All the forwarded functions. | ||
pub forwards: Vec<Forward<'input>>, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth considering using the structs with the same fields inside this enum if you haven't already. It may not make sense given how it's used -- I haven't read most of the code yet; I think this level of duplication can make sense though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the enum's fields are identical to the structs with the same/similar names. It'd be possible to remove most of the fields in the enum and replace it with 1 instance of the correct struct. It's a minor thing and I think there are convincing arguments for both doing it and not doing it.