]> granicus.if.org Git - icinga2/blob - lib/config/vmops.hpp
Implement the DateTime class
[icinga2] / lib / config / vmops.hpp
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 #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/function.hpp"
32 #include "base/scriptglobal.hpp"
33 #include "base/exception.hpp"
34 #include "base/convert.hpp"
35 #include "base/objectlock.hpp"
36 #include <boost/foreach.hpp>
37 #include <boost/smart_ptr/make_shared.hpp>
38 #include <map>
39 #include <vector>
40
41 namespace icinga
42 {
43
44 class VMOps
45 {
46 public:
47         static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
48         {
49                 Value value;
50                 if (frame.Locals && frame.Locals->Get(name, &value))
51                         return value;
52                 else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
53                         return GetField(frame.Self, name, frame.Sandboxed, debugInfo);
54                 else
55                         return ScriptGlobal::Get(name);
56         }
57
58         static inline Value ConstructorCall(const Type::Ptr& type, const std::vector<Value>& args, const DebugInfo& debugInfo = DebugInfo())
59         {
60                 if (type->GetName() == "String") {
61                         if (args.empty())
62                                 return "";
63                         else if (args.size() == 1)
64                                 return Convert::ToString(args[0]);
65                         else
66                                 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
67                 } else if (type->GetName() == "Number") {
68                         if (args.empty())
69                                 return 0;
70                         else if (args.size() == 1)
71                                 return Convert::ToDouble(args[0]);
72                         else
73                                 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
74                 } else if (type->GetName() == "Boolean") {
75                         if (args.empty())
76                                 return 0;
77                         else if (args.size() == 1)
78                                 return Convert::ToBool(args[0]);
79                         else
80                                 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
81                 } else if (args.size() == 1 && type->IsAssignableFrom(args[0].GetReflectionType()))
82                         return args[0];
83                 else
84                         return type->Instantiate(args);
85         }
86
87         static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
88         {
89                 ScriptFrame vframe;
90                 
91                 if (!self.IsEmpty() || self.IsString())
92                         vframe.Self = self;
93
94                 return func->Invoke(arguments);
95         }
96
97         static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args,
98             std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
99         {
100                 return new Function(boost::bind(&FunctionWrapper, _1, args,
101                     EvaluateClosedVars(frame, closedVars), expression));
102         }
103
104         static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
105                 const String& package, const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
106                 bool ignoreOnError, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
107         {
108                 ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
109                     fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
110
111                 return Empty;
112         }
113
114         static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
115                 const String& zone, const String& package, bool ignoreOnError, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
116         {
117                 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
118
119                 String checkName = name;
120
121                 if (!abstract) {
122                         Type::Ptr ptype = Type::GetByName(type);
123
124                         NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
125
126                         if (nc)
127                                 checkName = nc->MakeName(name, Dictionary::Ptr());
128                 }
129
130                 if (!checkName.IsEmpty()) {
131                         ConfigItem::Ptr oldItem = ConfigItem::GetByTypeAndName(type, checkName);
132
133                         if (oldItem) {
134                                 std::ostringstream msgbuf;
135                                 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
136                                 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
137                         }
138                 }
139
140                 item->SetType(type);
141                 item->SetName(name);
142
143                 item->AddExpression(new OwnedExpression(expression));
144                 item->SetAbstract(abstract);
145                 item->SetScope(EvaluateClosedVars(frame, closedVars));
146                 item->SetZone(zone);
147                 item->SetPackage(package);
148                 item->SetFilter(filter);
149                 item->SetIgnoreOnError(ignoreOnError);
150                 item->Compile()->Register();
151
152                 return Empty;
153         }
154
155         static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
156         {
157                 if (value.IsObjectType<Array>()) {
158                         if (!fvvar.IsEmpty())
159                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
160
161                         Array::Ptr arr = value;
162
163                         for (Array::SizeType i = 0; i < arr->GetLength(); i++) {
164                                 frame.Locals->Set(fkvar, arr->Get(i));
165                                 ExpressionResult res = expression->Evaluate(frame);
166                                 CHECK_RESULT_LOOP(res);
167                         }
168                 } else if (value.IsObjectType<Dictionary>()) {
169                         if (fvvar.IsEmpty())
170                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
171
172                         Dictionary::Ptr dict = value;
173                         std::vector<String> keys;
174
175                         {
176                                 ObjectLock olock(dict);
177                                 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
178                                         keys.push_back(kv.first);
179                                 }
180                         }
181
182                         BOOST_FOREACH(const String& key, keys) {
183                                 frame.Locals->Set(fkvar, key);
184                                 frame.Locals->Set(fvvar, dict->Get(key));
185                                 ExpressionResult res = expression->Evaluate(frame);
186                                 CHECK_RESULT_LOOP(res);
187                         }
188                 } else
189                         BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
190
191                 return Empty;
192         }
193
194         static inline bool HasField(const Object::Ptr& context, const String& field)
195         {
196                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
197
198                 if (dict)
199                         return dict->Contains(field);
200                 else {
201                         Type::Ptr type = context->GetReflectionType();
202
203                         if (!type)
204                                 return false;
205
206                         return type->GetFieldId(field) != -1;
207                 }
208         }
209
210         static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
211         {
212                 Type::Ptr ctype = context.GetReflectionType();
213                 Type::Ptr type = ctype;
214
215                 do {
216                         Object::Ptr object = type->GetPrototype();
217
218                         if (object && HasField(object, field))
219                                 return GetField(object, field, false, debugInfo);
220
221                         type = type->GetBaseType();
222                 } while (type);
223
224                 if (not_found_error)
225                         BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
226                 else
227                         return Empty;
228         }
229
230         static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
231         {
232                 if (context.IsEmpty() && !context.IsString())
233                         return Empty;
234
235                 if (!context.IsObject())
236                         return GetPrototypeField(context, field, true, debugInfo);
237
238                 Object::Ptr object = context;
239
240                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
241
242                 if (dict) {
243                         Value value;
244                         if (dict->Get(field, &value))
245                                 return value;
246                         else
247                                 return GetPrototypeField(context, field, false, debugInfo);
248                 }
249
250                 Array::Ptr arr = dynamic_pointer_cast<Array>(object);
251
252                 if (arr) {
253                         int index;
254
255                         try {
256                                 index = Convert::ToLong(field);
257                         } catch (...) {
258                                 return GetPrototypeField(context, field, true, debugInfo);
259                         }
260
261                         if (index < 0 || index >= arr->GetLength())
262                                 BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
263
264                         return arr->Get(index);
265                 }
266
267                 Type::Ptr type = object->GetReflectionType();
268
269                 if (!type)
270                         return Empty;
271
272                 int fid = type->GetFieldId(field);
273
274                 if (fid == -1)
275                         return GetPrototypeField(context, field, true, debugInfo);
276
277                 if (sandboxed) {
278                         Field fieldInfo = type->GetFieldInfo(fid);
279
280                         if (fieldInfo.Attributes & FANoUserView)
281                                 BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode."));
282                 }
283
284                 return object->GetField(fid);
285         }
286
287         static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
288         {
289                 if (!context)
290                         BOOST_THROW_EXCEPTION(ScriptError("Cannot set field '" + field + "' on a value that is not an object.", debugInfo));
291
292                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
293
294                 if (dict) {
295                         dict->Set(field, value);
296                         return;
297                 }
298
299                 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
300
301                 if (arr) {
302                         int index = Convert::ToLong(field);
303                         if (index >= arr->GetLength())
304                                 arr->Resize(index + 1);
305                         arr->Set(index, value);
306                         return;
307                 }
308
309                 Type::Ptr type = context->GetReflectionType();
310
311                 if (!type)
312                         BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
313
314                 int fid = type->GetFieldId(field);
315
316                 if (fid == -1)
317                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
318
319                 try {
320                         context->SetField(fid, value);
321                 } catch (const boost::bad_lexical_cast&) {
322                         Field fieldInfo = type->GetFieldInfo(fid);
323                         Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
324                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
325                 } catch (const std::bad_cast&) {
326                         Field fieldInfo = type->GetFieldInfo(fid);
327                         Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
328                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", 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 *frame = ScriptFrame::GetCurrentFrame();
340
341                 if (closedVars)
342                         closedVars->CopyTo(frame->Locals);
343
344                 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
345                         frame->Locals->Set(funcargs[i], arguments[i]);
346
347                 return expr->Evaluate(*frame);
348         }
349
350         static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
351         {
352                 Dictionary::Ptr locals;
353
354                 if (closedVars) {
355                         locals = new Dictionary();
356
357                         typedef std::pair<String, Expression *> ClosedVar;
358                         BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
359                                 locals->Set(cvar.first, cvar.second->Evaluate(frame));
360                         }
361                 }
362
363                 return locals;
364         }
365 };
366
367 }
368
369 #endif /* VMOPS_H */