Wasmtime
instance.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_INSTANCE_HH
6#define WASMTIME_INSTANCE_HH
7
8#include <wasmtime/extern.hh>
9#include <wasmtime/func.hh>
10#include <wasmtime/global.hh>
11#include <wasmtime/instance.h>
12#include <wasmtime/memory.hh>
13#include <wasmtime/module.hh>
14#include <wasmtime/store.hh>
15#include <wasmtime/table.hh>
16
17namespace wasmtime {
18
31class Instance {
32 friend class Linker;
33 friend class Caller;
34
35 wasmtime_instance_t instance;
36
37public:
39 Instance(wasmtime_instance_t instance) : instance(instance) {}
40
58 const std::vector<Extern> &imports) {
59 std::vector<wasmtime_extern_t> raw_imports;
60 for (const auto &item : imports) {
61 raw_imports.push_back(wasmtime_extern_t{});
62 auto &last = raw_imports.back();
63 detail::cvt_extern(item, last);
64 }
65 wasmtime_instance_t instance;
66 wasm_trap_t *trap = nullptr;
67 auto *error = wasmtime_instance_new(cx.ptr, m.ptr.get(), raw_imports.data(),
68 raw_imports.size(), &instance, &trap);
69 if (error != nullptr) {
70 return TrapError(Error(error));
71 }
72 if (trap != nullptr) {
73 return TrapError(Trap(trap));
74 }
75 return Instance(instance);
76 }
77
84 std::optional<Extern> get(Store::Context cx, std::string_view name) {
86 if (!wasmtime_instance_export_get(cx.ptr, &instance, name.data(),
87 name.size(), &e)) {
88 return std::nullopt;
89 }
90 return detail::cvt_extern(e);
91 }
92
99 std::optional<std::pair<std::string_view, Extern>> get(Store::Context cx,
100 size_t idx) {
102 // I'm not sure why clang-tidy thinks this is using va_list or anything
103 // related to that...
104 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
105 char *name = nullptr;
106 size_t len = 0;
107 if (!wasmtime_instance_export_nth(cx.ptr, &instance, idx, &name, &len,
108 &e)) {
109 return std::nullopt;
110 }
111 std::string_view n(name, len);
112 return std::pair(n, detail::cvt_extern(e));
113 }
114};
115
116} // namespace wasmtime
117
118#endif // WASMTIME_INSTANCE_HH
Structure provided to host functions to lookup caller information or acquire a Store::Context.
Definition: func.hh:28
Errors coming from Wasmtime.
Definition: error.hh:25
A WebAssembly instance.
Definition: instance.hh:31
Instance(wasmtime_instance_t instance)
Creates a new instance from the raw underlying C API representation.
Definition: instance.hh:39
std::optional< std::pair< std::string_view, Extern > > get(Store::Context cx, size_t idx)
Load an instance's export by index.
Definition: instance.hh:99
std::optional< Extern > get(Store::Context cx, std::string_view name)
Load an instance's export by name.
Definition: instance.hh:84
static TrapResult< Instance > create(Store::Context cx, const Module &m, const std::vector< Extern > &imports)
Instantiates the module m with the provided imports
Definition: instance.hh:57
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:25
Representation of a compiled WebAssembly module.
Definition: module.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:60
Information about a WebAssembly trap.
Definition: trap.hh:113
bool wasmtime_instance_export_get(wasmtime_context_t *store, const wasmtime_instance_t *instance, const char *name, size_t name_len, wasmtime_extern_t *item)
Get an export by name from an instance.
bool wasmtime_instance_export_nth(wasmtime_context_t *store, const wasmtime_instance_t *instance, size_t index, char **name, size_t *name_len, wasmtime_extern_t *item)
Get an export by index from an instance.
wasmtime_error_t * wasmtime_instance_new(wasmtime_context_t *store, const wasmtime_module_t *module, const wasmtime_extern_t *imports, size_t nimports, wasmtime_instance_t *instance, wasm_trap_t **trap)
Instantiate a wasm module.
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:165
Container for different kinds of extern items.
Definition: extern.h:145
Representation of a instance in Wasmtime.
Definition: instance.h:26