Skip to content

Commit 1830a6d

Browse files
authored
Update to wasm-tools 226 (#1167)
* Update to wasm-tools 226 Mostly handling the lack of multi-return in WIT now. * Remove old features from codegen tests * Switch to crates.io-based versions
1 parent 4700050 commit 1830a6d

File tree

18 files changed

+710
-718
lines changed

18 files changed

+710
-718
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ prettyplease = "0.2.20"
3333
syn = { version = "2.0.89", features = ["printing"] }
3434
futures = "0.3.31"
3535

36-
wasmparser = "0.225.0"
37-
wasm-encoder = "0.225.0"
38-
wasm-metadata = "0.225.0"
39-
wit-parser = "0.225.0"
40-
wit-component = "0.225.0"
36+
wasmparser = "0.226.0"
37+
wasm-encoder = "0.226.0"
38+
wasm-metadata = "0.226.0"
39+
wit-parser = "0.226.0"
40+
wit-component = "0.226.0"
4141

4242
wit-bindgen-core = { path = 'crates/core', version = '0.39.0' }
4343
wit-bindgen-c = { path = 'crates/c', version = '0.39.0' }

crates/c/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,15 +1995,11 @@ impl InterfaceGenerator<'_> {
19951995

19961996
fn classify_ret(&mut self, func: &Function, sig_flattening: bool) -> Return {
19971997
let mut ret = Return::default();
1998-
match func.results.len() {
1999-
0 => ret.scalar = Some(Scalar::Void),
2000-
1 => {
2001-
let ty = func.results.iter_types().next().unwrap();
1998+
match &func.result {
1999+
None => ret.scalar = Some(Scalar::Void),
2000+
Some(ty) => {
20022001
ret.return_single(self.resolve, ty, ty, sig_flattening);
20032002
}
2004-
_ => {
2005-
ret.retptrs.extend(func.results.iter_types().cloned());
2006-
}
20072003
}
20082004
return ret;
20092005
}
@@ -2821,8 +2817,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
28212817
}
28222818
Some(Scalar::Type(_)) => {
28232819
let ret = self.locals.tmp("ret");
2824-
let ty = func.results.iter_types().next().unwrap();
2825-
let ty = self.gen.gen.type_name(ty);
2820+
let ty = func.result.unwrap();
2821+
let ty = self.gen.gen.type_name(&ty);
28262822
uwriteln!(self.src, "{} {} = {}({});", ty, ret, self.sig.name, args);
28272823
results.push(ret);
28282824
}
@@ -2837,8 +2833,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
28372833
let payload_ty = self.gen.gen.type_name(ty);
28382834
uwriteln!(self.src, "{} {};", payload_ty, val);
28392835
uwriteln!(self.src, "bool {} = {}({});", ret, self.sig.name, args);
2840-
let ty = func.results.iter_types().next().unwrap();
2841-
let option_ty = self.gen.gen.type_name(ty);
2836+
let ty = func.result.unwrap();
2837+
let option_ty = self.gen.gen.type_name(&ty);
28422838
let option_ret = self.locals.tmp("ret");
28432839
uwrite!(
28442840
self.src,
@@ -2851,7 +2847,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
28512847
results.push(option_ret);
28522848
}
28532849
Some(Scalar::ResultBool(ok, err)) => {
2854-
let ty = func.results.iter_types().next().unwrap();
2850+
let ty = &func.result.unwrap();
28552851
let result_ty = self.gen.gen.type_name(ty);
28562852
let ret = self.locals.tmp("ret");
28572853
let mut ret_iter = self.sig.ret.retptrs.iter();

crates/core/src/abi.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use wit_parser::abi::{AbiVariant, WasmSignature, WasmType};
22
use wit_parser::{
3-
ElementInfo, Enum, Flags, FlagsRepr, Function, Handle, Int, Record, Resolve, Result_, Results,
3+
ElementInfo, Enum, Flags, FlagsRepr, Function, Handle, Int, Record, Resolve, Result_,
44
SizeAlign, Tuple, Type, TypeDefKind, TypeId, Variant,
55
};
66

@@ -505,7 +505,7 @@ def_instruction! {
505505
CallInterface {
506506
func: &'a Function,
507507
async_: bool,
508-
} : [func.params.len()] => [if *async_ { 1 } else { func.results.len() }],
508+
} : [func.params.len()] => [if *async_ { 1 } else { usize::from(func.result.is_some()) }],
509509

510510
/// Returns `amt` values on the stack. This is always the last
511511
/// instruction.
@@ -573,7 +573,7 @@ def_instruction! {
573573
///
574574
/// For example, this might include task management for the
575575
/// future/promise/task returned by the call made for `CallInterface`.
576-
AsyncPostCallInterface { func: &'a Function } : [1] => [func.results.len() + 1],
576+
AsyncPostCallInterface { func: &'a Function } : [1] => [usize::from(func.result.is_some()) + 1],
577577

578578
/// Call `task.return` for an async-lifted export once the task returned
579579
/// by `CallInterface` and managed by `AsyncPostCallInterface`
@@ -815,9 +815,9 @@ pub fn post_return(resolve: &Resolve, func: &Function, bindgen: &mut impl Bindge
815815
/// This is used when the return value contains a memory allocation such as
816816
/// a list or a string primarily.
817817
pub fn guest_export_needs_post_return(resolve: &Resolve, func: &Function) -> bool {
818-
func.results
819-
.iter_types()
820-
.any(|t| needs_post_return(resolve, t))
818+
func.result
819+
.map(|t| needs_post_return(resolve, &t))
820+
.unwrap_or(false)
821821
}
822822

823823
fn needs_post_return(resolve: &Resolve, ty: &Type) -> bool {
@@ -977,7 +977,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
977977
let dealloc_size_align =
978978
if let Some((params_size, params_align)) = params_size_align {
979979
let ElementInfo { size, align } =
980-
self.bindgen.sizes().record(func.results.iter_types());
980+
self.bindgen.sizes().record(func.result.iter());
981981
self.emit(&Instruction::AsyncMalloc {
982982
size: size.size_wasm32(),
983983
align: align.align_wasm32(),
@@ -995,7 +995,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
995995
Some((size, align))
996996
} else {
997997
if self.variant == AbiVariant::GuestImport && sig.retptr {
998-
let info = self.bindgen.sizes().params(func.results.iter_types());
998+
let info = self.bindgen.sizes().params(&func.result);
999999
let ptr = self
10001000
.bindgen
10011001
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
@@ -1015,7 +1015,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10151015
// With no return pointer in use we can simply lift the
10161016
// result(s) of the function from the result of the core
10171017
// wasm function.
1018-
for ty in func.results.iter_types() {
1018+
if let Some(ty) = &func.result {
10191019
self.lift(ty)
10201020
}
10211021
} else {
@@ -1042,9 +1042,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10421042
}
10431043
};
10441044

1045-
self.read_results_from_memory(&func.results, ptr.clone(), 0);
1045+
self.read_results_from_memory(&func.result, ptr.clone(), 0);
10461046
self.emit(&Instruction::Flush {
1047-
amt: func.results.len(),
1047+
amt: usize::from(func.result.is_some()),
10481048
});
10491049

10501050
if let Some((size, align)) = dealloc_size_align {
@@ -1058,7 +1058,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10581058

10591059
self.emit(&Instruction::Return {
10601060
func,
1061-
amt: func.results.len(),
1061+
amt: usize::from(func.result.is_some()),
10621062
});
10631063
}
10641064
LiftLower::LiftArgsLowerResults => {
@@ -1109,7 +1109,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11091109
self.emit(&Instruction::AsyncPostCallInterface { func });
11101110

11111111
let mut results = Vec::new();
1112-
for ty in func.results.iter_types() {
1112+
if let Some(ty) = &func.result {
11131113
self.resolve.push_flat(ty, &mut results);
11141114
}
11151115
(results.len() > MAX_FLAT_PARAMS, Some(results))
@@ -1137,12 +1137,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11371137
if !lower_to_memory {
11381138
// With no return pointer in use we simply lower the
11391139
// result(s) and return that directly from the function.
1140-
let results = self
1141-
.stack
1142-
.drain(self.stack.len() - func.results.len()..)
1143-
.collect::<Vec<_>>();
1144-
for (ty, result) in func.results.iter_types().zip(results) {
1145-
self.stack.push(result);
1140+
if let Some(ty) = &func.result {
11461141
self.lower(ty);
11471142
}
11481143
} else {
@@ -1158,7 +1153,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11581153
nth: sig.params.len() - 1,
11591154
});
11601155
let ptr = self.stack.pop().unwrap();
1161-
self.write_params_to_memory(func.results.iter_types(), ptr, 0);
1156+
self.write_params_to_memory(&func.result, ptr, 0);
11621157
}
11631158

11641159
// For a guest import this is a function defined in
@@ -1167,11 +1162,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11671162
// (statically) and then write the result into that
11681163
// memory, returning the pointer at the end.
11691164
AbiVariant::GuestExport => {
1170-
let info = self.bindgen.sizes().params(func.results.iter_types());
1165+
let info = self.bindgen.sizes().params(&func.result);
11711166
let ptr = self
11721167
.bindgen
11731168
.return_pointer(info.size.size_wasm32(), info.align.align_wasm32());
1174-
self.write_params_to_memory(func.results.iter_types(), ptr.clone(), 0);
1169+
self.write_params_to_memory(&func.result, ptr.clone(), 0);
11751170
self.stack.push(ptr);
11761171
}
11771172

@@ -1222,11 +1217,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
12221217

12231218
self.emit(&Instruction::GetArg { nth: 0 });
12241219
let addr = self.stack.pop().unwrap();
1225-
for (offset, ty) in self
1226-
.bindgen
1227-
.sizes()
1228-
.field_offsets(func.results.iter_types())
1229-
{
1220+
for (offset, ty) in self.bindgen.sizes().field_offsets(&func.result) {
12301221
let offset = offset.size_wasm32();
12311222
let offset = i32::try_from(offset).unwrap();
12321223
self.deallocate(ty, addr.clone(), offset);
@@ -1779,7 +1770,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17791770

17801771
fn write_params_to_memory<'b>(
17811772
&mut self,
1782-
params: impl IntoIterator<Item = &'b Type> + ExactSizeIterator,
1773+
params: impl IntoIterator<Item = &'b Type, IntoIter: ExactSizeIterator>,
17831774
addr: B::Operand,
17841775
offset: i32,
17851776
) {
@@ -1827,10 +1818,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
18271818

18281819
fn write_fields_to_memory<'b>(
18291820
&mut self,
1830-
tys: impl IntoIterator<Item = &'b Type> + ExactSizeIterator,
1821+
tys: impl IntoIterator<Item = &'b Type, IntoIter: ExactSizeIterator>,
18311822
addr: B::Operand,
18321823
offset: i32,
18331824
) {
1825+
let tys = tys.into_iter();
18341826
let fields = self
18351827
.stack
18361828
.drain(self.stack.len() - tys.len()..)
@@ -1966,8 +1958,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
19661958
}
19671959
}
19681960

1969-
fn read_results_from_memory(&mut self, results: &Results, addr: B::Operand, offset: i32) {
1970-
self.read_fields_from_memory(results.iter_types(), addr, offset)
1961+
fn read_results_from_memory(&mut self, result: &Option<Type>, addr: B::Operand, offset: i32) {
1962+
self.read_fields_from_memory(result, addr, offset)
19711963
}
19721964

19731965
fn read_variant_arms_from_memory<'b>(

crates/core/src/types.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Types {
109109
}
110110
}
111111
let mut live = LiveTypes::default();
112-
for ty in func.results.iter_types() {
112+
if let Some(ty) = &func.result {
113113
self.type_info(resolve, ty);
114114
live.add_type(resolve, ty);
115115
}
@@ -119,14 +119,10 @@ impl Types {
119119
}
120120
}
121121

122-
for ty in func.results.iter_types() {
123-
let id = match ty {
124-
Type::Id(id) => *id,
125-
_ => continue,
126-
};
122+
if let Some(Type::Id(id)) = func.result {
127123
let err = match &resolve.types[id].kind {
128124
TypeDefKind::Result(Result_ { err, .. }) => err,
129-
_ => continue,
125+
_ => return,
130126
};
131127
if let Some(Type::Id(id)) = err {
132128
// When an interface `use`s a type from another interface, it creates a new typeid

crates/csharp/src/function.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
242242
uwrite!(
243243
self.src,
244244
"\
245-
if ({previous}.IsOk)
245+
if ({previous}.IsOk)
246246
{{
247247
var {tmp} = {previous}.AsOk;
248248
"
@@ -274,8 +274,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
274274
uwrite!(
275275
self.src,
276276
"\
277-
}}
278-
else
277+
}}
278+
else
279279
{{
280280
throw new {exception_name}({var_name}.AsErr!, {level});
281281
}}
@@ -300,7 +300,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
300300
// otherwise generate exception code
301301
let ty = self
302302
.interface_gen
303-
.type_name_with_qualifier(func.results.iter_types().next().unwrap(), true);
303+
.type_name_with_qualifier(&func.result.unwrap(), true);
304304
uwriteln!(self.src, "{ty} {ret};");
305305
let mut cases = Vec::with_capacity(self.results.len());
306306
let mut oks = Vec::with_capacity(self.results.len());
@@ -321,7 +321,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
321321
let tail = oks.iter().map(|_| ")").collect::<Vec<_>>().concat();
322322
cases.push(format!(
323323
"\
324-
case {index}:
324+
case {index}:
325325
{{
326326
ret = {head}{ty}.Err(({err_ty}) e.Value){tail};
327327
break;
@@ -334,7 +334,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
334334
if !self.results.is_empty() {
335335
self.src.push_str(
336336
"
337-
try
337+
try
338338
{\n
339339
",
340340
);
@@ -353,10 +353,10 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
353353
let cases = cases.join("\n");
354354
uwriteln!(
355355
self.src,
356-
r#"}}
357-
catch (WitException e)
356+
r#"}}
357+
catch (WitException e)
358358
{{
359-
switch (e.NestingLevel)
359+
switch (e.NestingLevel)
360360
{{
361361
{cases}
362362
@@ -751,7 +751,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
751751
"
752752
);
753753
}
754-
results.push(format!("(nint){ptr}"));
754+
results.push(format!("(nint){ptr}"));
755755
results.push(format!("({list}).Length"));
756756
}
757757
Direction::Export => {
@@ -888,7 +888,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
888888
if (({size} * {list}.Count) < 1024) {{
889889
var {ret_area} = stackalloc {element_type}[({array_size}*{list}.Count)+1];
890890
{address} = (void*)(((int){ret_area}) + ({align} - 1) & -{align});
891-
}}
891+
}}
892892
else
893893
{{
894894
var {buffer_size} = {size} * (nuint){list}.Count;
@@ -1021,24 +1021,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
10211021
_ => format!("{class_name_root}Impl")
10221022
};
10231023

1024-
match func.results.len() {
1025-
0 => uwriteln!(self.src, "{target}.{func_name}({oper});"),
1026-
1 => {
1024+
match func.result {
1025+
None => uwriteln!(self.src, "{target}.{func_name}({oper});"),
1026+
Some(_ty) => {
10271027
let ret = self.handle_result_call(func, target, func_name, oper);
10281028
results.push(ret);
10291029
}
1030-
_ => {
1031-
let ret = self.locals.tmp("ret");
1032-
uwriteln!(
1033-
self.src,
1034-
"var {ret} = {target}.{func_name}({oper});"
1035-
);
1036-
let mut i = 1;
1037-
for _ in func.results.iter_types() {
1038-
results.push(format!("{ret}.Item{i}"));
1039-
i += 1;
1040-
}
1041-
}
10421030
}
10431031
}
10441032
FunctionKind::Constructor(id) => {

0 commit comments

Comments
 (0)