/src/WasmEdge/lib/executor/instantiate/table.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "executor/executor.h" |
5 | | |
6 | | #include <cstdint> |
7 | | |
8 | | namespace WasmEdge { |
9 | | namespace Executor { |
10 | | |
11 | | // Instantiate table instance. See "include/executor/executor.h". |
12 | | Expect<void> Executor::instantiate(Runtime::StackManager &StackMgr, |
13 | | Runtime::Instance::ModuleInstance &ModInst, |
14 | 0 | const AST::TableSection &TabSec) { |
15 | | // A frame with the temporary module is pushed onto the stack by the caller. |
16 | | |
17 | | // Iterate through the table segments to instantiate and initialize table |
18 | | // instances. |
19 | 0 | for (const auto &TabSeg : TabSec.getContent()) { |
20 | 0 | if (TabSeg.getExpr().getInstrs().size() > 0) { |
21 | | // Run the initialization expression. |
22 | 0 | EXPECTED_TRY(runExpression(StackMgr, TabSeg.getExpr().getInstrs()) |
23 | 0 | .map_error([](auto E) { |
24 | 0 | spdlog::error( |
25 | 0 | ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
26 | 0 | return E; |
27 | 0 | })); |
28 | | // Pop result from the stack. |
29 | 0 | RefVariant InitTabValue = StackMgr.pop().get<RefVariant>(); |
30 | | // Create and add the table instance to the module instance. |
31 | 0 | ModInst.addTable(TabSeg.getTableType(), InitTabValue); |
32 | 0 | } else { |
33 | | // No init expression case. Use the null reference to initialize. |
34 | | // Normalize the type to the bottom abstract heap type so that null |
35 | | // references always carry abstract types, as ref.cast/ref.test assume. |
36 | 0 | auto BotType = toBottomType(StackMgr, TabSeg.getTableType().getRefType()); |
37 | 0 | RefVariant InitTabValue(ValType(TypeCode::RefNull, BotType)); |
38 | 0 | ModInst.addTable(TabSeg.getTableType(), InitTabValue); |
39 | 0 | } |
40 | 0 | } |
41 | 0 | return {}; |
42 | 0 | } |
43 | | |
44 | | } // namespace Executor |
45 | | } // namespace WasmEdge |