slige/runtime/actions.cpp

86 lines
2.4 KiB
C++
Raw Permalink Normal View History

#include "actions.hpp"
2024-12-13 15:11:16 +00:00
#include "json.hpp"
#include "vm_provider.hpp"
2024-12-13 15:11:16 +00:00
#include <cstdlib>
#include <iostream>
#include <memory>
using namespace sliger::rpc::action;
2024-12-15 02:07:33 +00:00
auto Status::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer, VmProvider& vm) -> void
{
2024-12-15 00:32:21 +00:00
bool running = not vm.done();
2024-12-15 02:07:33 +00:00
writer->write(std::format(
"{{ \"ok\": true, \"status\": {{ \"running\": {} }} }}", running));
2024-12-12 10:33:38 +00:00
writer->flush();
};
2024-12-15 00:32:21 +00:00
auto FlameGraph::perform_action(
2024-12-15 02:07:33 +00:00
std::unique_ptr<sliger::rpc::BufferedWriter> writer, VmProvider& vm) -> void
{
auto json = vm.flame_graph_json();
if (json) {
2024-12-12 10:33:38 +00:00
writer->write(std::format(
"{{ \"ok\": true, \"flameGraph\": {} }}", json.value()));
} else {
2024-12-12 10:33:38 +00:00
writer->write("{ \"ok\": false }");
}
writer->flush();
};
2024-12-15 00:32:21 +00:00
auto CodeCoverage::perform_action(
2024-12-15 02:07:33 +00:00
std::unique_ptr<sliger::rpc::BufferedWriter> writer, VmProvider& vm) -> void
{
auto json = vm.code_coverage_json();
if (json) {
2024-12-12 10:33:38 +00:00
writer->write(std::format(
"{{ \"ok\": true, \"codeCoverage\": {} }}", json.value()));
} else {
2024-12-12 10:33:38 +00:00
writer->write("{ \"ok\": false }");
}
writer->flush();
};
2024-12-13 15:11:16 +00:00
2024-12-15 00:32:21 +00:00
auto RunDebug::perform_action(
2024-12-15 02:07:33 +00:00
std::unique_ptr<sliger::rpc::BufferedWriter> writer, VmProvider& vm) -> void
2024-12-15 00:32:21 +00:00
{
auto program = this->instructions;
2024-12-15 02:07:33 +00:00
vm.load_and_start(program);
2024-12-15 00:32:21 +00:00
writer->write("{ \"ok\": true }");
writer->flush();
};
auto sliger::rpc::action::action_from_json(
const sliger::json::Value& value) -> std::unique_ptr<Action>
2024-12-13 15:11:16 +00:00
{
auto& obj = value.as<sliger::json::Object>();
auto type = obj.fields.at("type")->as<sliger::json::String>();
2024-12-15 02:07:33 +00:00
if (type.value == "status") {
return std::make_unique<Status>();
}
2024-12-13 15:11:16 +00:00
if (type.value == "flame-graph") {
2024-12-15 02:07:33 +00:00
return std::make_unique<FlameGraph>();
2024-12-13 15:11:16 +00:00
}
if (type.value == "code-coverage") {
2024-12-15 02:07:33 +00:00
return std::make_unique<CodeCoverage>();
2024-12-13 15:11:16 +00:00
}
if (type.value == "run-debug") {
sliger::json::ArrayValues values = std::move(
obj.fields.at("program")->as<sliger::json::Array>().values);
auto instructions = std::vector<uint32_t>();
2024-12-15 02:07:33 +00:00
for (auto&& v : values) {
auto value = v->as<sliger::json::Number>().value;
2024-12-13 15:11:16 +00:00
instructions.push_back((uint32_t)value);
}
2024-12-15 02:07:33 +00:00
return std::make_unique<RunDebug>(instructions);
2024-12-13 15:11:16 +00:00
}
std::cout << "error: TODO " << __FILE__ << ":" << __LINE__ << "\n";
exit(1);
};