5#ifndef WASMTIME_FUNC_HH
6#define WASMTIME_FUNC_HH
10#include <wasmtime/extern_declare.hh>
39 std::optional<Extern>
get_export(std::string_view name);
45inline Store::Context::Context(
Caller &caller)
53template <
typename T>
struct WasmType {
54 static const bool valid =
false;
60#define NATIVE_WASM_TYPE(native, valkind, field) \
61 template <> struct WasmType<native> { \
62 static const bool valid = true; \
63 static const ValKind kind = ValKind::valkind; \
64 static void store(Store::Context cx, wasmtime_val_raw_t *p, \
68 static native load(Store::Context cx, wasmtime_val_raw_t *p) { \
80#undef NATIVE_WASM_TYPE
84template <>
struct WasmType<std::optional<ExternRef>> {
85 static const bool valid =
true;
86 static const ValKind kind = ValKind::ExternRef;
88 const std::optional<ExternRef> &ref) {
95 static std::optional<ExternRef> load(Store::Context cx,
102 return ExternRef(val);
107template <>
struct WasmType<
V128> {
108 static const bool valid =
true;
109 static const ValKind kind = ValKind::V128;
120template <
typename T>
struct WasmTypeList {
121 static const bool valid = WasmType<T>::valid;
122 static const size_t size = 1;
123 static bool matches(ValType::ListRef types) {
124 return WasmTypeList<std::tuple<T>>::matches(types);
128 WasmType<T>::store(cx, storage, t);
131 return WasmType<T>::load(cx, storage);
133 static std::vector<ValType> types() {
return {WasmType<T>::kind}; }
137template <>
struct WasmTypeList<std::monostate> {
138 static const bool valid =
true;
139 static const size_t size = 0;
140 static bool matches(ValType::ListRef types) {
return types.size() == 0; }
142 const std::monostate &t) {}
144 return std::monostate();
146 static std::vector<ValType> types() {
return {}; }
150template <
typename... T>
struct WasmTypeList<std::tuple<T...>> {
151 static const bool valid = (WasmType<T>::valid && ...);
152 static const size_t size =
sizeof...(T);
153 static bool matches(ValType::ListRef types) {
154 if (types.size() != size) {
158 return ((WasmType<T>::kind == types.begin()[n++].kind()) && ...);
161 const std::tuple<T...> &t) {
164 [&](
const auto &...val) {
165 (WasmType<T>::store(cx, &storage[n++], val), ...);
171 return std::tuple<T...>{WasmType<T>::load(cx, &storage[n++])...};
173 static std::vector<ValType> types() {
return {WasmType<T>::kind...}; }
179template <
typename R>
struct WasmHostRet {
180 using Results = WasmTypeList<R>;
182 template <
typename F,
typename... A>
185 auto ret = f(args...);
186 Results::store(cx, raw, ret);
192template <>
struct WasmHostRet<void> {
193 using Results = WasmTypeList<std::tuple<>>;
195 template <
typename F,
typename... A>
205template <>
struct WasmHostRet<std::monostate> :
public WasmHostRet<void> {};
209template <
typename R>
struct WasmHostRet<Result<R, Trap>> {
210 using Results = WasmTypeList<R>;
212 template <
typename F,
typename... A>
215 Result<R, Trap> ret = f(args...);
219 Results::store(cx, raw, ret.ok());
224template <
typename F,
typename =
void>
struct WasmHostFunc;
228template <
typename R,
typename... A>
struct WasmHostFunc<R (*)(A...)> {
229 using Params = WasmTypeList<std::tuple<A...>>;
230 using Results =
typename WasmHostRet<R>::Results;
232 template <
typename F>
234 auto params = Params::load(cx, raw);
236 [&](
const auto &...val) {
237 return WasmHostRet<R>::invoke(f, cx, raw, val...);
244template <
typename R,
typename... A>
245struct WasmHostFunc<R (*)(Caller, A...)> :
public WasmHostFunc<R (*)(A...)> {
247 template <
typename F>
249 auto params = WasmTypeList<std::tuple<A...>>::load(cx, raw);
251 [&](
const auto &...val) {
252 return WasmHostRet<R>::invoke(f, cx, raw, cx, val...);
259template <
typename R,
typename C,
typename... A>
260struct WasmHostFunc<R (C::*)(A...)> :
public WasmHostFunc<R (*)(A...)> {};
263template <
typename R,
typename C,
typename... A>
264struct WasmHostFunc<R (C::*)(A...) const> :
public WasmHostFunc<R (*)(A...)> {};
268template <
typename R,
typename C,
typename... A>
269struct WasmHostFunc<R (C::*)(Caller, A...)>
270 :
public WasmHostFunc<R (*)(Caller, A...)> {};
274template <
typename R,
typename C,
typename... A>
275struct WasmHostFunc<R (C::*)(Caller, A...) const>
276 :
public WasmHostFunc<R (*)(Caller, A...)> {};
281struct WasmHostFunc<T, std::void_t<decltype(&T::operator())>>
282 :
public WasmHostFunc<decltype(&T::operator())> {};
286using namespace detail;
289template <
typename Params,
typename Results>
class TypedFunc;
306 template <
typename Params,
typename Results>
friend class TypedFunc;
310 template <
typename F>
316 F *func =
reinterpret_cast<F *
>(env);
319 Span<Val> results_span(
reinterpret_cast<Val *
>(results),
322 (*func)(
Caller(caller), args_span, results_span);
324 return result.
err().ptr.release();
329 template <
typename F>
333 size_t nargs_and_results) {
334 using HostFunc = WasmHostFunc<F>;
336 F *func =
reinterpret_cast<F *
>(env);
337 auto trap = HostFunc::invoke(*func, cx, args_and_results);
339 return trap->ptr.release();
344 template <
typename F>
static void raw_finalize(
void *env) {
345 std::unique_ptr<F> ptr(
reinterpret_cast<F *
>(env));
383 template <
typename F,
385 std::is_invocable_r_v<Result<std::monostate, Trap>, F,
Caller,
390 std::make_unique<F>(f).release(), raw_finalize<F>, &func);
431 template <
typename F,
432 std::enable_if_t<WasmHostFunc<F>::Params::valid,
bool> =
true,
433 std::enable_if_t<WasmHostFunc<F>::Results::valid,
bool> =
true>
435 using HostFunc = WasmHostFunc<F>;
436 auto params = HostFunc::Params::types();
437 auto results = HostFunc::Results::types();
441 std::make_unique<F>(f).release(),
442 raw_finalize<F>, &func);
466 template <
typename I>
468 const I &end)
const {
469 std::vector<wasmtime_val_t> raw_params;
470 raw_params.reserve(end - begin);
471 for (
auto i = begin; i != end; i++) {
472 raw_params.push_back(i->val);
474 size_t nresults = this->type(cx)->results().size();
475 std::vector<wasmtime_val_t> raw_results(nresults);
480 raw_results.data(), raw_results.capacity(), &trap);
481 if (error !=
nullptr) {
484 if (trap !=
nullptr) {
488 std::vector<Val> results;
489 results.reserve(nresults);
490 for (
size_t i = 0; i < nresults; i++) {
491 results.push_back(raw_results[i]);
503 const std::vector<Val> ¶ms)
const {
504 return this->call(cx, params.begin(), params.end());
515 return this->call(cx, params.begin(), params.end());
540 template <
typename Params,
typename Results,
541 std::enable_if_t<WasmTypeList<Params>::valid,
bool> =
true,
542 std::enable_if_t<WasmTypeList<Results>::valid,
bool> =
true>
544 auto ty = this->type(cx);
545 if (!WasmTypeList<Params>::matches(ty->params()) ||
546 !WasmTypeList<Results>::matches(ty->results())) {
547 return Trap(
"static type for this function does not match actual type");
561template <
typename Params,
typename Results>
class TypedFunc {
579 WasmTypeList<Results>::size)>
584 WasmTypeList<Params>::store(cx, ptr, params);
587 storage.size(), &trap);
588 if (error !=
nullptr) {
591 if (trap !=
nullptr) {
594 return WasmTypeList<Results>::load(cx, ptr);
601inline Val::Val(std::optional<Func> func) : val{} {
606 wasmtime_funcref_set_null(&val.
of.
funcref);
610inline Val::Val(
Func func) :
Val(std::optional(func)) {}
625template <>
struct detail::WasmType<std::optional<Func>> {
627 static const bool valid =
true;
629 static const ValKind kind = ValKind::FuncRef;
632 const std::optional<Func> func) {
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
std::optional< Extern > get_export(std::string_view name)
Definition: extern.hh:53
Store::Context context()
Explicitly acquire a Store::Context from this Caller.
Definition: func.hh:42
Errors coming from Wasmtime.
Definition: error.hh:25
Representation of a WebAssembly externref value.
Definition: val.hh:28
Type information for a WebAssembly function.
Definition: types/func.hh:15
static FuncType from_iters(P params, R results)
Creates a new function type from the given list of parameters and results.
Definition: types/func.hh:75
Representation of a WebAssembly function.
Definition: func.hh:302
TrapResult< std::vector< Val > > call(Store::Context cx, const I &begin, const I &end) const
Invoke a WebAssembly function.
Definition: func.hh:467
Func(wasmtime_func_t func)
Creates a new function from the raw underlying C API representation.
Definition: func.hh:350
FuncType type(Store::Context cx) const
Returns the type of this function.
Definition: func.hh:519
TrapResult< std::vector< Val > > call(Store::Context cx, const std::vector< Val > ¶ms) const
Helper function for call(Store::Context cx, const I &begin, const I &end)
Definition: func.hh:502
static Func wrap(Store::Context cx, F f)
Creates a new host function from the provided callback f, inferring the WebAssembly function type fro...
Definition: func.hh:434
TrapResult< std::vector< Val > > call(Store::Context cx, const std::initializer_list< Val > ¶ms) const
Helper function for call(Store::Context cx, const I &begin, const I &end)
Definition: func.hh:514
const wasmtime_func_t & capi() const
Returns the raw underlying C API function this is using.
Definition: func.hh:554
Func(Store::Context cx, const FuncType &ty, F f)
Creates a new host-defined function.
Definition: func.hh:388
Result< TypedFunc< Params, Results >, Trap > typed(Store::Context cx) const
Statically checks this function against the provided types.
Definition: func.hh:543
A WebAssembly instance.
Definition: instance.hh:31
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:25
Fallible result type used for Wasmtime.
Definition: error.hh:82
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:96
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: store.hh:60
wasmtime_context_t * raw_context()
Returns the raw context pointer for the C API.
Definition: store.hh:153
Owner of all WebAssembly objects.
Definition: store.hh:33
Information about a WebAssembly trap.
Definition: trap.hh:113
A version of a WebAssembly Func where the type signature of the function is statically known.
Definition: func.hh:561
const Func & func() const
Returns the underlying un-typed Func for this function.
Definition: func.hh:598
TrapResult< Results > call(Store::Context cx, Params params) const
Calls this function with the provided parameters.
Definition: func.hh:577
Representation of a generic WebAssembly value.
Definition: val.hh:156
std::optional< Func > funcref() const
Definition: func.hh:614
void wasmtime_func_from_raw(wasmtime_context_t *context, void *raw, wasmtime_func_t *ret)
Converts a raw nonzero funcref value from wasmtime_val_raw_t into a wasmtime_func_t.
wasmtime_error_t * wasmtime_func_call(wasmtime_context_t *store, const wasmtime_func_t *func, const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, size_t nresults, wasm_trap_t **trap)
Call a WebAssembly function.
void * wasmtime_func_to_raw(wasmtime_context_t *context, const wasmtime_func_t *func)
Converts a func which belongs to context into a usize parameter that is suitable for insertion into a...
wasmtime_error_t * wasmtime_func_call_unchecked(wasmtime_context_t *store, const wasmtime_func_t *func, wasmtime_val_raw_t *args_and_results, size_t args_and_results_len, wasm_trap_t **trap)
Call a WebAssembly function in an "unchecked" fashion.
void wasmtime_func_new(wasmtime_context_t *store, const wasm_functype_t *type, wasmtime_func_callback_t callback, void *env, void(*finalizer)(void *), wasmtime_func_t *ret)
Creates a new host-defined function.
wasmtime_context_t * wasmtime_caller_context(wasmtime_caller_t *caller)
Returns the store context of the caller object.
void wasmtime_func_new_unchecked(wasmtime_context_t *store, const wasm_functype_t *type, wasmtime_func_unchecked_callback_t callback, void *env, void(*finalizer)(void *), wasmtime_func_t *ret)
Creates a new host function in the same manner of wasmtime_func_new, but the function-to-call has no ...
wasm_functype_t * wasmtime_func_type(const wasmtime_context_t *store, const wasmtime_func_t *func)
Returns the type of the function specified.
#define NATIVE_WASM_TYPE(native, valkind, field)
Definition: func.hh:60
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:165
A host-defined un-forgeable reference to pass into WebAssembly.
Definition: val.h:174
Representation of a function in Wasmtime.
Definition: extern.h:25
uint64_t store_id
Definition: extern.h:31
Container for different kinds of wasm values.
Definition: val.h:441
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:443
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:445
ValKind
Different kinds of types accepted by Wasmtime.
Definition: types/val.hh:16
@ V128
WebAssembly's v128 type from the simd proposal.
Container for possible wasm values.
Definition: val.h:372
uint32_t externref
Definition: val.h:408
wasmtime_v128 v128
Definition: val.h:392
void * funcref
Definition: val.h:415
wasmtime_func_t funcref
Definition: val.h:336
uint32_t wasmtime_externref_to_raw(wasmtime_context_t *context, const wasmtime_externref_t *ref)
Converts a wasmtime_externref_t to a raw value suitable for storing into a wasmtime_val_raw_t.
union wasmtime_val_raw wasmtime_val_raw_t
Convenience alias for wasmtime_val_raw.
uint8_t wasmtime_v128[16]
A 128-bit value representing the WebAssembly v128 type. Bytes are stored in little-endian order.
Definition: val.h:307
void wasmtime_externref_from_raw(wasmtime_context_t *context, uint32_t raw, wasmtime_externref_t *out)
Converts a raw externref value coming from wasmtime_val_raw_t into a wasmtime_externref_t.
#define WASMTIME_FUNCREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a funcref.
Definition: val.h:297