]> granicus.if.org Git - icinga2/blob - lib/config/vmops.hpp
Fix missing location information for included files
[icinga2] / lib / config / vmops.hpp
1 /******************************************************************************
2 * Icinga 2                                                                   *
3 * Copyright (C) 2012-2014 Icinga Development Team (http://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 #ifndef VMOPS_H
21 #define VMOPS_H
22
23 #include "config/i2-config.hpp"
24 #include "config/expression.hpp"
25 #include "config/configitembuilder.hpp"
26 #include "config/applyrule.hpp"
27 #include "config/objectrule.hpp"
28 #include "base/debuginfo.hpp"
29 #include "base/array.hpp"
30 #include "base/dictionary.hpp"
31 #include "base/scriptfunction.hpp"
32 #include "base/scriptsignal.hpp"
33 #include "base/scriptvariable.hpp"
34 #include "base/scripterror.hpp"
35 #include "base/convert.hpp"
36 #include "base/objectlock.hpp"
37 #include <boost/foreach.hpp>
38 #include <map>
39 #include <vector>
40
41 namespace icinga
42 {
43
44 class VMOps
45 {
46 public:
47         static inline Value Variable(VMFrame& frame, const String& name)
48         {
49                 if (name == "this")
50                         return frame.Self;
51
52                 if (frame.Locals && frame.Locals->Contains(name))
53                         return frame.Locals->Get(name);
54                 else if (frame.Locals != frame.Self && HasField(frame.Self, name))
55                         return GetField(frame.Self, name);
56                 else
57                         return ScriptVariable::Get(name);
58         }
59
60         static inline Value FunctionCall(VMFrame& frame, const Value& funcName, const std::vector<Value>& arguments)
61         {
62                 ScriptFunction::Ptr func;
63
64                 if (funcName.IsObjectType<ScriptFunction>())
65                         func = funcName;
66                 else
67                         func = ScriptFunction::GetByName(funcName);
68
69                 if (!func)
70                         BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
71
72                 return func->Invoke(arguments);
73         }
74
75         static inline Value Indexer(VMFrame& frame, const std::vector<Expression *>& indexer)
76         {
77                 Value result = indexer[0]->Evaluate(frame);
78
79                 for (int i = 1; i < indexer.size(); i++) {
80                         if (result.IsEmpty())
81                                 return Empty;
82
83                         Value index = indexer[i]->Evaluate(frame);
84                         result = GetField(result, index);
85                 }
86
87                 return result;
88         }
89
90         static inline Value NewFunction(VMFrame& frame, const String& name, const std::vector<String>& args,
91             std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
92         {
93                 ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
94                     EvaluateClosedVars(frame, closedVars), expression));
95
96                 if (!name.IsEmpty())
97                         ScriptFunction::Register(name, func);
98
99                 return func;
100         }
101
102         static inline Value NewSlot(VMFrame& frame, const String& signal, const Value& slot)
103         {
104                 ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal);
105
106                 if (!sig)
107                         BOOST_THROW_EXCEPTION(ScriptError("Signal '" + signal + "' does not exist."));
108
109                 sig->AddSlot(boost::bind(SlotWrapper, slot, _1));
110
111                 return Empty;
112         }
113
114         static inline Value NewApply(VMFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
115                 const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
116                 const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
117         {
118                 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
119                     fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
120
121                 return Empty;
122         }
123
124         static inline Value NewObject(VMFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
125                 const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
126         {
127                 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
128
129                 String checkName = name;
130
131                 if (!abstract) {
132                         Type::Ptr ptype = Type::GetByName(type);
133
134                         NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
135
136                         if (nc)
137                                 checkName = nc->MakeName(name, Dictionary::Ptr());
138                 }
139
140                 if (!checkName.IsEmpty()) {
141                         ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
142
143                         if (oldItem) {
144                                 std::ostringstream msgbuf;
145                                 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
146                                 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
147                         }
148                 }
149
150                 item->SetType(type);
151
152                 if (name.FindFirstOf("!") != String::NPos) {
153                         std::ostringstream msgbuf;
154                         msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
155                         BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
156                 }
157
158                 item->SetName(name);
159
160                 item->AddExpression(new OwnedExpression(expression));
161                 item->SetAbstract(abstract);
162                 item->SetScope(EvaluateClosedVars(frame, closedVars));
163                 item->SetZone(zone);
164                 item->SetFilter(filter);
165                 item->Compile()->Register();
166
167                 return Empty;
168         }
169
170         static inline Value For(VMFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
171         {
172                 if (value.IsObjectType<Array>()) {
173                         if (!fvvar.IsEmpty())
174                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
175
176                         Array::Ptr arr = value;
177
178                         ObjectLock olock(arr);
179                         BOOST_FOREACH(const Value& value, arr) {
180                                 frame.Locals->Set(fkvar, value);
181                                 expression->Evaluate(frame);
182                         }
183                 } else if (value.IsObjectType<Dictionary>()) {
184                         if (fvvar.IsEmpty())
185                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
186
187                         Dictionary::Ptr dict = value;
188
189                         ObjectLock olock(dict);
190                         BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
191                                 frame.Locals->Set(fkvar, kv.first);
192                                 frame.Locals->Set(fvvar, kv.second);
193                                 expression->Evaluate(frame);
194                         }
195                 }
196                 else
197                         BOOST_THROW_EXCEPTION(ScriptError("Invalid type in __for expression: " + value.GetTypeName(), debugInfo));
198
199                 return Empty;
200         }
201
202         static inline bool HasField(const Object::Ptr& context, const String& field)
203         {
204                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
205
206                 if (dict)
207                         return dict->Contains(field);
208                 else {
209                         Type::Ptr type = context->GetReflectionType();
210
211                         if (!type)
212                                 return false;
213
214                         return type->GetFieldId(field) != -1;
215                 }
216         }
217
218         static inline Value GetField(const Object::Ptr& context, const String& field)
219         {
220                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
221
222                 if (dict)
223                         return dict->Get(field);
224
225                 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
226
227                 if (arr) {
228                         int index = Convert::ToLong(field);
229                         return arr->Get(index);
230                 }
231
232                 Type::Ptr type = context->GetReflectionType();
233
234                 if (!type)
235                         return Empty;
236
237                 int fid = type->GetFieldId(field);
238
239                 if (fid == -1)
240                         return Empty;
241
242                 return context->GetField(fid);
243         }
244
245         static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
246         {
247                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
248
249                 if (dict) {
250                         dict->Set(field, value);
251                         return;
252                 }
253
254                 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
255
256                 if (arr) {
257                         int index = Convert::ToLong(field);
258                         if (index >= arr->GetLength())
259                                 arr->Resize(index + 1);
260                         arr->Set(index, value);
261                         return;
262                 }
263
264                 Type::Ptr type = context->GetReflectionType();
265
266                 if (!type)
267                         BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
268
269                 int fid = type->GetFieldId(field);
270
271                 if (fid == -1)
272                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
273
274                 try {
275                         context->SetField(fid, value);
276                 } catch (const boost::bad_lexical_cast&) {
277                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
278                 } catch (const std::bad_cast&) {
279                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
280                 }
281         }
282
283 private:
284         static inline Value FunctionWrapper(const std::vector<Value>& arguments,
285             const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
286         {
287                 if (arguments.size() < funcargs.size())
288                         BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
289
290                 VMFrame frame;
291
292                 if (closedVars)
293                         closedVars->CopyTo(frame.Locals);
294
295                 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
296                         frame.Locals->Set(funcargs[i], arguments[i]);
297
298                 Value result;
299                 try {
300                         result = expr->Evaluate(frame);
301                 } catch (const InterruptExecutionError& iee) {
302                         result = iee.GetResult();
303                 }
304
305                 return result;
306         }
307
308         static inline void SlotWrapper(const Value& funcName, const std::vector<Value>& arguments)
309         {
310                 ScriptFunction::Ptr func;
311
312                 if (funcName.IsObjectType<ScriptFunction>())
313                         func = funcName;
314                 else
315                         func = ScriptFunction::GetByName(funcName);
316
317                 if (!func)
318                         BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
319
320                 func->Invoke(arguments);
321         }
322
323         static inline Dictionary::Ptr EvaluateClosedVars(VMFrame& frame, std::map<String, Expression *> *closedVars)
324         {
325                 Dictionary::Ptr locals;
326
327                 if (closedVars) {
328                         locals = new Dictionary();
329
330                         typedef std::pair<String, Expression *> ClosedVar;
331                         BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
332                                 locals->Set(cvar.first, cvar.second->Evaluate(frame));
333                         }
334                 }
335
336                 return locals;
337         }
338 };
339
340 }
341
342 #endif /* VMOPS_H */