Wasmtime
global.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_GLOBAL_HH
6#define WASMTIME_GLOBAL_HH
7
8#include <wasmtime/error.hh>
9#include <wasmtime/global.h>
10#include <wasmtime/store.hh>
12#include <wasmtime/val.hh>
13
14namespace wasmtime {
15
28class Global {
29 friend class Instance;
30 wasmtime_global_t global;
31
32public:
34 Global(wasmtime_global_t global) : global(global) {}
35
46 const Val &init) {
47 wasmtime_global_t global;
48 auto *error = wasmtime_global_new(cx.ptr, ty.ptr.get(), &init.val, &global);
49 if (error != nullptr) {
50 return Error(error);
51 }
52 return Global(global);
53 }
54
57 return wasmtime_global_type(cx.ptr, &global);
58 }
59
62 Val val;
63 wasmtime_global_get(cx.ptr, &global, &val.val);
64 return std::move(val);
65 }
66
71 auto *error = wasmtime_global_set(cx.ptr, &global, &val.val);
72 if (error != nullptr) {
73 return Error(error);
74 }
75 return std::monostate();
76 }
77
79 const wasmtime_global_t &capi() const { return global; }
80};
81
82} // namespace wasmtime
83
84#endif // WASMTIME_GLOBAL_HH
Errors coming from Wasmtime.
Definition: error.hh:25
Type information about a WebAssembly global.
Definition: types/global.hh:15
A WebAssembly global.
Definition: global.hh:28
Global(wasmtime_global_t global)
Creates as global from the raw underlying C API representation.
Definition: global.hh:34
const wasmtime_global_t & capi() const
Returns the raw underlying C API global this is using.
Definition: global.hh:79
Result< std::monostate > set(Store::Context cx, const Val &val) const
Definition: global.hh:70
Val get(Store::Context cx) const
Returns the current value of this global.
Definition: global.hh:61
GlobalType type(Store::Context cx) const
Returns the type of this global.
Definition: global.hh:56
static Result< Global > create(Store::Context cx, const GlobalType &ty, const Val &init)
Create a new WebAssembly global.
Definition: global.hh:45
A WebAssembly instance.
Definition: instance.hh:31
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:60
Representation of a generic WebAssembly value.
Definition: val.hh:156
wasmtime_error_t * wasmtime_global_new(wasmtime_context_t *store, const wasm_globaltype_t *type, const wasmtime_val_t *val, wasmtime_global_t *ret)
Creates a new global value.
wasmtime_error_t * wasmtime_global_set(wasmtime_context_t *store, const wasmtime_global_t *global, const wasmtime_val_t *val)
Sets a global to a new value.
wasm_globaltype_t * wasmtime_global_type(const wasmtime_context_t *store, const wasmtime_global_t *global)
Returns the wasm type of the specified global.
void wasmtime_global_get(wasmtime_context_t *store, const wasmtime_global_t *global, wasmtime_val_t *out)
Get the value of the specified global.
Representation of a global in Wasmtime.
Definition: extern.h:71