Skip to content

Commit 5635975

Browse files
authored
Update wit-{parser,component} crate versions (#479)
* Update wit-{parser,component} crate versions Updates to a version that supports types-in-worlds. * Fix names for upstream changes
1 parent d7b1fde commit 5635975

File tree

18 files changed

+443
-259
lines changed

18 files changed

+443
-259
lines changed

Cargo.lock

Lines changed: 78 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ indexmap = "1.9.1"
2626

2727
wasm-encoder = "0.22.0"
2828
wat = "1.0.56"
29-
wit-parser = "0.4.1"
30-
wit-component = "0.4.4"
29+
wit-parser = "0.5.0"
30+
wit-component = "0.5.0"
3131

3232
wit-bindgen-core = { path = 'crates/bindgen-core', version = '0.3.0' }
3333
wit-bindgen-gen-guest-c = { path = 'crates/gen-guest-c', version = '0.3.0' }

crates/bindgen-core/src/lib.rs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,30 @@ impl Types {
4747
}
4848
for (_, iface) in resolve.interfaces.iter() {
4949
for (_, f) in iface.functions.iter() {
50-
for (_, ty) in f.params.iter() {
51-
self.set_param_result_ty(resolve, ty, true, false, false);
52-
}
53-
for ty in f.results.iter_types() {
54-
self.set_param_result_ty(resolve, ty, false, true, false);
50+
self.type_info_func(resolve, f);
51+
}
52+
}
53+
for (_, world) in resolve.worlds.iter() {
54+
for (_, item) in world.imports.iter().chain(&world.exports) {
55+
match item {
56+
WorldItem::Function(f) => {
57+
self.type_info_func(resolve, f);
58+
}
59+
WorldItem::Interface(_) | WorldItem::Type(_) => {}
5560
}
5661
}
5762
}
5863
}
5964

65+
fn type_info_func(&mut self, resolve: &Resolve, f: &Function) {
66+
for (_, ty) in f.params.iter() {
67+
self.set_param_result_ty(resolve, ty, true, false, false);
68+
}
69+
for ty in f.results.iter_types() {
70+
self.set_param_result_ty(resolve, ty, false, true, false);
71+
}
72+
}
73+
6074
pub fn get(&self, id: TypeId) -> TypeInfo {
6175
self.type_info[&id]
6276
}
@@ -434,18 +448,25 @@ pub trait WorldGenerator {
434448
match import {
435449
WorldItem::Function(f) => funcs.push((name.as_str(), f)),
436450
WorldItem::Interface(id) => self.import_interface(resolve, name, *id, files),
451+
WorldItem::Type(_) => unreachable!(),
437452
}
438453
}
439454
if !funcs.is_empty() {
440455
self.import_funcs(resolve, id, &funcs, files);
441456
}
442457
funcs.clear();
458+
459+
let mut types = Vec::new();
443460
for (name, export) in world.exports.iter() {
444461
match export {
445462
WorldItem::Function(f) => funcs.push((name.as_str(), f)),
446463
WorldItem::Interface(id) => self.export_interface(resolve, name, *id, files),
464+
WorldItem::Type(id) => types.push((name.as_str(), *id)),
447465
}
448466
}
467+
if !types.is_empty() {
468+
self.export_types(resolve, id, &types, files);
469+
}
449470
if !funcs.is_empty() {
450471
self.export_funcs(resolve, id, &funcs, files);
451472
}
@@ -485,6 +506,13 @@ pub trait WorldGenerator {
485506
funcs: &[(&str, &Function)],
486507
files: &mut Files,
487508
);
509+
fn export_types(
510+
&mut self,
511+
resolve: &Resolve,
512+
world: WorldId,
513+
types: &[(&str, TypeId)],
514+
files: &mut Files,
515+
);
488516
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files);
489517
}
490518

@@ -514,23 +542,26 @@ pub trait InterfaceGenerator<'a> {
514542
fn types(&mut self, iface: InterfaceId) {
515543
let iface = &self.resolve().interfaces[iface];
516544
for (name, id) in iface.types.iter() {
517-
let id = *id;
518-
let ty = &self.resolve().types[id];
519-
match &ty.kind {
520-
TypeDefKind::Record(record) => self.type_record(id, name, record, &ty.docs),
521-
TypeDefKind::Flags(flags) => self.type_flags(id, name, flags, &ty.docs),
522-
TypeDefKind::Tuple(tuple) => self.type_tuple(id, name, tuple, &ty.docs),
523-
TypeDefKind::Enum(enum_) => self.type_enum(id, name, enum_, &ty.docs),
524-
TypeDefKind::Variant(variant) => self.type_variant(id, name, variant, &ty.docs),
525-
TypeDefKind::Option(t) => self.type_option(id, name, t, &ty.docs),
526-
TypeDefKind::Result(r) => self.type_result(id, name, r, &ty.docs),
527-
TypeDefKind::Union(u) => self.type_union(id, name, u, &ty.docs),
528-
TypeDefKind::List(t) => self.type_list(id, name, t, &ty.docs),
529-
TypeDefKind::Type(t) => self.type_alias(id, name, t, &ty.docs),
530-
TypeDefKind::Future(_) => todo!("generate for future"),
531-
TypeDefKind::Stream(_) => todo!("generate for stream"),
532-
TypeDefKind::Unknown => unreachable!(),
533-
}
545+
self.define_type(name, *id);
546+
}
547+
}
548+
549+
fn define_type(&mut self, name: &str, id: TypeId) {
550+
let ty = &self.resolve().types[id];
551+
match &ty.kind {
552+
TypeDefKind::Record(record) => self.type_record(id, name, record, &ty.docs),
553+
TypeDefKind::Flags(flags) => self.type_flags(id, name, flags, &ty.docs),
554+
TypeDefKind::Tuple(tuple) => self.type_tuple(id, name, tuple, &ty.docs),
555+
TypeDefKind::Enum(enum_) => self.type_enum(id, name, enum_, &ty.docs),
556+
TypeDefKind::Variant(variant) => self.type_variant(id, name, variant, &ty.docs),
557+
TypeDefKind::Option(t) => self.type_option(id, name, t, &ty.docs),
558+
TypeDefKind::Result(r) => self.type_result(id, name, r, &ty.docs),
559+
TypeDefKind::Union(u) => self.type_union(id, name, u, &ty.docs),
560+
TypeDefKind::List(t) => self.type_list(id, name, t, &ty.docs),
561+
TypeDefKind::Type(t) => self.type_alias(id, name, t, &ty.docs),
562+
TypeDefKind::Future(_) => todo!("generate for future"),
563+
TypeDefKind::Stream(_) => todo!("generate for stream"),
564+
TypeDefKind::Unknown => unreachable!(),
534565
}
535566
}
536567
}

