MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / initMemory

Method initMemory

lib/executor/instantiate/data.cpp:60–96  ·  view source on GitHub ↗

Initialize memory with Data section. See "include/executor/executor.h".

Source from the content-addressed store, hash-verified

58
59// Initialize memory with Data section. See "include/executor/executor.h".
60Expect<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

Callers

nothing calls this directly

Calls 11

InfoASTClass · 0.85
setBytesMethod · 0.80
EXPECTED_TRYFunction · 0.50
errorFunction · 0.50
getContentMethod · 0.45
getModeMethod · 0.45
getIdxMethod · 0.45
getOffsetMethod · 0.45
getDataMethod · 0.45
sizeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected