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