Wasmtime
module.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_MODULE_HH
6#define WASMTIME_MODULE_HH
7
8#include <memory>
9#include <string_view>
10#include <wasmtime/engine.hh>
11#include <wasmtime/helpers.hh>
12#include <wasmtime/module.h>
13#include <wasmtime/span.hh>
16#include <wasmtime/wat.hh>
17
18namespace wasmtime {
19
28class Module {
29 WASMTIME_CLONE_WRAPPER(Module, wasmtime_module);
30
31#ifdef WASMTIME_FEATURE_COMPILER
38 static Result<Module> compile(Engine &engine, std::string_view wat) {
39 auto wasm = wat2wasm(wat);
40 if (!wasm) {
41 return wasm.err();
42 }
43 auto bytes = wasm.ok();
44 return compile(engine, bytes);
45 }
46
57 static Result<Module> compile(Engine &engine, Span<uint8_t> wasm) {
58 wasmtime_module_t *ret = nullptr;
59 auto *error =
60 wasmtime_module_new(engine.capi(), wasm.data(), wasm.size(), &ret);
61 if (error != nullptr) {
62 return Error(error);
63 }
64 return Module(ret);
65 }
66
73 static Result<std::monostate> validate(Engine &engine, Span<uint8_t> wasm) {
74 auto *error =
75 wasmtime_module_validate(engine.capi(), wasm.data(), wasm.size());
76 if (error != nullptr) {
77 return Error(error);
78 }
79 return std::monostate();
80 }
81#endif // WASMTIME_FEATURE_COMPILER
82
95 static Result<Module> deserialize(Engine &engine, Span<uint8_t> wasm) {
96 wasmtime_module_t *ret = nullptr;
97 auto *error = wasmtime_module_deserialize(engine.capi(), wasm.data(),
98 wasm.size(), &ret);
99 if (error != nullptr) {
100 return Error(error);
101 }
102 return Module(ret);
103 }
104
117 static Result<Module> deserialize_file(Engine &engine,
118 const std::string &path) {
119 wasmtime_module_t *ret = nullptr;
120 auto *error =
121 wasmtime_module_deserialize_file(engine.capi(), path.c_str(), &ret);
122 if (error != nullptr) {
123 return Error(error);
124 }
125 return Module(ret);
126 }
127
129 ImportType::List imports() const {
130 ImportType::List list;
131 wasmtime_module_imports(ptr.get(), &list.list);
132 return list;
133 }
134
136 ExportType::List exports() const {
137 ExportType::List list;
138 wasmtime_module_exports(ptr.get(), &list.list);
139 return list;
140 }
141
142#ifdef WASMTIME_FEATURE_COMPILER
149 Result<std::vector<uint8_t>> serialize() const {
150 wasm_byte_vec_t bytes;
151 auto *error = wasmtime_module_serialize(ptr.get(), &bytes);
152 if (error != nullptr) {
153 return Error(error);
154 }
155 std::vector<uint8_t> ret;
156 // NOLINTNEXTLINE TODO can this be done without triggering lints?
157 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
158 ret.assign(raw.begin(), raw.end());
159 wasm_byte_vec_delete(&bytes);
160 return ret;
161 }
162#endif // WASMTIME_FEATURE_COMPILER
163};
164
165} // namespace wasmtime
166
167#endif // WASMTIME_MODULE_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Errors coming from Wasmtime.
Definition: error.hh:26
Representation of a compiled WebAssembly module.
Definition: module.hh:28
Fallible result type used for Wasmtime.
Definition: error.hh:70
Span class used when c++20 is not available.
Definition: span.hh:47
std::size_t size() const
Returns number of data that referred by Span class.
Definition: span.hh:81
iterator end() const
Returns end iterator.
Definition: span.hh:87
T * data() const
Returns pointer to data.
Definition: span.hh:78
iterator begin() const
Returns begin iterator.
Definition: span.hh:84
WASM_API_EXTERN void wasmtime_module_imports(const wasmtime_module_t *module, wasm_importtype_vec_t *out)
Same as wasm_module_imports, but for wasmtime_module_t.
WASM_API_EXTERN wasmtime_error_t * wasmtime_module_serialize(wasmtime_module_t *module, wasm_byte_vec_t *ret)
This function serializes compiled module artifacts as blob data.
WASM_API_EXTERN void wasmtime_module_exports(const wasmtime_module_t *module, wasm_exporttype_vec_t *out)
Same as wasm_module_exports, but for wasmtime_module_t.
WASM_API_EXTERN wasmtime_error_t * wasmtime_module_deserialize(wasm_engine_t *engine, const uint8_t *bytes, size_t bytes_len, wasmtime_module_t **ret)
Build a module from serialized data.
WASM_API_EXTERN wasmtime_error_t * wasmtime_module_validate(wasm_engine_t *engine, const uint8_t *wasm, size_t wasm_len)
Validate a WebAssembly binary.
WASM_API_EXTERN wasmtime_error_t * wasmtime_module_new(wasm_engine_t *engine, const uint8_t *wasm, size_t wasm_len, wasmtime_module_t **ret)
Compiles a WebAssembly binary into a wasmtime_module_t.
WASM_API_EXTERN wasmtime_error_t * wasmtime_module_deserialize_file(wasm_engine_t *engine, const char *path, wasmtime_module_t **ret)
Deserialize a module from an on-disk file.
A list of bytes.
Definition: wasm.h:102
size_t size
Length of this vector.
Definition: wasm.h:102
wasm_byte_t * data
Pointer to the base of this vector.
Definition: wasm.h:102
A compiled Wasmtime module.
void wasm_byte_vec_delete(wasm_byte_vec_t *)
Deletes a byte vector.
Result< std::vector< uint8_t > > wat2wasm(std::string_view wat)
Converts the WebAssembly text format into the WebAssembly binary format.
Definition: wat.hh:30