add core.capture_output

This commit is contained in:
MihailRis
2025-03-13 22:05:14 +03:00
parent ffac010e54
commit 1bb25e99c6
4 changed files with 37 additions and 3 deletions
+28
View File
@@ -273,6 +273,33 @@ static int l_blank(lua::State*) {
return 0; return 0;
} }
static int l_capture_output(lua::State* L) {
int argc = lua::gettop(L) - 1;
if (!lua::isfunction(L, 1)) {
throw std::runtime_error("function expected as argument 1");
}
for (int i = 0; i < argc; i++) {
lua::pushvalue(L, i + 2);
}
lua::pushvalue(L, 1);
auto prev_output = output_stream;
auto prev_error = error_stream;
std::stringstream captured_output;
output_stream = &captured_output;
error_stream = &captured_output;
lua::call_nothrow(L, argc, 0);
output_stream = prev_output;
error_stream = prev_error;
lua::pushstring(L, captured_output.str());
return 1;
}
const luaL_Reg corelib[] = { const luaL_Reg corelib[] = {
{"blank", lua::wrap<l_blank>}, {"blank", lua::wrap<l_blank>},
{"get_version", lua::wrap<l_get_version>}, {"get_version", lua::wrap<l_get_version>},
@@ -292,6 +319,7 @@ const luaL_Reg corelib[] = {
{"get_setting_info", lua::wrap<l_get_setting_info>}, {"get_setting_info", lua::wrap<l_get_setting_info>},
{"open_folder", lua::wrap<l_open_folder>}, {"open_folder", lua::wrap<l_open_folder>},
{"quit", lua::wrap<l_quit>}, {"quit", lua::wrap<l_quit>},
{"capture_output", lua::wrap<l_capture_output>},
{"__load_texture", lua::wrap<l_load_texture>}, {"__load_texture", lua::wrap<l_load_texture>},
{NULL, NULL} {NULL, NULL}
}; };
+5 -3
View File
@@ -2,6 +2,8 @@
#include "libs/api_lua.hpp" #include "libs/api_lua.hpp"
using namespace scripting;
/// @brief Modified version of luaB_print from lbaselib.c /// @brief Modified version of luaB_print from lbaselib.c
int l_print(lua::State* L) { int l_print(lua::State* L) {
int n = lua::gettop(L); /* number of arguments */ int n = lua::gettop(L); /* number of arguments */
@@ -16,10 +18,10 @@ int l_print(lua::State* L) {
L, L,
LUA_QL("tostring") " must return a string to " LUA_QL("print") LUA_QL("tostring") " must return a string to " LUA_QL("print")
); );
if (i > 1) std::cout << "\t"; if (i > 1) *output_stream << "\t";
std::cout << s; *output_stream << s;
lua::pop(L); /* pop result */ lua::pop(L); /* pop result */
} }
std::cout << std::endl; *output_stream << std::endl;
return 0; return 0;
} }
+2
View File
@@ -34,6 +34,8 @@ static debug::Logger logger("scripting");
static inline const std::string STDCOMP = "stdcomp"; static inline const std::string STDCOMP = "stdcomp";
std::ostream* scripting::output_stream = &std::cout;
std::ostream* scripting::error_stream = &std::cerr;
Engine* scripting::engine = nullptr; Engine* scripting::engine = nullptr;
Level* scripting::level = nullptr; Level* scripting::level = nullptr;
const Content* scripting::content = nullptr; const Content* scripting::content = nullptr;
+2
View File
@@ -42,6 +42,8 @@ namespace scripting {
extern Level* level; extern Level* level;
extern BlocksController* blocks; extern BlocksController* blocks;
extern LevelController* controller; extern LevelController* controller;
extern std::ostream* output_stream;
extern std::ostream* error_stream;
void initialize(Engine* engine); void initialize(Engine* engine);