diff --git a/lib/base/namespace.cpp b/lib/base/namespace.cpp index cb7c872bd..950eee926 100644 --- a/lib/base/namespace.cpp +++ b/lib/base/namespace.cpp @@ -187,3 +187,4 @@ Namespace::Iterator icinga::end(const Namespace::Ptr& x) return x->End(); } +Namespace::Ptr l_IcingaNS = new Namespace(true); // 100% botched, just to work around some linker mess now diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index 7476f13e6..a0d24ba5b 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -11,7 +11,10 @@ using namespace icinga; boost::thread_specific_ptr > ScriptFrame::m_ScriptFrames; -static Namespace::Ptr l_SystemNS, l_StatsNS; +Namespace::Ptr l_SystemNS = new Namespace(true); +Configuration::Ptr g_SystemConfig = new Configuration(); + +static Namespace::Ptr l_StatsNS; /* Ensure that this gets called with highest priority * and wins against other static initializers in lib/icinga, etc. @@ -20,7 +23,6 @@ static Namespace::Ptr l_SystemNS, l_StatsNS; INITIALIZE_ONCE_WITH_PRIORITY([]() { Namespace::Ptr globalNS = ScriptGlobal::GetGlobals(); - l_SystemNS = new Namespace(true); l_SystemNS->Set("PlatformKernel", Utility::GetPlatformKernel()); l_SystemNS->Set("PlatformKernelVersion", Utility::GetPlatformKernelVersion()); l_SystemNS->Set("PlatformName", Utility::GetPlatformName()); @@ -31,7 +33,7 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { l_SystemNS->Set("BuildCompilerVersion", ICINGA_BUILD_COMPILER_VERSION); globalNS->Set("System", l_SystemNS, true); - l_SystemNS->Set("Configuration", new Configuration()); + l_SystemNS->Set("Configuration", g_SystemConfig); l_StatsNS = new Namespace(true); globalNS->Set("StatsFunctions", l_StatsNS, true); diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 14794cb91..cceb695ce 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -9,7 +9,7 @@ using namespace icinga; Type::Ptr Type::TypeInstance; -static Namespace::Ptr l_TypesNS = new Namespace(true); +Namespace::Ptr l_TypesNS = new Namespace(true); INITIALIZE_ONCE_WITH_PRIORITY([]() { ScriptGlobal::GetGlobals()->Set("Types", l_TypesNS, true); diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index a8e99861a..b4911cf18 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -14,6 +14,7 @@ #include "base/reference.hpp" #include "base/namespace.hpp" #include "base/defer.hpp" +#include "base/configuration.hpp" #include #include @@ -102,12 +103,15 @@ const DebugInfo& DebuggableExpression::GetDebugInfo() const VariableExpression::VariableExpression(String variable, std::vector imports, const DebugInfo& debugInfo) : DebuggableExpression(debugInfo), m_Variable(std::move(variable)), m_Imports(std::move(imports)) { - m_Imports.push_back(MakeIndexer(ScopeGlobal, "System").release()); - m_Imports.push_back(new IndexerExpression(MakeIndexer(ScopeGlobal, "System"), MakeLiteral("Configuration"))); - m_Imports.push_back(MakeIndexer(ScopeGlobal, "Types").release()); - m_Imports.push_back(MakeIndexer(ScopeGlobal, "Icinga").release()); +// m_Imports.push_back(MakeIndexer(ScopeGlobal, "System").release()); +// m_Imports.push_back(new IndexerExpression(MakeIndexer(ScopeGlobal, "System"), MakeLiteral("Configuration"))); +// m_Imports.push_back(MakeIndexer(ScopeGlobal, "Types").release()); +// m_Imports.push_back(MakeIndexer(ScopeGlobal, "Icinga").release()); } +extern Namespace::Ptr l_SystemNS, l_TypesNS, l_IcingaNS; +extern Configuration::Ptr g_SystemConfig; + ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { Value value; @@ -116,14 +120,21 @@ ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *d return value; else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get() && frame.Self.Get()->GetOwnField(m_Variable, &value)) return value; - else if (VMOps::FindVarImport(frame, m_Imports, m_Variable, &value, m_DebugInfo)) + else if (!m_Imports.empty() && VMOps::FindVarImport(frame, m_Imports, m_Variable, &value, m_DebugInfo)) + return value; + else if (l_SystemNS->Get(m_Variable, &value)) + return value; + else if (g_SystemConfig->GetOwnField(m_Variable, &value)) + return value; + else if (l_TypesNS->Get(m_Variable, &value)) + return value; + else if (l_IcingaNS->Get(m_Variable, &value)) return value; else return ScriptGlobal::Get(m_Variable); } -bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const -{ +bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const { *index = m_Variable; if (frame.Locals && frame.Locals->Contains(m_Variable)) { @@ -131,12 +142,25 @@ bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value if (dhint) *dhint = nullptr; - } else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get() && frame.Self.Get()->HasOwnField(m_Variable)) { + } else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get() && + frame.Self.Get()->HasOwnField(m_Variable)) { *parent = frame.Self; if (dhint && *dhint) *dhint = new DebugHint((*dhint)->GetChild(m_Variable)); - } else if (VMOps::FindVarImportRef(frame, m_Imports, m_Variable, parent, m_DebugInfo)) { + } else if (!m_Imports.empty() && VMOps::FindVarImportRef(frame, m_Imports, m_Variable, parent, m_DebugInfo)) { + return true; + } else if (l_SystemNS->Contains(m_Variable)) { + *parent = l_SystemNS; + return true; + } else if (g_SystemConfig->HasOwnField(m_Variable)) { + *parent = g_SystemConfig; + return true; + } else if (l_TypesNS->Contains(m_Variable)) { + *parent = l_TypesNS; + return true; + } else if (l_IcingaNS->Contains(m_Variable)) { + *parent = l_IcingaNS; return true; } else if (ScriptGlobal::Exists(m_Variable)) { *parent = ScriptGlobal::GetGlobals(); diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index 239fb985c..1830992c1 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -29,7 +29,7 @@ REGISTER_TYPE(IcingaApplication); /* Ensure that the priority is lower than the basic System namespace initialization in scriptframe.cpp. */ INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, InitializePriority::InitIcingaApplication); -static Namespace::Ptr l_IcingaNS; +extern Namespace::Ptr l_IcingaNS; void IcingaApplication::StaticInitialize() { @@ -61,7 +61,6 @@ void IcingaApplication::StaticInitialize() Namespace::Ptr globalNS = ScriptGlobal::GetGlobals(); VERIFY(globalNS); - l_IcingaNS = new Namespace(true); globalNS->Set("Icinga", l_IcingaNS, true); }