Skip to content
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

Add a new "abi" which supports the full type grammar #422

Closed
wants to merge 24 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
92a4e6e
Start adding support for a "next" ABI
alexcrichton Feb 19, 2021
abc2c9e
Implement reading/writing to/from memory
alexcrichton Feb 23, 2021
5e6c843
Add some tests for read/write memory
alexcrichton Feb 23, 2021
c008c8e
Fill out a bit and fix a few bugs
alexcrichton Feb 23, 2021
c1495cb
Update ABIs with more modes of calling
alexcrichton Feb 25, 2021
0030d39
Automatically infer structs as bitflags/tuple/etc
alexcrichton Feb 25, 2021
fc59732
Support bitflags in the Next ABI
alexcrichton Feb 25, 2021
eefa46c
Support `@witx tag` in the Next ABI
alexcrichton Feb 25, 2021
0881434
Trim to minimize the witx ABI
alexcrichton Feb 25, 2021
a6835be
Don't support export modes with `call` and the preview1 abi
alexcrichton Feb 25, 2021
a5b09ac
Add some initial fuzzing
alexcrichton Feb 25, 2021
9726d02
Add an option shorthand
alexcrichton Feb 26, 2021
439b4f8
Move a helper function to a method on `Type`
alexcrichton Mar 3, 2021
a7fcb5f
Tweak memory ownership in list lifting/lowering
alexcrichton Mar 8, 2021
98a294d
Bump witx to 0.10.0
alexcrichton Mar 8, 2021
2beb50f
Initial support for `in-buffer` and `out-buffer` types
alexcrichton Mar 15, 2021
00d4af8
Merge remote-tracking branch 'origin/main' into abi-next
alexcrichton Apr 2, 2021
59e67ae
Move typename, resource, and const declarations inside of module syntax.
sunfishcode Apr 20, 2021
5651d52
rustfmt
sunfishcode Apr 20, 2021
9013e18
Use the parsed module name where applicable.
sunfishcode May 3, 2021
b53b089
Don't error if the filename "-" doesn't match the module name.
sunfishcode May 4, 2021
d31ad1b
Move `use` syntax inside of modules.
sunfishcode May 4, 2021
85a887b
Say "core wasm" instead of "native wasm".
sunfishcode May 5, 2021
70a8c63
Merge pull request #1 from sunfishcode/abi-next-more
alexcrichton May 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add an option shorthand
alexcrichton committed Feb 26, 2021
commit 9726d024fff8ad1877f8114716695d2ee945ec7e
23 changes: 23 additions & 0 deletions tools/witx/src/ast.rs
Original file line number Diff line number Diff line change
@@ -447,6 +447,29 @@ impl Variant {
}
}

/// If this variant looks like an `option` shorthand, return the type
/// associated with option.
///
/// Only matches variants fo the form:
///
/// ```text
/// (variant
/// (case "none")
/// (case "some" ty))
/// ```
pub fn as_option(&self) -> Option<&TypeRef> {
if self.cases.len() != 2 {
return None;
}
if self.cases[0].name != "none" || self.cases[0].tref.is_some() {
return None;
}
if self.cases[1].name != "some" {
return None;
}
self.cases[1].tref.as_ref()
}

/// If this variant looks like an `expected` shorthand, return the ok/err
/// types associated with this result.
///
17 changes: 17 additions & 0 deletions tools/witx/src/parser.rs
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ mod kw {
wast::custom_keyword!(usize);
wast::custom_keyword!(variant);
wast::custom_keyword!(bool_ = "bool");
wast::custom_keyword!(option);
}

mod annotation {
@@ -283,6 +284,7 @@ pub enum TypedefSyntax<'a> {
Enum(EnumSyntax<'a>),
Tuple(TupleSyntax<'a>),
Expected(ExpectedSyntax<'a>),
Option(OptionSyntax<'a>),
Flags(FlagsSyntax<'a>),
Record(RecordSyntax<'a>),
Union(UnionSyntax<'a>),
@@ -319,6 +321,8 @@ impl<'a> Parse<'a> for TypedefSyntax<'a> {
Ok(TypedefSyntax::Tuple(parser.parse()?))
} else if l.peek::<kw::expected>() {
Ok(TypedefSyntax::Expected(parser.parse()?))
} else if l.peek::<kw::option>() {
Ok(TypedefSyntax::Option(parser.parse()?))
} else if l.peek::<kw::flags>() {
Ok(TypedefSyntax::Flags(parser.parse()?))
} else if l.peek::<kw::record>() {
@@ -433,6 +437,19 @@ impl<'a> Parse<'a> for ExpectedSyntax<'a> {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OptionSyntax<'a> {
pub ty: Box<TypedefSyntax<'a>>,
}

impl<'a> Parse<'a> for OptionSyntax<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::option>()?;
let ty = Box::new(parser.parse()?);
Ok(OptionSyntax { ty })
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstSyntax<'a> {
pub ty: wast::Id<'a>,
30 changes: 28 additions & 2 deletions tools/witx/src/validate.rs
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ use crate::{
io::{Filesystem, WitxIo},
parser::{
CommentSyntax, DeclSyntax, Documented, EnumSyntax, ExpectedSyntax, FlagsSyntax,
HandleSyntax, ImportTypeSyntax, ModuleDeclSyntax, RecordSyntax, TupleSyntax, TypedefSyntax,
UnionSyntax, VariantSyntax,
HandleSyntax, ImportTypeSyntax, ModuleDeclSyntax, OptionSyntax, RecordSyntax, TupleSyntax,
TypedefSyntax, UnionSyntax, VariantSyntax,
},
BuiltinType, Case, Constant, Definition, Document, Entry, HandleDatatype, Id, IntRepr,
InterfaceFunc, InterfaceFuncParam, Location, Module, ModuleDefinition, ModuleEntry,
@@ -309,6 +309,9 @@ impl DocValidationScope<'_> {
TypedefSyntax::Expected(syntax) => {
Type::Variant(self.validate_expected(&syntax, span)?)
}
TypedefSyntax::Option(syntax) => {
Type::Variant(self.validate_option(&syntax, span)?)
}
TypedefSyntax::Flags(syntax) => Type::Record(self.validate_flags(&syntax, span)?),
TypedefSyntax::Record(syntax) => Type::Record(self.validate_record(&syntax, span)?),
TypedefSyntax::Union(syntax) => Type::Variant(self.validate_union(&syntax, span)?),
@@ -416,6 +419,29 @@ impl DocValidationScope<'_> {
})
}

fn validate_option(
&self,
syntax: &OptionSyntax,
span: wast::Span,
) -> Result<Variant, ValidationError> {
let tref = self.validate_datatype(&syntax.ty, false, span)?;
Ok(Variant {
tag_repr: IntRepr::U8,
cases: vec![
Case {
name: Id::new("none"),
tref: None,
docs: String::new(),
},
Case {
name: Id::new("some"),
tref: Some(tref),
docs: String::new(),
},
],
})
}

fn validate_flags(
&self,
syntax: &FlagsSyntax,
6 changes: 6 additions & 0 deletions tools/witx/tests/witxt/shorthand.witxt
Original file line number Diff line number Diff line change
@@ -57,3 +57,9 @@
(witx $b
(typename $a (variant (case $0 u32) (case $1 u64))))
(assert_representable eq $a "a" $b "a")

(witx $a
(typename $a (option u32)))
(witx $b
(typename $a (variant (case $none) (case $some u32))))
(assert_representable eq $a "a" $b "a")