feat: reuse streams

This commit is contained in:
MihailRis 2026-10-12 18:54:26 +03:00
parent 64ae827f89
commit 1f5d415f75
2 changed files with 36 additions and 29 deletions

View file

@ -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

View file

@ -21,11 +21,13 @@
#include "world/Level.hpp"
#include <array>
#include <unordered_map>
using namespace scripting;
namespace {
static std::array<std::unique_ptr<SystemProcess>, MAX_SUBPROCESSES> processes;
static std::unordered_map<int, std::unique_ptr<SystemProcess>> 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];