Skip to content

Commit

Permalink
Synchronize the wit-bindgen type AST with the component model (#318)
Browse files Browse the repository at this point in the history
* WIP: Update for component model multireturn and removal of unit

This PR updates wit-bindgen for the latest changes to the component model:

- the return of multireturn
- removal of the `unit` type
- a new syntax for optional types in results, streams, futures and variants

all of which go hand in hand.

This also pulls in the latest versions of wasm-encoder, wasmprinter,
and wasmparser crates to get their updates for these component model changes.

* Get all wit-bindgen tests working

* Implement tests for multi-return

Exercise this throughout the runtime tests and additionally add a few
variants in codegen tests to ensure that this is exercised.

* Update markdown generator

Co-authored-by: George Kulakowski <gkulakowski@fastly.com>
  • Loading branch information
alexcrichton and kulakowski-wasm authored Sep 22, 2022
1 parent d375b85 commit 6519762
Show file tree
Hide file tree
Showing 56 changed files with 1,844 additions and 1,004 deletions.
29 changes: 10 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 37 additions & 13 deletions crates/bindgen-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ impl Types {
for (_, ty) in f.params.iter() {
self.set_param_result_ty(iface, ty, true, false);
}
self.set_param_result_ty(iface, &f.result, false, true);
for ty in f.results.iter_types() {
self.set_param_result_ty(iface, ty, false, true);
}
}
}

Expand Down Expand Up @@ -232,7 +234,7 @@ impl Types {
TypeDefKind::Enum(_) => {}
TypeDefKind::Variant(v) => {
for case in v.cases.iter() {
info |= self.type_info(iface, &case.ty);
info |= self.optional_type_info(iface, case.ty.as_ref());
}
}
TypeDefKind::List(ty) => {
Expand All @@ -246,20 +248,20 @@ impl Types {
info = self.type_info(iface, ty);
}
TypeDefKind::Result(r) => {
info = self.type_info(iface, &r.ok);
info |= self.type_info(iface, &r.err);
info = self.optional_type_info(iface, r.ok.as_ref());
info |= self.optional_type_info(iface, r.err.as_ref());
}
TypeDefKind::Union(u) => {
for case in u.cases.iter() {
info |= self.type_info(iface, &case.ty);
}
}
TypeDefKind::Future(ty) => {
info = self.type_info(iface, ty);
info = self.optional_type_info(iface, ty.as_ref());
}
TypeDefKind::Stream(stream) => {
info = self.type_info(iface, &stream.element);
info |= self.type_info(iface, &stream.end);
info = self.optional_type_info(iface, stream.element.as_ref());
info |= self.optional_type_info(iface, stream.end.as_ref());
}
}
self.type_info.insert(ty, info);
Expand All @@ -277,6 +279,13 @@ impl Types {
info
}

fn optional_type_info(&mut self, iface: &Interface, ty: Option<&Type>) -> TypeInfo {
match ty {
Some(ty) => self.type_info(iface, ty),
None => TypeInfo::default(),
}
}

fn set_param_result_id(&mut self, iface: &Interface, ty: TypeId, param: bool, result: bool) {
match &iface.types[ty].kind {
TypeDefKind::Record(r) => {
Expand All @@ -293,25 +302,27 @@ impl Types {
TypeDefKind::Enum(_) => {}
TypeDefKind::Variant(v) => {
for case in v.cases.iter() {
self.set_param_result_ty(iface, &case.ty, param, result)
self.set_param_result_optional_ty(iface, case.ty.as_ref(), param, result)
}
}
TypeDefKind::List(ty) | TypeDefKind::Type(ty) | TypeDefKind::Option(ty) => {
self.set_param_result_ty(iface, ty, param, result)
}
TypeDefKind::Result(r) => {
self.set_param_result_ty(iface, &r.ok, param, result);
self.set_param_result_ty(iface, &r.err, param, result);
self.set_param_result_optional_ty(iface, r.ok.as_ref(), param, result);
self.set_param_result_optional_ty(iface, r.err.as_ref(), param, result);
}
TypeDefKind::Union(u) => {
for case in u.cases.iter() {
self.set_param_result_ty(iface, &case.ty, param, result)
}
}
TypeDefKind::Future(ty) => self.set_param_result_ty(iface, ty, param, result),
TypeDefKind::Future(ty) => {
self.set_param_result_optional_ty(iface, ty.as_ref(), param, result)
}
TypeDefKind::Stream(stream) => {
self.set_param_result_ty(iface, &stream.element, param, result);
self.set_param_result_ty(iface, &stream.end, param, result);
self.set_param_result_optional_ty(iface, stream.element.as_ref(), param, result);
self.set_param_result_optional_ty(iface, stream.end.as_ref(), param, result);
}
}
}
Expand All @@ -330,6 +341,19 @@ impl Types {
_ => {}
}
}

fn set_param_result_optional_ty(
&mut self,
iface: &Interface,
ty: Option<&Type>,
param: bool,
result: bool,
) {
match ty {
Some(ty) => self.set_param_result_ty(iface, ty, param, result),
None => (),
}
}
}

#[derive(Default)]
Expand Down
Loading

0 comments on commit 6519762

Please sign in to comment.