mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 10:29:47 +00:00
feat: reuse streams
This commit is contained in:
parent
64ae827f89
commit
1f5d415f75
2 changed files with 36 additions and 29 deletions
|
|
@ -27,6 +27,7 @@ local function tb_frame_tostring(frame)
|
||||||
end
|
end
|
||||||
|
|
||||||
local __vc__app_script_coroutine
|
local __vc__app_script_coroutine
|
||||||
|
local __vc_process_streams = { stdin = {}, stdout = {} }
|
||||||
|
|
||||||
local function complete_app_lib(app)
|
local function complete_app_lib(app)
|
||||||
local __app_load_content = app.load_content
|
local __app_load_content = app.load_content
|
||||||
|
|
@ -49,20 +50,30 @@ local function complete_app_lib(app)
|
||||||
close = file.__close_descriptor,
|
close = file.__close_descriptor,
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get_stdout_stream = function(handle)
|
local function get_process_stream(kind, handle, getter)
|
||||||
local descriptor = __app_get_stdout_stream(handle)
|
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
|
if descriptor == nil then
|
||||||
return nil
|
return nil
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
app.get_stdin_stream = function(handle)
|
app.get_stdin_stream = function(handle)
|
||||||
local descriptor = __app_get_stdin_stream(handle)
|
return get_process_stream("stdin", handle, __app_get_stdin_stream)
|
||||||
if descriptor == nil then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
return io_stream.new(descriptor, false, stdio_stream_lib)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
app.sleep = sleep
|
app.sleep = sleep
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,13 @@
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
namespace {
|
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
|
/// @brief Check if content is loaded
|
||||||
|
|
@ -445,25 +447,17 @@ static int l_start_background_instance(lua::State* L) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle = -1;
|
if (::processes.size() >= MAX_SUBPROCESSES) {
|
||||||
for (int i = 0; i < ::processes.size(); i++) {
|
|
||||||
if (!::processes[i] || !::processes[i]->isActive()) {
|
|
||||||
handle = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (handle == -1) {
|
|
||||||
throw std::runtime_error("sub-processes limit exceeded");
|
throw std::runtime_error("sub-processes limit exceeded");
|
||||||
}
|
}
|
||||||
::processes[handle] =
|
int id = ::next_process_id++;
|
||||||
platform::new_engine_instance(std::move(args), "", true);
|
::processes[id] = platform::new_engine_instance(std::move(args), "", true);
|
||||||
|
return lua::pushinteger(L, id);
|
||||||
return lua::pushinteger(L, handle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_is_instance_alive(lua::State* L) {
|
static int l_is_instance_alive(lua::State* L) {
|
||||||
int handle = lua::tointeger(L, 1);
|
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");
|
throw std::runtime_error("invalid process handle");
|
||||||
}
|
}
|
||||||
auto& process = ::processes[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) {
|
static int l_terminate_instance(lua::State* L) {
|
||||||
int handle = lua::tointeger(L, 1);
|
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");
|
throw std::runtime_error("invalid process handle");
|
||||||
}
|
}
|
||||||
auto& process = ::processes[handle];
|
auto it = ::processes.find(handle);
|
||||||
if (process == nullptr) {
|
if (it->second == nullptr) {
|
||||||
|
::processes.erase(it);
|
||||||
return lua::pushboolean(L, false);
|
return lua::pushboolean(L, false);
|
||||||
}
|
}
|
||||||
bool active = process->isActive();
|
bool active = it->second->isActive();
|
||||||
process.reset();
|
it->second.reset();
|
||||||
|
::processes.erase(it);
|
||||||
return lua::pushboolean(L, active);
|
return lua::pushboolean(L, active);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_get_stdin_stream(lua::State* L) {
|
static int l_get_stdin_stream(lua::State* L) {
|
||||||
int handle = lua::tointeger(L, 1);
|
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");
|
throw std::runtime_error("invalid process handle");
|
||||||
}
|
}
|
||||||
auto& process = ::processes[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) {
|
static int l_get_stdout_stream(lua::State* L) {
|
||||||
int handle = lua::tointeger(L, 1);
|
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");
|
throw std::runtime_error("invalid process handle");
|
||||||
}
|
}
|
||||||
auto& process = ::processes[handle];
|
auto& process = ::processes[handle];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue