]> granicus.if.org Git - icinga2/blob - lib/config/vmops.hpp
Refactor the stack frame handling for scripts
[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/configerror.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(ConfigError("Function '" + funcName + "' does not exist."));
71
72                 return func->Invoke(arguments);
73         }
74
75         static inline Value Indexer(VMFrame& frame, const Value& value, const String& index)
76         {
77                 if (value.IsObjectType<Dictionary>()) {
78                         Dictionary::Ptr dict = value;
79                         return dict->Get(index);
80                 } else if (value.IsObjectType<Array>()) {
81                         Array::Ptr arr = value;
82                         return arr->Get(Convert::ToLong(index));
83                 } else if (value.IsObject()) {
84                         Object::Ptr object = value;
85                         Type::Ptr type = object->GetReflectionType();
86
87                         if (!type)
88                                 BOOST_THROW_EXCEPTION(ConfigError("Dot operator applied to object which does not support reflection"));
89
90                         int field = type->GetFieldId(index);
91
92                         if (field == -1)
93                                 BOOST_THROW_EXCEPTION(ConfigError("Tried to access invalid property '" + index + "'"));
94
95                         return object->GetField(field);
96                 } else if (value.IsEmpty()) {
97                         return Empty;
98                 } else {
99                         BOOST_THROW_EXCEPTION(ConfigError("Dot operator cannot be applied to type '" + value.GetTypeName() + "'"));
100                 }
101         }
102
103         static inline Value NewFunction(VMFrame& frame, const String& name, const std::vector<String>& args,
104             std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
105         {
106                 ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
107                     EvaluateClosedVars(frame, closedVars), expression));
108
109                 if (!name.IsEmpty())
110                         ScriptFunction::Register(name, func);
111
112                 return func;
113         }
114
115         static inline Value NewSlot(VMFrame& frame, const String& signal, const Value& slot)
116         {
117                 ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal);
118
119                 if (!sig)
120                         BOOST_THROW_EXCEPTION(ConfigError("Signal '" + signal + "' does not exist."));
121
122                 sig->AddSlot(boost::bind(SlotWrapper, slot, _1));
123
124                 return Empty;
125         }
126
127         static inline Value NewApply(VMFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
128                 const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
129                 const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
130         {
131                 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
132                     fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
133
134                 return Empty;
135         }
136
137         static inline Value NewObject(VMFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
138                 const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
139         {
140                 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
141
142                 String checkName = name;
143
144                 if (!abstract) {
145                         Type::Ptr ptype = Type::GetByName(type);
146
147                         NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
148
149                         if (nc)
150                                 checkName = nc->MakeName(name, Dictionary::Ptr());
151                 }
152
153                 if (!checkName.IsEmpty()) {
154                         ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
155
156                         if (oldItem) {
157                                 std::ostringstream msgbuf;
158                                 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
159                                 BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(debugInfo));
160                         }
161                 }
162
163                 item->SetType(type);
164
165                 if (name.FindFirstOf("!") != String::NPos) {
166                         std::ostringstream msgbuf;
167                         msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
168                         BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(debugInfo));
169                 }
170
171                 item->SetName(name);
172
173                 item->AddExpression(new OwnedExpression(expression));
174                 item->SetAbstract(abstract);
175                 item->SetScope(EvaluateClosedVars(frame, closedVars));
176                 item->SetZone(zone);
177                 item->SetFilter(filter);
178                 item->Compile()->Register();
179
180                 return Empty;
181         }
182
183         static inline Value For(VMFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
184         {
185                 if (value.IsObjectType<Array>()) {
186                         if (!fvvar.IsEmpty())
187                                 BOOST_THROW_EXCEPTION(ConfigError("Cannot use dictionary iterator for array.") << errinfo_debuginfo(debugInfo));
188
189                         Array::Ptr arr = value;
190
191                         ObjectLock olock(arr);
192                         BOOST_FOREACH(const Value& value, arr) {
193                                 frame.Locals->Set(fkvar, value);
194                                 expression->Evaluate(frame);
195                         }
196                 } else if (value.IsObjectType<Dictionary>()) {
197                         if (fvvar.IsEmpty())
198                                 BOOST_THROW_EXCEPTION(ConfigError("Cannot use array iterator for dictionary.") << errinfo_debuginfo(debugInfo));
199
200                         Dictionary::Ptr dict = value;
201
202                         ObjectLock olock(dict);
203                         BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
204                                 frame.Locals->Set(fkvar, kv.first);
205                                 frame.Locals->Set(fvvar, kv.second);
206                                 expression->Evaluate(frame);
207                         }
208                 }
209                 else
210                         BOOST_THROW_EXCEPTION(ConfigError("Invalid type in __for expression: " + value.GetTypeName()) << errinfo_debuginfo(debugInfo));
211
212                 return Empty;
213         }
214
215         static inline bool HasField(const Object::Ptr& context, const String& field)
216         {
217                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
218
219                 if (dict)
220                         return dict->Contains(field);
221                 else {
222                         Type::Ptr type = context->GetReflectionType();
223
224                         if (!type)
225                                 return false;
226
227                         return type->GetFieldId(field) != -1;
228                 }
229         }
230
231         static inline Value GetField(const Object::Ptr& context, const String& field)
232         {
233                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
234
235                 if (dict)
236                         return dict->Get(field);
237                 else {
238                         Type::Ptr type = context->GetReflectionType();
239
240                         if (!type)
241                                 return Empty;
242
243                         int fid = type->GetFieldId(field);
244
245                         if (fid == -1)
246                                 return Empty;
247
248                         return context->GetField(fid);
249                 }
250         }
251
252         static inline void SetField(const Object::Ptr& context, const String& field, const Value& value)
253         {
254                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
255
256                 if (dict)
257                         dict->Set(field, value);
258                 else {
259                         Type::Ptr type = context->GetReflectionType();
260
261                         if (!type)
262                                 BOOST_THROW_EXCEPTION(ConfigError("Cannot set field on object."));
263
264                         int fid = type->GetFieldId(field);
265
266                         if (fid == -1)
267                                 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' does not exist."));
268
269                         try {
270                                 context->SetField(fid, value);
271                         } catch (const boost::bad_lexical_cast&) {
272                                 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
273                         } catch (const std::bad_cast&) {
274                                 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
275                         }
276                 }
277         }
278
279 private:
280         static inline Value FunctionWrapper(const std::vector<Value>& arguments,
281             const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
282         {
283                 if (arguments.size() < funcargs.size())
284                         BOOST_THROW_EXCEPTION(ConfigError("Too few arguments for function"));
285
286                 VMFrame frame;
287
288                 if (closedVars)
289                         closedVars->CopyTo(frame.Locals);
290
291                 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
292                         frame.Locals->Set(funcargs[i], arguments[i]);
293
294                 expr->Evaluate(frame);
295                 return frame.Result;
296         }
297
298         static inline void SlotWrapper(const Value& funcName, const std::vector<Value>& arguments)
299         {
300                 ScriptFunction::Ptr func;
301
302                 if (funcName.IsObjectType<ScriptFunction>())
303                         func = funcName;
304                 else
305                         func = ScriptFunction::GetByName(funcName);
306
307                 if (!func)
308                         BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
309
310                 func->Invoke(arguments);
311         }
312
313         static inline Dictionary::Ptr EvaluateClosedVars(VMFrame& frame, std::map<String, Expression *> *closedVars)
314         {
315                 Dictionary::Ptr locals;
316
317                 if (closedVars) {
318                         locals = new Dictionary();
319
320                         typedef std::pair<String, Expression *> ClosedVar;
321                         BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
322                                 locals->Set(cvar.first, cvar.second->Evaluate(frame));
323                         }
324                 }
325
326                 return locals;
327         }
328 };
329
330 }
331
332 #endif /* VMOPS_H */