/src/WasmEdge/lib/executor/instantiate/global.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #include "executor/executor.h" |
5 | | |
6 | | #include <cstdint> |
7 | | |
8 | | namespace WasmEdge { |
9 | | namespace Executor { |
10 | | |
11 | | // Instantiate global instance. See "include/executor/executor.h". |
12 | | Expect<void> Executor::instantiate(Runtime::StackManager &StackMgr, |
13 | | Runtime::Instance::ModuleInstance &ModInst, |
14 | 0 | const AST::GlobalSection &GlobSec) { |
15 | | // A frame with temp. module is pushed into the stack in caller. |
16 | | |
17 | | // Prepare pointers for compiled functions. |
18 | 0 | ModInst.GlobalPtrs.resize(ModInst.getGlobalNum() + |
19 | 0 | GlobSec.getContent().size()); |
20 | | |
21 | | // Set the global pointers of imported globals. |
22 | 0 | for (uint32_t I = 0; I < ModInst.getGlobalNum(); ++I) { |
23 | 0 | ModInst.GlobalPtrs[I] = &((*ModInst.getGlobal(I))->getValue()); |
24 | 0 | } |
25 | | |
26 | | // Iterate through the global segments to instantiate and initialize global |
27 | | // instances. |
28 | 0 | for (const auto &GlobSeg : GlobSec.getContent()) { |
29 | | // Run initialize expression. |
30 | 0 | EXPECTED_TRY(runExpression(StackMgr, GlobSeg.getExpr().getInstrs()) |
31 | 0 | .map_error([](auto E) { |
32 | 0 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Expression)); |
33 | 0 | return E; |
34 | 0 | })); |
35 | | |
36 | | // Pop result from stack. |
37 | 0 | ValVariant InitValue = StackMgr.pop(); |
38 | | |
39 | | // Create and add the global instance into the module instance. |
40 | 0 | ModInst.addGlobal(GlobSeg.getGlobalType(), InitValue); |
41 | 0 | const auto Index = ModInst.getGlobalNum() - 1; |
42 | 0 | Runtime::Instance::GlobalInstance *GlobInst = *ModInst.getGlobal(Index); |
43 | | |
44 | | // Set the global pointers of instantiated globals. |
45 | 0 | ModInst.GlobalPtrs[Index] = &(GlobInst->getValue()); |
46 | 0 | } |
47 | 0 | return {}; |
48 | 0 | } |
49 | | |
50 | | } // namespace Executor |
51 | | } // namespace WasmEdge |