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
6 changes: 4 additions & 2 deletions crates/wac-graph/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,8 @@ impl<'a> CompositionGraphEncoder<'a> {

// Populate the implicit argument map
for (name, node) in implicit_imports {
let (kind, index) = encoded[name];
let canonical = aggregator.canonical_import_name(name);
let (kind, index) = encoded[canonical];
state
.implicit_args
.entry(node)
Expand All @@ -1563,7 +1564,8 @@ impl<'a> CompositionGraphEncoder<'a> {

// Finally, populate the node indexes with the encoded explicit imports
for (name, node_index) in explicit_imports {
let (_, encoded_index) = encoded[name];
let canonical = aggregator.canonical_import_name(name);
let (_, encoded_index) = encoded[canonical];
state.node_indexes.insert(node_index, encoded_index);
}

Expand Down
30 changes: 24 additions & 6 deletions crates/wac-graph/src/plug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{types::SubtypeChecker, CompositionGraph, PackageId};
use crate::{
types::{are_semver_compatible, SubtypeChecker},
CompositionGraph, PackageId,
};
use thiserror::Error;

/// Represents an error that can occur when plugging components together.
Expand Down Expand Up @@ -28,31 +31,46 @@ pub fn plug(
let socket_instantiation = graph.instantiate(socket);

for plug in plugs {
let mut plug_exports = Vec::new();
// Collect matching (plug_export_name, socket_import_name) pairs.
// The names may differ when matched via semver compatibility.
let mut plug_exports: Vec<(String, String)> = Vec::new();
let mut cache = Default::default();
let mut checker = SubtypeChecker::new(&mut cache);
for (name, plug_ty) in &graph.types()[graph[plug].ty()].exports {
if let Some(socket_ty) = graph.types()[graph[socket].ty()].imports.get(name) {
// Try exact name match first, then fall back to semver-compatible match
let matching_import = graph.types()[graph[socket].ty()]
.imports
.get(name)
.map(|ty| (name.clone(), ty))
.or_else(|| {
graph.types()[graph[socket].ty()]
.imports
.iter()
.find(|(import_name, _)| are_semver_compatible(name, import_name))
.map(|(import_name, ty)| (import_name.clone(), ty))
});

if let Some((socket_name, socket_ty)) = matching_import {
if checker
.is_subtype(*plug_ty, graph.types(), *socket_ty, graph.types())
.is_ok()
{
plug_exports.push(name.clone());
plug_exports.push((name.clone(), socket_name));
}
}
}

// Instantiate the plug component
let mut plug_instantiation = None;
for plug_name in plug_exports {
for (plug_name, socket_name) in plug_exports {
log::debug!("using export `{plug_name}` for plug");
let plug_instantiation =
*plug_instantiation.get_or_insert_with(|| graph.instantiate(plug));
let export = graph
.alias_instance_export(plug_instantiation, &plug_name)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
graph
.set_instantiation_argument(socket_instantiation, &plug_name, export)
.set_instantiation_argument(socket_instantiation, &socket_name, export)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(component
(type (;0;)
(instance
(export (;0;) "w" (type (sub resource)))
(type (;1;) (record (field "x" u32)))
(export (;2;) "x" (type (eq 1)))
(type (;3;) (func (param "x" 2)))
(export (;0;) "y" (func (type 3)))
(export (;4;) "z" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
(type (;1;)
(component
(type (;0;)
(instance
(export (;0;) "w" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
)
)
(import "unlocked-dep=<test:higher>" (component (;0;) (type 1)))
(instance (;1;) (instantiate 0
(with "foo:bar/types@0.2.1" (instance 0))
)
)
(type (;2;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" u32)))
(export (;1;) "x" (type (eq 0)))
(type (;2;) (func (param "x" 1)))
(export (;0;) "y" (func (type 2)))
(export (;3;) "z" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.0" (instance (;0;) (type 0)))
)
)
(import "unlocked-dep=<test:lower>" (component (;1;) (type 2)))
(instance (;2;) (instantiate 1
(with "foo:bar/types@0.2.0" (instance 0))
)
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"packages": [
{
"name": "test:higher",
"path": "higher.wat"
},
{
"name": "test:lower",
"path": "lower.wat"
}
],
"nodes": [
{
"type": "instantiation",
"package": 0
},
{
"type": "instantiation",
"package": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(component
(type
(instance
(export "w" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (type 0)))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(component
(type
(instance
(type (record (field "x" u32)))
(export "x" (type (eq 0)))
(type (func (param "x" 1)))
(export "y" (func (type 2)))
(export "z" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.0" (instance (type 0)))
)
8 changes: 8 additions & 0 deletions crates/wac-graph/tests/graphs/simple-versioned/bar.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(component
(type
(instance
(export "w" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (type 0)))
)
47 changes: 47 additions & 0 deletions crates/wac-graph/tests/graphs/simple-versioned/encoded.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" u32)))
(export (;1;) "x" (type (eq 0)))
(type (;2;) (func (param "x" 1)))
(export (;0;) "y" (func (type 2)))
(export (;3;) "z" (type (sub resource)))
(export (;4;) "w" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
(type (;1;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "x" u32)))
(export (;1;) "x" (type (eq 0)))
(type (;2;) (func (param "x" 1)))
(export (;0;) "y" (func (type 2)))
(export (;3;) "z" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.0" (instance (;0;) (type 0)))
)
)
(import "unlocked-dep=<test:foo>" (component (;0;) (type 1)))
(instance (;1;) (instantiate 0
(with "foo:bar/types@0.2.0" (instance 0))
)
)
(type (;2;)
(component
(type (;0;)
(instance
(export (;0;) "w" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
)
)
(import "unlocked-dep=<test:bar>" (component (;1;) (type 2)))
(instance (;2;) (instantiate 1
(with "foo:bar/types@0.2.1" (instance 0))
)
)
)
12 changes: 12 additions & 0 deletions crates/wac-graph/tests/graphs/simple-versioned/foo.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(component
(type
(instance
(type (record (field "x" u32)))
(export "x" (type (eq 0)))
(type (func (param "x" 1)))
(export "y" (func (type 2)))
(export "z" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.0" (instance (type 0)))
)
22 changes: 22 additions & 0 deletions crates/wac-graph/tests/graphs/simple-versioned/graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"packages": [
{
"name": "test:foo",
"path": "foo.wat"
},
{
"name": "test:bar",
"path": "bar.wat"
}
],
"nodes": [
{
"type": "instantiation",
"package": 0
},
{
"type": "instantiation",
"package": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package foo:bar@0.2.0;

interface types {
resource r;
}

interface api {
use types.{r};
do-something: func(x: r);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test:a;

world w {
import foo:bar/types@0.2.0;
import foo:bar/api@0.2.0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package foo:bar@0.2.1;

interface types {
resource r;
resource s;
}

interface api {
use types.{r};
do-something: func(x: r);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test:b;

world w {
import foo:bar/types@0.2.1;
import foo:bar/api@0.2.1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(component
(type (;0;)
(instance
(export (;0;) "r" (type (sub resource)))
(export (;1;) "s" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
(alias export 0 "r" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "r" (type (eq 0)))
(type (;2;) (own 1))
(type (;3;) (func (param "x" 2)))
(export (;0;) "do-something" (func (type 3)))
)
)
(import "foo:bar/api@0.2.1" (instance (;1;) (type 2)))
(type (;3;)
(component
(type (;0;)
(instance
(export (;0;) "r" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.0" (instance (;0;) (type 0)))
(alias export 0 "r" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "r" (type (eq 0)))
(type (;2;) (own 1))
(type (;3;) (func (param "x" 2)))
(export (;0;) "do-something" (func (type 3)))
)
)
(import "foo:bar/api@0.2.0" (instance (;1;) (type 2)))
)
)
(import "unlocked-dep=<test:a>" (component (;0;) (type 3)))
(instance (;2;) (instantiate 0
(with "foo:bar/types@0.2.0" (instance 0))
(with "foo:bar/api@0.2.0" (instance 1))
)
)
(type (;4;)
(component
(type (;0;)
(instance
(export (;0;) "r" (type (sub resource)))
(export (;1;) "s" (type (sub resource)))
)
)
(import "foo:bar/types@0.2.1" (instance (;0;) (type 0)))
(alias export 0 "r" (type (;1;)))
(type (;2;)
(instance
(alias outer 1 1 (type (;0;)))
(export (;1;) "r" (type (eq 0)))
(type (;2;) (own 1))
(type (;3;) (func (param "x" 2)))
(export (;0;) "do-something" (func (type 3)))
)
)
(import "foo:bar/api@0.2.1" (instance (;1;) (type 2)))
)
)
(import "unlocked-dep=<test:b>" (component (;1;) (type 4)))
(instance (;3;) (instantiate 1
(with "foo:bar/types@0.2.1" (instance 0))
(with "foo:bar/api@0.2.1" (instance 1))
)
)
)
Loading