fix commands execution thread

This commit is contained in:
MihailRis 2026-02-11 23:58:42 +03:00
parent 4478afa1ef
commit 48f5de8adf
3 changed files with 18 additions and 16 deletions

View file

@ -1,5 +1,6 @@
#include "stdin_reader.hpp" #include "stdin_reader.hpp"
#include "engine/Engine.hpp"
#include "logic/CommandsInterpreter.hpp" #include "logic/CommandsInterpreter.hpp"
#include "coders/json.hpp" #include "coders/json.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
@ -11,8 +12,9 @@ static debug::Logger logger("stdin-reader");
static std::thread reader_thread; static std::thread reader_thread;
void start_stdin_reader(cmd::CommandsInterpreter& interpreter) { void start_stdin_reader(Engine& engine) {
reader_thread = std::thread([&interpreter]() { reader_thread = std::thread([&engine]() {
auto& interpreter = engine.getCmd();
logger.info() << "reader thread started"; logger.info() << "reader thread started";
std::string line; std::string line;
@ -20,16 +22,18 @@ void start_stdin_reader(cmd::CommandsInterpreter& interpreter) {
if (line.empty()) { if (line.empty()) {
continue; continue;
} }
try { engine.postRunnable([line, &interpreter] () {
auto result = interpreter.execute(line); try {
if (result.isString()) { auto result = interpreter.execute(line);
logger.info() << result.asString(); if (result.isString()) {
} else { logger.info() << result.asString();
logger.info() << json::stringify(result, true); } else {
logger.info() << json::stringify(result, true);
}
} catch (const std::exception& err) {
logger.error() << err.what();
} }
} catch (const std::exception& err) { });
logger.error() << err.what();
}
} }
}); });
reader_thread.detach(); reader_thread.detach();

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
namespace cmd { class Engine;
class CommandsInterpreter;
}
void start_stdin_reader(cmd::CommandsInterpreter& interpreter); void start_stdin_reader(Engine& engine);

View file

@ -193,7 +193,7 @@ void Engine::initialize(CoreParameters coreParameters) {
project->loadProjectClientScript(); project->loadProjectClientScript();
} }
if (params.stdinCommands) { if (params.stdinCommands) {
start_stdin_reader(*cmd); start_stdin_reader(*this);
} }
} }