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