This commit is contained in:
Julian Brost 2023-02-10 10:16:09 +01:00
parent 14d7ee2777
commit d113534856
5 changed files with 41 additions and 15 deletions

View File

@ -187,3 +187,4 @@ Namespace::Iterator icinga::end(const Namespace::Ptr& x)
return x->End(); return x->End();
} }
Namespace::Ptr l_IcingaNS = new Namespace(true); // 100% botched, just to work around some linker mess now

View File

@ -11,7 +11,10 @@ using namespace icinga;
boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames; boost::thread_specific_ptr<std::stack<ScriptFrame *> > 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 /* Ensure that this gets called with highest priority
* and wins against other static initializers in lib/icinga, etc. * 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([]() { INITIALIZE_ONCE_WITH_PRIORITY([]() {
Namespace::Ptr globalNS = ScriptGlobal::GetGlobals(); Namespace::Ptr globalNS = ScriptGlobal::GetGlobals();
l_SystemNS = new Namespace(true);
l_SystemNS->Set("PlatformKernel", Utility::GetPlatformKernel()); l_SystemNS->Set("PlatformKernel", Utility::GetPlatformKernel());
l_SystemNS->Set("PlatformKernelVersion", Utility::GetPlatformKernelVersion()); l_SystemNS->Set("PlatformKernelVersion", Utility::GetPlatformKernelVersion());
l_SystemNS->Set("PlatformName", Utility::GetPlatformName()); l_SystemNS->Set("PlatformName", Utility::GetPlatformName());
@ -31,7 +33,7 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() {
l_SystemNS->Set("BuildCompilerVersion", ICINGA_BUILD_COMPILER_VERSION); l_SystemNS->Set("BuildCompilerVersion", ICINGA_BUILD_COMPILER_VERSION);
globalNS->Set("System", l_SystemNS, true); globalNS->Set("System", l_SystemNS, true);
l_SystemNS->Set("Configuration", new Configuration()); l_SystemNS->Set("Configuration", g_SystemConfig);
l_StatsNS = new Namespace(true); l_StatsNS = new Namespace(true);
globalNS->Set("StatsFunctions", l_StatsNS, true); globalNS->Set("StatsFunctions", l_StatsNS, true);

View File

@ -9,7 +9,7 @@ using namespace icinga;
Type::Ptr Type::TypeInstance; Type::Ptr Type::TypeInstance;
static Namespace::Ptr l_TypesNS = new Namespace(true); Namespace::Ptr l_TypesNS = new Namespace(true);
INITIALIZE_ONCE_WITH_PRIORITY([]() { INITIALIZE_ONCE_WITH_PRIORITY([]() {
ScriptGlobal::GetGlobals()->Set("Types", l_TypesNS, true); ScriptGlobal::GetGlobals()->Set("Types", l_TypesNS, true);

View File

@ -14,6 +14,7 @@
#include "base/reference.hpp" #include "base/reference.hpp"
#include "base/namespace.hpp" #include "base/namespace.hpp"
#include "base/defer.hpp" #include "base/defer.hpp"
#include "base/configuration.hpp"
#include <boost/exception_ptr.hpp> #include <boost/exception_ptr.hpp>
#include <boost/exception/errinfo_nested_exception.hpp> #include <boost/exception/errinfo_nested_exception.hpp>
@ -102,12 +103,15 @@ const DebugInfo& DebuggableExpression::GetDebugInfo() const
VariableExpression::VariableExpression(String variable, std::vector<Expression::Ptr> imports, const DebugInfo& debugInfo) VariableExpression::VariableExpression(String variable, std::vector<Expression::Ptr> imports, const DebugInfo& debugInfo)
: DebuggableExpression(debugInfo), m_Variable(std::move(variable)), m_Imports(std::move(imports)) : DebuggableExpression(debugInfo), m_Variable(std::move(variable)), m_Imports(std::move(imports))
{ {
m_Imports.push_back(MakeIndexer(ScopeGlobal, "System").release()); // m_Imports.push_back(MakeIndexer(ScopeGlobal, "System").release());
m_Imports.push_back(new IndexerExpression(MakeIndexer(ScopeGlobal, "System"), MakeLiteral("Configuration"))); // 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, "Types").release());
m_Imports.push_back(MakeIndexer(ScopeGlobal, "Icinga").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 ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{ {
Value value; Value value;
@ -116,14 +120,21 @@ ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *d
return value; return value;
else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get<Object::Ptr>() && frame.Self.Get<Object::Ptr>()->GetOwnField(m_Variable, &value)) else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get<Object::Ptr>() && frame.Self.Get<Object::Ptr>()->GetOwnField(m_Variable, &value))
return 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; return value;
else else
return ScriptGlobal::Get(m_Variable); 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; *index = m_Variable;
if (frame.Locals && frame.Locals->Contains(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) if (dhint)
*dhint = nullptr; *dhint = nullptr;
} else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get<Object::Ptr>() && frame.Self.Get<Object::Ptr>()->HasOwnField(m_Variable)) { } else if (frame.Self.IsObject() && frame.Locals != frame.Self.Get<Object::Ptr>() &&
frame.Self.Get<Object::Ptr>()->HasOwnField(m_Variable)) {
*parent = frame.Self; *parent = frame.Self;
if (dhint && *dhint) if (dhint && *dhint)
*dhint = new DebugHint((*dhint)->GetChild(m_Variable)); *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; return true;
} else if (ScriptGlobal::Exists(m_Variable)) { } else if (ScriptGlobal::Exists(m_Variable)) {
*parent = ScriptGlobal::GetGlobals(); *parent = ScriptGlobal::GetGlobals();

View File

@ -29,7 +29,7 @@ REGISTER_TYPE(IcingaApplication);
/* Ensure that the priority is lower than the basic System namespace initialization in scriptframe.cpp. */ /* Ensure that the priority is lower than the basic System namespace initialization in scriptframe.cpp. */
INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, InitializePriority::InitIcingaApplication); INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, InitializePriority::InitIcingaApplication);
static Namespace::Ptr l_IcingaNS; extern Namespace::Ptr l_IcingaNS;
void IcingaApplication::StaticInitialize() void IcingaApplication::StaticInitialize()
{ {
@ -61,7 +61,6 @@ void IcingaApplication::StaticInitialize()
Namespace::Ptr globalNS = ScriptGlobal::GetGlobals(); Namespace::Ptr globalNS = ScriptGlobal::GetGlobals();
VERIFY(globalNS); VERIFY(globalNS);
l_IcingaNS = new Namespace(true);
globalNS->Set("Icinga", l_IcingaNS, true); globalNS->Set("Icinga", l_IcingaNS, true);
} }