Skip to content

Commit

Permalink
Distinguish name constructors for zt strings (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg authored Aug 6, 2020
1 parent af6e0fb commit b922f77
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ own wasm_trap_t* fail_callback(
) {
printf("Calling back...\n");
own wasm_name_t message;
wasm_name_new_from_string(&message, "callback abort");
wasm_name_new_from_string_nt(&message, "callback abort");
own wasm_trap_t* trap = wasm_trap_new((wasm_store_t*)env, &message);
wasm_name_delete(&message);
return trap;
Expand Down
2 changes: 1 addition & 1 deletion example/trap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ auto fail_callback(
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl;
auto store = reinterpret_cast<wasm::Store*>(env);
auto message = wasm::Name::make(std::string("callback abort"));
auto message = wasm::Name::make_nt(std::string("callback abort"));
return wasm::Trap::make(store, message);
}

Expand Down
6 changes: 6 additions & 0 deletions include/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ typedef wasm_byte_vec_t wasm_name_t;

static inline void wasm_name_new_from_string(
own wasm_name_t* out, const char* s
) {
wasm_name_new(out, strlen(s), s);
}

static inline void wasm_name_new_from_string_nt(
own wasm_name_t* out, const char* s
) {
wasm_name_new(out, strlen(s) + 1, s);
}
Expand Down
6 changes: 6 additions & 0 deletions include/wasm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public:
}

static auto make(std::string s) -> vec<char> {
auto v = vec(s.length());
if (v) std::strncpy(v.get(), s.data(), s.length());
return v;
}

static auto make_nt(std::string s) -> vec<char> {
auto v = vec(s.length() + 1);
if (v) std::strcpy(v.get(), s.data());
return v;
Expand Down

0 comments on commit b922f77

Please sign in to comment.