Avoid printing the last script result when we run a script from UI

This commit is contained in:
David Capello 2016-04-06 16:56:10 -03:00
parent 21921fdfd5
commit de8ef623a6
3 changed files with 13 additions and 2 deletions

View File

@ -696,6 +696,7 @@ void App::run()
if (m_isShell) {
StdoutEngineDelegate delegate;
AppScripting engine(&delegate);
engine.printLastResult();
Shell shell;
shell.run(engine);
}

View File

@ -423,6 +423,7 @@ Engine::Engine(EngineDelegate* delegate)
(void*)this,
&on_fatal_handler))
, m_delegate(delegate)
, m_printLastResult(false)
{
// Set 'on_search_module' as the function to search modules with
// require('modulename') on JavaScript.
@ -437,6 +438,11 @@ Engine::~Engine()
duk_destroy_heap(m_ctx.handle());
}
void Engine::printLastResult()
{
m_printLastResult = true;
}
void Engine::eval(const std::string& jsCode)
{
try {
@ -444,7 +450,8 @@ void Engine::eval(const std::string& jsCode)
duk_eval_string(handle, jsCode.c_str());
if (!duk_is_null_or_undefined(handle, -1))
if (m_printLastResult &&
!duk_is_null_or_undefined(handle, -1)) {
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
duk_pop(handle);
@ -487,7 +494,8 @@ void Engine::evalFile(const std::string& file)
duk_push_string(handle, duk_to_string(handle, -1));
duk_eval_raw(handle, nullptr, 0, DUK_COMPILE_EVAL);
if (!duk_is_null_or_undefined(handle, -1))
if (m_printLastResult &&
!duk_is_null_or_undefined(handle, -1)) {
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
duk_pop(handle);

View File

@ -126,6 +126,7 @@ namespace script {
Engine(EngineDelegate* delegate);
~Engine();
void printLastResult();
void eval(const std::string& jsCode);
void evalFile(const std::string& file);
@ -136,6 +137,7 @@ namespace script {
private:
Context m_ctx;
EngineDelegate* m_delegate;
bool m_printLastResult;
};
}