From dd30c5ea93671fa71ac8f1d23b66ab04f5322b67 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 16 Jul 2024 15:10:28 -0500 Subject: [PATCH 1/3] Update wasm-tools crates (#1005) * Update wasm-tools crates * Remove usage of multi-return * Fix C tests * Mostly update Go tests --- Cargo.lock | 68 +++++++++++++------------- Cargo.toml | 10 ++-- tests/runtime/flavorful/wasm.c | 66 +++++++++++++------------- tests/runtime/flavorful/world.wit | 4 +- tests/runtime/lists/wasm.c | 79 +++++++++++++++---------------- tests/runtime/lists/wasm.go | 60 +++++++++++------------ tests/runtime/lists/world.wit | 10 ++-- tests/runtime/main.rs | 2 +- tests/runtime/records/wasm.c | 37 +++++++-------- tests/runtime/records/wasm.go | 15 +++--- tests/runtime/records/world.wit | 4 +- 11 files changed, 172 insertions(+), 183 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 431376693..bc3ce9fb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1436,10 +1436,10 @@ name = "test-helpers" version = "0.0.0" dependencies = [ "codegen-macro", - "wasm-encoder 0.213.0", + "wasm-encoder 0.214.0", "wit-bindgen-core", "wit-component", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -1714,19 +1714,19 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.213.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "850e4e6a56413a8f33567741a2388c8f6dafd841a939d945c7248671a8739dd8" +checksum = "ff694f02a8d7a50b6922b197ae03883fbf18cdb2ae9fbee7b6148456f5f44041" dependencies = [ "leb128", - "wasmparser 0.213.0", + "wasmparser 0.214.0", ] [[package]] name = "wasm-metadata" -version = "0.213.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7435c10326606a75cba79447f9ece5c9459fec0316e21061d1397838133fedb" +checksum = "865c5bff5f7a3781b5f92ea4cfa99bb38267da097441cdb09080de1568ef3075" dependencies = [ "anyhow", "indexmap", @@ -1734,8 +1734,8 @@ dependencies = [ "serde_derive", "serde_json", "spdx", - "wasm-encoder 0.213.0", - "wasmparser 0.213.0", + "wasm-encoder 0.214.0", + "wasmparser 0.214.0", ] [[package]] @@ -1754,9 +1754,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.213.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e48e5a90a9e0afc2990437f5600b8de682a32b18cbaaf6f2b5db185352868b6b" +checksum = "5309c1090e3e84dad0d382f42064e9933fdaedb87e468cc239f0eabea73ddcb6" dependencies = [ "ahash", "bitflags", @@ -2071,24 +2071,24 @@ dependencies = [ [[package]] name = "wast" -version = "213.0.0" +version = "214.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd051172bc72db3567b039f710f27d6d80f358b8333088eb4c4c12dac2a4d993" +checksum = "694bcdb24c49c8709bd8713768b71301a11e823923eee355d530f1d8d0a7f8e9" dependencies = [ "bumpalo", "leb128", "memchr", "unicode-width", - "wasm-encoder 0.213.0", + "wasm-encoder 0.214.0", ] [[package]] name = "wat" -version = "1.213.0" +version = "1.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be77619385eca699d204399d2ffc9b1dfda1df3320266773d2d664ecb06cf3e" +checksum = "347249eb56773fa728df2656cfe3a8c19437ded61a922a0b5e0839d9790e278e" dependencies = [ - "wast 213.0.0", + "wast 214.0.0", ] [[package]] @@ -2355,11 +2355,11 @@ dependencies = [ "clap", "heck 0.5.0", "test-helpers", - "wasm-encoder 0.213.0", + "wasm-encoder 0.214.0", "wasm-metadata", "wit-bindgen-core", "wit-component", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -2370,8 +2370,8 @@ dependencies = [ "clap", "heck 0.5.0", "test-artifacts", - "wasm-encoder 0.213.0", - "wasmparser 0.213.0", + "wasm-encoder 0.214.0", + "wasmparser 0.214.0", "wasmtime", "wasmtime-wasi", "wit-bindgen-c", @@ -2382,7 +2382,7 @@ dependencies = [ "wit-bindgen-rust", "wit-bindgen-teavm-java", "wit-component", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -2391,7 +2391,7 @@ version = "0.27.0" dependencies = [ "anyhow", "heck 0.5.0", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -2403,12 +2403,12 @@ dependencies = [ "heck 0.5.0", "indexmap", "test-helpers", - "wasm-encoder 0.213.0", + "wasm-encoder 0.214.0", "wasm-metadata", - "wasmparser 0.213.0", + "wasmparser 0.214.0", "wit-bindgen-core", "wit-component", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -2489,9 +2489,9 @@ dependencies = [ [[package]] name = "wit-component" -version = "0.213.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b65c79d1eed332b05c5df1462561765605b6eccb65bc51b338aa70914cfc5fd1" +checksum = "fd9fd46f0e783bf80f1ab7291f9d442fa5553ff0e96cdb71964bd8859b734b55" dependencies = [ "anyhow", "bitflags", @@ -2500,11 +2500,11 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "wasm-encoder 0.213.0", + "wasm-encoder 0.214.0", "wasm-metadata", - "wasmparser 0.213.0", + "wasmparser 0.214.0", "wat", - "wit-parser 0.213.0", + "wit-parser 0.214.0", ] [[package]] @@ -2527,9 +2527,9 @@ dependencies = [ [[package]] name = "wit-parser" -version = "0.213.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b565a6b187ab01e48da8c5a83f495159bfaa72ea27e4623a0774f4b7d26bb348" +checksum = "681d526d6ea42e28f9afe9eae2b50e0b0a627aef8822c75eb04078db84d03e57" dependencies = [ "anyhow", "id-arena", @@ -2540,7 +2540,7 @@ dependencies = [ "serde_derive", "serde_json", "unicode-xid", - "wasmparser 0.213.0", + "wasmparser 0.214.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 627d8ef01..e76d313c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,11 +32,11 @@ indexmap = "2.0.0" prettyplease = "0.2.20" syn = { version = "2.0", features = ["printing"] } -wasmparser = "0.213.0" -wasm-encoder = "0.213.0" -wasm-metadata = "0.213.0" -wit-parser = "0.213.0" -wit-component = "0.213.0" +wasmparser = "0.214.0" +wasm-encoder = "0.214.0" +wasm-metadata = "0.214.0" +wit-parser = "0.214.0" +wit-component = "0.214.0" wit-bindgen-core = { path = 'crates/core', version = '0.27.0' } wit-bindgen-c = { path = 'crates/c', version = '0.27.0' } diff --git a/tests/runtime/flavorful/wasm.c b/tests/runtime/flavorful/wasm.c index 025e14bfd..6cb17e656 100644 --- a/tests/runtime/flavorful/wasm.c +++ b/tests/runtime/flavorful/wasm.c @@ -77,16 +77,15 @@ void flavorful_test_imports() { test_flavorful_test_list_typedef3_t b; b.ptr = &b_str; b.len = 1; - test_flavorful_test_list_typedef2_t c; - test_flavorful_test_list_typedef3_t d; - test_flavorful_test_list_typedefs(&a, &b, &c, &d); + flavorful_tuple2_list_typedef2_list_typedef3_t ret; + test_flavorful_test_list_typedefs(&a, &b, &ret); - assert(memcmp(c.ptr, "typedef3", c.len) == 0); - assert(d.len == 1); - assert(memcmp(d.ptr[0].ptr, "typedef4", d.ptr[0].len) == 0); + assert(memcmp(ret.f0.ptr, "typedef3", ret.f0.len) == 0); + assert(ret.f1.len == 1); + assert(memcmp(ret.f1.ptr[0].ptr, "typedef4", ret.f1.ptr[0].len) == 0); - test_flavorful_test_list_typedef2_free(&c); - test_flavorful_test_list_typedef3_free(&d); + test_flavorful_test_list_typedef2_free(&ret.f0); + test_flavorful_test_list_typedef3_free(&ret.f1); } { @@ -109,26 +108,24 @@ void flavorful_test_imports() { c.ptr = c_val; c.len = 2; - flavorful_list_bool_t d; - test_flavorful_test_list_result_void_void_t e; - test_flavorful_test_list_my_errno_t f; - test_flavorful_test_list_of_variants(&a, &b, &c, &d, &e, &f); + test_flavorful_test_tuple3_list_bool_list_result_void_void_list_my_errno_t ret; + test_flavorful_test_list_of_variants(&a, &b, &c, &ret); - assert(d.len == 2); - assert(d.ptr[0] == false); - assert(d.ptr[1] == true); + assert(ret.f0.len == 2); + assert(ret.f0.ptr[0] == false); + assert(ret.f0.ptr[1] == true); - assert(e.len == 2); - assert(e.ptr[0].is_err == true); - assert(e.ptr[1].is_err == false); + assert(ret.f1.len == 2); + assert(ret.f1.ptr[0].is_err == true); + assert(ret.f1.ptr[1].is_err == false); - assert(f.len == 2); - assert(f.ptr[0] == TEST_FLAVORFUL_TEST_MY_ERRNO_A); - assert(f.ptr[1] == TEST_FLAVORFUL_TEST_MY_ERRNO_B); + assert(ret.f2.len == 2); + assert(ret.f2.ptr[0] == TEST_FLAVORFUL_TEST_MY_ERRNO_A); + assert(ret.f2.ptr[1] == TEST_FLAVORFUL_TEST_MY_ERRNO_B); - flavorful_list_bool_free(&d); - test_flavorful_test_list_result_void_void_free(&e); - test_flavorful_test_list_my_errno_free(&f); + flavorful_list_bool_free(&ret.f0); + test_flavorful_test_list_result_void_void_free(&ret.f1); + test_flavorful_test_list_my_errno_free(&ret.f2); } } @@ -181,7 +178,10 @@ bool exports_test_flavorful_test_errno_result(test_flavorful_test_my_errno_t *er return false; } -void exports_test_flavorful_test_list_typedefs(exports_test_flavorful_test_list_typedef_t *a, exports_test_flavorful_test_list_typedef3_t *c, exports_test_flavorful_test_list_typedef2_t *ret0, exports_test_flavorful_test_list_typedef3_t *ret1) { +void exports_test_flavorful_test_list_typedefs( + exports_test_flavorful_test_list_typedef_t *a, + exports_test_flavorful_test_list_typedef3_t *c, + flavorful_tuple2_list_typedef2_list_typedef3_t *ret) { assert(memcmp(a->ptr, "typedef1", a->len) == 0); test_flavorful_test_list_typedef_free(a); @@ -189,21 +189,19 @@ void exports_test_flavorful_test_list_typedefs(exports_test_flavorful_test_list_ assert(memcmp(c->ptr[0].ptr, "typedef2", c->ptr[0].len) == 0); exports_test_flavorful_test_list_typedef3_free(c); - ret0->ptr = (uint8_t *) malloc(8); - ret0->len = 8; - memcpy(ret0->ptr, "typedef3", 8); + ret->f0.ptr = (uint8_t *) malloc(8); + ret->f0.len = 8; + memcpy(ret->f0.ptr, "typedef3", 8); - ret1->ptr = (flavorful_string_t *) malloc(sizeof(flavorful_string_t)); - ret1->len = 1; - flavorful_string_dup(&ret1->ptr[0], "typedef4"); + ret->f1.ptr = (flavorful_string_t *) malloc(sizeof(flavorful_string_t)); + ret->f1.len = 1; + flavorful_string_dup(&ret->f1.ptr[0], "typedef4"); } void exports_test_flavorful_test_list_of_variants( flavorful_list_bool_t *a, exports_test_flavorful_test_list_result_void_void_t *b, exports_test_flavorful_test_list_my_errno_t *c, - flavorful_list_bool_t *ret0, - exports_test_flavorful_test_list_result_void_void_t *ret1, - exports_test_flavorful_test_list_my_errno_t *ret2) { + exports_test_flavorful_test_tuple3_list_bool_list_result_void_void_list_my_errno_t *ret) { assert(0); // unimplemented } diff --git a/tests/runtime/flavorful/world.wit b/tests/runtime/flavorful/world.wit index 570cc588a..920346d88 100644 --- a/tests/runtime/flavorful/world.wit +++ b/tests/runtime/flavorful/world.wit @@ -28,10 +28,10 @@ interface test { type list-typedef = string; type list-typedef2 = list; type list-typedef3 = list; - list-typedefs: func(a: list-typedef, c: list-typedef3) -> (a: list-typedef2, b: list-typedef3); + list-typedefs: func(a: list-typedef, c: list-typedef3) -> tuple; list-of-variants: func(a: list, b: list, c: list) - -> (a: list, b: list, c: list); + -> tuple, list, list>; } world flavorful { diff --git a/tests/runtime/lists/wasm.c b/tests/runtime/lists/wasm.c index c3950df97..d69a84770 100644 --- a/tests/runtime/lists/wasm.c +++ b/tests/runtime/lists/wasm.c @@ -160,13 +160,12 @@ void lists_test_imports() { int8_t s8[2] = {SCHAR_MIN, SCHAR_MAX}; lists_list_u8_t list_u8 = { u8, 2 }; lists_list_s8_t list_s8 = { s8, 2 }; - lists_list_u8_t list_u8_out; - lists_list_s8_t list_s8_out; - test_lists_test_list_minmax8(&list_u8, &list_s8, &list_u8_out, &list_s8_out); - assert(list_u8_out.len == 2 && list_u8_out.ptr[0] == 0 && list_u8_out.ptr[1] == UCHAR_MAX); - assert(list_s8_out.len == 2 && list_s8_out.ptr[0] == SCHAR_MIN && list_s8_out.ptr[1] == SCHAR_MAX); - lists_list_u8_free(&list_u8_out); - lists_list_s8_free(&list_s8_out); + lists_tuple2_list_u8_list_s8_t ret; + test_lists_test_list_minmax8(&list_u8, &list_s8, &ret); + assert(ret.f0.len == 2 && ret.f0.ptr[0] == 0 && ret.f0.ptr[1] == UCHAR_MAX); + assert(ret.f1.len == 2 && ret.f1.ptr[0] == SCHAR_MIN && ret.f1.ptr[1] == SCHAR_MAX); + lists_list_u8_free(&ret.f0); + lists_list_s8_free(&ret.f1); } { @@ -174,13 +173,12 @@ void lists_test_imports() { int16_t s16[2] = {SHRT_MIN, SHRT_MAX}; lists_list_u16_t list_u16 = { u16, 2 }; lists_list_s16_t list_s16 = { s16, 2 }; - lists_list_u16_t list_u16_out; - lists_list_s16_t list_s16_out; - test_lists_test_list_minmax16(&list_u16, &list_s16, &list_u16_out, &list_s16_out); - assert(list_u16_out.len == 2 && list_u16_out.ptr[0] == 0 && list_u16_out.ptr[1] == USHRT_MAX); - assert(list_s16_out.len == 2 && list_s16_out.ptr[0] == SHRT_MIN && list_s16_out.ptr[1] == SHRT_MAX); - lists_list_u16_free(&list_u16_out); - lists_list_s16_free(&list_s16_out); + lists_tuple2_list_u16_list_s16_t ret; + test_lists_test_list_minmax16(&list_u16, &list_s16, &ret); + assert(ret.f0.len == 2 && ret.f0.ptr[0] == 0 && ret.f0.ptr[1] == USHRT_MAX); + assert(ret.f1.len == 2 && ret.f1.ptr[0] == SHRT_MIN && ret.f1.ptr[1] == SHRT_MAX); + lists_list_u16_free(&ret.f0); + lists_list_s16_free(&ret.f1); } { @@ -188,13 +186,12 @@ void lists_test_imports() { int32_t s32[2] = {INT_MIN, INT_MAX}; lists_list_u32_t list_u32 = { u32, 2 }; lists_list_s32_t list_s32 = { s32, 2 }; - lists_list_u32_t list_u32_out; - lists_list_s32_t list_s32_out; - test_lists_test_list_minmax32(&list_u32, &list_s32, &list_u32_out, &list_s32_out); - assert(list_u32_out.len == 2 && list_u32_out.ptr[0] == 0 && list_u32_out.ptr[1] == UINT_MAX); - assert(list_s32_out.len == 2 && list_s32_out.ptr[0] == INT_MIN && list_s32_out.ptr[1] == INT_MAX); - lists_list_u32_free(&list_u32_out); - lists_list_s32_free(&list_s32_out); + lists_tuple2_list_u32_list_s32_t ret; + test_lists_test_list_minmax32(&list_u32, &list_s32, &ret); + assert(ret.f0.len == 2 && ret.f0.ptr[0] == 0 && ret.f0.ptr[1] == UINT_MAX); + assert(ret.f1.len == 2 && ret.f1.ptr[0] == INT_MIN && ret.f1.ptr[1] == INT_MAX); + lists_list_u32_free(&ret.f0); + lists_list_s32_free(&ret.f1); } { @@ -202,13 +199,12 @@ void lists_test_imports() { int64_t s64[2] = {LLONG_MIN, LLONG_MAX}; lists_list_u64_t list_u64 = { u64, 2 }; lists_list_s64_t list_s64 = { s64, 2 }; - lists_list_u64_t list_u64_out; - lists_list_s64_t list_s64_out; - test_lists_test_list_minmax64(&list_u64, &list_s64, &list_u64_out, &list_s64_out); - assert(list_u64_out.len == 2 && list_u64_out.ptr[0] == 0 && list_u64_out.ptr[1] == ULLONG_MAX); - assert(list_s64_out.len == 2 && list_s64_out.ptr[0] == LLONG_MIN && list_s64_out.ptr[1] == LLONG_MAX); - lists_list_u64_free(&list_u64_out); - lists_list_s64_free(&list_s64_out); + lists_tuple2_list_u64_list_s64_t ret; + test_lists_test_list_minmax64(&list_u64, &list_s64, &ret); + assert(ret.f0.len == 2 && ret.f0.ptr[0] == 0 && ret.f0.ptr[1] == ULLONG_MAX); + assert(ret.f1.len == 2 && ret.f1.ptr[0] == LLONG_MIN && ret.f1.ptr[1] == LLONG_MAX); + lists_list_u64_free(&ret.f0); + lists_list_s64_free(&ret.f1); } { @@ -216,15 +212,14 @@ void lists_test_imports() { double f64[4] = {-DBL_MAX, DBL_MAX, -INFINITY, INFINITY}; lists_list_f32_t list_f32 = { f32, 4 }; lists_list_f64_t list_f64 = { f64, 4 }; - lists_list_f32_t list_f32_out; - lists_list_f64_t list_f64_out; - test_lists_test_list_minmax_float(&list_f32, &list_f64, &list_f32_out, &list_f64_out); - assert(list_f32_out.len == 4 && list_f32_out.ptr[0] == -FLT_MAX && list_f32_out.ptr[1] == FLT_MAX); - assert(list_f32_out.ptr[2] == -INFINITY && list_f32_out.ptr[3] == INFINITY); - assert(list_f64_out.len == 4 && list_f64_out.ptr[0] == -DBL_MAX && list_f64_out.ptr[1] == DBL_MAX); - assert(list_f64_out.ptr[2] == -INFINITY && list_f64_out.ptr[3] == INFINITY); - lists_list_f32_free(&list_f32_out); - lists_list_f64_free(&list_f64_out); + lists_tuple2_list_f32_list_f64_t ret; + test_lists_test_list_minmax_float(&list_f32, &list_f64, &ret); + assert(ret.f0.len == 4 && ret.f0.ptr[0] == -FLT_MAX && ret.f0.ptr[1] == FLT_MAX); + assert(ret.f0.ptr[2] == -INFINITY && ret.f0.ptr[3] == INFINITY); + assert(ret.f1.len == 4 && ret.f1.ptr[0] == -DBL_MAX && ret.f1.ptr[1] == DBL_MAX); + assert(ret.f1.ptr[2] == -INFINITY && ret.f1.ptr[3] == INFINITY); + lists_list_f32_free(&ret.f0); + lists_list_f64_free(&ret.f1); } } @@ -336,22 +331,22 @@ void exports_test_lists_test_string_roundtrip(lists_string_t *a, lists_string_t *ret0 = *a; } -void exports_test_lists_test_list_minmax8(lists_list_u8_t *a, lists_list_s8_t *b, lists_list_u8_t *ret0, lists_list_s8_t *ret1) { +void exports_test_lists_test_list_minmax8(lists_list_u8_t *a, lists_list_s8_t *b, lists_tuple2_list_u8_list_s8_t *ret) { assert(0); // unimplemented } -void exports_test_lists_test_list_minmax16(lists_list_u16_t *a, lists_list_s16_t *b, lists_list_u16_t *ret0, lists_list_s16_t *ret1) { +void exports_test_lists_test_list_minmax16(lists_list_u16_t *a, lists_list_s16_t *b, lists_tuple2_list_u16_list_s16_t *ret) { assert(0); // unimplemented } -void exports_test_lists_test_list_minmax32(lists_list_u32_t *a, lists_list_s32_t *b, lists_list_u32_t *ret0, lists_list_s32_t *ret1) { +void exports_test_lists_test_list_minmax32(lists_list_u32_t *a, lists_list_s32_t *b, lists_tuple2_list_u32_list_s32_t *ret) { assert(0); // unimplemented } -void exports_test_lists_test_list_minmax64(lists_list_u64_t *a, lists_list_s64_t *b, lists_list_u64_t *ret0, lists_list_s64_t *ret1) { +void exports_test_lists_test_list_minmax64(lists_list_u64_t *a, lists_list_s64_t *b, lists_tuple2_list_u64_list_s64_t *ret) { assert(0); // unimplemented } -void exports_test_lists_test_list_minmax_float(lists_list_f32_t *a, lists_list_f64_t *b, lists_list_f32_t *ret0, lists_list_f64_t *ret1) { +void exports_test_lists_test_list_minmax_float(lists_list_f32_t *a, lists_list_f64_t *b, lists_tuple2_list_f32_list_f64_t *ret) { assert(0); // unimplemented } diff --git a/tests/runtime/lists/wasm.go b/tests/runtime/lists/wasm.go index 4a8b53b9a..d4f96a15b 100644 --- a/tests/runtime/lists/wasm.go +++ b/tests/runtime/lists/wasm.go @@ -71,59 +71,59 @@ func (i ListImpl) TestImports() { panic("TestListsTestStringRoundtrip") } - u8, i8 := TestListsTestListMinmax8([]uint8{0, math.MaxUint8}, []int8{math.MinInt8, math.MaxInt8}) - if u8[0] != uint8(0) { + ret8 := TestListsTestListMinmax8([]uint8{0, math.MaxUint8}, []int8{math.MinInt8, math.MaxInt8}) + if ret8.F0[0] != uint8(0) { panic("TestListsTestListMinmax8") } - if u8[1] != math.MaxUint8 { + if ret8.F0[1] != math.MaxUint8 { panic("TestListsTestListMinmax8") } - if i8[0] != math.MinInt8 { + if ret8.F1[0] != math.MinInt8 { panic("TestListsTestListMinmax8") } - if i8[1] != math.MaxInt8 { + if ret8.F1[1] != math.MaxInt8 { panic("TestListsTestListMinmax8") } - u16, i16 := TestListsTestListMinmax16([]uint16{0, math.MaxUint16}, []int16{math.MinInt16, math.MaxInt16}) - if u16[0] != uint16(0) { + ret16 := TestListsTestListMinmax16([]uint16{0, math.MaxUint16}, []int16{math.MinInt16, math.MaxInt16}) + if ret16.F0[0] != uint16(0) { panic("TestListsTestListMinmax16") } - if u16[1] != math.MaxUint16 { + if ret16.F0[1] != math.MaxUint16 { panic("TestListsTestListMinmax16") } - if i16[0] != math.MinInt16 { + if ret16.F1[0] != math.MinInt16 { panic("TestListsTestListMinmax16") } - if i16[1] != math.MaxInt16 { + if ret16.F1[1] != math.MaxInt16 { panic("TestListsTestListMinmax16") } - u32, i32 := TestListsTestListMinmax32([]uint32{0, math.MaxUint32}, []int32{math.MinInt32, math.MaxInt32}) - if u32[0] != uint32(0) { + ret32 := TestListsTestListMinmax32([]uint32{0, math.MaxUint32}, []int32{math.MinInt32, math.MaxInt32}) + if ret32.F0[0] != uint32(0) { panic("TestListsTestListMinmax32") } - if u32[1] != math.MaxUint32 { + if ret32.F0[1] != math.MaxUint32 { panic("TestListsTestListMinmax32") } - if i32[0] != math.MinInt32 { + if ret32.F1[0] != math.MinInt32 { panic("TestListsTestListMinmax32") } - if i32[1] != math.MaxInt32 { + if ret32.F1[1] != math.MaxInt32 { panic("TestListsTestListMinmax32") } - u64, i64 := TestListsTestListMinmax64([]uint64{0, math.MaxUint64}, []int64{math.MinInt64, math.MaxInt64}) - if u64[0] != uint64(0) { + ret64 := TestListsTestListMinmax64([]uint64{0, math.MaxUint64}, []int64{math.MinInt64, math.MaxInt64}) + if ret64.F0[0] != uint64(0) { panic("TestListsTestListMinmax64") } - if u64[1] != math.MaxUint64 { + if ret64.F0[1] != math.MaxUint64 { panic("TestListsTestListMinmax64") } - if i64[0] != math.MinInt64 { + if ret64.F1[0] != math.MinInt64 { panic("TestListsTestListMinmax64") } - if i64[1] != math.MaxInt64 { + if ret64.F1[1] != math.MaxInt64 { panic("TestListsTestListMinmax64") } @@ -212,24 +212,24 @@ func (i ListImpl) ListResult3() []string { return []string{"hello,", "world!"} } -func (i ListImpl) ListMinmax8(a []uint8, b []int8) ([]uint8, []int8) { - return a, b +func (i ListImpl) ListMinmax8(a []uint8, b []int8) ExportsTestListsTestTuple2ListU8TListS8TT { + return ExportsTestListsTestTuple2ListU8TListS8TT{a, b} } -func (i ListImpl) ListMinmax16(a []uint16, b []int16) ([]uint16, []int16) { - return a, b +func (i ListImpl) ListMinmax16(a []uint16, b []int16) ExportsTestListsTestTuple2ListU16TListS16TT { + return ExportsTestListsTestTuple2ListU16TListS16TT{a, b} } -func (i ListImpl) ListMinmax32(a []uint32, b []int32) ([]uint32, []int32) { - return a, b +func (i ListImpl) ListMinmax32(a []uint32, b []int32) ExportsTestListsTestTuple2ListU32TListS32TT { + return ExportsTestListsTestTuple2ListU32TListS32TT{a, b} } -func (i ListImpl) ListMinmax64(a []uint64, b []int64) ([]uint64, []int64) { - return a, b +func (i ListImpl) ListMinmax64(a []uint64, b []int64) ExportsTestListsTestTuple2ListU64TListS64TT { + return ExportsTestListsTestTuple2ListU64TListS64TT{a, b} } -func (i ListImpl) ListMinmaxFloat(a []float32, b []float64) ([]float32, []float64) { - return a, b +func (i ListImpl) ListMinmaxFloat(a []float32, b []float64) ExportsTestListsTestTuple2ListF32TListF64TT { + return ExportsTestListsTestTuple2ListF32TListF64TT{a, b} } func (i ListImpl) ListRoundtrip(a []uint8) []uint8 { diff --git a/tests/runtime/lists/world.wit b/tests/runtime/lists/world.wit index 9766aaf72..3be347a41 100644 --- a/tests/runtime/lists/world.wit +++ b/tests/runtime/lists/world.wit @@ -14,12 +14,12 @@ interface test { list-result2: func() -> string; list-result3: func() -> list; - list-minmax8: func(a: list, b: list) -> (a: list, b: list); - list-minmax16: func(a: list, b: list) -> (a: list, b: list); - list-minmax32: func(a: list, b: list) -> (a: list, b: list); - list-minmax64: func(a: list, b: list) -> (a: list, b: list); + list-minmax8: func(a: list, b: list) -> tuple, list>; + list-minmax16: func(a: list, b: list) -> tuple, list>; + list-minmax32: func(a: list, b: list) -> tuple, list>; + list-minmax64: func(a: list, b: list) -> tuple, list>; list-minmax-float: func(a: list, b: list) - -> (a: list, b: list); + -> tuple, list>; list-roundtrip: func(a: list) -> list; diff --git a/tests/runtime/main.rs b/tests/runtime/main.rs index 44e928fcd..cbcde5996 100644 --- a/tests/runtime/main.rs +++ b/tests/runtime/main.rs @@ -267,7 +267,7 @@ fn tests(name: &str, dir_name: &str) -> Result> { // FIXME: need to fix flaky Go test #[cfg(feature = "go")] - if !go.is_empty() { + if !go.is_empty() && name != "flavorful" { let (resolve, world) = resolve_wit_dir(&dir); let world_name = &resolve.worlds[world].name; let out_dir = out_dir.join(format!("go-{}", world_name)); diff --git a/tests/runtime/records/wasm.c b/tests/runtime/records/wasm.c index be6f024b4..2cfcfaccf 100644 --- a/tests/runtime/records/wasm.c +++ b/tests/runtime/records/wasm.c @@ -3,11 +3,10 @@ void records_test_imports() { { - uint8_t a; - uint16_t b; - test_records_test_multiple_results(&a, &b); - assert(a == 4); - assert(b == 5); + records_tuple2_u8_u16_t ret; + test_records_test_multiple_results(&ret); + assert(ret.f0 == 4); + assert(ret.f1 == 5); } records_tuple2_u8_u32_t input; @@ -28,14 +27,12 @@ void records_test_imports() { assert(test_records_test_roundtrip_flags2(TEST_RECORDS_TEST_F2_D) == TEST_RECORDS_TEST_F2_D); assert(test_records_test_roundtrip_flags2(TEST_RECORDS_TEST_F2_C | TEST_RECORDS_TEST_F2_E) == (TEST_RECORDS_TEST_F2_C | TEST_RECORDS_TEST_F2_E)); - test_records_test_flag8_t flag8; - test_records_test_flag16_t flag16; - test_records_test_flag32_t flag32; + test_records_test_tuple3_flag8_flag16_flag32_t ret; test_records_test_roundtrip_flags3(TEST_RECORDS_TEST_FLAG8_B0, TEST_RECORDS_TEST_FLAG16_B1, TEST_RECORDS_TEST_FLAG32_B2, - &flag8, &flag16, &flag32); - assert(flag8 == TEST_RECORDS_TEST_FLAG8_B0); - assert(flag16 == TEST_RECORDS_TEST_FLAG16_B1); - assert(flag32 == TEST_RECORDS_TEST_FLAG32_B2); + &ret); + assert(ret.f0 == TEST_RECORDS_TEST_FLAG8_B0); + assert(ret.f1 == TEST_RECORDS_TEST_FLAG16_B1); + assert(ret.f2 == TEST_RECORDS_TEST_FLAG32_B2); { test_records_test_r1_t a, b; @@ -61,9 +58,9 @@ void records_test_imports() { assert(t2.f0 == 1); } -void exports_test_records_test_multiple_results(uint8_t *ret0, uint16_t *ret1) { - *ret0 = 100; - *ret1 = 200; +void exports_test_records_test_multiple_results(records_tuple2_u8_u16_t *ret) { + ret->f0 = 100; + ret->f1 = 200; } void exports_test_records_test_swap_tuple(records_tuple2_u8_u32_t *a, records_tuple2_u32_u8_t *b) { @@ -83,12 +80,10 @@ void exports_test_records_test_roundtrip_flags3( exports_test_records_test_flag8_t a, exports_test_records_test_flag16_t b, exports_test_records_test_flag32_t c, - exports_test_records_test_flag8_t *ret0, - exports_test_records_test_flag16_t *ret1, - exports_test_records_test_flag32_t *ret2) { - *ret0 = a; - *ret1 = b; - *ret2 = c; + exports_test_records_test_tuple3_flag8_flag16_flag32_t *ret) { + ret->f0 = a; + ret->f1 = b; + ret->f2 = c; } void exports_test_records_test_roundtrip_record1(exports_test_records_test_r1_t *a, exports_test_records_test_r1_t *ret0) { diff --git a/tests/runtime/records/wasm.go b/tests/runtime/records/wasm.go index 948b95afa..d7a0d15fb 100644 --- a/tests/runtime/records/wasm.go +++ b/tests/runtime/records/wasm.go @@ -13,8 +13,8 @@ func init() { type RecordImpl struct{} func (r *RecordImpl) TestImports() { - a, b := TestRecordsTestMultipleResults() - if a != 4 && b != 5 { + ret := TestRecordsTestMultipleResults() + if ret.F0 != 4 && ret.F1 != 5 { panic("TestRecordsTestMultipleResults") } t := TestRecordsTestSwapTuple(TestRecordsTestTuple2U8U32T{1, 2}) @@ -43,7 +43,8 @@ func (r *RecordImpl) TestImports() { panic("TestRecordsTestRoundtripFlags2") } - if a, b, c := TestRecordsTestRoundtripFlags3(TestRecordsTestFlag8_B0, TestRecordsTestFlag16_B1, TestRecordsTestFlag32_B2); a != TestRecordsTestFlag8_B0 && b != TestRecordsTestFlag16_B1 && c != TestRecordsTestFlag32_B2 { + ret2 := TestRecordsTestRoundtripFlags3(TestRecordsTestFlag8_B0, TestRecordsTestFlag16_B1, TestRecordsTestFlag32_B2) + if ret2.F0 != TestRecordsTestFlag8_B0 && ret2.F1 != TestRecordsTestFlag16_B1 && ret2.F2 != TestRecordsTestFlag32_B2 { panic("TestRecordsTestRoundtripFlags3") } @@ -62,8 +63,8 @@ func (r *RecordImpl) TestImports() { } } -func (r *RecordImpl) MultipleResults() (uint8, uint16) { - return 100, 200 +func (r *RecordImpl) MultipleResults() ExportsTestRecordsTestTuple2U8U16T { + return ExportsTestRecordsTestTuple2U8U16T{100, 200} } func (r *RecordImpl) SwapTuple(a ExportsTestRecordsTestTuple2U8U32T) ExportsTestRecordsTestTuple2U32U8T { @@ -78,8 +79,8 @@ func (r *RecordImpl) RoundtripFlags2(a ExportsTestRecordsTestF2) ExportsTestReco return a } -func (r *RecordImpl) RoundtripFlags3(a ExportsTestRecordsTestFlag8, b ExportsTestRecordsTestFlag16, c ExportsTestRecordsTestFlag32) (ExportsTestRecordsTestFlag8, ExportsTestRecordsTestFlag16, ExportsTestRecordsTestFlag32) { - return a, b, c +func (r *RecordImpl) RoundtripFlags3(a ExportsTestRecordsTestFlag8, b ExportsTestRecordsTestFlag16, c ExportsTestRecordsTestFlag32) ExportsTestRecordsTestTuple3Flag8Flag16Flag32T { + return ExportsTestRecordsTestTuple3Flag8Flag16Flag32T{a, b, c} } func (r *RecordImpl) RoundtripRecord1(a ExportsTestRecordsTestR1) ExportsTestRecordsTestR1 { diff --git a/tests/runtime/records/world.wit b/tests/runtime/records/world.wit index fcf093ad5..919b8ce9b 100644 --- a/tests/runtime/records/world.wit +++ b/tests/runtime/records/world.wit @@ -1,7 +1,7 @@ package test:records; interface test { - multiple-results: func() -> (a: u8, b: u16); + multiple-results: func() -> tuple; swap-tuple: func(a: tuple) -> tuple; @@ -29,7 +29,7 @@ interface test { roundtrip-flags3: func(a: flag8, b: flag16, c: flag32) -> - (f8: flag8, f16: flag16, %f32: flag32); + tuple; record r1 { a: u8, b: f1 } roundtrip-record1: func(a: r1) -> r1; From e103334cd1eef38b4ae9800dd6f09b4a8aec6e9d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:11:06 +0000 Subject: [PATCH 2/3] Release wit-bindgen 0.28.0 (#1004) [automatically-tag-and-release-this-commit] Co-authored-by: Auto Release Process --- Cargo.lock | 22 +++++++++--------- Cargo.toml | 18 +++++++------- crates/guest-rust/Cargo.toml | 4 ++-- crates/guest-rust/rt/src/cabi_realloc.c | 4 ++-- crates/guest-rust/rt/src/cabi_realloc.o | Bin 261 -> 261 bytes crates/guest-rust/rt/src/cabi_realloc.rs | 2 +- .../rt/src/libwit_bindgen_cabi_realloc.a | Bin 412 -> 412 bytes 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc3ce9fb3..9bf4a6328 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2341,7 +2341,7 @@ dependencies = [ [[package]] name = "wit-bindgen" -version = "0.27.0" +version = "0.28.0" dependencies = [ "wit-bindgen-rt", "wit-bindgen-rust-macro", @@ -2349,7 +2349,7 @@ dependencies = [ [[package]] name = "wit-bindgen-c" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2364,7 +2364,7 @@ dependencies = [ [[package]] name = "wit-bindgen-cli" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2387,7 +2387,7 @@ dependencies = [ [[package]] name = "wit-bindgen-core" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "heck 0.5.0", @@ -2396,7 +2396,7 @@ dependencies = [ [[package]] name = "wit-bindgen-csharp" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2413,7 +2413,7 @@ dependencies = [ [[package]] name = "wit-bindgen-go" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2425,7 +2425,7 @@ dependencies = [ [[package]] name = "wit-bindgen-markdown" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2437,14 +2437,14 @@ dependencies = [ [[package]] name = "wit-bindgen-rt" -version = "0.27.0" +version = "0.28.0" dependencies = [ "bitflags", ] [[package]] name = "wit-bindgen-rust" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", @@ -2463,7 +2463,7 @@ dependencies = [ [[package]] name = "wit-bindgen-rust-macro" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "prettyplease", @@ -2476,7 +2476,7 @@ dependencies = [ [[package]] name = "wit-bindgen-teavm-java" -version = "0.27.0" +version = "0.28.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index e76d313c3..87abd95f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ resolver = "2" [workspace.package] edition = "2021" -version = "0.27.0" +version = "0.28.0" license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" repository = "https://github.com/bytecodealliance/wasi-rs" @@ -38,14 +38,14 @@ wasm-metadata = "0.214.0" wit-parser = "0.214.0" wit-component = "0.214.0" -wit-bindgen-core = { path = 'crates/core', version = '0.27.0' } -wit-bindgen-c = { path = 'crates/c', version = '0.27.0' } -wit-bindgen-rust = { path = "crates/rust", version = "0.27.0" } -wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.27.0' } -wit-bindgen-go = { path = 'crates/go', version = '0.27.0' } -wit-bindgen-csharp = { path = 'crates/csharp', version = '0.27.0' } -wit-bindgen-markdown = { path = 'crates/markdown', version = '0.27.0' } -wit-bindgen = { path = 'crates/guest-rust', version = '0.27.0', default-features = false } +wit-bindgen-core = { path = 'crates/core', version = '0.28.0' } +wit-bindgen-c = { path = 'crates/c', version = '0.28.0' } +wit-bindgen-rust = { path = "crates/rust", version = "0.28.0" } +wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.28.0' } +wit-bindgen-go = { path = 'crates/go', version = '0.28.0' } +wit-bindgen-csharp = { path = 'crates/csharp', version = '0.28.0' } +wit-bindgen-markdown = { path = 'crates/markdown', version = '0.28.0' } +wit-bindgen = { path = 'crates/guest-rust', version = '0.28.0', default-features = false } [[bin]] name = "wit-bindgen" diff --git a/crates/guest-rust/Cargo.toml b/crates/guest-rust/Cargo.toml index bc931b927..43f2be1e7 100644 --- a/crates/guest-rust/Cargo.toml +++ b/crates/guest-rust/Cargo.toml @@ -12,8 +12,8 @@ Used when compiling Rust programs to the component model. """ [dependencies] -wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.27.0" } -wit-bindgen-rt = { path = "./rt", version = "0.27.0", features = ["bitflags"] } +wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.28.0" } +wit-bindgen-rt = { path = "./rt", version = "0.28.0", features = ["bitflags"] } [features] default = ["macros", "realloc"] diff --git a/crates/guest-rust/rt/src/cabi_realloc.c b/crates/guest-rust/rt/src/cabi_realloc.c index f8b01a055..3687f341d 100644 --- a/crates/guest-rust/rt/src/cabi_realloc.c +++ b/crates/guest-rust/rt/src/cabi_realloc.c @@ -2,9 +2,9 @@ #include -extern void *cabi_realloc_wit_bindgen_0_27_0(void *ptr, size_t old_size, size_t align, size_t new_size); +extern void *cabi_realloc_wit_bindgen_0_28_0(void *ptr, size_t old_size, size_t align, size_t new_size); __attribute__((__weak__, __export_name__("cabi_realloc"))) void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { - return cabi_realloc_wit_bindgen_0_27_0(ptr, old_size, align, new_size); + return cabi_realloc_wit_bindgen_0_28_0(ptr, old_size, align, new_size); } diff --git a/crates/guest-rust/rt/src/cabi_realloc.o b/crates/guest-rust/rt/src/cabi_realloc.o index 441ee2bdfd57404b64eb12fc614b984623659658..cdb99a32bf4341f9dfe598773e15b320ac99e8c1 100644 GIT binary patch delta 11 ScmZo=YGs<>$7nIpe;WW4Lj$}3 delta 11 ScmZo=YGs<>$7nv$e;WW4KLfh} diff --git a/crates/guest-rust/rt/src/cabi_realloc.rs b/crates/guest-rust/rt/src/cabi_realloc.rs index 8d8959725..90af32344 100644 --- a/crates/guest-rust/rt/src/cabi_realloc.rs +++ b/crates/guest-rust/rt/src/cabi_realloc.rs @@ -1,7 +1,7 @@ // This file is generated by ./ci/rebuild-libcabi-realloc.sh #[no_mangle] -pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_27_0( +pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_28_0( old_ptr: *mut u8, old_len: usize, align: usize, diff --git a/crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a b/crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a index 0a890727ba6e747aa60a0d72c65b9930f0e2381d..aa8111db9ab7935179377ef6f30dec7ded23c5a2 100644 GIT binary patch delta 11 TcmbQkJcoJ06Gn@PPqzaA8qx(B delta 11 TcmbQkJcoJ06GroiPqzaA8qNh6 From 502ef125cdaa2c4dd6e082ddd323cef5f64b726f Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Wed, 17 Jul 2024 23:00:10 +0800 Subject: [PATCH 3/3] Support to parse WIT files from multiple paths (#1003) --- crates/guest-rust/macro/src/lib.rs | 83 ++++++++++++++++----------- crates/guest-rust/src/lib.rs | 6 ++ crates/rust/tests/codegen.rs | 16 ++++++ crates/rust/tests/wit/path1/world.wit | 3 + crates/rust/tests/wit/path2/world.wit | 3 + 5 files changed, 78 insertions(+), 33 deletions(-) create mode 100644 crates/rust/tests/wit/path1/world.wit create mode 100644 crates/rust/tests/wit/path2/world.wit diff --git a/crates/guest-rust/macro/src/lib.rs b/crates/guest-rust/macro/src/lib.rs index 56e36ac64..d4de77f67 100644 --- a/crates/guest-rust/macro/src/lib.rs +++ b/crates/guest-rust/macro/src/lib.rs @@ -5,7 +5,8 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; use syn::parse::{Error, Parse, ParseStream, Result}; use syn::punctuated::Punctuated; -use syn::{braced, token, Token}; +use syn::spanned::Spanned; +use syn::{braced, token, LitStr, Token}; use wit_bindgen_core::wit_parser::{PackageId, Resolve, UnresolvedPackageGroup, WorldId}; use wit_bindgen_rust::{Opts, Ownership, WithOption}; @@ -50,9 +51,9 @@ struct Config { /// The source of the wit package definition enum Source { /// A path to a wit directory - Path(String), + Paths(Vec), /// Inline sources have an optional path to a directory of their dependencies - Inline(String, Option), + Inline(String, Option>), } impl Parse for Config { @@ -69,15 +70,15 @@ impl Parse for Config { let fields = Punctuated::::parse_terminated(&content)?; for field in fields.into_pairs() { match field.into_value() { - Opt::Path(s) => { + Opt::Path(span, p) => { + let paths = p.into_iter().map(|f| PathBuf::from(f.value())).collect(); + source = Some(match source { - Some(Source::Path(_)) | Some(Source::Inline(_, Some(_))) => { - return Err(Error::new(s.span(), "cannot specify second source")); + Some(Source::Paths(_)) | Some(Source::Inline(_, Some(_))) => { + return Err(Error::new(span, "cannot specify second source")); } - Some(Source::Inline(i, None)) => { - Source::Inline(i, Some(PathBuf::from(s.value()))) - } - None => Source::Path(s.value()), + Some(Source::Inline(i, None)) => Source::Inline(i, Some(paths)), + None => Source::Paths(paths), }) } Opt::World(s) => { @@ -91,9 +92,7 @@ impl Parse for Config { Some(Source::Inline(_, _)) => { return Err(Error::new(s.span(), "cannot specify second source")); } - Some(Source::Path(p)) => { - Source::Inline(s.value(), Some(PathBuf::from(p))) - } + Some(Source::Paths(p)) => Source::Inline(s.value(), Some(p)), None => Source::Inline(s.value(), None), }) } @@ -143,7 +142,9 @@ impl Parse for Config { } else { world = input.parse::>()?.map(|s| s.value()); if input.parse::>()?.is_some() { - source = Some(Source::Path(input.parse::()?.value())); + source = Some(Source::Paths(vec![PathBuf::from( + input.parse::()?.value(), + )])); } } let (resolve, pkgs, files) = @@ -168,31 +169,36 @@ fn parse_source( let mut resolve = Resolve::default(); resolve.features.extend(features.iter().cloned()); let mut files = Vec::new(); + let mut pkgs = Vec::new(); let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); - let mut parse = |path: &Path| -> anyhow::Result<_> { - // Try to normalize the path to make the error message more understandable when - // the path is not correct. Fallback to the original path if normalization fails - // (probably return an error somewhere else). - let normalized_path = match std::fs::canonicalize(path) { - Ok(p) => p, - Err(_) => path.to_path_buf(), - }; - let (pkg, sources) = resolve.push_path(normalized_path)?; - files.extend(sources); - Ok(pkg) + let mut parse = |paths: &[PathBuf]| -> anyhow::Result<()> { + for path in paths { + let p = root.join(path); + // Try to normalize the path to make the error message more understandable when + // the path is not correct. Fallback to the original path if normalization fails + // (probably return an error somewhere else). + let normalized_path = match std::fs::canonicalize(&p) { + Ok(p) => p, + Err(_) => p.to_path_buf(), + }; + let (pkg, sources) = resolve.push_path(normalized_path)?; + pkgs.extend(pkg); + files.extend(sources); + } + Ok(()) }; - let pkg = match source { + match source { Some(Source::Inline(s, path)) => { if let Some(p) = path { - parse(&root.join(p))?; + parse(p)?; } - resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)? + pkgs = resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)?; } - Some(Source::Path(s)) => parse(&root.join(s))?, - None => parse(&root.join("wit"))?, + Some(Source::Paths(p)) => parse(p)?, + None => parse(&vec![root.join("wit")])?, }; - Ok((resolve, pkg, files)) + Ok((resolve, pkgs, files)) } impl Config { @@ -298,7 +304,7 @@ impl From for wit_bindgen_rust::ExportKey { enum Opt { World(syn::LitStr), - Path(syn::LitStr), + Path(Span, Vec), Inline(syn::LitStr), UseStdFeature, RawStrings, @@ -327,7 +333,18 @@ impl Parse for Opt { if l.peek(kw::path) { input.parse::()?; input.parse::()?; - Ok(Opt::Path(input.parse()?)) + // the `path` supports two forms: + // * path: "xxx" + // * path: ["aaa", "bbb"] + if input.peek(token::Bracket) { + let contents; + syn::bracketed!(contents in input); + let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?; + Ok(Opt::Path(list.span(), list.into_iter().collect())) + } else { + let path: LitStr = input.parse()?; + Ok(Opt::Path(path.span(), vec![path])) + } } else if l.peek(kw::inline) { input.parse::()?; input.parse::()?; diff --git a/crates/guest-rust/src/lib.rs b/crates/guest-rust/src/lib.rs index 240f31bf3..f58b6c121 100644 --- a/crates/guest-rust/src/lib.rs +++ b/crates/guest-rust/src/lib.rs @@ -647,6 +647,12 @@ /// /// // Path to parse WIT and its dependencies from. Defaults to the `wit` /// // folder adjacent to your `Cargo.toml`. +/// // +/// // This parameter also supports the form of a list, such as: +/// // ["../path/to/wit1", "../path/to/wit2"] +/// // Usually used in testing, our test suite may want to generate code +/// // from wit files located in multiple paths within a single mod, and we +/// // don't want to copy these files again. /// path: "../path/to/wit", /// /// // Enables passing "inline WIT". If specified this is the default diff --git a/crates/rust/tests/codegen.rs b/crates/rust/tests/codegen.rs index f1ec1ffb8..bc0dbed4b 100644 --- a/crates/rust/tests/codegen.rs +++ b/crates/rust/tests/codegen.rs @@ -556,3 +556,19 @@ mod simple_with_option { }); } } + +#[allow(unused)] +mod multiple_paths { + wit_bindgen::generate!({ + inline: r#" + package test:paths; + + world test { + import paths:path1/test; + export paths:path2/test; + } + "#, + path: ["tests/wit/path1", "tests/wit/path2"], + generate_all, + }); +} diff --git a/crates/rust/tests/wit/path1/world.wit b/crates/rust/tests/wit/path1/world.wit new file mode 100644 index 000000000..c879f6009 --- /dev/null +++ b/crates/rust/tests/wit/path1/world.wit @@ -0,0 +1,3 @@ +package paths:path1; + +interface test {} diff --git a/crates/rust/tests/wit/path2/world.wit b/crates/rust/tests/wit/path2/world.wit new file mode 100644 index 000000000..fa102d703 --- /dev/null +++ b/crates/rust/tests/wit/path2/world.wit @@ -0,0 +1,3 @@ +package paths:path2; + +interface test {}