Skip to content

Commit 0220c95

Browse files
authored
Migrate all tests to using world-file-syntax (#432)
* Update all codegen tests to world syntax This enables simplifying codegen tests as well by having each test exercise imports/exports instead of having all code generators have one case for imports and one for exports. * Update codegen tests for the C guest generator * Update teavm codegen tests for worlds * Remove the `--name` argument from the CLI No longer needed as the name is inferred from the `*.wit` world. * Get all js host generator tests working Update all `runtime/*` tests with new naming conventions, world files, etc. Minor updates were made to names as I ended up using different conventions for the `*.wit` world files than were previously exercised. This also fixes a few minor issues with the output for the JS generator in the default ESM mode. * Get all host-wasmtime tests working with worlds Various small updates here and there to namings and such. * Get all Python tests working with worlds Fix support for exported instances by using correct import paths for various types/intrinsics/etc. Additionally update the codegen tests to specify a mypy cache dir to avoid racing between tests. * Update demo build for worlds Also fix an issue with the Rust host generator where it used an interface's name instead of the name of the import for import module names. * Update demo with world files
1 parent 717ab1e commit 0220c95

119 files changed

Lines changed: 2068 additions & 2062 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.

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl WorldGenerator for C {
7272
}
7373

7474
fn import(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
75-
let mut gen = self.interface(iface, true);
75+
let mut gen = self.interface(name, iface, true);
7676
gen.types();
7777

7878
for (i, func) in iface.functions.iter().enumerate() {
@@ -88,7 +88,7 @@ impl WorldGenerator for C {
8888
}
8989

9090
fn export(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
91-
let mut gen = self.interface(iface, false);
91+
let mut gen = self.interface(name, iface, false);
9292
gen.types();
9393

9494
for (i, func) in iface.functions.iter().enumerate() {
@@ -104,7 +104,7 @@ impl WorldGenerator for C {
104104
}
105105

106106
fn export_default(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
107-
let mut gen = self.interface(iface, false);
107+
let mut gen = self.interface(name, iface, false);
108108
gen.types();
109109

110110
for (i, func) in iface.functions.iter().enumerate() {
@@ -294,12 +294,14 @@ impl WorldGenerator for C {
294294
impl C {
295295
fn interface<'a>(
296296
&'a mut self,
297+
name: &'a str,
297298
iface: &'a Interface,
298299
in_import: bool,
299300
) -> InterfaceGenerator<'a> {
300301
let mut sizes = SizeAlign::default();
301302
sizes.fill(iface);
302303
InterfaceGenerator {
304+
name,
303305
src: Source::default(),
304306
gen: self,
305307
iface,
@@ -325,6 +327,7 @@ impl C {
325327
}
326328

327329
struct InterfaceGenerator<'a> {
330+
name: &'a str,
328331
src: Source,
329332
in_import: bool,
330333
gen: &'a mut C,
@@ -483,7 +486,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
483486
uwriteln!(
484487
self.src.h_defs,
485488
"#define {}_{}_{} (1 << {})",
486-
self.iface.name.to_shouty_snake_case(),
489+
self.name.to_shouty_snake_case(),
487490
name.to_shouty_snake_case(),
488491
flag.name.to_shouty_snake_case(),
489492
i,
@@ -520,7 +523,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
520523
uwriteln!(
521524
self.src.h_defs,
522525
"#define {}_{}_{} {}",
523-
self.iface.name.to_shouty_snake_case(),
526+
self.name.to_shouty_snake_case(),
524527
name.to_shouty_snake_case(),
525528
case.name.to_shouty_snake_case(),
526529
i,
@@ -603,7 +606,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
603606
uwriteln!(
604607
self.src.h_defs,
605608
"#define {}_{}_{} {}",
606-
self.iface.name.to_shouty_snake_case(),
609+
self.name.to_shouty_snake_case(),
607610
name.to_shouty_snake_case(),
608611
case.name.to_shouty_snake_case(),
609612
i,
@@ -655,12 +658,12 @@ impl InterfaceGenerator<'_> {
655658
uwriteln!(
656659
self.src.h_fns,
657660
"__attribute__((import_module(\"{}\"), import_name(\"{}\")))",
658-
self.iface.name,
661+
self.name,
659662
func.name
660663
);
661664
let import_name = self.gen.names.tmp(&format!(
662665
"__wasm_import_{}_{}",
663-
self.iface.name.to_snake_case(),
666+
self.name.to_snake_case(),
664667
func.name.to_snake_case()
665668
));
666669
match sig.results.len() {
@@ -732,7 +735,7 @@ impl InterfaceGenerator<'_> {
732735
);
733736
let import_name = self.gen.names.tmp(&format!(
734737
"__wasm_export_{}_{}",
735-
self.iface.name.to_snake_case(),
738+
self.name.to_snake_case(),
736739
func.name.to_snake_case()
737740
));
738741

@@ -851,7 +854,7 @@ impl InterfaceGenerator<'_> {
851854
fn print_sig(&mut self, func: &Function) -> CSig {
852855
let name = format!(
853856
"{}_{}",
854-
self.iface.name.to_snake_case(),
857+
self.name.to_snake_case(),
855858
func.name.to_snake_case()
856859
);
857860
self.gen.names.insert(&name).expect("duplicate symbols");
@@ -958,7 +961,7 @@ impl InterfaceGenerator<'_> {
958961
}
959962

960963
fn print_typedef_target(&mut self, name: &str) {
961-
let iface_snake = self.iface.name.to_snake_case();
964+
let iface_snake = self.name.to_snake_case();
962965
let snake = name.to_snake_case();
963966
self.print_namespace(SourceType::HDefs);
964967
self.src.h_defs(&snake);
@@ -970,7 +973,7 @@ impl InterfaceGenerator<'_> {
970973
}
971974

972975
fn print_namespace(&mut self, stype: SourceType) {
973-
self.src.print(stype, &self.iface.name.to_snake_case());
976+
self.src.print(stype, &self.name.to_snake_case());
974977
self.src.print(stype, "_");
975978
}
976979

@@ -2070,7 +2073,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
20702073
self.src.push_str(");\n");
20712074
}
20722075

2073-
Instruction::CallInterface { module: _, func } => {
2076+
Instruction::CallInterface { func } => {
20742077
let mut args = String::new();
20752078
for (i, (op, (byref, _))) in operands.iter().zip(&self.sig.params).enumerate() {
20762079
if i > 0 {

crates/gen-guest-c/tests/codegen.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,25 @@ use std::env;
33
use std::path::{Path, PathBuf};
44
use std::process::Command;
55

6-
macro_rules! gen_test {
7-
($name:ident $test:tt $dir:ident) => {
6+
macro_rules! codegen_test {
7+
($name:ident $test:tt) => {
88
#[test]
99
fn $name() {
1010
test_helpers::run_world_codegen_test(
1111
"guest-c",
1212
$test.as_ref(),
13-
test_helpers::Direction::$dir,
1413
|name, interfaces, files| {
1514
wit_bindgen_gen_guest_c::Opts::default()
1615
.build()
1716
.generate(name, interfaces, files)
1817
},
19-
super::verify,
18+
verify,
2019
)
2120
}
2221
};
2322
}
2423

25-
mod exports {
26-
macro_rules! codegen_test {
27-
($name:ident $test:tt) => (gen_test!($name $test Export);)
28-
}
29-
test_helpers::codegen_tests!("*.wit");
30-
}
31-
32-
mod imports {
33-
macro_rules! codegen_test {
34-
($name:ident $test:tt) => (gen_test!($name $test Import);)
35-
}
36-
test_helpers::codegen_tests!("*.wit");
37-
}
24+
test_helpers::codegen_tests!("*.wit");
3825

3926
fn verify(dir: &Path, name: &str) {
4027
let path = PathBuf::from(env::var_os("WASI_SDK_PATH").unwrap());

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl RustWasm {
8686

8787
fn interface<'a>(
8888
&'a mut self,
89+
name: &'a str,
8990
iface: &'a Interface,
9091
default_param_mode: TypeMode,
9192
in_import: bool,
@@ -96,6 +97,7 @@ impl RustWasm {
9697
types.analyze(iface);
9798

9899
InterfaceGenerator {
100+
name,
99101
src: Source::default(),
100102
in_import,
101103
gen: self,
@@ -111,7 +113,7 @@ impl RustWasm {
111113

112114
impl WorldGenerator for RustWasm {
113115
fn import(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
114-
let mut gen = self.interface(iface, TypeMode::AllBorrowed("'a"), true);
116+
let mut gen = self.interface(name, iface, TypeMode::AllBorrowed("'a"), true);
115117
gen.types();
116118

117119
for func in iface.functions.iter() {
@@ -122,12 +124,12 @@ impl WorldGenerator for RustWasm {
122124
}
123125

124126
fn export(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
125-
self.interface(iface, TypeMode::Owned, false)
127+
self.interface(name, iface, TypeMode::Owned, false)
126128
.generate_exports(name, Some(name));
127129
}
128130

129131
fn export_default(&mut self, name: &str, iface: &Interface, _files: &mut Files) {
130-
self.interface(iface, TypeMode::Owned, false)
132+
self.interface(name, iface, TypeMode::Owned, false)
131133
.generate_exports(name, None);
132134
}
133135

@@ -237,6 +239,7 @@ struct InterfaceGenerator<'a> {
237239
types: Types,
238240
sizes: SizeAlign,
239241
gen: &'a mut RustWasm,
242+
name: &'a str,
240243
iface: &'a Interface,
241244
default_param_mode: TypeMode,
242245
return_pointer_area_size: usize,
@@ -374,6 +377,7 @@ impl InterfaceGenerator<'_> {
374377
"
375378
#[doc(hidden)]
376379
#[export_name = \"{export_name}\"]
380+
#[allow(non_snake_case)]
377381
unsafe extern \"C\" fn __export_{iface_snake}_{name_snake}(\
378382
",
379383
);
@@ -443,6 +447,7 @@ impl InterfaceGenerator<'_> {
443447
"
444448
#[doc(hidden)]
445449
#[export_name = \"cabi_post_{export_name}\"]
450+
#[allow(non_snake_case)]
446451
unsafe extern \"C\" fn __post_return_{iface_snake}_{name_snake}(\
447452
"
448453
);
@@ -662,25 +667,22 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
662667

663668
fn declare_import(
664669
&mut self,
665-
iface: &Interface,
670+
module_name: &str,
666671
name: &str,
667672
params: &[WasmType],
668673
results: &[WasmType],
669674
) -> String {
670675
// Define the actual function we're calling inline
671-
self.push_str("#[link(wasm_import_module = \"");
672-
self.push_str(&iface.name);
673-
self.push_str("\")]\n");
674-
self.push_str("extern \"C\" {\n");
675-
self.push_str("#[cfg_attr(target_arch = \"wasm32\", link_name = \"");
676-
self.push_str(name);
677-
self.push_str("\")]\n");
678-
self.push_str("#[cfg_attr(not(target_arch = \"wasm32\"), link_name = \"");
679-
self.push_str(&iface.name);
680-
self.push_str("_");
681-
self.push_str(name);
682-
self.push_str("\")]\n");
683-
self.push_str("fn wit_import(");
676+
uwriteln!(
677+
self.src,
678+
"
679+
#[link(wasm_import_module = \"{module_name}\")]
680+
extern \"C\" {{
681+
#[cfg_attr(target_arch = \"wasm32\", link_name = \"{name}\")]
682+
#[cfg_attr(not(target_arch = \"wasm32\"), link_name = \"{module_name}_{name}\")]
683+
fn wit_import(\
684+
"
685+
);
684686
for param in params.iter() {
685687
self.push_str("_: ");
686688
self.push_str(wasm_type(*param));
@@ -1312,8 +1314,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
13121314

13131315
Instruction::IterBasePointer => results.push("base".to_string()),
13141316

1315-
Instruction::CallWasm { iface, name, sig } => {
1316-
let func = self.declare_import(iface, name, &sig.params, &sig.results);
1317+
Instruction::CallWasm { name, sig, .. } => {
1318+
let func = self.declare_import(self.gen.name, name, &sig.params, &sig.results);
13171319

13181320
// ... then call the function with all our operands
13191321
if sig.results.len() > 0 {

0 commit comments

Comments
 (0)