1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/scriptframe.hpp"
4 #include "base/scriptglobal.hpp"
5 #include "base/namespace.hpp"
6 #include "base/exception.hpp"
7 #include "base/configuration.hpp"
9 using namespace icinga;
11 boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
13 static auto l_InternalNSBehavior = new ConstNamespaceBehavior();
15 /* Ensure that this gets called with highest priority
16 * and wins against other static initializers in lib/icinga, etc.
17 * LTO-enabled builds will cause trouble otherwise, see GH #6575.
19 INITIALIZE_ONCE_WITH_PRIORITY([]() {
20 Namespace::Ptr globalNS = ScriptGlobal::GetGlobals();
22 auto systemNSBehavior = new ConstNamespaceBehavior();
23 systemNSBehavior->Freeze();
24 Namespace::Ptr systemNS = new Namespace(systemNSBehavior);
25 globalNS->SetAttribute("System", std::make_shared<ConstEmbeddedNamespaceValue>(systemNS));
27 systemNS->SetAttribute("Configuration", std::make_shared<EmbeddedNamespaceValue>(new Configuration()));
29 auto typesNSBehavior = new ConstNamespaceBehavior();
30 typesNSBehavior->Freeze();
31 Namespace::Ptr typesNS = new Namespace(typesNSBehavior);
32 globalNS->SetAttribute("Types", std::make_shared<ConstEmbeddedNamespaceValue>(typesNS));
34 auto statsNSBehavior = new ConstNamespaceBehavior();
35 statsNSBehavior->Freeze();
36 Namespace::Ptr statsNS = new Namespace(statsNSBehavior);
37 globalNS->SetAttribute("StatsFunctions", std::make_shared<ConstEmbeddedNamespaceValue>(statsNS));
39 Namespace::Ptr internalNS = new Namespace(l_InternalNSBehavior);
40 globalNS->SetAttribute("Internal", std::make_shared<ConstEmbeddedNamespaceValue>(internalNS));
43 INITIALIZE_ONCE_WITH_PRIORITY([]() {
44 l_InternalNSBehavior->Freeze();
47 ScriptFrame::ScriptFrame(bool allocLocals)
48 : Locals(allocLocals ? new Dictionary() : nullptr), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0)
53 ScriptFrame::ScriptFrame(bool allocLocals, Value self)
54 : Locals(allocLocals ? new Dictionary() : nullptr), Self(std::move(self)), Sandboxed(false), Depth(0)
59 void ScriptFrame::InitializeFrame()
61 std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
63 if (frames && !frames->empty()) {
64 ScriptFrame *frame = frames->top();
66 Sandboxed = frame->Sandboxed;
72 ScriptFrame::~ScriptFrame()
74 ScriptFrame *frame = PopFrame();
75 ASSERT(frame == this);
82 void ScriptFrame::IncreaseStackDepth()
85 BOOST_THROW_EXCEPTION(ScriptError("Stack overflow while evaluating expression: Recursion level too deep."));
90 void ScriptFrame::DecreaseStackDepth()
95 ScriptFrame *ScriptFrame::GetCurrentFrame()
97 std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
99 ASSERT(!frames->empty());
100 return frames->top();
103 ScriptFrame *ScriptFrame::PopFrame()
105 std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
107 ASSERT(!frames->empty());
109 ScriptFrame *frame = frames->top();
115 void ScriptFrame::PushFrame(ScriptFrame *frame)
117 std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
120 frames = new std::stack<ScriptFrame *>();
121 m_ScriptFrames.reset(frames);
124 if (!frames->empty()) {
125 ScriptFrame *parent = frames->top();
126 frame->Depth += parent->Depth;