Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ impl ComponentGenerator<'_> {
let mut config = Config::new();
config.wasm_component_model(true);
config.wasm_component_model_async(true);
config.wasm_component_model_map(true);

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

Expand Down
25 changes: 24 additions & 1 deletion src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ impl<'a> Summary<'a> {
TypeDefKind::List(ty) => {
self.visit_type(*ty, world);
}
TypeDefKind::Map(k, v) => {
self.visit_type(*k, world);
self.visit_type(*v, world);
}
TypeDefKind::Type(ty) => {
// When visiting a type alias, we must use the state
// already stored for any `use`d resources rather than
Expand Down Expand Up @@ -1127,6 +1131,10 @@ impl<'a> Summary<'a> {
TypeDefKind::List(ty) => {
self.sort(*ty, sorted, visited);
}
TypeDefKind::Map(k, v) => {
self.sort(*k, sorted, visited);
self.sort(*v, sorted, visited);
}
TypeDefKind::Type(ty) => {
self.sort(*ty, sorted, visited);
}
Expand Down Expand Up @@ -1759,6 +1767,7 @@ class {camel}(Protocol):
}
TypeDefKind::Tuple(_)
| TypeDefKind::List(_)
| TypeDefKind::Map(_, _)
| TypeDefKind::Option(_)
| TypeDefKind::Result(_)
| TypeDefKind::Handle(_) => (None, Vec::new()),
Expand Down Expand Up @@ -2089,7 +2098,7 @@ def {snake}_future(default: Callable[[], {camel}]) -> tuple[FutureWriter[{camel}
}

let python_imports =
"from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self, Callable
"from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Mapping, Any, Self, Callable
from types import TracebackType
from enum import Flag, Enum, auto
from dataclasses import dataclass
Expand Down Expand Up @@ -2356,6 +2365,10 @@ from componentize_py_types import Result, Ok, Err, Some
TypeDefKind::Option(ty) | TypeDefKind::List(ty) | TypeDefKind::Type(ty) => {
self.has_imported_and_exported_resource(*ty)
}
TypeDefKind::Map(k, v) => {
self.has_imported_and_exported_resource(*k)
|| self.has_imported_and_exported_resource(*v)
}
TypeDefKind::Resource => {
let empty = &ResourceInfo::default();
let info = self.resource_info.get(&id).unwrap_or(empty);
Expand Down Expand Up @@ -2465,6 +2478,13 @@ impl<'a> TypeNames<'a> {
format!("List[{}]", self.type_name(*ty, seen, resource))
}
}
TypeDefKind::Map(k, v) => {
format!(
"Mapping[{}, {}]",
self.type_name(*k, seen, resource),
self.type_name(*v, seen, resource)
)
}
TypeDefKind::Tuple(tuple) => {
let types = tuple
.types
Expand Down Expand Up @@ -2565,6 +2585,9 @@ impl<'a> TypeNames<'a> {
TypeDefKind::List(ty) => {
format!("list_{}", self.mangle_name(*ty))
}
TypeDefKind::Map(k, v) => {
format!("map_{}_{}", self.mangle_name(*k), self.mangle_name(*v))
}
TypeDefKind::Tuple(tuple) => {
let types = tuple
.types
Expand Down
1 change: 1 addition & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static ENGINE: Lazy<Engine> = Lazy::new(|| {
let mut config = Config::new();
config.wasm_component_model(true);
config.wasm_component_model_async(true);
config.wasm_component_model_map(true);

Engine::new(&config).unwrap()
});
Expand Down
32 changes: 32 additions & 0 deletions src/test/echoes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
anyhow::Result,
once_cell::sync::Lazy,
proptest::strategy::{Just, Strategy},
std::collections::HashMap,
wasmtime::{
Store,
component::{HasSelf, InstancePre, Linker},
Expand Down Expand Up @@ -136,6 +137,13 @@ impl componentize_py::test::echoes::Host for Ctx {
Ok(v)
}

async fn echo_map_u32_string(
&mut self,
v: HashMap<u32, String>,
) -> wasmtime::Result<HashMap<u32, String>> {
Ok(v)
}

async fn echo_option_u8(&mut self, v: Option<u8>) -> wasmtime::Result<Option<u8>> {
Ok(v)
}
Expand Down Expand Up @@ -307,6 +315,9 @@ class Echoes(exports.Echoes):
def echo_list_list_list_u8(self, v):
return echoes.echo_list_list_list_u8(v)

def echo_map_u32_string(self, v):
return echoes.echo_map_u32_string(v)

def echo_option_u8(self, v):
return echoes.echo_option_u8(v)

Expand Down Expand Up @@ -742,6 +753,27 @@ fn list_f64s() -> Result<()> {
)
}

#[test]
fn map_u32_strings() -> Result<()> {
TESTER.all_eq(
&proptest::collection::hash_map(
proptest::num::u32::ANY,
&proptest::string::string_regex(".*")?,
0..MAX_SIZE,
),
|v, instance, store, runtime| {
Ok(runtime.block_on(
instance
.componentize_py_test_echoes()
.call_echo_map_u32_string(
store,
v.iter().map(|(k, v)| (*k, v.as_str())).collect(),
),
)?)
},
)
}

#[test]
fn many() -> Result<()> {
TESTER.all_eq(
Expand Down
1 change: 1 addition & 0 deletions src/test/wit/echoes.wit
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface echoes {
echo-list-string: func(v: list<string>) -> list<string>;
echo-list-list-u8: func(v: list<list<u8>>) -> list<list<u8>>;
echo-list-list-list-u8: func(v: list<list<list<u8>>>) -> list<list<list<u8>>>;
echo-map-u32-string: func(v: map<u32, string>) -> map<u32, string>;
echo-option-u8: func(v: option<u8>) -> option<u8>;
echo-option-option-u8: func(v: option<option<u8>>) -> option<option<u8>>;
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>>;
Expand Down
Loading