1 /// \file wasmtime/component/func.hh 2 3 #ifndef WASMTIME_COMPONENT_FUNC_HH 4 #define WASMTIME_COMPONENT_FUNC_HH 5 6 #include <wasmtime/conf.h> 7 8 #ifdef WASMTIME_FEATURE_COMPONENT_MODEL 9 10 #include <string_view> 11 #include <wasmtime/component/func.h> 12 #include <wasmtime/component/types/func.hh> 13 #include <wasmtime/component/val.hh> 14 #include <wasmtime/error.hh> 15 #include <wasmtime/span.hh> 16 #include <wasmtime/store.hh> 17 18 namespace wasmtime { 19 namespace component { 20 21 /** 22 * \brief Class representing an instantiated WebAssembly component. 23 */ 24 class Func { 25 wasmtime_component_func_t func; 26 27 public: 28 /// \brief Constructs an Func from the underlying C API struct. Func(const wasmtime_component_func_t & func)29 explicit Func(const wasmtime_component_func_t &func) : func(func) {} 30 31 /// \brief Returns the underlying C API pointer. capi() const32 const wasmtime_component_func_t *capi() const { return &func; } 33 34 /// \brief Invokes this component function with the provided `args` and the 35 /// results are placed in `results`. call(Store::Context cx,Span<const Val> args,Span<Val> results) const36 Result<std::monostate> call(Store::Context cx, Span<const Val> args, 37 Span<Val> results) const { 38 wasmtime_error_t *error = wasmtime_component_func_call( 39 &func, cx.capi(), Val::to_capi(args.data()), args.size(), 40 Val::to_capi(results.data()), results.size()); 41 if (error != nullptr) { 42 return Error(error); 43 } 44 return std::monostate(); 45 } 46 47 /** 48 * \brief Invokes the `post-return` canonical ABI option, if specified. 49 */ post_return(Store::Context cx) const50 Result<std::monostate> post_return(Store::Context cx) const { 51 wasmtime_error_t *error = 52 wasmtime_component_func_post_return(&func, cx.capi()); 53 if (error != nullptr) { 54 return Error(error); 55 } 56 return std::monostate(); 57 } 58 59 /// \brief Returns the type of this function. type(Store::Context cx) const60 FuncType type(Store::Context cx) const { 61 return FuncType(wasmtime_component_func_type(&func, cx.capi())); 62 } 63 }; 64 65 } // namespace component 66 } // namespace wasmtime 67 68 #endif // WASMTIME_FEATURE_COMPONENT_MODEL 69 70 #endif // WASMTIME_COMPONENT_FUNC_H 71