From 1f5d415f75ea659f42a29bcda7a45e2872801e37 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 12 Oct 2026 18:54:26 +0300 Subject: [PATCH] feat: reuse streams --- res/scripts/stdlib.lua | 27 ++++++++++++------ src/logic/scripting/lua/libs/libapp.cpp | 38 +++++++++++-------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index ebb057573..0b86243e7 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -27,6 +27,7 @@ local function tb_frame_tostring(frame) end local __vc__app_script_coroutine +local __vc_process_streams = { stdin = {}, stdout = {} } local function complete_app_lib(app) local __app_load_content = app.load_content @@ -49,20 +50,30 @@ local function complete_app_lib(app) close = file.__close_descriptor, } - app.get_stdout_stream = function(handle) - local descriptor = __app_get_stdout_stream(handle) + local function get_process_stream(kind, handle, getter) + local cache = __vc_process_streams[kind] + local stream = cache[handle] + if stream and stream:is_alive() then + return stream + end + cache[handle] = nil + + local descriptor = getter(handle) if descriptor == nil then return nil end - return io_stream.new(descriptor, false, stdio_stream_lib) + + stream = io_stream.new(descriptor, false, stdio_stream_lib) + cache[handle] = stream + return stream + end + + app.get_stdout_stream = function(handle) + return get_process_stream("stdout", handle, __app_get_stdout_stream) end app.get_stdin_stream = function(handle) - local descriptor = __app_get_stdin_stream(handle) - if descriptor == nil then - return nil - end - return io_stream.new(descriptor, false, stdio_stream_lib) + return get_process_stream("stdin", handle, __app_get_stdin_stream) end app.sleep = sleep diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 9e6b520eb..243c9a154 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -21,11 +21,13 @@ #include "world/Level.hpp" #include +#include using namespace scripting; namespace { - static std::array, MAX_SUBPROCESSES> processes; + static std::unordered_map> processes; + static int next_process_id = 1; } /// @brief Check if content is loaded @@ -445,25 +447,17 @@ static int l_start_background_instance(lua::State* L) { } } - int handle = -1; - for (int i = 0; i < ::processes.size(); i++) { - if (!::processes[i] || !::processes[i]->isActive()) { - handle = i; - break; - } - } - if (handle == -1) { + if (::processes.size() >= MAX_SUBPROCESSES) { throw std::runtime_error("sub-processes limit exceeded"); } - ::processes[handle] = - platform::new_engine_instance(std::move(args), "", true); - - return lua::pushinteger(L, handle); + int id = ::next_process_id++; + ::processes[id] = platform::new_engine_instance(std::move(args), "", true); + return lua::pushinteger(L, id); } static int l_is_instance_alive(lua::State* L) { int handle = lua::tointeger(L, 1); - if (handle < 0 || handle >= ::processes.size()) { + if (handle < 0 || ::processes.find(handle) == ::processes.end()) { throw std::runtime_error("invalid process handle"); } auto& process = ::processes[handle]; @@ -472,21 +466,23 @@ static int l_is_instance_alive(lua::State* L) { static int l_terminate_instance(lua::State* L) { int handle = lua::tointeger(L, 1); - if (handle < 0 || handle >= ::processes.size()) { + if (handle < 0 || ::processes.find(handle) == ::processes.end()) { throw std::runtime_error("invalid process handle"); } - auto& process = ::processes[handle]; - if (process == nullptr) { + auto it = ::processes.find(handle); + if (it->second == nullptr) { + ::processes.erase(it); return lua::pushboolean(L, false); } - bool active = process->isActive(); - process.reset(); + bool active = it->second->isActive(); + it->second.reset(); + ::processes.erase(it); return lua::pushboolean(L, active); } static int l_get_stdin_stream(lua::State* L) { int handle = lua::tointeger(L, 1); - if (handle < 0 || handle >= ::processes.size()) { + if (handle < 0 || ::processes.find(handle) == ::processes.end()) { throw std::runtime_error("invalid process handle"); } auto& process = ::processes[handle]; @@ -498,7 +494,7 @@ static int l_get_stdin_stream(lua::State* L) { static int l_get_stdout_stream(lua::State* L) { int handle = lua::tointeger(L, 1); - if (handle < 0 || handle >= ::processes.size()) { + if (handle < 0 || ::processes.find(handle) == ::processes.end()) { throw std::runtime_error("invalid process handle"); } auto& process = ::processes[handle];