Coverage Report

Created: 2025-08-28 06:48

/src/hermes/lib/BCGen/HBC/Bytecode.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#include "hermes/BCGen/HBC/Bytecode.h"
9
#include "hermes/SourceMap/SourceMapGenerator.h"
10
11
#include "llvh/ADT/SmallVector.h"
12
13
using namespace hermes;
14
using namespace hbc;
15
16
void BytecodeModule::setFunction(
17
    uint32_t index,
18
1.86k
    std::unique_ptr<BytecodeFunction> F) {
19
1.86k
  assert(index < getNumFunctions() && "Function ID out of bound");
20
1.86k
  functions_[index] = std::move(F);
21
1.86k
}
22
23
356
BytecodeFunction &BytecodeModule::getFunction(unsigned index) {
24
356
  assert(index < getNumFunctions() && "Function ID out of bound");
25
356
  assert(functions_[index] && "Invalid function");
26
356
  return *functions_[index];
27
356
}
28
29
0
void BytecodeModule::populateSourceMap(SourceMapGenerator *sourceMap) const {
30
  /// Construct a list of virtual function offsets, and pass it to DebugInfo to
31
  /// help it populate the source map.
32
0
  std::vector<uint32_t> functionOffsets;
33
0
  functionOffsets.reserve(functions_.size());
34
0
  uint32_t offset = 0;
35
0
  for (const auto &func : functions_) {
36
0
    functionOffsets.push_back(offset);
37
0
    offset += func->getHeader().bytecodeSizeInBytes;
38
0
  }
39
0
  debugInfo_.populateSourceMap(
40
0
      sourceMap, std::move(functionOffsets), segmentID_);
41
0
}
42
43
0
ArrayRef<uint32_t> BytecodeFunction::getJumpTablesOnly() const {
44
  // The jump tables (if there are any) start at the nearest 4-byte boundary
45
  // from the end of the opcodes.
46
0
  uint32_t jumpTableStartIdx =
47
0
      llvh::alignTo<sizeof(uint32_t)>(header_.bytecodeSizeInBytes);
48
49
0
  if (jumpTableStartIdx > opcodesAndJumpTables_.size()) {
50
0
    return {};
51
0
  }
52
53
0
  uint32_t jumpTableBytes = opcodesAndJumpTables_.size() - jumpTableStartIdx;
54
0
  assert(jumpTableBytes % sizeof(uint32_t) == 0);
55
0
  uint32_t jumpTableSize = jumpTableBytes / sizeof(uint32_t);
56
0
  const uint32_t *jumpTableStart = reinterpret_cast<const uint32_t *>(
57
0
      opcodesAndJumpTables_.data() + jumpTableStartIdx);
58
59
0
  return {jumpTableStart, jumpTableSize};
60
0
}