Skip to content

Commit bae7474

Browse files
authored
Remove usage of Interface::parse_file in wit-component tests (#430)
* Remove a dead `load` function in bindgen-core * Update all `interfaces` tests to use worlds Migrate all `import-*.wit` and `export-*.wit` files to `world.wit` and additionally plumb through various fixes and such throughout `wit-component` to print the right names and support worlds when printing. * Migrate `components` tests to world syntax Replace all various `*.wit` files with singular `world.wit` files, plus optional `adapt-*-world.wit` files for adapters in their tests. The CLI interface for `wit-component` has also changed here to take a world file as input, optionally take a module to represent types-only encodings, and then additionally prints to stdout by default instead of a file. * Fix guest generators for recent changes * Fix CLI build
1 parent f41c9e0 commit bae7474

136 files changed

Lines changed: 1302 additions & 1053 deletions

File tree

Some content is hidden

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

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindgen-core/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use anyhow::Result;
21
use std::collections::{btree_map::Entry, BTreeMap, HashMap};
32
use std::fmt::{self, Write};
43
use std::ops::Deref;
5-
use std::path::Path;
64
use wit_component::ComponentInterfaces;
75
use wit_parser::*;
86

@@ -261,10 +259,6 @@ impl Files {
261259
}
262260
}
263261

264-
pub fn load(path: impl AsRef<Path>) -> Result<Interface> {
265-
Interface::parse_file(path)
266-
}
267-
268262
#[derive(Default)]
269263
pub struct Source {
270264
s: String,

crates/gen-guest-c/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl WorldGenerator for C {
9595
if i == 0 {
9696
uwriteln!(gen.src.h_fns, "\n// Exported Functions from `{name}`");
9797
}
98-
gen.export(func, false);
98+
gen.export(func, Some(name));
9999
}
100100

101101
gen.finish();
@@ -111,7 +111,7 @@ impl WorldGenerator for C {
111111
if i == 0 {
112112
uwriteln!(gen.src.h_fns, "\n// Exported Functions from `{name}`");
113113
}
114-
gen.export(func, true);
114+
gen.export(func, None);
115115
}
116116

117117
gen.finish();
@@ -715,12 +715,10 @@ impl InterfaceGenerator<'_> {
715715
self.src.c_adapters("}\n");
716716
}
717717

718-
fn export(&mut self, func: &Function, is_default: bool) {
718+
fn export(&mut self, func: &Function, interface_name: Option<&str>) {
719719
let sig = self.iface.wasm_signature(AbiVariant::GuestExport, func);
720720

721-
// Currently the C generator always emits default exports
722-
// This needs to change once the generator works from a world
723-
let export_name = self.iface.core_export_name(is_default, func);
721+
let export_name = func.core_export_name(interface_name);
724722

725723
// Print the actual header for this function into the header file, and
726724
// it's what we'll be calling.

crates/gen-guest-rust/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ impl WorldGenerator for RustWasm {
123123

124124
fn export(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
125125
self.interface(iface, TypeMode::Owned, false)
126-
.generate_exports(name, false);
126+
.generate_exports(name, Some(name));
127127
}
128128

129129
fn export_default(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
130130
self.interface(iface, TypeMode::Owned, false)
131-
.generate_exports(name, true);
131+
.generate_exports(name, None);
132132
}
133133

134134
fn finish(&mut self, name: &str, interfaces: &ComponentInterfaces, files: &mut Files) {
@@ -244,7 +244,7 @@ struct InterfaceGenerator<'a> {
244244
}
245245

246246
impl InterfaceGenerator<'_> {
247-
fn generate_exports(mut self, name: &str, default_export: bool) {
247+
fn generate_exports(mut self, name: &str, interface_name: Option<&str>) {
248248
self.types();
249249

250250
let camel = name.to_upper_camel_case();
@@ -261,7 +261,7 @@ impl InterfaceGenerator<'_> {
261261
uwriteln!(self.src, "}}");
262262

263263
for func in self.iface.functions.iter() {
264-
self.generate_guest_export(name, func, default_export);
264+
self.generate_guest_export(name, func, interface_name);
265265
}
266266

267267
self.append_submodule(name);
@@ -338,7 +338,12 @@ impl InterfaceGenerator<'_> {
338338
}
339339
}
340340

341-
fn generate_guest_export(&mut self, module_name: &str, func: &Function, default_export: bool) {
341+
fn generate_guest_export(
342+
&mut self,
343+
module_name: &str,
344+
func: &Function,
345+
interface_name: Option<&str>,
346+
) {
342347
if self.gen.skip.contains(&func.name) {
343348
return;
344349
}
@@ -347,7 +352,7 @@ impl InterfaceGenerator<'_> {
347352
let trait_bound = module_name.to_upper_camel_case();
348353
let iface_snake = self.iface.name.to_snake_case();
349354
let name_snake = func.name.to_snake_case();
350-
let export_name = self.iface.core_export_name(default_export, func);
355+
let export_name = func.core_export_name(interface_name);
351356
let mut macro_src = Source::default();
352357
// Generate, simultaneously, the actual lifting/lowering function within
353358
// the original module (`call_{name_snake}`) as well as the function

crates/gen-guest-teavm-java/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl WorldGenerator for TeaVmJava {
9696
gen.types();
9797

9898
for func in iface.functions.iter() {
99-
gen.export(func, false);
99+
gen.export(func, Some(name));
100100
}
101101

102102
gen.add_class();
@@ -107,7 +107,7 @@ impl WorldGenerator for TeaVmJava {
107107
gen.types();
108108

109109
for func in iface.functions.iter() {
110-
gen.export(func, true);
110+
gen.export(func, None);
111111
}
112112

113113
gen.add_class();
@@ -399,12 +399,10 @@ impl InterfaceGenerator<'_> {
399399
);
400400
}
401401

402-
fn export(&mut self, func: &Function, is_default: bool) {
402+
fn export(&mut self, func: &Function, interface_name: Option<&str>) {
403403
let sig = self.iface.wasm_signature(AbiVariant::GuestExport, func);
404404

405-
// Currently the Java generator always emits default exports
406-
// This needs to change once the generator works from a world
407-
let export_name = self.iface.core_export_name(is_default, func);
405+
let export_name = func.core_export_name(interface_name);
408406

409407
let mut bindgen = FunctionBindgen::new(
410408
self,

crates/test-helpers/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ pub fn dummy_module(interfaces: &ComponentInterfaces) -> Vec<u8> {
171171
}
172172
}
173173

174-
for (_, export) in interfaces.exports.iter() {
174+
for (name, export) in interfaces.exports.iter() {
175175
for func in export.functions.iter() {
176-
let name = format!("{}#{}", export.name, func.name);
176+
let name = func.core_export_name(Some(name));
177177
push_func(&mut wat, &name, export, func);
178178
}
179179
}

crates/wit-component/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ env_logger = { workspace = true, optional = true }
2525
log = "0.4.17"
2626
bitflags = { workspace = true }
2727
indexmap = { workspace = true }
28+
atty = { version = "0.2.14", optional = true }
29+
wasmprinter = { workspace = true, optional = true }
2830

2931
[dev-dependencies]
3032
wasmprinter = { workspace = true }
@@ -35,4 +37,4 @@ test-helpers = { path = '../test-helpers', default-features = false }
3537

3638
[features]
3739
default = ["cli"]
38-
cli = ["dep:clap", "dep:env_logger"]
40+
cli = ["dep:clap", "dep:env_logger", "dep:atty", "dep:wasmprinter"]

0 commit comments

Comments
 (0)