1 /**
2  * \file wasmtime/wat.hh
3  */
4 
5 #ifndef WASMTIME_WAT_HH
6 #define WASMTIME_WAT_HH
7 
8 #include <string_view>
9 #include <vector>
10 #include <wasmtime/conf.h>
11 #include <wasmtime/error.hh>
12 #include <wasmtime/span.hh>
13 #include <wasmtime/wat.h>
14 
15 namespace wasmtime {
16 
17 #ifdef WASMTIME_FEATURE_WAT
18 
19 /**
20  * \brief Converts the WebAssembly text format into the WebAssembly binary
21  * format.
22  *
23  * This will parse the text format and attempt to translate it to the binary
24  * format. Note that the text parser assumes that all WebAssembly features are
25  * enabled and will parse syntax of future proposals. The exact syntax here
26  * parsed may be tweaked over time.
27  *
28  * Returns either an error if parsing failed or the wasm binary.
29  */
30 inline Result<std::vector<uint8_t>> wat2wasm(std::string_view wat) {
31   wasm_byte_vec_t ret;
32   auto *error = wasmtime_wat2wasm(wat.data(), wat.size(), &ret);
33   if (error != nullptr) {
34     return Error(error);
35   }
36   std::vector<uint8_t> vec;
37   // NOLINTNEXTLINE TODO can this be done without triggering lints?
38   Span<uint8_t> raw(reinterpret_cast<uint8_t *>(ret.data), ret.size);
39   vec.assign(raw.begin(), raw.end());
40   wasm_byte_vec_delete(&ret);
41   return vec;
42 }
43 
44 #endif // WASMTIME_FEATURE_WAT
45 
46 } // namespace wasmtime
47 
48 #endif // WASMTIME_WAT_HH
49