Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/wac-graph/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ impl<'a> TypeEncoder<'a> {
DefinedType::Tuple(types) => self.tuple(state, types),
DefinedType::List(ty) => self.list(state, *ty),
DefinedType::FixedSizeList(ty, elements) => self.fixed_size_list(state, *ty, *elements),
DefinedType::Map(key, value) => self.map(state, *key, *value),
DefinedType::Option(ty) => self.option(state, *ty),
DefinedType::Result { ok, err } => self.result(state, *ok, *err),
DefinedType::Variant(v) => self.variant(state, v),
Expand Down Expand Up @@ -640,6 +641,14 @@ impl<'a> TypeEncoder<'a> {
index
}

fn map(&self, state: &mut State, key: ValueType, value: ValueType) -> u32 {
let key = self.value_type(state, key);
let value = self.value_type(state, value);
let index = state.current.encodable.type_count();
state.current.encodable.ty().defined_type().map(key, value);
index
}

fn fixed_size_list(&self, state: &mut State, ty: ValueType, elements: u32) -> u32 {
let ty = self.value_type(state, ty);
let index = state.current.encodable.type_count();
Expand Down
8 changes: 8 additions & 0 deletions crates/wac-graph/tests/graphs/map-type-mismatch/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
failed to add argument edge from source node 2 to target node 1 referenced in argument 0 for test case `map-type-mismatch`

Caused by:
0: mismatched instantiation argument `test:foo/consume`
1: mismatched type for export `lookup`
2: mismatched type for function parameter `m`
3: mismatched type for map value
4: expected u64, found u32
14 changes: 14 additions & 0 deletions crates/wac-graph/tests/graphs/map-type-mismatch/foo.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test:foo;

interface produce {
lookup: func(m: map<string, u32>) -> u32;
}

interface consume {
lookup: func(m: map<string, u64>) -> u32;
}

world w {
import consume;
export produce;
}
30 changes: 30 additions & 0 deletions crates/wac-graph/tests/graphs/map-type-mismatch/graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"packages": [
{
"name": "test:foo",
"path": "foo.wit"
}
],
"nodes": [
{
"type": "instantiation",
"package": 0
},
{
"type": "instantiation",
"package": 0
},
{
"type": "alias",
"source": 0,
"export": "test:foo/produce"
}
],
"arguments": [
{
"source": 2,
"target": 1,
"name": "test:foo/consume"
}
]
}
63 changes: 63 additions & 0 deletions crates/wac-graph/tests/graphs/map-types/encoded.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
(component
(type (;0;)
(instance
(type (;0;) (map string string))
(export (;1;) "headers" (type (eq 0)))
(type (;2;) (map string u32))
(type (;3;) (option u32))
(type (;4;) (func (param "m" 2) (result 3)))
(export (;0;) "lookup" (func (type 4)))
(type (;5;) (list u8))
(type (;6;) (map string 5))
(type (;7;) (func (param "h" 1) (result 6)))
(export (;1;) "bundle" (func (type 7)))
)
)
(import "test:foo/i" (instance (;0;) (type 0)))
(type (;1;)
(component
(type (;0;)
(instance
(type (;0;) (map string string))
(export (;1;) "headers" (type (eq 0)))
(type (;2;) (map string u32))
(type (;3;) (option u32))
(type (;4;) (func (param "m" 2) (result 3)))
(export (;0;) "lookup" (func (type 4)))
(type (;5;) (list u8))
(type (;6;) (map string 5))
(type (;7;) (func (param "h" 1) (result 6)))
(export (;1;) "bundle" (func (type 7)))
)
)
(import "test:foo/i" (instance (;0;) (type 0)))
(type (;1;)
(instance
(type (;0;) (map string string))
(export (;1;) "headers" (type (eq 0)))
(type (;2;) (map string u32))
(type (;3;) (option u32))
(type (;4;) (func (param "m" 2) (result 3)))
(export (;0;) "lookup" (func (type 4)))
(type (;5;) (list u8))
(type (;6;) (map string 5))
(type (;7;) (func (param "h" 1) (result 6)))
(export (;1;) "bundle" (func (type 7)))
)
)
(export (;1;) "test:foo/i" (instance (type 1)))
)
)
(import "unlocked-dep=<test:foo>" (component (;0;) (type 1)))
(instance (;1;) (instantiate 0
(with "test:foo/i" (instance 0))
)
)
(alias export 1 "test:foo/i" (instance (;2;)))
(instance (;3;) (instantiate 0
(with "test:foo/i" (instance 2))
)
)
(alias export 3 "test:foo/i" (instance (;4;)))
(export (;5;) "test:foo/i" (instance 4))
)
12 changes: 12 additions & 0 deletions crates/wac-graph/tests/graphs/map-types/foo.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test:foo;

interface i {
type headers = map<string, string>;
lookup: func(m: map<string, u32>) -> option<u32>;
bundle: func(h: headers) -> map<string, list<u8>>;
}

world w {
import i;
export i;
}
41 changes: 41 additions & 0 deletions crates/wac-graph/tests/graphs/map-types/graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"packages": [
{
"name": "test:foo",
"path": "foo.wit"
}
],
"nodes": [
{
"type": "instantiation",
"package": 0
},
{
"type": "instantiation",
"package": 0
},
{
"type": "alias",
"source": 0,
"export": "test:foo/i"
},
{
"type": "alias",
"source": 1,
"export": "test:foo/i"
}
],
"arguments": [
{
"source": 2,
"target": 1,
"name": "test:foo/i"
}
],
"exports": [
{
"node": 3,
"name": "test:foo/i"
}
]
}
4 changes: 4 additions & 0 deletions crates/wac-types/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,10 @@ impl TypeAggregator {
DefinedType::FixedSizeList(ty, elements) => {
DefinedType::FixedSizeList(self.remap_value_type(types, *ty, checker)?, *elements)
}
DefinedType::Map(key, value) => DefinedType::Map(
self.remap_value_type(types, *key, checker)?,
self.remap_value_type(types, *value, checker)?,
),
DefinedType::Option(ty) => {
DefinedType::Option(self.remap_value_type(types, *ty, checker)?)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/wac-types/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ impl<'a> SubtypeChecker<'a> {
self.value_type(*a, at, *b, bt)
.context("mismatched type for fixed size list element")
}
(DefinedType::Map(akey, avalue), DefinedType::Map(bkey, bvalue)) => {
self.value_type(*akey, at, *bkey, bt)
.context("mismatched type for map key")?;
self.value_type(*avalue, at, *bvalue, bt)
.context("mismatched type for map value")
}
(DefinedType::Future(a), DefinedType::Future(b)) => self
.payload(*a, at, *b, bt)
.context("mismatched type for future payload"),
Expand Down Expand Up @@ -616,6 +622,7 @@ impl<'a> SubtypeChecker<'a> {
(DefinedType::Tuple(_), _)
| (DefinedType::List(_), _)
| (DefinedType::FixedSizeList(_, _), _)
| (DefinedType::Map(_, _), _)
| (DefinedType::Option(_), _)
| (DefinedType::Result { .. }, _)
| (DefinedType::Variant(_), _)
Expand Down
8 changes: 8 additions & 0 deletions crates/wac-types/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ pub enum DefinedType {
List(ValueType),
/// A fixed size array
FixedSizeList(ValueType, u32),
/// A map type.
Map(ValueType, ValueType),
/// An option type.
Option(ValueType),
/// A result type.
Expand Down Expand Up @@ -692,6 +694,7 @@ impl DefinedType {
match self {
Self::Tuple(tys) => tys.iter().any(|ty| ty.contains_borrow(types)),
Self::List(ty) | Self::FixedSizeList(ty, _) => ty.contains_borrow(types),
Self::Map(key, value) => key.contains_borrow(types) || value.contains_borrow(types),
Self::Option(ty) => ty.contains_borrow(types),
Self::Result { ok, err } => {
ok.map(|ty| ty.contains_borrow(types)).unwrap_or(false)
Expand Down Expand Up @@ -726,6 +729,10 @@ impl DefinedType {
DefinedType::List(ty) | DefinedType::Option(ty) | DefinedType::FixedSizeList(ty, _) => {
ty._visit_defined_types(types, visitor, false)
}
DefinedType::Map(key, value) => {
key._visit_defined_types(types, visitor, false)?;
value._visit_defined_types(types, visitor, false)
}
DefinedType::Result { ok, err } => {
if let Some(ty) = ok.as_ref() {
ty._visit_defined_types(types, visitor, false)?;
Expand Down Expand Up @@ -768,6 +775,7 @@ impl DefinedType {
Self::Tuple(_) => "tuple",
Self::List(_) => "list",
Self::FixedSizeList(_, _) => "list<,N>",
Self::Map(_, _) => "map",
Self::Option(_) => "option",
Self::Result { .. } => "result",
Self::Variant(_) => "variant",
Expand Down
9 changes: 7 additions & 2 deletions crates/wac-types/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,13 @@ impl<'a> TypeConverter<'a> {
.add_defined_type(DefinedType::FixedSizeList(ty, *length)),
)
}
wasm::ComponentDefinedType::Map { .. } => {
bail!("ComponentDefinedType::Map is not yet supported");
wasm::ComponentDefinedType::Map { key, value, .. } => {
let key_ty = self.component_val_type(*key)?;
let value_ty = self.component_val_type(*value)?;
ValueType::Defined(
self.types
.add_defined_type(DefinedType::Map(key_ty, value_ty)),
)
}
};

Expand Down
Loading