Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
  // Prepare the table-size and element-buffer pointers vectors for compiled
18
  // functions.
19
0
  ModInst.TableSizePtrs.resize(ModInst.getTableNum() +
20
0
                               TabSec.getContent().size());
21
0
  ModInst.TableRefPtrs.resize(ModInst.getTableNum() +
22
0
                              TabSec.getContent().size());
23
24
  // Set the table pointers of imported tables.
25
0
  for (uint32_t I = 0; I < ModInst.getTableNum(); ++I) {
26
0
    auto *TabInst = ModInst.unsafeGetTable(I);
27
0
    ModInst.TableSizePtrs[I] = TabInst->getSizePtr();
28
0
    ModInst.TableRefPtrs[I] = &TabInst->getDataPtr();
29
0
  }
30
31
  // Iterate through the table segments to instantiate and initialize table
32
  // instances.
33
0
  for (const auto &TabSeg : TabSec.getContent()) {
34
0
    if (TabSeg.getExpr().getInstrs().size() > 0) {
35
      // Run the initialization expression.
36
0
      EXPECTED_TRY(runExpression(StackMgr, TabSeg.getExpr().getInstrs())
37
0
                       .map_error([](auto E) {
38
0
                         spdlog::error(
39
0
                             ErrInfo::InfoAST(ASTNodeAttr::Expression));
40
0
                         return E;
41
0
                       }));
42
      // Pop result from the stack.
43
0
      RefVariant InitTabValue = StackMgr.pop().get<RefVariant>();
44
      // Create and add the table instance to the module instance.
45
0
      ModInst.addTable(TabSeg.getTableType(), InitTabValue);
46
0
    } else {
47
      // No init expression case. Use the null reference to initialize.
48
      // Normalize the type to the bottom abstract heap type so that null
49
      // references always carry abstract types, as ref.cast/ref.test assume.
50
0
      auto BotType = toBottomType(StackMgr, TabSeg.getTableType().getRefType());
51
0
      RefVariant InitTabValue(ValType(TypeCode::RefNull, BotType));
52
0
      ModInst.addTable(TabSeg.getTableType(), InitTabValue);
53
0
    }
54
    // Set the table pointers of the instantiated table.
55
0
    const auto Index = ModInst.getTableNum() - 1;
56
0
    auto *TabInst = ModInst.unsafeGetTable(Index);
57
0
    ModInst.TableSizePtrs[Index] = TabInst->getSizePtr();
58
0
    ModInst.TableRefPtrs[Index] = &TabInst->getDataPtr();
59
0
  }
60
0
  return {};
61
0
}
62
63
} // namespace Executor
64
} // namespace WasmEdge