diff --git a/runtime/actions.cpp b/runtime/actions.cpp new file mode 100644 index 0000000..b63a223 --- /dev/null +++ b/runtime/actions.cpp @@ -0,0 +1,36 @@ +#include "actions.hpp" +#include "vm_provider.hpp" + +auto sliger::rpc::action::RunDebug::perform_action( + std::unique_ptr writer, + vm_provider::VmProvider vm) -> void +{ + auto program = this->instructions; + vm.load_and_run(program); +}; + +auto sliger::rpc::action::FlameGraph::perform_action( + std::unique_ptr writer, + vm_provider::VmProvider vm) -> void +{ + auto json = vm.flame_graph_json(); + if (json) { + writer->write(json.value()); + } else { + writer->write("null"); + } + writer->flush(); +}; + +auto sliger::rpc::action::CodeCoverage::perform_action( + std::unique_ptr writer, + vm_provider::VmProvider vm) -> void +{ + auto json = vm.code_coverage_json(); + if (json) { + writer->write(json.value()); + } else { + writer->write("null"); + } + writer->flush(); +}; diff --git a/runtime/actions.hpp b/runtime/actions.hpp index d39a50f..1e722ea 100644 --- a/runtime/actions.hpp +++ b/runtime/actions.hpp @@ -1,22 +1,38 @@ #include "rpc_server.hpp" +#include "vm_provider.hpp" #include namespace sliger::rpc::action { struct Action { virtual auto perform_action( - std::unique_ptr writer) -> void = 0; + std::unique_ptr writer, + sliger::rpc::vm_provider::VmProvider vm_provider) -> void = 0; virtual ~Action() = default; }; +class FlameGraph : public Action { +public: + FlameGraph() { } + auto perform_action(std::unique_ptr writer, + sliger::rpc::vm_provider::VmProvider vm_provider) -> void; +}; + +class CodeCoverage : public Action { +public: + CodeCoverage() { } + auto perform_action(std::unique_ptr writer, + sliger::rpc::vm_provider::VmProvider vm_provider) -> void; +}; + class RunDebug : public Action { public: RunDebug(std::vector instructions) : instructions(instructions) { } - auto perform_action( - std::unique_ptr writer) -> void; + auto perform_action(std::unique_ptr writer, + sliger::rpc::vm_provider::VmProvider vm_provider) -> void; private: std::vector instructions; @@ -28,6 +44,16 @@ static auto action_from_json( auto& obj = value->as(); auto type = obj.fields.at("type")->as(); + if (type.value == "flamegraph") { + auto action = FlameGraph(); + return std::make_unique(action); + } + + if (type.value == "code-coverage") { + auto action = CodeCoverage(); + return std::make_unique(action); + } + if (type.value == "run-debug") { sliger::json::ArrayValues values = std::move( obj.fields.at("program")->as().values); diff --git a/runtime/main.cpp b/runtime/main.cpp index 2c697b4..22bbcc7 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -1,9 +1,12 @@ #include "actions.hpp" #include "json.hpp" #include "rpc_server.hpp" +#include "vm_provider.hpp" int main() { + auto state = sliger::rpc::vm_provider::VmProvider(); + auto rpc = sliger::rpc::RpcServer( [&](std::unique_ptr req, std::unique_ptr writer) { diff --git a/runtime/vm_provider.cpp b/runtime/vm_provider.cpp new file mode 100644 index 0000000..53ce67b --- /dev/null +++ b/runtime/vm_provider.cpp @@ -0,0 +1,33 @@ +#include "vm_provider.hpp" +#include "vm.hpp" + +using namespace sliger::rpc::vm_provider; + +auto VmProvider::load_and_run(std::vector instructions) -> void +{ + auto vm = VM(instructions, + { + .flame_graph = true, + .code_coverage = true, + }); + vm.run_until_done(); + this->vm = vm; +} + +auto VmProvider::flame_graph_json() -> std::optional +{ + if (this->vm) { + return this->vm->flame_graph_json(); + } else { + return {}; + } +} + +auto VmProvider::code_coverage_json() -> std::optional +{ + if (this->vm) { + return this->vm->code_coverage_json(); + } else { + return {}; + } +} diff --git a/runtime/vm_provider.hpp b/runtime/vm_provider.hpp new file mode 100644 index 0000000..ad49507 --- /dev/null +++ b/runtime/vm_provider.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "vm.hpp" + +namespace sliger::rpc::vm_provider { +class VmProvider { +public: + VmProvider() { } + + auto load_and_run(std::vector instructions) -> void; + auto flame_graph_json() -> std::optional; + auto code_coverage_json() -> std::optional; + +private: + std::optional vm; +}; + +}