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/module.h>
12#include <wasmtime/span.hh>
15#include <wasmtime/wat.hh>
16
17namespace wasmtime {
18
27class Module {
28 friend class Store;
29 friend class Instance;
30 friend class Linker;
31
32 struct deleter {
33 void operator()(wasmtime_module_t *p) const { wasmtime_module_delete(p); }
34 };
35
36 std::unique_ptr<wasmtime_module_t, deleter> ptr;
37
38 Module(wasmtime_module_t *raw) : ptr(raw) {}
39
40public:
42 Module(const Module &other) : ptr(wasmtime_module_clone(other.ptr.get())) {}
44 Module &operator=(const Module &other) {
45 ptr.reset(wasmtime_module_clone(other.ptr.get()));
46 return *this;
47 }
48 ~Module() = default;
50 Module(Module &&other) = default;
52 Module &operator=(Module &&other) = default;
53
54#ifdef WASMTIME_FEATURE_COMPILER
61 static Result<Module> compile(Engine &engine, std::string_view wat) {
62 auto wasm = wat2wasm(wat);
63 if (!wasm) {
64 return wasm.err();
65 }
66 auto bytes = wasm.ok();
67 return compile(engine, bytes);
68 }
69
81 wasmtime_module_t *ret = nullptr;
82 auto *error =
83 wasmtime_module_new(engine.ptr.get(), wasm.data(), wasm.size(), &ret);
84 if (error != nullptr) {
85 return Error(error);
86 }
87 return Module(ret);
88 }
89
97 auto *error =
98 wasmtime_module_validate(engine.ptr.get(), wasm.data(), wasm.size());
99 if (error != nullptr) {
100 return Error(error);
101 }
102 return std::monostate();
103 }
104#endif // WASMTIME_FEATURE_COMPILER
105
119 wasmtime_module_t *ret = nullptr;
120 auto *error = wasmtime_module_deserialize(engine.ptr.get(), wasm.data(),
121 wasm.size(), &ret);
122 if (error != nullptr) {
123 return Error(error);
124 }
125 return Module(ret);
126 }
127
141 const std::string &path) {
142 wasmtime_module_t *ret = nullptr;
143 auto *error =
144 wasmtime_module_deserialize_file(engine.ptr.get(), path.c_str(), &ret);
145 if (error != nullptr) {
146 return Error(error);
147 }
148 return Module(ret);
149 }
150
153 ImportType::List list;
154 wasmtime_module_imports(ptr.get(), &list.list);
155 return list;
156 }
157
160 ExportType::List list;
161 wasmtime_module_exports(ptr.get(), &list.list);
162 return list;
163 }
164
165#ifdef WASMTIME_FEATURE_COMPILER
173 wasm_byte_vec_t bytes;
174 auto *error = wasmtime_module_serialize(ptr.get(), &bytes);
175 if (error != nullptr) {
176 return Error(error);
177 }
178 std::vector<uint8_t> ret;
179 // NOLINTNEXTLINE TODO can this be done without triggering lints?
180 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
181 ret.assign(raw.begin(), raw.end());
182 wasm_byte_vec_delete(&bytes);
183 return ret;
184 }
185#endif // WASMTIME_FEATURE_COMPILER
186};
187
188} // namespace wasmtime
189
190#endif // WASMTIME_MODULE_HH
191
Global compilation state in Wasmtime.
Definition: engine.hh:21
Errors coming from Wasmtime.
Definition: error.hh:24
An owned list of ExportType instances.
Definition: export.hh:41
An owned list of ImportType instances.
Definition: import.hh:46
A WebAssembly instance.
Definition: wasmtime.hh:929
Helper class for linking modules together with name-based resolution.
Definition: wasmtime.hh:1061
Representation of a compiled WebAssembly module.
Definition: module.hh:27
Result< std::vector< uint8_t > > serialize() const
Serializes this module to a list of bytes.
Definition: module.hh:172
Module & operator=(Module &&other)=default
Moves resources from another module into this one.
ImportType::List imports() const
Returns the list of types imported by this module.
Definition: module.hh:152
Module(Module &&other)=default
Moves resources from another module into this one.
static Result< Module > deserialize_file(Engine &engine, const std::string &path)
Deserializes a module from an on-disk file.
Definition: module.hh:140
static Result< Module > compile(Engine &engine, std::string_view wat)
Compiles a module from the WebAssembly text format.
Definition: module.hh:61
ExportType::List exports() const
Returns the list of types exported by this module.
Definition: module.hh:159
static Result< Module > compile(Engine &engine, Span< uint8_t > wasm)
Compiles a module from the WebAssembly binary format.
Definition: module.hh:80
static Result< std::monostate > validate(Engine &engine, Span< uint8_t > wasm)
Validates the provided WebAssembly binary without compiling it.
Definition: module.hh:96
static Result< Module > deserialize(Engine &engine, Span< uint8_t > wasm)
Deserializes a previous list of bytes created with serialize.
Definition: module.hh:118
Module(const Module &other)
Copies another module into this one.
Definition: module.hh:42
Module & operator=(const Module &other)
Copies another module into this one.
Definition: module.hh:44
Fallible result type used for Wasmtime.
Definition: error.hh:83
Span class used when c++20 is not available.
Definition: span.hh:45
std::size_t size() const
Returns number of data that referred by Span class.
Definition: span.hh:79
iterator end() const
Returns end iterator.
Definition: span.hh:85
T * data() const
Returns pointer to data.
Definition: span.hh:76
iterator begin() const
Returns begin iterator.
Definition: span.hh:82
Owner of all WebAssembly objects.
Definition: store.hh:33
wasmtime_error_t * wasmtime_module_serialize(wasmtime_module_t *module, wasm_byte_vec_t *ret)
This function serializes compiled module artifacts as blob data.
void wasmtime_module_imports(const wasmtime_module_t *module, wasm_importtype_vec_t *out)
Same as wasm_module_imports, but for wasmtime_module_t.
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.
wasmtime_module_t * wasmtime_module_clone(wasmtime_module_t *m)
Creates a shallow clone of the specified module, increasing the internal reference count.
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.
wasmtime_error_t * wasmtime_module_validate(wasm_engine_t *engine, const uint8_t *wasm, size_t wasm_len)
Validate a WebAssembly binary.
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.
void wasmtime_module_exports(const wasmtime_module_t *module, wasm_exporttype_vec_t *out)
Same as wasm_module_exports, but for wasmtime_module_t.
void wasmtime_module_delete(wasmtime_module_t *m)
Deletes a module.
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