/src/WasmEdge/include/runtime/instance/component/function.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | #pragma once |
4 | | |
5 | | #include "ast/component/type.h" |
6 | | #include "common/types.h" |
7 | | #include "runtime/instance/function.h" |
8 | | #include "runtime/instance/memory.h" |
9 | | |
10 | | #include <memory> |
11 | | |
12 | | namespace WasmEdge { |
13 | | namespace Runtime { |
14 | | namespace Instance { |
15 | | |
16 | | class ComponentInstance; // forward decl for parent component pointer |
17 | | |
18 | | namespace Component { |
19 | | |
20 | | class FunctionInstance { |
21 | | // The component function instance currently can only be instantiated by the |
22 | | // `canon lift` operation. For the component host functions, the extension may |
23 | | // be implemented in the future. |
24 | | public: |
25 | | FunctionInstance() = delete; |
26 | | /// Move constructor. |
27 | | FunctionInstance(FunctionInstance &&Inst) noexcept |
28 | | : FuncType(Inst.FuncType), LowerFunc(Inst.LowerFunc), |
29 | | MemInst(Inst.MemInst), ReallocFunc(Inst.ReallocFunc), |
30 | | PostReturnFunc(Inst.PostReturnFunc), ParentComp(Inst.ParentComp), |
31 | 0 | Enc(Inst.Enc) {} |
32 | | /// Constructor for component native function. `PR` is the optional |
33 | | /// post-return core function (CanonicalABI.md L3367-3372); pass nullptr |
34 | | /// when the canon lift declared no post-return option. |
35 | | FunctionInstance(const AST::Component::FuncType &Type, |
36 | | Runtime::Instance::FunctionInstance *F, |
37 | | Runtime::Instance::MemoryInstance *M, |
38 | | Runtime::Instance::FunctionInstance *R, |
39 | | const Runtime::Instance::ComponentInstance *P, |
40 | | Runtime::Instance::FunctionInstance *PR = nullptr, |
41 | | StringEncoding E = StringEncoding::UTF8) noexcept |
42 | 0 | : FuncType(Type), LowerFunc(F), MemInst(M), ReallocFunc(R), |
43 | 0 | PostReturnFunc(PR), ParentComp(P), Enc(E) {} |
44 | | |
45 | | /// Getter for component function type. |
46 | 0 | const AST::Component::FuncType &getFuncType() const noexcept { |
47 | 0 | return FuncType; |
48 | 0 | } |
49 | | |
50 | | /// Getter for lower core function instance. |
51 | 0 | Runtime::Instance::FunctionInstance *getLowerFunction() const noexcept { |
52 | 0 | return LowerFunc; |
53 | 0 | } |
54 | | |
55 | | /// Getter for memory instance to value conversion. |
56 | 0 | Runtime::Instance::MemoryInstance *getMemoryInstance() const noexcept { |
57 | 0 | return MemInst; |
58 | 0 | } |
59 | | |
60 | | /// Getter for allocation core function instance. |
61 | 0 | Runtime::Instance::FunctionInstance *getAllocFunction() const noexcept { |
62 | 0 | return ReallocFunc; |
63 | 0 | } |
64 | | |
65 | | /// Getter for the owning component instance. Required for resolving |
66 | | /// TypeIndex-based component types through the canonical ABI. |
67 | | const Runtime::Instance::ComponentInstance * |
68 | 0 | getComponentInstance() const noexcept { |
69 | 0 | return ParentComp; |
70 | 0 | } |
71 | | |
72 | | /// Getter for the post-return core function instance, or nullptr when the |
73 | | /// canon lift declared no post-return option (CanonicalABI.md L3367-3372). |
74 | 0 | Runtime::Instance::FunctionInstance *getPostReturnFunction() const noexcept { |
75 | 0 | return PostReturnFunc; |
76 | 0 | } |
77 | | |
78 | | /// Getter for the guest string encoding declared by the canon lift's |
79 | | /// `string-encoding` option (defaults to UTF-8). |
80 | 0 | StringEncoding getStringEncoding() const noexcept { return Enc; } |
81 | | |
82 | | protected: |
83 | | const AST::Component::FuncType &FuncType; |
84 | | Runtime::Instance::FunctionInstance *LowerFunc; |
85 | | Runtime::Instance::MemoryInstance *MemInst; |
86 | | Runtime::Instance::FunctionInstance *ReallocFunc; |
87 | | Runtime::Instance::FunctionInstance *PostReturnFunc; |
88 | | const Runtime::Instance::ComponentInstance *ParentComp; |
89 | | StringEncoding Enc; |
90 | | }; |
91 | | |
92 | | } // namespace Component |
93 | | } // namespace Instance |
94 | | } // namespace Runtime |
95 | | } // namespace WasmEdge |