icinga2/lib/base/scriptframe.cpp
Julian Brost fbb68dbcd0 Namespace: replace behavior classes with a bool
In essence, namespace behaviors acted as hooks for update operations on
namespaces. Two behaviors were implemented:

- `NamespaceBehavior`: allows the update operation unless it acts on a value
  that itself was explicitly marked as constant.
- `ConstNamespaceBehavior`: initially allows insert operations but marks the
  individual values as const. Additionally provides a `Freeze()` member
  function. After this was called, updates are rejected unless a special
  `overrideFrozen` flag is set explicitly.

This marvel of object-oriented programming can be replaced with a simple bool.
This commit basically replaces `Namespace::m_Behavior` with
`Namespace::m_ConstValues` and inlines the behavior functions where they were
called. While doing so, the code was slightly simplified by assuming that
`m_ConstValues` is true if `m_Frozen` is true. This is similar to what the API
allowed in the old code as you could only freeze a `ConstNamespaceBehavior`.
However, this PR moves the `Freeze()` member function and the related
`m_Freeze` member variable to the `Namespace` class. So now the API allows any
namespace to be frozen. The new code also makes sense with the previously
mentioned simplification: a `Namespace` with `m_ConstValues = false` can be
modified without restrictions until `Freeze()` is called. When this is done, it
becomes read-only.

The changes outside of `namespace.*` just adapt the code to the slightly
changed API.
2022-12-09 09:25:46 +01:00

128 lines
3.0 KiB
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/scriptframe.hpp"
#include "base/scriptglobal.hpp"
#include "base/namespace.hpp"
#include "base/exception.hpp"
#include "base/configuration.hpp"
using namespace icinga;
boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
static Namespace::Ptr l_InternalNS;
/* Ensure that this gets called with highest priority
* and wins against other static initializers in lib/icinga, etc.
* LTO-enabled builds will cause trouble otherwise, see GH #6575.
*/
INITIALIZE_ONCE_WITH_PRIORITY([]() {
Namespace::Ptr globalNS = ScriptGlobal::GetGlobals();
Namespace::Ptr systemNS = new Namespace(true);
systemNS->Freeze();
globalNS->SetAttribute("System", new ConstEmbeddedNamespaceValue(systemNS));
systemNS->SetAttribute("Configuration", new EmbeddedNamespaceValue(new Configuration()));
Namespace::Ptr typesNS = new Namespace(true);
typesNS->Freeze();
globalNS->SetAttribute("Types", new ConstEmbeddedNamespaceValue(typesNS));
Namespace::Ptr statsNS = new Namespace(true);
statsNS->Freeze();
globalNS->SetAttribute("StatsFunctions", new ConstEmbeddedNamespaceValue(statsNS));
l_InternalNS = new Namespace(true);
globalNS->SetAttribute("Internal", new ConstEmbeddedNamespaceValue(l_InternalNS));
}, 1000);
INITIALIZE_ONCE_WITH_PRIORITY([]() {
l_InternalNS->Freeze();
}, 0);
ScriptFrame::ScriptFrame(bool allocLocals)
: Locals(allocLocals ? new Dictionary() : nullptr), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0)
{
InitializeFrame();
}
ScriptFrame::ScriptFrame(bool allocLocals, Value self)
: Locals(allocLocals ? new Dictionary() : nullptr), Self(std::move(self)), Sandboxed(false), Depth(0)
{
InitializeFrame();
}
void ScriptFrame::InitializeFrame()
{
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
if (frames && !frames->empty()) {
ScriptFrame *frame = frames->top();
Sandboxed = frame->Sandboxed;
}
PushFrame(this);
}
ScriptFrame::~ScriptFrame()
{
ScriptFrame *frame = PopFrame();
ASSERT(frame == this);
#ifndef I2_DEBUG
(void)frame;
#endif /* I2_DEBUG */
}
void ScriptFrame::IncreaseStackDepth()
{
if (Depth + 1 > 300)
BOOST_THROW_EXCEPTION(ScriptError("Stack overflow while evaluating expression: Recursion level too deep."));
Depth++;
}
void ScriptFrame::DecreaseStackDepth()
{
Depth--;
}
ScriptFrame *ScriptFrame::GetCurrentFrame()
{
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
ASSERT(!frames->empty());
return frames->top();
}
ScriptFrame *ScriptFrame::PopFrame()
{
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
ASSERT(!frames->empty());
ScriptFrame *frame = frames->top();
frames->pop();
return frame;
}
void ScriptFrame::PushFrame(ScriptFrame *frame)
{
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
if (!frames) {
frames = new std::stack<ScriptFrame *>();
m_ScriptFrames.reset(frames);
}
if (!frames->empty()) {
ScriptFrame *parent = frames->top();
frame->Depth += parent->Depth;
}
frames->push(frame);
}