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/val.hh>
13 #include <wasmtime/error.hh>
14 #include <wasmtime/span.hh>
15 #include <wasmtime/store.hh>
16 
17 namespace wasmtime {
18 namespace component {
19 
20 /**
21  * \brief Class representing an instantiated WebAssembly component.
22  */
23 class Func {
24   wasmtime_component_func_t func;
25 
26 public:
27   /// \brief Constructs an Func from the underlying C API struct.
28   explicit Func(const wasmtime_component_func_t &func) : func(func) {}
29 
30   /// \brief Returns the underlying C API pointer.
31   const wasmtime_component_func_t *capi() const { return &func; }
32 
33   /// \brief Invokes this component function with the provided `args` and the
34   /// results are placed in `results`.
35   Result<std::monostate> call(Store::Context cx, Span<const Val> args,
36                               Span<Val> results) const {
37     wasmtime_error_t *error = wasmtime_component_func_call(
38         &func, cx.capi(), Val::to_capi(args.data()), args.size(),
39         Val::to_capi(results.data()), results.size());
40     if (error != nullptr) {
41       return Error(error);
42     }
43     return std::monostate();
44   }
45 
46   /**
47    * \brief Invokes the `post-return` canonical ABI option, if specified.
48    */
49   Result<std::monostate> post_return(Store::Context cx) const {
50     wasmtime_error_t *error =
51         wasmtime_component_func_post_return(&func, cx.capi());
52     if (error != nullptr) {
53       return Error(error);
54     }
55     return std::monostate();
56   }
57 };
58 
59 } // namespace component
60 } // namespace wasmtime
61 
62 #endif // WASMTIME_FEATURE_COMPONENT_MODEL
63 
64 #endif // WASMTIME_COMPONENT_FUNC_H
65