Initialize memory with Data section. See "include/executor/executor.h".
| 58 | |
| 59 | // Initialize memory with Data section. See "include/executor/executor.h". |
| 60 | Expect<void> Executor::initMemory(Runtime::StackManager &StackMgr, |
| 61 | const AST::DataSection &DataSec) { |
| 62 | // Initialize memory. |
| 63 | uint32_t Idx = 0; |
| 64 | for (const auto &DataSeg : DataSec.getContent()) { |
| 65 | // Initialize memory if data mode is active. |
| 66 | if (DataSeg.getMode() == AST::DataSegment::DataMode::Active) { |
| 67 | // Memory and data indices are checked in the validation phase. |
| 68 | auto *MemInst = getMemInstByIdx(StackMgr, DataSeg.getIdx()); |
| 69 | assuming(MemInst); |
| 70 | auto *DataInst = getDataInstByIdx(StackMgr, Idx); |
| 71 | assuming(DataInst); |
| 72 | const uint64_t Off = DataInst->getOffset(); |
| 73 | |
| 74 | // Replace mem[Off : Off + n] with data[0 : n]. |
| 75 | EXPECTED_TRY(MemInst |
| 76 | ->setBytes(DataInst->getData(), Off, 0, |
| 77 | DataInst->getData().size()) |
| 78 | .map_error([](auto E) { |
| 79 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Seg_Data)); |
| 80 | return E; |
| 81 | })); |
| 82 | |
| 83 | // Drop the data instance. |
| 84 | DataInst->clear(); |
| 85 | |
| 86 | // Operation above is equal to the following instruction sequence: |
| 87 | // expr(init) -> i32.const off |
| 88 | // i32.const 0 |
| 89 | // i32.const n |
| 90 | // memory.init idx |
| 91 | // data.drop idx |
| 92 | } |
| 93 | Idx++; |
| 94 | } |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | } // namespace Executor |
| 99 | } // namespace WasmEdge |
nothing calls this directly
no test coverage detected