slige/runtime/actions.cpp
2024-12-13 16:12:09 +01:00

79 lines
2.4 KiB
C++

#include "actions.hpp"
#include "json.hpp"
#include "vm_provider.hpp"
#include <cstdlib>
#include <iostream>
#include <memory>
using namespace sliger::rpc::action;
auto sliger::rpc::action::RunDebug::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider& vm) -> void
{
auto program = this->instructions;
vm.load_and_run(program);
writer->write("{ \"ok\": true }");
writer->flush();
};
auto sliger::rpc::action::FlameGraph::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider& vm) -> void
{
auto json = vm.flame_graph_json();
if (json) {
writer->write(std::format(
"{{ \"ok\": true, \"flameGraph\": {} }}", json.value()));
} else {
writer->write("{ \"ok\": false }");
}
writer->flush();
};
auto sliger::rpc::action::CodeCoverage::perform_action(
std::unique_ptr<sliger::rpc::BufferedWriter> writer,
vm_provider::VmProvider& vm) -> void
{
auto json = vm.code_coverage_json();
if (json) {
writer->write(std::format(
"{{ \"ok\": true, \"codeCoverage\": {} }}", json.value()));
} else {
writer->write("{ \"ok\": false }");
}
writer->flush();
};
auto sliger::rpc::action::action_from_json(const sliger::json::Value& value)
-> std::unique_ptr<Action>
{
auto& obj = value.as<sliger::json::Object>();
auto type = obj.fields.at("type")->as<sliger::json::String>();
if (type.value == "flame-graph") {
auto action = FlameGraph();
return std::make_unique<FlameGraph>(action);
}
if (type.value == "code-coverage") {
auto action = CodeCoverage();
return std::make_unique<CodeCoverage>(action);
}
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>();
for (auto& v : values) {
std::unique_ptr<sliger::json::Value> moved = std::move(v);
auto value = moved->as<sliger::json::Number>().value;
instructions.push_back((uint32_t)value);
}
auto action = RunDebug(instructions);
return std::make_unique<RunDebug>(action);
}
std::cout << "error: TODO " << __FILE__ << ":" << __LINE__ << "\n";
exit(1);
};