| 30 | class ModuleInstance; |
| 31 | |
| 32 | class FunctionInstance : public CompositeBase { |
| 33 | public: |
| 34 | using CompiledFunction = void; |
| 35 | |
| 36 | FunctionInstance() = delete; |
| 37 | /// Move constructor. |
| 38 | FunctionInstance(FunctionInstance &&Inst) noexcept |
| 39 | : CompositeBase(Inst.ModInst, Inst.TypeIdx), FuncType(Inst.FuncType), |
| 40 | Data(std::move(Inst.Data)) { |
| 41 | assuming(ModInst); |
| 42 | } |
| 43 | /// Constructor for native function. |
| 44 | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
| 45 | const AST::FunctionType &Type, |
| 46 | Span<const std::pair<uint32_t, ValType>> Locs, |
| 47 | AST::InstrView Expr) noexcept |
| 48 | : CompositeBase(Mod, TIdx), FuncType(Type), |
| 49 | Data(std::in_place_type_t<WasmFunction>(), Locs, Expr) { |
| 50 | assuming(ModInst); |
| 51 | } |
| 52 | /// Constructor for compiled function. |
| 53 | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
| 54 | const AST::FunctionType &Type, |
| 55 | Symbol<CompiledFunction> S) noexcept |
| 56 | : CompositeBase(Mod, TIdx), FuncType(Type), |
| 57 | Data(std::in_place_type_t<Symbol<CompiledFunction>>(), std::move(S)) { |
| 58 | assuming(ModInst); |
| 59 | } |
| 60 | /// Constructors for host function. |
| 61 | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
| 62 | std::unique_ptr<HostFunctionBase> &&Func) noexcept |
| 63 | : CompositeBase(Mod, TIdx), FuncType(Func->getFuncType()), |
| 64 | Data(std::in_place_type_t<std::unique_ptr<HostFunctionBase>>(), |
| 65 | std::move(Func)) { |
| 66 | assuming(ModInst); |
| 67 | } |
| 68 | FunctionInstance(std::unique_ptr<HostFunctionBase> &&Func) noexcept |
| 69 | : CompositeBase(), FuncType(Func->getFuncType()), |
| 70 | Data(std::in_place_type_t<std::unique_ptr<HostFunctionBase>>(), |
| 71 | std::move(Func)) {} |
| 72 | |
| 73 | /// Check whether this is a native wasm function. |
| 74 | bool isWasmFunction() const noexcept { |
| 75 | return std::holds_alternative<WasmFunction>(Data); |
| 76 | } |
| 77 | |
| 78 | /// Check whether this is a compiled function. |
| 79 | bool isCompiledFunction() const noexcept { |
| 80 | return std::holds_alternative<Symbol<CompiledFunction>>(Data); |
| 81 | } |
| 82 | |
| 83 | /// Check whether this is a host function. |
| 84 | bool isHostFunction() const noexcept { |
| 85 | return std::holds_alternative<std::unique_ptr<HostFunctionBase>>(Data); |
| 86 | } |
| 87 | |
| 88 | /// Getter for function type. |
| 89 | const AST::FunctionType &getFuncType() const noexcept { return FuncType; } |