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#include <wasmtime/tag.hh>
17
18namespace wasmtime {
19
32class Instance {
33 friend class Linker;
34 friend class Caller;
35
36 wasmtime_instance_t instance;
37
38public:
40 Instance(wasmtime_instance_t instance) : instance(instance) {}
41
59 const std::vector<Extern> &imports) {
60 std::vector<wasmtime_extern_t> raw_imports;
61 for (const auto &item : imports) {
62 raw_imports.push_back(wasmtime_extern_t{});
63 auto &last = raw_imports.back();
64 detail::cvt_extern(item, last);
65 }
66 wasmtime_instance_t instance;
67 wasm_trap_t *trap = nullptr;
68 auto *error = wasmtime_instance_new(cx.ptr, m.capi(), raw_imports.data(),
69 raw_imports.size(), &instance, &trap);
70 if (error != nullptr) {
71 return TrapError(Error(error));
72 }
73 if (trap != nullptr) {
74 return TrapError(Trap(trap));
75 }
76 return Instance(instance);
77 }
78
85 std::optional<Extern> get(Store::Context cx, std::string_view name) {
87 if (!wasmtime_instance_export_get(cx.ptr, &instance, name.data(),
88 name.size(), &e)) {
89 return std::nullopt;
90 }
91 return detail::cvt_extern(e);
92 }
93
100 std::optional<std::pair<std::string_view, Extern>> get(Store::Context cx,
101 size_t idx) {
103 // I'm not sure why clang-tidy thinks this is using va_list or anything
104 // related to that...
105 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
106 char *name = nullptr;
107 size_t len = 0;
108 if (!wasmtime_instance_export_nth(cx.ptr, &instance, idx, &name, &len,
109 &e)) {
110 return std::nullopt;
111 }
112 std::string_view n(name, len);
113 return std::pair(n, detail::cvt_extern(e));
114 }
115};
116
117} // namespace wasmtime
118
119#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:26
A WebAssembly instance.
Definition: instance.hh:32
Instance(wasmtime_instance_t instance)
Creates a new instance from the raw underlying C API representation.
Definition: instance.hh:40
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:100
std::optional< Extern > get(Store::Context cx, std::string_view name)
Load an instance's export by name.
Definition: instance.hh:85
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:58
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:26
Representation of a compiled WebAssembly module.
Definition: module.hh:28
Fallible result type used for Wasmtime.
Definition: error.hh:70
An interior pointer into a Store.
Definition: store.hh:69
Information about a WebAssembly trap.
Definition: trap.hh:113
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:153
Container for different kinds of extern items.
Definition: extern.h:151
Representation of a instance in Wasmtime.
Definition: instance.h:26