Skip to content

Commit 6afe0a3

Browse files
new function Param type that includes span property (#2445)
add and fix tests
1 parent 39cd181 commit 6afe0a3

16 files changed

Lines changed: 251 additions & 112 deletions

File tree

crates/wasm-wave/src/value/wit.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use alloc::{string::String, vec::Vec};
22
use wit_parser::{
3-
Enum, Flags, Function, Record, Resolve, Result_, Tuple, Type, TypeDefKind, TypeId, Variant,
3+
Enum, Flags, Function, Param, Record, Resolve, Result_, Tuple, Type, TypeDefKind, TypeId,
4+
Variant,
45
};
56

67
use crate::{value, wasm::WasmValueError};
@@ -45,13 +46,13 @@ impl<'a> TypeResolver<'a> {
4546

4647
fn resolve_params(
4748
&self,
48-
params: &[(String, Type)],
49+
params: &[Param],
4950
) -> Result<Vec<(String, value::Type)>, WasmValueError> {
5051
params
5152
.iter()
52-
.map(|(name, ty)| {
53-
let ty = self.resolve_type(*ty)?;
54-
Ok((name.clone(), ty))
53+
.map(|p| {
54+
let ty = self.resolve_type(p.ty)?;
55+
Ok((p.name.clone(), ty))
5556
})
5657
.collect()
5758
}

crates/wit-component/src/encoding.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ use std::mem;
8585
use wasm_encoder::*;
8686
use wasmparser::{Validator, WasmFeatures};
8787
use wit_parser::{
88-
Function, FunctionKind, InterfaceId, LiveTypes, Resolve, Stability, Type, TypeDefKind, TypeId,
89-
TypeOwner, WorldItem, WorldKey,
88+
Function, FunctionKind, InterfaceId, LiveTypes, Param, Resolve, Stability, Type, TypeDefKind,
89+
TypeId, TypeOwner, WorldItem, WorldKey,
9090
abi::{AbiVariant, WasmSignature, WasmType},
9191
};
9292

@@ -167,7 +167,7 @@ impl RequiredOptions {
167167
// Lift the params and lower the results for imports
168168
ret.add_lift(TypeContents::for_types(
169169
resolve,
170-
func.params.iter().map(|(_, t)| t),
170+
func.params.iter().map(|p| &p.ty),
171171
));
172172
ret.add_lower(TypeContents::for_types(resolve, &func.result));
173173

@@ -188,7 +188,7 @@ impl RequiredOptions {
188188
// Lower the params and lift the results for exports
189189
ret.add_lower(TypeContents::for_types(
190190
resolve,
191-
func.params.iter().map(|(_, t)| t),
191+
func.params.iter().map(|p| &p.ty),
192192
));
193193
ret.add_lift(TypeContents::for_types(resolve, &func.result));
194194

@@ -2926,7 +2926,11 @@ impl<'a> Shims<'a> {
29262926
name: String::new(),
29272927
kind: FunctionKind::Freestanding,
29282928
params: match wit_param {
2929-
Some(ty) => vec![("a".to_string(), ty)],
2929+
Some(ty) => vec![Param {
2930+
name: "a".to_string(),
2931+
ty,
2932+
span: Default::default(),
2933+
}],
29302934
None => Vec::new(),
29312935
},
29322936
result: wit_result,
@@ -3024,7 +3028,11 @@ fn task_return_options_and_type(
30243028
name: String::new(),
30253029
kind: FunctionKind::Freestanding,
30263030
params: match ty {
3027-
Some(ty) => vec![("a".to_string(), ty)],
3031+
Some(ty) => vec![Param {
3032+
name: "a".to_string(),
3033+
ty,
3034+
span: Default::default(),
3035+
}],
30283036
None => Vec::new(),
30293037
},
30303038
result: None,

crates/wit-component/src/encoding/types.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,42 @@ use anyhow::Result;
33
use std::collections::HashMap;
44
use wasm_encoder::*;
55
use wit_parser::{
6-
Enum, Flags, Function, Handle, InterfaceId, Record, Resolve, Result_, Tuple, Type, TypeDefKind,
7-
TypeId, TypeOwner, Variant,
6+
Enum, Flags, Function, Handle, InterfaceId, Param, Record, Resolve, Result_, Tuple, Type,
7+
TypeDefKind, TypeId, TypeOwner, Variant,
88
};
99

10+
/// A view of `&[Param]` that compares and hashes by name and type only,
11+
/// ignoring source spans.
12+
#[derive(Clone)]
13+
struct ParamSignatures<'a>(&'a [Param]);
14+
15+
impl PartialEq for ParamSignatures<'_> {
16+
fn eq(&self, other: &Self) -> bool {
17+
self.0.len() == other.0.len()
18+
&& self
19+
.0
20+
.iter()
21+
.zip(other.0)
22+
.all(|(a, b)| a.name == b.name && a.ty == b.ty)
23+
}
24+
}
25+
26+
impl Eq for ParamSignatures<'_> {}
27+
28+
impl std::hash::Hash for ParamSignatures<'_> {
29+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
30+
for p in self.0 {
31+
p.name.hash(state);
32+
p.ty.hash(state);
33+
}
34+
}
35+
}
36+
1037
/// Represents a key type for interface function definitions.
1138
#[derive(Hash, PartialEq, Eq, Clone)]
1239
pub struct FunctionKey<'a> {
1340
async_: bool,
14-
params: &'a [(String, Type)],
41+
params: ParamSignatures<'a>,
1542
result: &'a Option<Type>,
1643
}
1744

@@ -102,7 +129,7 @@ pub trait ValtypeEncoder<'a> {
102129
fn encode_func_type(&mut self, resolve: &'a Resolve, func: &'a Function) -> Result<u32> {
103130
let key = FunctionKey {
104131
async_: func.kind.is_async(),
105-
params: &func.params,
132+
params: ParamSignatures(&func.params),
106133
result: &func.result,
107134
};
108135
if let Some(index) = self.type_encoding_maps().func_type_map.get(&key) {
@@ -128,11 +155,11 @@ pub trait ValtypeEncoder<'a> {
128155
fn encode_params(
129156
&mut self,
130157
resolve: &'a Resolve,
131-
params: &'a [(String, Type)],
158+
params: &'a [Param],
132159
) -> Result<Vec<(&'a str, ComponentValType)>> {
133160
params
134161
.iter()
135-
.map(|(name, ty)| Ok((name.as_str(), self.encode_valtype(resolve, ty)?)))
162+
.map(|p| Ok((p.name.as_str(), self.encode_valtype(resolve, &p.ty)?)))
136163
.collect::<Result<_>>()
137164
}
138165

crates/wit-component/src/printing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ impl<O: Output> WitPrinter<O> {
352352
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => 1,
353353
_ => 0,
354354
};
355-
for (i, (name, ty)) in func.params.iter().skip(params_to_skip).enumerate() {
355+
for (i, param) in func.params.iter().skip(params_to_skip).enumerate() {
356356
if i > 0 {
357357
self.output.str(", ");
358358
}
359-
self.print_name_param(name);
359+
self.print_name_param(&param.name);
360360
self.output.str(": ");
361-
self.print_type_name(resolve, ty)?;
361+
self.print_type_name(resolve, &param.ty)?;
362362
}
363363
self.output.str(")");
364364

crates/wit-dylib/src/bindgen.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn async_import_abi_area_types<'a>(
196196
let include_params = sig.indirect_params;
197197
func.params
198198
.iter()
199-
.filter_map(move |(_, ty)| if include_params { Some(ty) } else { None })
199+
.filter_map(move |p| if include_params { Some(&p.ty) } else { None })
200200
.chain(func.result.iter())
201201
}
202202

@@ -414,7 +414,11 @@ pub fn task_return(
414414
let mut dummy_func = func.clone();
415415
dummy_func.params = Vec::new();
416416
if let Some(ty) = dummy_func.result.take() {
417-
dummy_func.params.push(("x".to_string(), ty));
417+
dummy_func.params.push(wit_parser::Param {
418+
name: "x".to_string(),
419+
ty,
420+
span: Default::default(),
421+
});
418422
}
419423
import(
420424
adapter,
@@ -618,10 +622,7 @@ impl<'a> FunctionCompiler<'a> {
618622
// the responsibility of the caller to provide an appropriate parameter.
619623
let (abi_param_size, abi_param_align) = match abi {
620624
AbiVariant::GuestImport if sig.indirect_params => {
621-
let info = self
622-
.adapter
623-
.sizes
624-
.params(func.params.iter().map(|(_, ty)| ty));
625+
let info = self.adapter.sizes.params(func.params.iter().map(|p| &p.ty));
625626
(
626627
info.size.size_wasm32().try_into().unwrap(),
627628
info.align.align_wasm32().try_into().unwrap(),
@@ -700,7 +701,7 @@ impl<'a> FunctionCompiler<'a> {
700701
indirect_params: impl FnOnce(&Self) -> Memory,
701702
retptr: impl FnOnce(&Self) -> Memory,
702703
) {
703-
let tys = func.params.iter().map(|(_, ty)| ty);
704+
let tys = func.params.iter().map(|p| &p.ty);
704705

705706
if sig.indirect_params {
706707
let mem = indirect_params(self);
@@ -721,10 +722,8 @@ impl<'a> FunctionCompiler<'a> {
721722
if sig.retptr {
722723
self.free_temp_local(locals.pop().unwrap());
723724
}
724-
let dsts = self.record_field_locs(
725-
&AbiLoc::Stack(&locals),
726-
func.params.iter().map(|(_, ty)| ty),
727-
);
725+
let dsts =
726+
self.record_field_locs(&AbiLoc::Stack(&locals), func.params.iter().map(|p| &p.ty));
728727
for (param, dst) in tys.zip(dsts) {
729728
self.lower(*param, &dst);
730729
}
@@ -783,20 +782,17 @@ impl<'a> FunctionCompiler<'a> {
783782
AbiLoc::Stack(&temps)
784783
};
785784

786-
let srcs = self.record_field_locs(&src, func.params.iter().map(|(_, ty)| ty));
787-
for ((_, param), src) in func.params.iter().zip(srcs) {
788-
self.lift(&src, *param);
785+
let srcs = self.record_field_locs(&src, func.params.iter().map(|p| &p.ty));
786+
for (param, src) in func.params.iter().zip(srcs) {
787+
self.lift(&src, param.ty);
789788
}
790789

791790
// If parameters were passed indirectly than the caller of this export
792791
// made a dynamic allocation with `cabi_realloc`. Cleanup said
793792
// allocation here after all the parameters were lowered onto the stack.
794793
if let AbiLoc::Memory(mem) = &src {
795794
assert!(sig.indirect_params);
796-
let info = self
797-
.adapter
798-
.sizes
799-
.record(func.params.iter().map(|(_, ty)| ty));
795+
let info = self.adapter.sizes.record(func.params.iter().map(|p| &p.ty));
800796
self.local_get_ctx();
801797
self.ins().local_get(mem.addr.idx);
802798
assert_eq!(mem.offset, 0);

crates/wit-dylib/src/lib.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,7 @@ impl Adapter {
10151015
sync_import_elem_index,
10161016
async_import_elem_index,
10171017
async_import_lift_results_elem_index,
1018-
args: func
1019-
.params
1020-
.iter()
1021-
.map(|(_, ty)| self.lookup_ty(ty))
1022-
.collect(),
1018+
args: func.params.iter().map(|p| self.lookup_ty(&p.ty)).collect(),
10231019
result: func.result.map(|t| self.lookup_ty(&t)),
10241020
async_abi_area: self.async_import_abi_area(resolve, mangling, func),
10251021
})
@@ -1149,11 +1145,7 @@ impl Adapter {
11491145
interface: export.interface.map(|k| resolve.name_world_key(k)),
11501146
name: func.name.clone(),
11511147
async_export_task_return_elem_index,
1152-
args: func
1153-
.params
1154-
.iter()
1155-
.map(|(_, ty)| self.lookup_ty(ty))
1156-
.collect(),
1148+
args: func.params.iter().map(|p| self.lookup_ty(&p.ty)).collect(),
11571149
result: func.result.map(|t| self.lookup_ty(&t)),
11581150
})
11591151
}

crates/wit-dylib/tests/roundtrip.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::process::Command;
3939
use tempfile::TempDir;
4040
use wit_parser::decoding::DecodedWasm;
4141
use wit_parser::{
42-
Function, FunctionKind, Handle, Interface, Package, PackageName, Resolve, Type, TypeDef,
42+
Function, FunctionKind, Handle, Interface, Package, PackageName, Param, Resolve, Type, TypeDef,
4343
TypeDefKind, TypeOwner, World, WorldItem, WorldKey,
4444
};
4545

@@ -142,7 +142,11 @@ fn run_one(u: &mut Unstructured<'_>) -> Result<()> {
142142
Function {
143143
name: "set-seed".to_string(),
144144
kind: FunctionKind::Freestanding,
145-
params: vec![("seed".to_string(), Type::U64)],
145+
params: vec![Param {
146+
name: "seed".to_string(),
147+
ty: Type::U64,
148+
span: Default::default(),
149+
}],
146150
result: None,
147151
stability: Default::default(),
148152
docs: Default::default(),
@@ -215,8 +219,16 @@ fn run_one(u: &mut Unstructured<'_>) -> Result<()> {
215219
name: "run".to_string(),
216220
kind: FunctionKind::Freestanding,
217221
params: vec![
218-
("iters".to_string(), Type::U32),
219-
("seed".to_string(), Type::U64),
222+
Param {
223+
name: "iters".to_string(),
224+
ty: Type::U32,
225+
span: Default::default(),
226+
},
227+
Param {
228+
name: "seed".to_string(),
229+
ty: Type::U64,
230+
span: Default::default(),
231+
},
220232
],
221233
result: None,
222234
stability: Default::default(),
@@ -345,7 +357,11 @@ fn update_resources(resolve: &mut Resolve) {
345357
Function {
346358
name: ctor,
347359
kind: FunctionKind::Constructor(resource_id),
348-
params: vec![("rep".to_string(), Type::U32)],
360+
params: vec![Param {
361+
name: "rep".to_string(),
362+
ty: Type::U32,
363+
span: Default::default(),
364+
}],
349365
result: Some(Type::Id(own)),
350366
stability: Default::default(),
351367
docs: Default::default(),
@@ -357,7 +373,11 @@ fn update_resources(resolve: &mut Resolve) {
357373
Function {
358374
name: rep,
359375
kind: FunctionKind::Method(resource_id),
360-
params: vec![("self".to_string(), Type::Id(borrow))],
376+
params: vec![Param {
377+
name: "self".to_string(),
378+
ty: Type::Id(borrow),
379+
span: Default::default(),
380+
}],
361381
result: Some(Type::U32),
362382
stability: Default::default(),
363383
docs: Default::default(),

crates/wit-encoder/src/from_parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ impl<'a> Converter<'a> {
466466
}
467467
}
468468

469-
fn convert_params(&self, params: &[(String, wit_parser::Type)]) -> Params {
469+
fn convert_params(&self, params: &[wit_parser::Param]) -> Params {
470470
let mut output = Params::empty();
471-
for (name, ty) in params.iter() {
472-
let name = name.to_string();
473-
let ty = self.convert_type(ty);
471+
for param in params.iter() {
472+
let name = param.name.to_string();
473+
let ty = self.convert_type(&param.ty);
474474
output.push(name, ty);
475475
}
476476
output

crates/wit-parser/src/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl Resolve {
192192
// is needed down below for imports.
193193
let mut storage = [WasmType::I32; Self::MAX_FLAT_PARAMS + 1];
194194
let mut params = FlatTypes::new(&mut storage);
195-
let ok = self.push_flat_list(func.params.iter().map(|(_, param)| param), &mut params);
195+
let ok = self.push_flat_list(func.params.iter().map(|p| &p.ty), &mut params);
196196
assert_eq!(ok, !params.overflow);
197197

198198
let max = match variant {

0 commit comments

Comments
 (0)