Coverage Report

Created: 2025-08-28 06:48

/src/hermes/lib/VM/DummyObject.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/VM/DummyObject.h"
9
10
#include "hermes/VM/GC.h"
11
#include "hermes/VM/GCBase-inline.h"
12
#include "hermes/VM/GCPointer-inline.h"
13
#include "hermes/VM/HermesValue-inline.h"
14
15
namespace hermes {
16
namespace vm {
17
namespace testhelpers {
18
19
const VTable DummyObject::vt{
20
    CellKind::DummyObjectKind,
21
    cellSize<DummyObject>(),
22
    _finalizeImpl,
23
    _mallocSizeImpl,
24
    nullptr
25
#ifdef HERMES_MEMORY_INSTRUMENTATION
26
    ,
27
    VTable::HeapSnapshotMetadata{
28
        HeapSnapshot::NodeType::Object,
29
        nullptr,
30
        _snapshotAddEdgesImpl,
31
        nullptr,
32
        nullptr}
33
#endif
34
};
35
36
0
DummyObject::DummyObject(GC &gc) : other(), x(1), y(2) {
37
0
  hvBool.setNonPtr(HermesValue::encodeBoolValue(true), gc);
38
0
  hvDouble.setNonPtr(HermesValue::encodeUntrustedNumberValue(3.14), gc);
39
0
  hvNative.setNonPtr(HermesValue::encodeNativeUInt32(0xE), gc);
40
0
  hvUndefined.setNonPtr(HermesValue::encodeUndefinedValue(), gc);
41
0
  hvEmpty.setNonPtr(HermesValue::encodeEmptyValue(), gc);
42
0
  hvNull.setNonPtr(HermesValue::encodeNullValue(), gc);
43
0
}
44
45
0
void DummyObject::acquireExtMem(GC &gc, uint32_t sz) {
46
0
  assert(externalBytes == 0);
47
0
  externalBytes = sz;
48
0
  gc.creditExternalMemory(this, sz);
49
0
}
50
0
void DummyObject::releaseExtMem(GC &gc) {
51
0
  gc.debitExternalMemory(this, externalBytes);
52
0
  externalBytes = 0;
53
0
}
54
55
0
void DummyObject::setPointer(GC &gc, DummyObject *obj) {
56
0
  other.set(gc.getPointerBase(), obj, gc);
57
0
}
58
59
0
/* static */ constexpr CellKind DummyObject::getCellKind() {
60
0
  return CellKind::DummyObjectKind;
61
0
}
62
63
0
DummyObject *DummyObject::create(GC &gc, PointerBase &base) {
64
0
  auto *cell = gc.makeAFixed<DummyObject, HasFinalizer::Yes>(gc);
65
0
  cell->finalizerCallback.set(gc, nullptr);
66
0
  cell->weak.emplace(base, gc, cell);
67
0
  return cell;
68
0
}
69
0
DummyObject *DummyObject::createLongLived(GC &gc) {
70
0
  return gc.makeAFixed<DummyObject, HasFinalizer::Yes, LongLived::Yes>(gc);
71
0
}
72
73
0
bool DummyObject::classof(const GCCell *cell) {
74
0
  return cell->getKind() == CellKind::DummyObjectKind;
75
0
}
76
77
0
void DummyObject::_finalizeImpl(GCCell *cell, GC &gc) {
78
0
  auto *self = vmcast<DummyObject>(cell);
79
0
  auto callback = self->finalizerCallback.get(gc);
80
0
  if (callback)
81
0
    (*callback)();
82
0
  if (self->weak)
83
0
    self->weak->releaseSlot();
84
0
  self->releaseExtMem(gc);
85
86
  // Callback is assumed to point to allocated memory
87
0
  delete callback;
88
0
  self->~DummyObject();
89
0
}
90
91
0
size_t DummyObject::_mallocSizeImpl(GCCell *cell) {
92
0
  return vmcast<DummyObject>(cell)->extraBytes;
93
0
}
94
95
#ifdef HERMES_MEMORY_INSTRUMENTATION
96
void DummyObject::_snapshotAddEdgesImpl(
97
    GCCell *cell,
98
    GC &gc,
99
0
    HeapSnapshot &snap) {
100
0
  auto *const self = vmcast<DummyObject>(cell);
101
0
  if (!self->weak)
102
0
    return;
103
  // Filter out empty refs from adding edges.
104
0
  if (!self->weak->isValid())
105
0
    return;
106
  // DummyObject has only one WeakRef field.
107
0
  snap.addNamedEdge(
108
0
      HeapSnapshot::EdgeType::Weak,
109
0
      "weak",
110
0
      gc.getObjectID(self->weak->getNoBarrierUnsafe(gc.getPointerBase())));
111
0
}
112
#endif
113
114
} // namespace testhelpers
115
116
1
void DummyObjectBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
117
1
  const auto *self = static_cast<const testhelpers::DummyObject *>(cell);
118
1
  mb.setVTable(&testhelpers::DummyObject::vt);
119
1
  mb.addField("HermesBool", &self->hvBool);
120
1
  mb.addField("HermesDouble", &self->hvDouble);
121
1
  mb.addField("HermesUndefined", &self->hvUndefined);
122
1
  mb.addField("HermesEmpty", &self->hvEmpty);
123
1
  mb.addField("HermesNative", &self->hvNative);
124
1
  mb.addField("HermesNull", &self->hvNull);
125
1
  mb.addField("other", &self->other);
126
1
}
127
128
} // namespace vm
129
} // namespace hermes