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
51 changes: 48 additions & 3 deletions crates/wac-graph/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ impl<'a> TypeEncoder<'a> {
Self(types)
}

// Aliases a value type from an imported instance that exports it, rather
// than re-encoding it locally: a type import must reference a named type,
// not a local definition.
fn alias_from_instance_export(&self, state: &mut State, ty: ValueType) -> Option<u32> {
if state.current.instances.is_empty() {
return None;
}
let ty = Type::Value(ty);
let (instance, name) = self.0.interfaces().find_map(|interface| {
let iid = interface.id.as_ref()?;
let instance = *state.current.instances.get(iid)?;
interface.exports.iter().find_map(|(name, kind)| {
(*kind == ItemKind::Type(ty)).then(|| (instance, name.clone()))
})
})?;
let index = state.current.encodable.type_count();
state.current.encodable.alias(Alias::InstanceExport {
instance,
kind: ComponentExportKind::Type,
name: &name,
});
state.current.type_indexes.insert(ty, index);
Some(index)
}

pub fn ty(&self, state: &mut State, ty: Type, name: Option<&str>) -> u32 {
if let Some(index) = state.current.type_indexes.get(&ty) {
return *index;
Expand Down Expand Up @@ -264,7 +289,12 @@ impl<'a> TypeEncoder<'a> {
index
}

fn use_aliases(&self, state: &mut State, uses: &'a IndexMap<String, UsedType>) {
fn use_aliases(
&self,
state: &mut State,
uses: &'a IndexMap<String, UsedType>,
items: &'a IndexMap<String, ItemKind>,
) {
state.current.type_aliases.clear();

for (name, used) in uses {
Expand All @@ -286,6 +316,17 @@ impl<'a> TypeEncoder<'a> {
);

state.current.type_aliases.insert(name.clone(), index);

// Record the alias under both ids that denote this used type (the
// interface's export and the using entity's own item) so a later
// id-keyed reference reuses it instead of re-encoding a local copy:
// a type import must reference a named type, not a local definition.
if let ItemKind::Type(ty) = kind {
state.current.type_indexes.insert(*ty, index);
}
if let Some(ItemKind::Type(ty)) = items.get(name) {
state.current.type_indexes.insert(*ty, index);
}
}
}

Expand All @@ -297,7 +338,7 @@ impl<'a> TypeEncoder<'a> {
}

// Encode any required aliases
self.use_aliases(state, &interface.uses);
self.use_aliases(state, &interface.uses, &interface.exports);
state.push(Encodable::Instance(InstanceType::default()));

// Otherwise, export all exports
Expand Down Expand Up @@ -335,7 +376,7 @@ impl<'a> TypeEncoder<'a> {
self.import_deps(state, used.interface);
}

self.use_aliases(state, &world.uses);
self.use_aliases(state, &world.uses, &world.imports);

for (name, kind) in &world.imports {
self.import(state, name, *kind);
Expand Down Expand Up @@ -516,6 +557,10 @@ impl<'a> TypeEncoder<'a> {
return ComponentValType::Type(*index);
}

if let Some(index) = self.alias_from_instance_export(state, ty) {
return ComponentValType::Type(index);
}

let index = match ty {
ValueType::Primitive(ty) => return ComponentValType::Primitive(ty.into()),
ValueType::Borrow(id) => self.borrow(state, id),
Expand Down
8 changes: 6 additions & 2 deletions crates/wac-graph/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,12 @@ impl<'a> CompositionGraphEncoder<'a> {

let mut encoded = HashMap::new();

// Next encode the imports
for (name, kind) in aggregator.imports() {
// Instances first, so a later type import can alias a type from an
// instance export instead of re-encoding it as a local definition.
let (instances, rest): (Vec<_>, Vec<_>) = aggregator
.imports()
.partition(|(_, kind)| matches!(kind, ItemKind::Instance(_)));
for (name, kind) in instances.into_iter().chain(rest) {
log::debug!("import `{name}` is being imported");
let index = self.import(state, name, aggregator.types(), kind);
encoded.insert(name, (kind.into(), index));
Expand Down
21 changes: 10 additions & 11 deletions crates/wac-graph/tests/graphs/implicit-resource-import/encoded.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
)
(import "foo:dependency/types" (instance (;0;) (type 0)))
(alias export 0 "x" (type (;1;)))
(import "x" (type (;2;) (eq 1)))
(alias export 0 "x" (type (;3;)))
(type (;4;)
(type (;2;)
(instance
(alias outer 1 3 (type (;0;)))
(alias outer 1 1 (type (;0;)))
(export (;1;) "x" (type (eq 0)))
(type (;2;) (own 1))
(type (;3;) (func (result 2)))
(export (;0;) "my-func" (func (type 3)))
)
)
(import "foo:test-import/my-interface" (instance (;1;) (type 4)))
(type (;5;)
(import "foo:test-import/my-interface" (instance (;1;) (type 2)))
(import "x" (type (;3;) (eq 1)))
(type (;4;)
(component
(type (;0;)
(instance
Expand All @@ -28,30 +27,30 @@
(export (;0;) "foo:dependency/types" (instance (type 0)))
)
)
(import "unlocked-dep=<test:bar>" (component (;0;) (type 5)))
(import "unlocked-dep=<test:bar>" (component (;0;) (type 4)))
(instance (;2;) (instantiate 0))
(alias export 1 "my-func" (func (;0;)))
(alias export 2 "foo:dependency/types" (instance (;3;)))
(type (;6;)
(type (;5;)
(component
(type (;0;)
(instance
(export (;0;) "x" (type (sub resource)))
)
)
(import "foo:dependency/types" (instance (;0;) (type 0)))
(alias outer 1 3 (type (;1;)))
(alias outer 1 1 (type (;1;)))
(import "x" (type (;2;) (eq 1)))
(type (;3;) (own 2))
(type (;4;) (func (result 3)))
(import "my-func" (func (;0;) (type 4)))
)
)
(import "unlocked-dep=<test:baz>" (component (;1;) (type 6)))
(import "unlocked-dep=<test:baz>" (component (;1;) (type 5)))
(instance (;4;) (instantiate 1
(with "foo:dependency/types" (instance 3))
(with "my-func" (func 0))
(with "x" (type 2))
(with "x" (type 3))
)
)
)
8 changes: 8 additions & 0 deletions crates/wac-graph/tests/graphs/nested-use-type/cons/cons.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test:cons;

world cons {
import test:prod/defs;
use test:prod/defs.{region};
import make: func() -> region;
export run: func() -> s32;
}
15 changes: 15 additions & 0 deletions crates/wac-graph/tests/graphs/nested-use-type/cons/deps/prod.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test:prod;

interface ext {
record point {
x: s32,
}
}

interface defs {
use ext.{point};

record region {
inner: point,
}
}
102 changes: 102 additions & 0 deletions crates/wac-graph/tests/graphs/nested-use-type/encoded.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" s32)))
(export (;1;) "point" (type (eq 0)))
)
)
(import "test:prod/ext" (instance (;0;) (type 0)))
(alias export 0 "point" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "point" (type (eq 0)))
(type (;2;) (record (field "inner" 1)))
(export (;3;) "region" (type (eq 2)))
)
)
(import "test:prod/defs" (instance (;1;) (type 2)))
(type (;3;) (record (field "inner" 1)))
(import "region" (type (;4;) (eq 3)))
(type (;5;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" s32)))
(export (;1;) "point" (type (eq 0)))
)
)
(import "test:prod/ext" (instance (;0;) (type 0)))
(alias export 0 "point" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "point" (type (eq 0)))
(type (;2;) (record (field "inner" 1)))
(export (;3;) "region" (type (eq 2)))
)
)
(import "test:prod/defs" (instance (;1;) (type 2)))
(type (;3;) (record (field "inner" 1)))
(import "region" (type (;4;) (eq 3)))
(type (;5;) (func (result 4)))
(export (;0;) "make" (func (type 5)))
(alias export 0 "point" (type (;6;)))
(type (;7;)
(instance
(alias outer 1 6 (type (;0;)))
(export (;1;) "point" (type (eq 0)))
(type (;2;) (record (field "inner" 1)))
(export (;3;) "region" (type (eq 2)))
)
)
(export (;2;) "test:prod/defs" (instance (type 7)))
)
)
(import "unlocked-dep=<test:prod>" (component (;0;) (type 5)))
(instance (;2;) (instantiate 0
(with "test:prod/ext" (instance 0))
(with "test:prod/defs" (instance 1))
(with "region" (type 4))
)
)
(alias export 2 "test:prod/defs" (instance (;3;)))
(alias export 2 "make" (func (;0;)))
(type (;6;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" s32)))
(export (;1;) "point" (type (eq 0)))
)
)
(import "test:prod/ext" (instance (;0;) (type 0)))
(alias export 0 "point" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "point" (type (eq 0)))
(type (;2;) (record (field "inner" 1)))
(export (;3;) "region" (type (eq 2)))
)
)
(import "test:prod/defs" (instance (;1;) (type 2)))
(type (;3;) (record (field "inner" 1)))
(import "region" (type (;4;) (eq 3)))
(type (;5;) (func (result 4)))
(import "make" (func (;0;) (type 5)))
(type (;6;) (func (result s32)))
(export (;1;) "run" (func (type 6)))
)
)
(import "unlocked-dep=<test:cons>" (component (;1;) (type 6)))
(instance (;4;) (instantiate 1
(with "make" (func 0))
(with "test:prod/defs" (instance 3))
(with "test:prod/ext" (instance 0))
(with "region" (type 4))
)
)
(alias export 4 "run" (func (;1;)))
(export (;2;) "run" (func 1))
)
55 changes: 55 additions & 0 deletions crates/wac-graph/tests/graphs/nested-use-type/graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"packages": [
{
"name": "test:prod",
"path": "prod.wit"
},
{
"name": "test:cons",
"path": "cons"
}
],
"nodes": [
{
"type": "instantiation",
"package": 0
},
{
"type": "alias",
"source": 0,
"export": "test:prod/defs"
},
{
"type": "alias",
"source": 0,
"export": "make"
},
{
"type": "instantiation",
"package": 1
},
{
"type": "alias",
"source": 3,
"export": "run"
}
],
"arguments": [
{
"source": 1,
"target": 3,
"name": "test:prod/defs"
},
{
"source": 2,
"target": 3,
"name": "make"
}
],
"exports": [
{
"node": 4,
"name": "run"
}
]
}
22 changes: 22 additions & 0 deletions crates/wac-graph/tests/graphs/nested-use-type/prod.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test:prod;

interface ext {
record point {
x: s32,
}
}

interface defs {
use ext.{point};

record region {
inner: point,
}
}

world prod {
import ext;
export defs;
use defs.{region};
export make: func() -> region;
}
Loading
Loading