add core.capture_output
This commit is contained in:
parent
ffac010e54
commit
1bb25e99c6
@ -273,6 +273,33 @@ static int l_blank(lua::State*) {
|
||||
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[] = {
|
||||
{"blank", lua::wrap<l_blank>},
|
||||
{"get_version", lua::wrap<l_get_version>},
|
||||
@ -292,6 +319,7 @@ const luaL_Reg corelib[] = {
|
||||
{"get_setting_info", lua::wrap<l_get_setting_info>},
|
||||
{"open_folder", lua::wrap<l_open_folder>},
|
||||
{"quit", lua::wrap<l_quit>},
|
||||
{"capture_output", lua::wrap<l_capture_output>},
|
||||
{"__load_texture", lua::wrap<l_load_texture>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
#include "libs/api_lua.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
/// @brief Modified version of luaB_print from lbaselib.c
|
||||
int l_print(lua::State* L) {
|
||||
int n = lua::gettop(L); /* number of arguments */
|
||||
@ -16,10 +18,10 @@ int l_print(lua::State* L) {
|
||||
L,
|
||||
LUA_QL("tostring") " must return a string to " LUA_QL("print")
|
||||
);
|
||||
if (i > 1) std::cout << "\t";
|
||||
std::cout << s;
|
||||
if (i > 1) *output_stream << "\t";
|
||||
*output_stream << s;
|
||||
lua::pop(L); /* pop result */
|
||||
}
|
||||
std::cout << std::endl;
|
||||
*output_stream << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -34,6 +34,8 @@ static debug::Logger logger("scripting");
|
||||
|
||||
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;
|
||||
Level* scripting::level = nullptr;
|
||||
const Content* scripting::content = nullptr;
|
||||
|
||||
@ -42,6 +42,8 @@ namespace scripting {
|
||||
extern Level* level;
|
||||
extern BlocksController* blocks;
|
||||
extern LevelController* controller;
|
||||
extern std::ostream* output_stream;
|
||||
extern std::ostream* error_stream;
|
||||
|
||||
void initialize(Engine* engine);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user