* \brief Representation of a WebAssembly function. * * This class represents a WebAssembly function, either created through * instantiating a module or a host function. * * Note that this type does not itself own any resources. It points to resources * owned within a `Store` and the `Store` must be passed in as the first * argument to the functions defined on `Func`. Note that if the wrong
| 106 | * is passed in then the process will be aborted. |
| 107 | */ |
| 108 | class Func { |
| 109 | friend class Val; |
| 110 | friend class Instance; |
| 111 | friend class Linker; |
| 112 | template <typename Params, typename Results> friend class TypedFunc; |
| 113 | |
| 114 | wasmtime_func_t func; |
| 115 | |
| 116 | template <typename F> |
| 117 | static wasm_trap_t *raw_callback(void *env, wasmtime_caller_t *caller, |
| 118 | const wasmtime_val_t *args, size_t nargs, |
| 119 | wasmtime_val_t *results, size_t nresults); |
| 120 | |
| 121 | template <typename F> |
| 122 | static wasm_trap_t * |
| 123 | raw_callback_unchecked(void *env, wasmtime_caller_t *caller, |
| 124 | wasmtime_val_raw_t *args_and_results, |
| 125 | size_t nargs_and_results) { |
| 126 | (void)nargs_and_results; |
| 127 | using HostFunc = WasmHostFunc<F>; |
| 128 | Caller cx(caller); |
| 129 | F *func = reinterpret_cast<F *>(env); // NOLINT |
| 130 | auto trap = HostFunc::invoke(*func, cx, args_and_results); |
| 131 | if (trap) { |
| 132 | return trap->capi_release(); |
| 133 | } |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | template <typename F> static void raw_finalize(void *env) { |
| 138 | std::unique_ptr<F> ptr(reinterpret_cast<F *>(env)); // NOLINT |
| 139 | } |
| 140 | |
| 141 | public: |
| 142 | /// Creates a new function from the raw underlying C API representation. |
| 143 | Func(wasmtime_func_t func) : func(func) {} |
| 144 | |
| 145 | /** |
| 146 | * \brief Creates a new host-defined function. |
| 147 | * |
| 148 | * This constructor is used to create a host function within the store |
| 149 | * provided. This is how WebAssembly can call into the host and make use of |
| 150 | * external functionality. |
| 151 | * |
| 152 | * > **Note**: host functions created this way are more flexible but not |
| 153 | * > as fast to call as those created by `Func::wrap`. |
| 154 | * |
| 155 | * \param cx the store to create the function within |
| 156 | * \param ty the type of the function that will be created |
| 157 | * \param f the host callback to be executed when this function is called. |
| 158 | * |
| 159 | * The parameter `f` is expected to be a lambda (or a lambda lookalike) which |
| 160 | * takes three parameters: |
| 161 | * |
| 162 | * * The first parameter is a `Caller` to get recursive access to the store |
| 163 | * and other caller state. |
| 164 | * * The second parameter is a `Span<const Val>` which is the list of |
| 165 | * parameters to the function. These parameters are guaranteed to be of the |
no outgoing calls
no test coverage detected