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
32
33#ifdef WASMTIME_FEATURE_WAT
40 static Result<Module> compile(Engine &engine, std::string_view wat) {
41 auto wasm = wat2wasm(wat);
42 if (!wasm) {
43 return wasm.err();
44 }
45 auto bytes = wasm.ok();
46 return compile(engine, bytes);
47 }
48#endif // WASMTIME_FEATURE_WAT
49
60 static Result<Module> compile(Engine &engine, Span<uint8_t> wasm) {
61 wasmtime_module_t *ret = nullptr;
62 auto *error =
63 wasmtime_module_new(engine.capi(), wasm.data(), wasm.size(), &ret);
64 if (error != nullptr) {
65 return Error(error);
66 }
67 return Module(ret);
68 }
69
76 static Result<std::monostate> validate(Engine &engine, Span<uint8_t> wasm) {
77 auto *error =
78 wasmtime_module_validate(engine.capi(), wasm.data(), wasm.size());
79 if (error != nullptr) {
80 return Error(error);
81 }
82 return std::monostate();
83 }
84#endif // WASMTIME_FEATURE_COMPILER
85
98 static Result<Module> deserialize(Engine &engine, Span<uint8_t> wasm) {
99 wasmtime_module_t *ret = nullptr;
100 auto *error = wasmtime_module_deserialize(engine.capi(), wasm.data(),
101 wasm.size(), &ret);
102 if (error != nullptr) {
103 return Error(error);
104 }
105 return Module(ret);
106 }
107
120 static Result<Module> deserialize_file(Engine &engine,
121 const std::string &path) {
122 wasmtime_module_t *ret = nullptr;
123 auto *error =
124 wasmtime_module_deserialize_file(engine.capi(), path.c_str(), &ret);
125 if (error != nullptr) {
126 return Error(error);
127 }
128 return Module(ret);
129 }
130
132 ImportType::List imports() const {
133 ImportType::List list;
134 wasmtime_module_imports(ptr.get(), &list.list);
135 return list;
136 }
137
139 ExportType::List exports() const {
140 ExportType::List list;
141 wasmtime_module_exports(ptr.get(), &list.list);
142 return list;
143 }
144
145#ifdef WASMTIME_FEATURE_COMPILER
152 Result<std::vector<uint8_t>> serialize() const {
153 wasm_byte_vec_t bytes;
154 auto *error = wasmtime_module_serialize(ptr.get(), &bytes);
155 if (error != nullptr) {
156 return Error(error);
157 }
158 std::vector<uint8_t> ret;
159 // NOLINTNEXTLINE TODO can this be done without triggering lints?
160 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
161 ret.assign(raw.begin(), raw.end());
162 wasm_byte_vec_delete(&bytes);
163 return ret;
164 }
165#endif // WASMTIME_FEATURE_COMPILER
166};
167
168} // namespace wasmtime
169
170#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