Skip to content

Commit

Permalink
test the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Sep 2, 2024
1 parent a0864a6 commit fe95f9d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,12 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {

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() {
Expand Down
29 changes: 29 additions & 0 deletions tests/runtime/strings/wasm.new.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <assert.h>
#include <strings_cpp.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

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);
}

0 comments on commit fe95f9d

Please sign in to comment.