diff --git a/src/app/commands/cmd_run_script.cpp b/src/app/commands/cmd_run_script.cpp index 23eb5ed2c..bc7762f78 100644 --- a/src/app/commands/cmd_run_script.cpp +++ b/src/app/commands/cmd_run_script.cpp @@ -31,7 +31,6 @@ public: void onConsolePrint(const char* text) override { m_console.printf("%s\n", text); } - private: Console m_console; }; @@ -69,12 +68,11 @@ void RunScriptCommand::onLoadParams(const Params& params) void RunScriptCommand::onExecute(Context* context) { script::Engine* engine = App::instance()->scriptEngine(); - - ConsoleEngineDelegate delegate; - engine->setDelegate(&delegate); - engine->evalFile(m_filename); - engine->setDelegate(nullptr); - + { + ConsoleEngineDelegate delegate; + script::ScopedEngineDelegate scoped(engine, &delegate); + engine->evalFile(m_filename); + } ui::Manager::getDefault()->invalidate(); } diff --git a/src/app/script/engine.h b/src/app/script/engine.h index 7fbc8e2d2..aa7ccb4ba 100644 --- a/src/app/script/engine.h +++ b/src/app/script/engine.h @@ -48,6 +48,7 @@ namespace app { Engine(); ~Engine(); + EngineDelegate* delegate() { return m_delegate; } void setDelegate(EngineDelegate* delegate) { m_delegate = delegate; } @@ -65,6 +66,21 @@ namespace app { bool m_printLastResult; }; + class ScopedEngineDelegate { + public: + ScopedEngineDelegate(Engine* engine, EngineDelegate* delegate) + : m_engine(engine), + m_oldDelegate(engine->delegate()) { + m_engine->setDelegate(delegate); + } + ~ScopedEngineDelegate() { + m_engine->setDelegate(m_oldDelegate); + } + private: + Engine* m_engine; + EngineDelegate* m_oldDelegate; + }; + int push_image_iterator_function(lua_State* L, const doc::ImageRef& image); void push_cel_image(lua_State* L, doc::Cel* cel); void push_sprite_cel(lua_State* L, doc::Cel* cel);