Initialize table with Element section. See "include/executor/executor.h".
| 71 | |
| 72 | // Initialize table with Element section. See "include/executor/executor.h". |
| 73 | Expect<void> Executor::initTable(Runtime::StackManager &StackMgr, |
| 74 | const AST::ElementSection &ElemSec) { |
| 75 | // Initialize tables. |
| 76 | uint32_t Idx = 0; |
| 77 | for (const auto &ElemSeg : ElemSec.getContent()) { |
| 78 | // Element index is checked in validation phase. |
| 79 | auto *ElemInst = getElemInstByIdx(StackMgr, Idx); |
| 80 | assuming(ElemInst); |
| 81 | if (ElemSeg.getMode() == AST::ElementSegment::ElemMode::Active) { |
| 82 | // Table index is checked in validation phase. |
| 83 | auto *TabInst = getTabInstByIdx(StackMgr, ElemSeg.getIdx()); |
| 84 | assuming(TabInst); |
| 85 | const uint64_t Off = ElemInst->getOffset(); |
| 86 | |
| 87 | // Replace table[Off : Off + n] with elem[0 : n]. |
| 88 | EXPECTED_TRY( |
| 89 | TabInst |
| 90 | ->setRefs(ElemInst->getRefs(), Off, 0, ElemInst->getRefs().size()) |
| 91 | .map_error([](auto E) { |
| 92 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Element)); |
| 93 | return E; |
| 94 | })); |
| 95 | |
| 96 | // Drop the element instance. |
| 97 | ElemInst->clear(); |
| 98 | |
| 99 | // Operation above is equal to the following instruction sequence: |
| 100 | // expr(init) -> i32.const off |
| 101 | // i32.const 0 |
| 102 | // i32.const n |
| 103 | // table.init idx |
| 104 | // elem.drop idx |
| 105 | } else if (ElemSeg.getMode() == |
| 106 | AST::ElementSegment::ElemMode::Declarative) { |
| 107 | // Drop the element instance. |
| 108 | ElemInst->clear(); |
| 109 | } |
| 110 | Idx++; |
| 111 | } |
| 112 | return {}; |
| 113 | } |
| 114 | |
| 115 | } // namespace Executor |
| 116 | } // namespace WasmEdge |
nothing calls this directly
no test coverage detected