From b74014fa5e1ec3c4225b04e113e9b74c1afbcaef Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 12 Aug 2016 15:36:47 +0200 Subject: [PATCH] Fix auto-completion suggestions for "icinga2 console" refs #12408 --- lib/base/scriptframe.cpp | 19 +++++++++++++------ lib/base/scriptframe.hpp | 2 ++ lib/remote/consolehandler.cpp | 14 +++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index 8f4fa800f..f2b14211d 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -26,6 +26,19 @@ using namespace icinga; boost::thread_specific_ptr > ScriptFrame::m_ScriptFrames; Array::Ptr ScriptFrame::m_Imports; +INITIALIZE_ONCE_WITH_PRIORITY(&ScriptFrame::StaticInitialize, 50); + +void ScriptFrame::StaticInitialize(void) +{ + Dictionary::Ptr systemNS = new Dictionary(); + ScriptGlobal::Set("System", systemNS); + AddImport(systemNS); + + Dictionary::Ptr deprecatedNS = new Dictionary(); + ScriptGlobal::Set("Deprecated", deprecatedNS); + AddImport(deprecatedNS); +} + ScriptFrame::ScriptFrame(void) : Locals(new Dictionary()), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0) { @@ -109,12 +122,6 @@ void ScriptFrame::PushFrame(ScriptFrame *frame) Array::Ptr ScriptFrame::GetImports(void) { - if (!m_Imports) { - m_Imports = new Array(); - m_Imports->Add(ScriptGlobal::Get("System")); - m_Imports->Add(ScriptGlobal::Get("Deprecated")); - } - return m_Imports; } diff --git a/lib/base/scriptframe.hpp b/lib/base/scriptframe.hpp index e60b40022..e980a1bd5 100644 --- a/lib/base/scriptframe.hpp +++ b/lib/base/scriptframe.hpp @@ -40,6 +40,8 @@ struct I2_BASE_API ScriptFrame ScriptFrame(const Value& self); ~ScriptFrame(void); + static void StaticInitialize(void); + void IncreaseStackDepth(void); void DecreaseStackDepth(void); diff --git a/lib/remote/consolehandler.cpp b/lib/remote/consolehandler.cpp index ac99b038d..d7b1321c6 100644 --- a/lib/remote/consolehandler.cpp +++ b/lib/remote/consolehandler.cpp @@ -215,7 +215,7 @@ static void AddSuggestion(std::vector& matches, const String& word, cons matches.push_back(suggestion); } -static void AddSuggestions(std::vector& matches, const String& word, const String& pword, bool withPrototype, const Value& value) +static void AddSuggestions(std::vector& matches, const String& word, const String& pword, bool withFields, const Value& value) { String prefix; @@ -231,15 +231,15 @@ static void AddSuggestions(std::vector& matches, const String& word, con } } - Type::Ptr type = value.GetReflectionType(); + if (withFields) { + Type::Ptr type = value.GetReflectionType(); - for (int i = 0; i < type->GetFieldCount(); i++) { - Field field = type->GetFieldInfo(i); + for (int i = 0; i < type->GetFieldCount(); i++) { + Field field = type->GetFieldInfo(i); - AddSuggestion(matches, word, prefix + field.Name); - } + AddSuggestion(matches, word, prefix + field.Name); + } - if (withPrototype) { while (type) { Object::Ptr prototype = type->GetPrototype(); Dictionary::Ptr dict = dynamic_pointer_cast(prototype);