slige/runtime/to_json.cpp

42 lines
1.2 KiB
C++
Raw Normal View History

2024-11-20 21:12:08 +00:00
#include "vm.hpp"
#include <string>
using namespace sliger;
void FlameGraphBuilder::to_json(json::Writer& writer) const
{
fg_node_to_json(writer, 0);
}
void FlameGraphBuilder::fg_node_to_json(
json::Writer& writer, size_t node_index) const
{
const auto& node = this->nodes[node_index];
writer << "{\"fn\":" << std::to_string(node.fn)
<< ",\"acc\":" << std::to_string(node.acc)
<< ",\"parent\":" << std::to_string(node.parent)
2024-11-21 03:12:07 +00:00
<< ",\"children\":[";
2024-11-20 21:12:08 +00:00
auto first = true;
for (auto child_index : node.children) {
if (!first) {
writer << ",";
}
first = false;
fg_node_to_json(writer, child_index);
}
writer << "]}";
}
void CodeCoverageBuilder::to_json(json::Writer& writer) const
{
writer << "[";
writer.add_comma_seperated(
this->entries, [&](json::Writer& writer, CCPosEntry entry) {
writer << "{\"index\":" << std::to_string(entry.pos.index)
<< ",\"line\":" << std::to_string(entry.pos.line)
<< ",\"col\":" << std::to_string(entry.pos.col)
<< ",\"covers\":" << std::to_string(entry.covers) << "}";
});
writer << "]";
}