diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 3573bda07..0deb5c2d8 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -35,6 +35,26 @@ local function complete_app_lib(app) local __app_tick = coroutine.yield local __app_set_setting = app.set_setting local __app_quit = app.quit + local __app_get_stdout_stream = app.get_stdout_stream + local io_stream = require "core:io_stream" + local stdout_stream_lib = { + read = file.__read_descriptor, + write = file.__write_descriptor, + seek = file.__seek_descriptor, + tell = file.__tell_descriptor, + flush = file.__flush_descriptor, + available = file.__available_descriptor, + is_alive = file.__has_descriptor, + close = file.__close_descriptor, + } + + app.get_stdout_stream = function(handle) + local descriptor = __app_get_stdout_stream(handle) + if descriptor == nil then + return nil + end + return io_stream.new(descriptor, false, stdout_stream_lib) + end app.sleep = sleep app.script = __VC_SCRIPT_NAME diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index e08c6d948..cc7faec9b 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -483,6 +483,18 @@ static int l_terminate_instance(lua::State* L) { return lua::pushboolean(L, active); } +static int l_get_stdout_stream(lua::State* L) { + int handle = lua::tointeger(L, 1); + if (handle < 0 || handle >= ::processes.size()) { + throw std::runtime_error("invalid process handle"); + } + auto& process = ::processes[handle]; + if (process == nullptr) { + return 0; + } + return lua::pushinteger(L, process->getStdoutDescriptor()); +} + const luaL_Reg applib[] = { /// content {"is_content_loaded", lua::wrap}, @@ -515,9 +527,11 @@ const luaL_Reg applib[] = { /// other {"get_version", lua::wrap}, {"create_memory_device", lua::wrap}, + // sub-instances {"start_debug_instance", lua::wrap}, {"start_background_instance", lua::wrap}, {"is_instance_alive", lua::wrap}, {"terminate_instance", lua::wrap}, + {"get_stdout_stream", lua::wrap}, {nullptr, nullptr} };