Skip to content

Commit 403d1a3

Browse files
committed
bulk
1 parent af8879c commit 403d1a3

File tree

63 files changed

+564
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+564
-64
lines changed

crates/wit-component/src/encoding/wit/v1.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ impl InterfaceEncoder<'_> {
170170
let iface = &self.resolve.interfaces[interface];
171171
let mut type_order = IndexSet::new();
172172
for (_, id) in iface.types.iter() {
173+
let ty = &self.resolve.types[*id];
174+
match ty.owner {
175+
TypeOwner::Interface(iface_id) => {
176+
self.interface = Some(iface_id);
177+
}
178+
_ => unreachable!(),
179+
}
173180
self.encode_valtype(self.resolve, &Type::Id(*id))?;
174181
type_order.insert(*id);
175182
}

crates/wit-component/src/encoding/wit/v2.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::encoding::types::{FunctionKey, ValtypeEncoder};
2-
use anyhow::Result;
2+
use anyhow::{bail, Result};
33
use indexmap::IndexSet;
44
use std::collections::HashMap;
55
use std::mem;
@@ -184,6 +184,13 @@ impl InterfaceEncoder<'_> {
184184
let iface = &self.resolve.interfaces[interface];
185185
let mut type_order = IndexSet::new();
186186
for (_, id) in iface.types.iter() {
187+
let ty = &self.resolve.types[*id];
188+
match ty.owner {
189+
TypeOwner::Interface(iface_id) => {
190+
self.interface = Some(iface_id);
191+
}
192+
_ => unreachable!(),
193+
}
187194
self.encode_valtype(self.resolve, &Type::Id(*id))?;
188195
type_order.insert(*id);
189196
}
@@ -218,7 +225,48 @@ impl InterfaceEncoder<'_> {
218225
.unwrap()
219226
.export(name, ComponentTypeRef::Func(ty));
220227
}
221-
let instance = self.pop_instance();
228+
let mut instance = self.pop_instance();
229+
for (orig_name, _) in &iface.nested {
230+
let mut pkg_parts = orig_name.split("/");
231+
let pkg = pkg_parts.next().expect("expected projection");
232+
let iface_name = pkg_parts.next().expect("expected projection");
233+
let mut parts = pkg.split(":");
234+
let namespace = parts.next().expect("expected <namespace>:<id>");
235+
let name = parts.next().expect("expected <namespace>:<id>");
236+
let name = PackageName {
237+
namespace: namespace.to_string(),
238+
name: name.to_string(),
239+
version: None,
240+
};
241+
242+
let package_id = self.resolve.package_names.get(&name).unwrap();
243+
let package = &self.resolve.packages[*package_id];
244+
let nested = package.interfaces.get(iface_name).unwrap();
245+
let nested_iface = &self.resolve.interfaces[*nested];
246+
let mut inst = InterfaceEncoder::new(&self.resolve);
247+
inst.push_instance();
248+
for (_, id) in &nested_iface.types {
249+
let ty = &self.resolve.types[*id];
250+
match ty.owner {
251+
TypeOwner::Interface(iface_id) => {
252+
inst.interface = Some(iface_id);
253+
}
254+
_ => unreachable!(),
255+
}
256+
inst.encode_valtype(self.resolve, &Type::Id(*id))?;
257+
}
258+
for (_, _) in &nested_iface.nested {
259+
bail!("Using `nest` in a nested interface is not yet supported");
260+
}
261+
let ty = instance.ty();
262+
let nested = inst.pop_instance();
263+
ty.instance(&nested);
264+
instance.export(
265+
orig_name,
266+
ComponentTypeRef::Instance(instance.type_count() - 1),
267+
);
268+
}
269+
222270
let idx = self.outer.type_count();
223271
self.outer.ty().instance(&instance);
224272
self.import_map.insert(interface, self.instances);

crates/wit-component/src/printing.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ impl WitPrinter {
8282
self.output.push_str("interface ");
8383
self.print_name(name);
8484
self.output.push_str(" {\n");
85+
let nested = &resolve.interfaces[*id].nested;
86+
for item in nested {
87+
self.output.push_str("nest ");
88+
self.print_name(item.0);
89+
self.output.push_str(";\n")
90+
}
8591
self.print_interface(resolve, *id)?;
8692
writeln!(&mut self.output, "}}\n")?;
8793
}

crates/wit-component/tests/interfaces.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ fn run_test(path: &Path, is_dir: bool) -> Result<()> {
5353
} else {
5454
resolve.append(UnresolvedPackageGroup::parse_file(path)?)?
5555
};
56-
5756
for package in packages {
5857
assert_print(&resolve, &[package], path, is_dir)?;
5958

@@ -77,7 +76,6 @@ fn run_test(path: &Path, is_dir: bool) -> Result<()> {
7776
decoded.packages().len(),
7877
"Each input WIT package should produce WASM that contains only one package"
7978
);
80-
8179
let decoded_package = decoded.packages()[0];
8280
let resolve = decoded.resolve();
8381

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
(component
2+
(type (;0;)
3+
(component
4+
(type (;0;)
5+
(instance
6+
(type (;0;) (record (field "foo" string)))
7+
(export (;1;) "usable-record" (type (eq 0)))
8+
)
9+
)
10+
(import "foo:nestee/usable" (instance (;0;) (type 0)))
11+
(alias export 0 "usable-record" (type (;1;)))
12+
(type (;2;)
13+
(instance
14+
(alias outer 1 1 (type (;0;)))
15+
(export (;1;) "usable-record" (type (eq 0)))
16+
(type (;2;) (record (field "foo" string)))
17+
(export (;3;) "my-record" (type (eq 2)))
18+
(type (;4;) (func (result string)))
19+
(export (;0;) "hello" (func (type 4)))
20+
(type (;5;)
21+
(instance
22+
(type (;0;) (record (field "foo" string)))
23+
(export (;1;) "nestrecord" (type (eq 0)))
24+
)
25+
)
26+
(export (;0;) "foo:nestee/things" (instance (type 5)))
27+
(type (;6;)
28+
(instance
29+
(export (;0;) "foo" (type (sub resource)))
30+
)
31+
)
32+
(export (;1;) "foo:nestee/more" (instance (type 6)))
33+
)
34+
)
35+
(export (;1;) "foo:thing/something" (instance (type 2)))
36+
)
37+
)
38+
(export (;1;) "something" (type 0))
39+
(type (;2;)
40+
(component
41+
(type (;0;)
42+
(instance
43+
(type (;0;) (list u8))
44+
(export (;1;) "random" (type (eq 0)))
45+
)
46+
)
47+
(export (;0;) "foo:thing/flat" (instance (type 0)))
48+
)
49+
)
50+
(export (;3;) "flat" (type 2))
51+
(type (;4;)
52+
(component
53+
(type (;0;)
54+
(component
55+
(type (;0;)
56+
(instance
57+
(type (;0;) (record (field "foo" string)))
58+
(export (;1;) "usable-record" (type (eq 0)))
59+
)
60+
)
61+
(import "foo:nestee/usable" (instance (;0;) (type 0)))
62+
(alias export 0 "usable-record" (type (;1;)))
63+
(type (;2;)
64+
(instance
65+
(alias outer 1 1 (type (;0;)))
66+
(export (;1;) "usable-record" (type (eq 0)))
67+
(type (;2;) (record (field "foo" string)))
68+
(export (;3;) "my-record" (type (eq 2)))
69+
(type (;4;) (func (result string)))
70+
(export (;0;) "hello" (func (type 4)))
71+
)
72+
)
73+
(import "foo:thing/something" (instance (;1;) (type 2)))
74+
(type (;3;)
75+
(instance
76+
(type (;0;) (list u8))
77+
(export (;1;) "random" (type (eq 0)))
78+
)
79+
)
80+
(import "foo:thing/flat" (instance (;2;) (type 3)))
81+
(type (;4;)
82+
(instance
83+
(alias outer 1 1 (type (;0;)))
84+
(export (;1;) "usable-record" (type (eq 0)))
85+
(type (;2;) (record (field "foo" string)))
86+
(export (;3;) "my-record" (type (eq 2)))
87+
(type (;4;) (func (result string)))
88+
(export (;0;) "hello" (func (type 4)))
89+
)
90+
)
91+
(export (;3;) "foo:thing/something" (instance (type 4)))
92+
(type (;5;)
93+
(instance
94+
(type (;0;) (list u8))
95+
(export (;1;) "random" (type (eq 0)))
96+
)
97+
)
98+
(export (;4;) "foo:thing/flat" (instance (type 5)))
99+
)
100+
)
101+
(export (;0;) "foo:thing/my-world" (component (type 0)))
102+
)
103+
)
104+
(export (;5;) "my-world" (type 4))
105+
(@custom "package-docs" "\01{\22interfaces\22:{\22something\22:{\22types\22:{\22my-record\22:{\22stability\22:{\22stable\22:{\22since\22:\221.0.0\22}}}}}}}")
106+
(@producers
107+
(processed-by "wit-component" "$CARGO_PKG_VERSION")
108+
)
109+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package foo:nestee;
2+
3+
interface things {
4+
record nestrecord {
5+
foo: string
6+
}
7+
hello: func() -> string;
8+
}
9+
10+
interface more {
11+
resource foo {
12+
bar: func() -> option<string>;
13+
}
14+
}
15+
16+
interface usable {
17+
record usable-record {
18+
foo: string
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package foo:nestnest;
2+
3+
interface deep {
4+
record deep-record {
5+
foo: string
6+
}
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package foo:thing;
2+
3+
interface something {
4+
use foo:nestee/usable.{usable-record};
5+
nest foo:nestee/things;
6+
nest foo:nestee/more;
7+
@since(version = 1.0.0)
8+
record my-record {
9+
foo: string
10+
}
11+
12+
hello: func() -> string;
13+
}
14+
15+
interface flat {
16+
type random = list<u8>;
17+
}
18+
19+
world my-world {
20+
import something;
21+
export something;
22+
import flat;
23+
export flat;
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package foo:thing;
2+
3+
interface something {
4+
nest foo:nestee/things;
5+
nest foo:nestee/more;
6+
use foo:nestee/usable.{usable-record};
7+
8+
@since(version = 1.0.0)
9+
record my-record {
10+
foo: string,
11+
}
12+
13+
hello: func() -> string;
14+
}
15+
16+
interface flat {
17+
type random = list<u8>;
18+
}
19+
20+
world my-world {
21+
import foo:nestee/usable;
22+
import something;
23+
import flat;
24+
25+
export something;
26+
export flat;
27+
}

crates/wit-parser/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ enum InterfaceItem<'a> {
564564
struct Nest<'a> {
565565
id: PackageName<'a>,
566566
name: Id<'a>,
567-
attributes: Vec<Attribute<'a>>,
567+
// attributes: Vec<Attribute<'a>>,
568568
}
569569

570570
impl<'a> Nest<'a> {
571-
fn parse(tokens: &mut Tokenizer<'a>, attributes: Vec<Attribute<'a>>) -> Result<Self> {
571+
fn parse(tokens: &mut Tokenizer<'a>, _attributes: Vec<Attribute<'a>>) -> Result<Self> {
572572
tokens.eat(Token::Nest)?;
573573
let id = parse_id(tokens)?;
574574
tokens.expect(Token::Colon)?;
@@ -591,7 +591,7 @@ impl<'a> Nest<'a> {
591591
version,
592592
},
593593
name,
594-
attributes,
594+
// attributes,
595595
})
596596
}
597597
}

0 commit comments

Comments
 (0)