crates/gen-guest-c/src/lib.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ impl WorldGenerator for C {
166166
gen.gen.src.append(&gen.src);
167167
}
168168

169+
fn export_types(
170+
&mut self,
171+
resolve: &Resolve,
172+
world: WorldId,
173+
types: &[(&str, TypeId)],
174+
_files: &mut Files,
175+
) {
176+
let name = &resolve.worlds[world].name;
177+
let mut gen = self.interface(name, resolve, false);
178+
for (name, id) in types {
179+
gen.define_type(name, *id);
180+
}
181+
gen.finish();
182+
gen.gen.src.append(&gen.src);
183+
}
184+
169185
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
170186
let world = &resolve.worlds[id];
171187
let linking_symbol = component_type_object::linking_symbol(&world.name);
@@ -1109,10 +1125,20 @@ impl InterfaceGenerator<'_> {
11091125
let ty = &self.resolve.types[*id];
11101126
match &ty.name {
11111127
Some(name) => {
1112-
if let TypeOwner::Interface(owner) = ty.owner {
1113-
self.src
1114-
.print(stype, &self.gen.interface_names[&owner].to_snake_case());
1115-
self.src.print(stype, "_");
1128+
match ty.owner {
1129+
TypeOwner::Interface(owner) => {
1130+
self.src.print(
1131+
stype,
1132+
&self.gen.interface_names[&owner].to_snake_case(),
1133+
);
1134+
self.src.print(stype, "_");
1135+
}
1136+
TypeOwner::World(owner) => {
1137+
self.src
1138+
.print(stype, &self.resolve.worlds[owner].name.to_snake_case());
1139+
self.src.print(stype, "_");
1140+
}
1141+
TypeOwner::None => {}
11161142
}
11171143

11181144
self.src.print(stype, &name.to_snake_case());

crates/gen-guest-rust/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ impl WorldGenerator for RustWasm {
182182
self.src.push_str(&src);
183183
}
184184

185+
fn export_types(
186+
&mut self,
187+
resolve: &Resolve,
188+
_world: WorldId,
189+
types: &[(&str, TypeId)],
190+
_files: &mut Files,
191+
) {
192+
let mut gen = self.interface(None, resolve, TypeMode::Owned, false);
193+
for (name, ty) in types {
194+
gen.define_type(name, *ty);
195+
}
196+
let src = gen.finish();
197+
self.src.push_str(&src);
198+
}
199+
185200
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) {
186201
let name = &resolve.worlds[world].name;
187202
if !self.exports.is_empty() {

crates/gen-guest-teavm-java/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ impl WorldGenerator for TeaVmJava {
150150
gen.add_class();
151151
}
152152

153+
fn export_types(
154+
&mut self,
155+
resolve: &Resolve,
156+
world: WorldId,
157+
types: &[(&str, TypeId)],
158+
_files: &mut Files,
159+
) {
160+
let name = &resolve.worlds[world].name;
161+
let mut gen = self.interface(resolve, name);
162+
163+
for (name, ty) in types {
164+
gen.define_type(name, *ty);
165+
}
166+
167+
gen.add_class();
168+
}
169+
153170
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
154171
let world = &resolve.worlds[id];
155172
let package = format!("wit_{}", world.name.to_snake_case());

crates/gen-guest-teavm-java/tests/codegen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::path::Path;
44
use std::process::Command;
55

66
macro_rules! codegen_test {
7+
(worlds_with_types $name:tt $test:tt) => {};
8+
79
($id:ident $name:tt $test:tt) => {
810
#[test]
911
fn $id() {

crates/gen-markdown/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ impl WorldGenerator for Markdown {
9090
}
9191
}
9292

93+
fn export_types(
94+
&mut self,
95+
resolve: &Resolve,
96+
world: WorldId,
97+
types: &[(&str, TypeId)],
98+
_files: &mut Files,
99+
) {
100+
let name = &resolve.worlds[world].name;
101+
uwriteln!(self.src, "# Exported types from world `{name}`\n");
102+
let mut gen = self.interface(resolve);
103+
for (name, ty) in types {
104+
gen.define_type(name, *ty);
105+
}
106+
}
107+
93108
fn finish(&mut self, resolve: &Resolve, world: WorldId, files: &mut Files) {
94109
let world = &resolve.worlds[world];
95110
let parser = Parser::new(&self.src);

tests/codegen/worlds-with-types.wit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface i {
2+
type type1 = u32
3+
}
4+
5+
default world foo {
6+
use self.i.{type1}
7+
8+
type type2 = u32
9+
10+
record type3 {}
11+
12+
export foo: func() -> tuple<type1, type2, type3>
13+
}

0 commit comments

Comments
 (0)