Skip to content

Commit 0df9615

Browse files
authored
add support for the WIT map type (#239)
Fixes #231
1 parent b7e6ebf commit 0df9615

5 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ impl ComponentGenerator<'_> {
813813
let mut config = Config::new();
814814
config.wasm_component_model(true);
815815
config.wasm_component_model_async(true);
816+
config.wasm_component_model_map(true);
816817

817818
let engine = Engine::new(&config)?;
818819

src/summary.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ impl<'a> Summary<'a> {
308308
TypeDefKind::List(ty) => {
309309
self.visit_type(*ty, world);
310310
}
311+
TypeDefKind::Map(k, v) => {
312+
self.visit_type(*k, world);
313+
self.visit_type(*v, world);
314+
}
311315
TypeDefKind::Type(ty) => {
312316
// When visiting a type alias, we must use the state
313317
// already stored for any `use`d resources rather than
@@ -1127,6 +1131,10 @@ impl<'a> Summary<'a> {
11271131
TypeDefKind::List(ty) => {
11281132
self.sort(*ty, sorted, visited);
11291133
}
1134+
TypeDefKind::Map(k, v) => {
1135+
self.sort(*k, sorted, visited);
1136+
self.sort(*v, sorted, visited);
1137+
}
11301138
TypeDefKind::Type(ty) => {
11311139
self.sort(*ty, sorted, visited);
11321140
}
@@ -1759,6 +1767,7 @@ class {camel}(Protocol):
17591767
}
17601768
TypeDefKind::Tuple(_)
17611769
| TypeDefKind::List(_)
1770+
| TypeDefKind::Map(_, _)
17621771
| TypeDefKind::Option(_)
17631772
| TypeDefKind::Result(_)
17641773
| TypeDefKind::Handle(_) => (None, Vec::new()),
@@ -2089,7 +2098,7 @@ def {snake}_future(default: Callable[[], {camel}]) -> tuple[FutureWriter[{camel}
20892098
}
20902099

20912100
let python_imports =
2092-
"from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self, Callable
2101+
"from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Mapping, Any, Self, Callable
20932102
from types import TracebackType
20942103
from enum import Flag, Enum, auto
20952104
from dataclasses import dataclass
@@ -2356,6 +2365,10 @@ from componentize_py_types import Result, Ok, Err, Some
23562365
TypeDefKind::Option(ty) | TypeDefKind::List(ty) | TypeDefKind::Type(ty) => {
23572366
self.has_imported_and_exported_resource(*ty)
23582367
}
2368+
TypeDefKind::Map(k, v) => {
2369+
self.has_imported_and_exported_resource(*k)
2370+
|| self.has_imported_and_exported_resource(*v)
2371+
}
23592372
TypeDefKind::Resource => {
23602373
let empty = &ResourceInfo::default();
23612374
let info = self.resource_info.get(&id).unwrap_or(empty);
@@ -2465,6 +2478,13 @@ impl<'a> TypeNames<'a> {
24652478
format!("List[{}]", self.type_name(*ty, seen, resource))
24662479
}
24672480
}
2481+
TypeDefKind::Map(k, v) => {
2482+
format!(
2483+
"Mapping[{}, {}]",
2484+
self.type_name(*k, seen, resource),
2485+
self.type_name(*v, seen, resource)
2486+
)
2487+
}
24682488
TypeDefKind::Tuple(tuple) => {
24692489
let types = tuple
24702490
.types
@@ -2565,6 +2585,9 @@ impl<'a> TypeNames<'a> {
25652585
TypeDefKind::List(ty) => {
25662586
format!("list_{}", self.mangle_name(*ty))
25672587
}
2588+
TypeDefKind::Map(k, v) => {
2589+
format!("map_{}_{}", self.mangle_name(*k), self.mangle_name(*v))
2590+
}
25682591
TypeDefKind::Tuple(tuple) => {
25692592
let types = tuple
25702593
.types

src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static ENGINE: Lazy<Engine> = Lazy::new(|| {
3838
let mut config = Config::new();
3939
config.wasm_component_model(true);
4040
config.wasm_component_model_async(true);
41+
config.wasm_component_model_map(true);
4142

4243
Engine::new(&config).unwrap()
4344
});

src/test/echoes.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use {
33
anyhow::Result,
44
once_cell::sync::Lazy,
55
proptest::strategy::{Just, Strategy},
6+
std::collections::HashMap,
67
wasmtime::{
78
Store,
89
component::{HasSelf, InstancePre, Linker},
@@ -136,6 +137,13 @@ impl componentize_py::test::echoes::Host for Ctx {
136137
Ok(v)
137138
}
138139

140+
async fn echo_map_u32_string(
141+
&mut self,
142+
v: HashMap<u32, String>,
143+
) -> wasmtime::Result<HashMap<u32, String>> {
144+
Ok(v)
145+
}
146+
139147
async fn echo_option_u8(&mut self, v: Option<u8>) -> wasmtime::Result<Option<u8>> {
140148
Ok(v)
141149
}
@@ -307,6 +315,9 @@ class Echoes(exports.Echoes):
307315
def echo_list_list_list_u8(self, v):
308316
return echoes.echo_list_list_list_u8(v)
309317
318+
def echo_map_u32_string(self, v):
319+
return echoes.echo_map_u32_string(v)
320+
310321
def echo_option_u8(self, v):
311322
return echoes.echo_option_u8(v)
312323
@@ -742,6 +753,27 @@ fn list_f64s() -> Result<()> {
742753
)
743754
}
744755

756+
#[test]
757+
fn map_u32_strings() -> Result<()> {
758+
TESTER.all_eq(
759+
&proptest::collection::hash_map(
760+
proptest::num::u32::ANY,
761+
&proptest::string::string_regex(".*")?,
762+
0..MAX_SIZE,
763+
),
764+
|v, instance, store, runtime| {
765+
Ok(runtime.block_on(
766+
instance
767+
.componentize_py_test_echoes()
768+
.call_echo_map_u32_string(
769+
store,
770+
v.iter().map(|(k, v)| (*k, v.as_str())).collect(),
771+
),
772+
)?)
773+
},
774+
)
775+
}
776+
745777
#[test]
746778
fn many() -> Result<()> {
747779
TESTER.all_eq(

src/test/wit/echoes.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface echoes {
3030
echo-list-string: func(v: list<string>) -> list<string>;
3131
echo-list-list-u8: func(v: list<list<u8>>) -> list<list<u8>>;
3232
echo-list-list-list-u8: func(v: list<list<list<u8>>>) -> list<list<list<u8>>>;
33+
echo-map-u32-string: func(v: map<u32, string>) -> map<u32, string>;
3334
echo-option-u8: func(v: option<u8>) -> option<u8>;
3435
echo-option-option-u8: func(v: option<option<u8>>) -> option<option<u8>>;
3536
echo-many: func(v1: bool, v2: u8, v3: u16, v4: u32, v5: u64, v6: s8, v7: s16, v8: s32, v9: s64, v10: f32, v11: f64, v12: char, v13: string, v14: list<bool>, v15: list<u8>, v16: list<u16>) -> tuple<bool, u8, u16, u32, u64, s8, s16, s32, s64, f32, f64, char, string, list<bool>, list<u8>, list<u16>>;

0 commit comments

Comments
 (0)