Skip to content

Commit d560df7

Browse files
committed
Add test case and code review feedback.
Also fixed an enum variant misspelling and an out of date doc comment.
1 parent 1b53a07 commit d560df7

7 files changed

Lines changed: 150 additions & 39 deletions

File tree

crates/wac-graph/src/encoding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ impl Default for Encodable {
9696

9797
#[derive(Default)]
9898
pub struct Scope {
99-
/// The map of types to their encoded indexes.
99+
/// The map from types to encoded type index.
100100
pub type_indexes: IndexMap<Type, u32>,
101-
/// The map of imported instances in this scope.
101+
/// The map from interface name (i.e. id) to encoded instance index.
102102
pub instances: IndexMap<String, u32>,
103103
/// The map of import/export name to their alias indexes.
104104
type_aliases: IndexMap<String, u32>,

crates/wac-graph/src/graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub enum Error {
193193
MustExportDefinition,
194194
/// An implicit import on an instantiation conflicts with an explicit import node.
195195
#[error("an instantiation of package `{package}` implicitly imports an item named `{name}`, but it conflicts with an explicit import node of the same name")]
196-
ImplicitImportConfig {
196+
ImplicitImportConflict {
197197
/// The existing import node.
198198
import: NodeId,
199199
/// The instantiation node with the implicit import
@@ -627,7 +627,7 @@ impl CompositionGraph {
627627
///
628628
/// Initially the instantiation will have no satisfied arguments.
629629
///
630-
/// Use `add_argument_edge` to satisfy an argument on an instantiation node.
630+
/// Use `set_instantiation_argument` to set an argument on an instantiation node.
631631
pub fn instantiate(&mut self, package: PackageId) -> GraphResult<NodeId> {
632632
let pkg = self.get_package(package).ok_or(Error::InvalidPackageId)?;
633633
let node = self.alloc_node(NodeData::new(
@@ -1376,7 +1376,7 @@ impl<'a> CompositionGraphEncoder<'a> {
13761376
.filter(|(i, _)| !data.is_arg_satisfied(*i))
13771377
{
13781378
if let Some(import) = self.0.imports.get(name).copied() {
1379-
return Err(Error::ImplicitImportConfig {
1379+
return Err(Error::ImplicitImportConflict {
13801380
import,
13811381
instantiation: node,
13821382
package: PackageKey::new(package),

crates/wac-graph/tests/encoding.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ impl GraphFile {
111111
Ok(graph)
112112
}
113113

114+
fn load_wit_package(test_case: &str, path: &Path) -> Result<Vec<u8>> {
115+
let mut resolve = Resolve::default();
116+
let (id, _) = resolve.push_path(path).with_context(|| {
117+
format!(
118+
"failed to parse package file `{path}` for test case `{test_case}`",
119+
path = path.display()
120+
)
121+
})?;
122+
let world = resolve.select_world(id, None).with_context(|| {
123+
format!(
124+
"failed to select world from `{path}` for test case `{test_case}`",
125+
path = path.display()
126+
)
127+
})?;
128+
129+
let mut module = wit_component::dummy_module(&resolve, world);
130+
wit_component::embed_component_metadata(
131+
&mut module,
132+
&resolve,
133+
world,
134+
StringEncoding::default(),
135+
)
136+
.with_context(|| {
137+
format!(
138+
"failed to embed component metadata from package `{path}` for test case `{test_case}`",
139+
path = path.display()
140+
)
141+
})?;
142+
143+
let encoder = ComponentEncoder::default().validate(true).module(&module)?;
144+
encoder
145+
.encode()
146+
.with_context(|| format!("failed to encode a component from module derived from package `{path}` for test case `{test_case}`", path = path.display()))
147+
}
148+
114149
fn register_packages(
115150
&self,
116151
root: &Path,
@@ -122,40 +157,7 @@ impl GraphFile {
122157
for (index, package) in self.packages.iter().enumerate() {
123158
let path = root.join(&package.path);
124159
let bytes = match path.extension().and_then(OsStr::to_str) {
125-
Some("wit") => {
126-
let mut resolve = Resolve::default();
127-
let id = resolve.push_file(&path).with_context(|| {
128-
format!(
129-
"failed to parse package file `{path}` for test case `{test_case}`",
130-
path = path.display()
131-
)
132-
})?;
133-
let world = resolve.select_world(id, None).with_context(|| {
134-
format!(
135-
"failed to select world from `{path}` for test case `{test_case}`",
136-
path = path.display()
137-
)
138-
})?;
139-
140-
let mut module = wit_component::dummy_module(&resolve, world);
141-
wit_component::embed_component_metadata(
142-
&mut module,
143-
&resolve,
144-
world,
145-
StringEncoding::default(),
146-
)
147-
.with_context(|| {
148-
format!(
149-
"failed to embed component metadata from package `{path}` for test case `{test_case}`",
150-
path = path.display()
151-
)
152-
})?;
153-
154-
let encoder = ComponentEncoder::default().validate(true).module(&module)?;
155-
encoder
156-
.encode()
157-
.with_context(|| format!("failed to encode a component from module derived from package `{path}` for test case `{test_case}`", path = path.display()))?
158-
}
160+
Some("wit") => Self::load_wit_package(test_case, &path)?,
159161
Some("wat") => wat::parse_file(&path).with_context(|| {
160162
format!(
161163
"failed to parse package `{path}` for test case `{test_case}`",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(component
2+
(type (;0;)
3+
(instance
4+
(export (;0;) "x" (type (sub resource)))
5+
)
6+
)
7+
(import "test:shared/types" (instance (;0;) (type 0)))
8+
(alias export 0 "x" (type (;1;)))
9+
(type (;2;)
10+
(instance
11+
(alias outer 1 1 (type (;0;)))
12+
(export (;1;) "x" (type (eq 0)))
13+
(type (;2;) (own 1))
14+
(type (;3;) (func (result 2)))
15+
(export (;0;) "f" (func (type 3)))
16+
)
17+
)
18+
(import "test:foo/i" (instance (;1;) (type 2)))
19+
(type (;3;)
20+
(component
21+
(type (;0;)
22+
(instance
23+
(export (;0;) "x" (type (sub resource)))
24+
)
25+
)
26+
(import "test:shared/types" (instance (;0;) (type 0)))
27+
(alias export 0 "x" (type (;1;)))
28+
(type (;2;)
29+
(instance
30+
(alias outer 1 1 (type (;0;)))
31+
(export (;1;) "x" (type (eq 0)))
32+
(type (;2;) (own 1))
33+
(type (;3;) (func (result 2)))
34+
(export (;0;) "f" (func (type 3)))
35+
)
36+
)
37+
(import "test:foo/i" (instance (;1;) (type 2)))
38+
(alias export 0 "x" (type (;3;)))
39+
(type (;4;)
40+
(instance
41+
(alias outer 1 3 (type (;0;)))
42+
(export (;1;) "x" (type (eq 0)))
43+
(type (;2;) (own 1))
44+
(type (;3;) (func (result 2)))
45+
(export (;0;) "f" (func (type 3)))
46+
)
47+
)
48+
(export (;2;) "test:foo/i" (instance (type 4)))
49+
)
50+
)
51+
(import "unlocked-dep=<test:foo>" (component (;0;) (type 3)))
52+
(instance (;2;) (instantiate 0
53+
(with "test:shared/types" (instance 0))
54+
(with "test:foo/i" (instance 1))
55+
)
56+
)
57+
(alias export 2 "test:foo/i" (instance (;3;)))
58+
(instance (;4;) (instantiate 0
59+
(with "test:foo/i" (instance 3))
60+
(with "test:shared/types" (instance 0))
61+
)
62+
)
63+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:shared;
2+
3+
interface types {
4+
resource x;
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test:foo;
2+
3+
interface i {
4+
use test:shared/types.{x};
5+
f: func() -> x;
6+
}
7+
8+
world w {
9+
import i;
10+
export i;
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "test:foo",
5+
"path": "foo.wit"
6+
}
7+
],
8+
"nodes": [
9+
{
10+
"type": "instantiation",
11+
"package": 0
12+
},
13+
{
14+
"type": "instantiation",
15+
"package": 0
16+
},
17+
{
18+
"type": "alias",
19+
"source": 0,
20+
"export": "test:foo/i"
21+
}
22+
],
23+
"arguments": [
24+
{
25+
"source": 2,
26+
"target": 1,
27+
"name": "test:foo/i"
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)