diff --git a/tests/runtime/main.rs b/tests/runtime/main.rs index 89fccbe5f..3047170a4 100644 --- a/tests/runtime/main.rs +++ b/tests/runtime/main.rs @@ -278,7 +278,12 @@ fn tests(name: &str, dir_name: &str) -> Result> { let snake = world_name.replace("-", "_"); let mut files = Default::default(); - let opts = wit_bindgen_cpp::Opts::default(); + let mut opts = wit_bindgen_cpp::Opts::default(); + if let Some(path) = path.file_name().and_then(|s| s.to_str()) { + if path.contains(".new.") { + opts.new_api = true; + } + } opts.build().generate(&resolve, world, &mut files).unwrap(); for (file, contents) in files.iter() { diff --git a/tests/runtime/strings/wasm.new.cpp b/tests/runtime/strings/wasm.new.cpp new file mode 100644 index 000000000..6988f224d --- /dev/null +++ b/tests/runtime/strings/wasm.new.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +void assert_str(std::string_view str, const char* expected) { + size_t expected_len = strlen(expected); + assert(str.size() == expected_len); + assert(memcmp(str.data(), expected, expected_len) == 0); +} + +void exports::strings::TestImports() { + test::strings::imports::TakeBasic(std::string_view("latin utf16")); + + wit::string str2 = test::strings::imports::ReturnUnicode(); + assert_str(str2.get_view(), "🚀🚀🚀 𠈄𓀀"); +} + +wit::string exports::strings::ReturnEmpty() { + // return a non-zero address (follows cabi_realloc logic) + return wit::string((char const*)1, 0); +} + +// new API: Identical for guest import and export +wit::string exports::strings::Roundtrip(std::string_view str) { + assert(str.size() > 0); + return wit::string::from_view(str); +}