Wasmtime
store.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_STORE_HH
6#define WASMTIME_STORE_HH
7
8#include <any>
9#include <memory>
10#include <optional>
11#include <wasmtime/conf.h>
12#include <wasmtime/engine.hh>
13#include <wasmtime/error.hh>
14#include <wasmtime/store.h>
15#include <wasmtime/wasi.hh>
16
17namespace wasmtime {
18
19class Caller;
20
33class Store {
34 struct deleter {
35 void operator()(wasmtime_store_t *p) const { wasmtime_store_delete(p); }
36 };
37
38 std::unique_ptr<wasmtime_store_t, deleter> ptr;
39
40 static void finalizer(void *ptr) {
41 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
42 }
43
44public:
46 explicit Store(Engine &engine)
47 : ptr(wasmtime_store_new(engine.ptr.get(), nullptr, finalizer)) {}
48
60 class Context {
61 friend class Global;
62 friend class Table;
63 friend class Memory;
64 friend class Func;
65 friend class Instance;
66 friend class Linker;
67 friend class ExternRef;
68 friend class AnyRef;
69 friend class Val;
70 friend class Store;
72
73 Context(wasmtime_context_t *ptr) : ptr(ptr) {}
74
75 public:
77 Context(Store &store) : Context(wasmtime_store_context(store.ptr.get())) {}
79 Context(Store *store) : Context(*store) {}
81 Context(Caller &caller);
83 Context(Caller *caller);
84
87 void gc() { wasmtime_context_gc(ptr); }
88
96 auto *error = wasmtime_context_set_fuel(ptr, fuel);
97 if (error != nullptr) {
98 return Error(error);
99 }
100 return std::monostate();
101 }
102
107 uint64_t fuel = 0;
108 auto *error = wasmtime_context_get_fuel(ptr, &fuel);
109 if (error != nullptr) {
110 return Error(error);
111 }
112 return fuel;
113 }
114
116 void set_data(std::any data) const {
117 finalizer(static_cast<std::any *>(wasmtime_context_get_data(ptr)));
119 ptr, std::make_unique<std::any>(std::move(data)).release());
120 }
121
123 std::any &get_data() const {
124 return *static_cast<std::any *>(wasmtime_context_get_data(ptr));
125 }
126
127#ifdef WASMTIME_FEATURE_WASI
134 auto *error = wasmtime_context_set_wasi(ptr, config.ptr.release());
135 if (error != nullptr) {
136 return Error(error);
137 }
138 return std::monostate();
139 }
140#endif // WASMTIME_FEATURE_WASI
141
148 void set_epoch_deadline(uint64_t ticks_beyond_current) {
149 wasmtime_context_set_epoch_deadline(ptr, ticks_beyond_current);
150 }
151
154 };
155
186 void limiter(int64_t memory_size, int64_t table_elements, int64_t instances,
187 int64_t tables, int64_t memories) {
188 wasmtime_store_limiter(ptr.get(), memory_size, table_elements, instances,
189 tables, memories);
190 }
191
193 Context context() { return this; }
194};
195
196} // namespace wasmtime
197
198#endif // WASMTIME_STORE_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:80
Structure provided to host functions to lookup caller information or acquire a Store::Context.
Definition: func.hh:28
Global compilation state in Wasmtime.
Definition: engine.hh:21
Errors coming from Wasmtime.
Definition: error.hh:25
Representation of a WebAssembly externref value.
Definition: val.hh:28
Representation of a WebAssembly function.
Definition: func.hh:302
A WebAssembly global.
Definition: global.hh:28
A WebAssembly instance.
Definition: instance.hh:31
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:25
A WebAssembly linear memory.
Definition: memory.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:60
void gc()
Definition: store.hh:87
void set_epoch_deadline(uint64_t ticks_beyond_current)
Definition: store.hh:148
wasmtime_context_t * raw_context()
Returns the raw context pointer for the C API.
Definition: store.hh:153
Result< std::monostate > set_wasi(WasiConfig config)
Definition: store.hh:133
Result< std::monostate > set_fuel(uint64_t fuel)
Definition: store.hh:95
Context(Store &store)
Creates a context referencing the provided Store.
Definition: store.hh:77
void set_data(std::any data) const
Set user specified data associated with this store.
Definition: store.hh:116
Result< uint64_t > get_fuel() const
Definition: store.hh:106
std::any & get_data() const
Get user specified data associated with this store.
Definition: store.hh:123
Context(Store *store)
Creates a context referencing the provided Store.
Definition: store.hh:79
Owner of all WebAssembly objects.
Definition: store.hh:33
Store(Engine &engine)
Creates a new Store within the provided Engine.
Definition: store.hh:46
Context context()
Explicit function to acquire a Context from this store.
Definition: store.hh:193
void limiter(int64_t memory_size, int64_t table_elements, int64_t instances, int64_t tables, int64_t memories)
Provides limits for a store. Used by hosts to limit resource consumption of instances....
Definition: store.hh:186
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: val.hh:156
Configuration for an instance of WASI.
Definition: wasi.hh:23
Build-time defines for how the C API was built.
void wasmtime_store_limiter(wasmtime_store_t *store, int64_t memory_size, int64_t table_elements, int64_t instances, int64_t tables, int64_t memories)
Provides limits for a store. Used by hosts to limit resource consumption of instances....
void wasmtime_context_set_data(wasmtime_context_t *context, void *data)
Overwrites the user-specified data associated with this store.
void wasmtime_context_gc(wasmtime_context_t *context)
Perform garbage collection within the given context.
void wasmtime_store_delete(wasmtime_store_t *store)
Deletes a store.
void wasmtime_context_set_epoch_deadline(wasmtime_context_t *context, uint64_t ticks_beyond_current)
Configures the relative deadline at which point WebAssembly code will trap or invoke the callback fun...
wasmtime_context_t * wasmtime_store_context(wasmtime_store_t *store)
Returns the interior wasmtime_context_t pointer to this store.
void * wasmtime_context_get_data(const wasmtime_context_t *context)
Returns the user-specified data associated with the specified store.
wasmtime_store_t * wasmtime_store_new(wasm_engine_t *engine, void *data, void(*finalizer)(void *))
Creates a new store within the specified engine.
wasmtime_error_t * wasmtime_context_set_fuel(wasmtime_context_t *store, uint64_t fuel)
Set fuel to this context's store for wasm to consume while executing.
wasmtime_error_t * wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi)
Configures WASI state within the specified store.
wasmtime_error_t * wasmtime_context_get_fuel(const wasmtime_context_t *context, uint64_t *fuel)
Returns the amount of fuel remaining in this context's store.
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Storage of WebAssembly objects.