From 79239df01df26f9e6b46dc3d55bcefc6f22f2088 Mon Sep 17 00:00:00 2001 From: Andreas Rossberg Date: Thu, 6 Aug 2020 12:51:10 +0200 Subject: [PATCH] Distinguish name constructors for zt strings --- example/trap.c | 2 +- example/trap.cc | 2 +- include/wasm.h | 6 ++++++ include/wasm.hh | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/example/trap.c b/example/trap.c index 074728e..1146f69 100644 --- a/example/trap.c +++ b/example/trap.c @@ -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; diff --git a/example/trap.cc b/example/trap.cc index 10763b7..2e70c76 100644 --- a/example/trap.cc +++ b/example/trap.cc @@ -12,7 +12,7 @@ auto fail_callback( ) -> wasm::own { std::cout << "Calling back..." << std::endl; auto store = reinterpret_cast(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); } diff --git a/include/wasm.h b/include/wasm.h index 5739556..5831fac 100644 --- a/include/wasm.h +++ b/include/wasm.h @@ -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); } diff --git a/include/wasm.hh b/include/wasm.hh index 1e46b3b..597c10f 100644 --- a/include/wasm.hh +++ b/include/wasm.hh @@ -146,6 +146,12 @@ public: } static auto make(std::string s) -> vec { + 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 { auto v = vec(s.length() + 1); if (v) std::strcpy(v.get(), s.data()); return v;