#ifndef WASMTIME_TYPES_FUNC_HH
#define WASMTIME_TYPES_FUNC_HH
#include <wasmtime/types/val.hh>
namespace wasmtime {
class FuncType {
friend class Func;
friend class Linker;
friend class TagType;
struct deleter {
void operator()(wasm_functype_t *p) const { wasm_functype_delete(p); }
};
std::unique_ptr<wasm_functype_t, deleter> ptr;
public:
class Ref {
friend class FuncType;
const wasm_functype_t *ptr;
public:
Ref(const wasm_functype_t *ptr) : ptr(ptr) {}
Ref(const FuncType &ty) : Ref(ty.ptr.get()) {}
ValType::ListRef params() const { return wasm_functype_params(ptr); }
ValType::ListRef results() const { return wasm_functype_results(ptr); }
};
private:
Ref ref;
FuncType(wasm_functype_t *ptr) : ptr(ptr), ref(ptr) {}
public:
FuncType(std::initializer_list<ValType> params,
std::initializer_list<ValType> results)
: ref(nullptr) {
*this = FuncType::from_iters(params, results);
}
FuncType(Ref other) : FuncType(wasm_functype_copy(other.ptr)) {}
FuncType(const FuncType &other)
: FuncType(wasm_functype_copy(other.ptr.get())) {}
FuncType &operator=(const FuncType &other) {
ptr.reset(wasm_functype_copy(other.ptr.get()));
return *this;
}
~FuncType() = default;
FuncType(FuncType &&other) = default;
FuncType &operator=(FuncType &&other) = default;
template <typename P, typename R>
static FuncType from_iters(P params, R results) {
wasm_valtype_vec_t param_vec;
wasm_valtype_vec_t result_vec;
wasm_valtype_vec_new_uninitialized(¶m_vec, params.size());
wasm_valtype_vec_new_uninitialized(&result_vec, results.size());
size_t i = 0;
for (auto val : params) {
param_vec.data[i++] = val.ptr.release(); }
i = 0;
for (auto val : results) {
result_vec.data[i++] = val.ptr.release(); }
return wasm_functype_new(¶m_vec, &result_vec);
}
Ref *operator->() { return &ref; }
Ref *operator*() { return &ref; }
};
};
#endif