]> granicus.if.org Git - icinga2/blob - lib/base/scriptframe.cpp
Update copyright headers for 2016
[icinga2] / lib / base / scriptframe.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/scriptframe.hpp"
21 #include "base/scriptglobal.hpp"
22 #include "base/exception.hpp"
23
24 using namespace icinga;
25
26 boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
27
28 ScriptFrame::ScriptFrame(void)
29         : Locals(new Dictionary()), Self(ScriptGlobal::GetGlobals()), Sandboxed(false)
30 {
31         PushFrame(this);
32 }
33
34 ScriptFrame::ScriptFrame(const Value& self)
35         : Locals(new Dictionary()), Self(self), Sandboxed(false)
36 {
37         PushFrame(this);
38 }
39
40 ScriptFrame::~ScriptFrame(void)
41 {
42         ScriptFrame *frame = PopFrame();
43         ASSERT(frame == this);
44 }
45
46 ScriptFrame *ScriptFrame::GetCurrentFrame(void)
47 {
48         std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
49
50         ASSERT(!frames->empty());
51         return frames->top();
52 }
53
54 ScriptFrame *ScriptFrame::PopFrame(void)
55 {
56         std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
57
58         ASSERT(!frames->empty());
59
60         ScriptFrame *frame = frames->top();
61         frames->pop();
62
63         return frame;
64 }
65
66 void ScriptFrame::PushFrame(ScriptFrame *frame)
67 {
68         std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
69
70         if (!frames) {
71                 frames = new std::stack<ScriptFrame *>();
72                 m_ScriptFrames.reset(frames);
73         }
74
75         if (frames->size() > 500)
76                 BOOST_THROW_EXCEPTION(ScriptError("Recursion level too deep."));
77
78         frames->push(frame);
79 }