1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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/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>
47 static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
49 if (frame.Locals && frame.Locals->Contains(name))
50 return frame.Locals->Get(name);
51 else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
52 return GetField(frame.Self, name, debugInfo);
54 return ScriptGlobal::Get(name);
57 static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const ScriptFunction::Ptr& func, const std::vector<Value>& arguments)
59 boost::shared_ptr<ScriptFrame> vframe;
62 vframe = boost::make_shared<ScriptFrame>(self); /* passes self to the callee using a TLS variable */
64 vframe = boost::make_shared<ScriptFrame>();
66 return func->Invoke(arguments);
69 static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args,
70 std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
72 return new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
73 EvaluateClosedVars(frame, closedVars), expression));
76 static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
77 const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
78 const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
80 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
81 fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
86 static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
87 const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
89 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
91 String checkName = name;
94 Type::Ptr ptype = Type::GetByName(type);
96 NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
99 checkName = nc->MakeName(name, Dictionary::Ptr());
102 if (!checkName.IsEmpty()) {
103 ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
106 std::ostringstream msgbuf;
107 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
108 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
114 if (name.FindFirstOf("!") != String::NPos) {
115 std::ostringstream msgbuf;
116 msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
117 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
122 item->AddExpression(new OwnedExpression(expression));
123 item->SetAbstract(abstract);
124 item->SetScope(EvaluateClosedVars(frame, closedVars));
126 item->SetFilter(filter);
127 item->Compile()->Register();
132 static inline Value For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
134 if (value.IsObjectType<Array>()) {
135 if (!fvvar.IsEmpty())
136 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
138 Array::Ptr arr = value;
140 ObjectLock olock(arr);
141 BOOST_FOREACH(const Value& value, arr) {
142 frame.Locals->Set(fkvar, value);
143 expression->Evaluate(frame);
145 } else if (value.IsString()) {
146 if (!fvvar.IsEmpty())
147 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for string.", debugInfo));
151 BOOST_FOREACH(char ch, str) {
152 frame.Locals->Set(fkvar, String(1, ch));
153 expression->Evaluate(frame);
155 } else if (value.IsObjectType<Dictionary>()) {
157 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
159 Dictionary::Ptr dict = value;
161 ObjectLock olock(dict);
162 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
163 frame.Locals->Set(fkvar, kv.first);
164 frame.Locals->Set(fvvar, kv.second);
165 expression->Evaluate(frame);
168 BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
173 static inline bool HasField(const Object::Ptr& context, const String& field)
175 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
178 return dict->Contains(field);
180 Type::Ptr type = context->GetReflectionType();
185 return type->GetFieldId(field) != -1;
189 static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
191 Type::Ptr type = context.GetReflectionType();
194 Object::Ptr object = type->GetPrototype();
196 if (object && HasField(object, field))
197 return GetField(object, field, debugInfo);
199 type = type->GetBaseType();
203 BOOST_THROW_EXCEPTION(ScriptError("Invalid field name: '" + field + "'", debugInfo));
208 static inline Value GetField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
210 if (context.IsEmpty())
213 if (!context.IsObject())
214 return GetPrototypeField(context, field, true, debugInfo);
216 Object::Ptr object = context;
218 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
221 if (dict->Contains(field))
222 return dict->Get(field);
224 return GetPrototypeField(context, field, false, debugInfo);
227 Array::Ptr arr = dynamic_pointer_cast<Array>(object);
233 index = Convert::ToLong(field);
235 return GetPrototypeField(context, field, true, debugInfo);
238 if (index < 0 || index >= arr->GetLength())
239 BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
241 return arr->Get(index);
244 Type::Ptr type = object->GetReflectionType();
249 int fid = type->GetFieldId(field);
252 return GetPrototypeField(context, field, true, debugInfo);
254 return object->GetField(fid);
257 static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
259 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
262 dict->Set(field, value);
266 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
269 int index = Convert::ToLong(field);
270 if (index >= arr->GetLength())
271 arr->Resize(index + 1);
272 arr->Set(index, value);
276 Type::Ptr type = context->GetReflectionType();
279 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
281 int fid = type->GetFieldId(field);
284 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
287 context->SetField(fid, value);
288 } catch (const boost::bad_lexical_cast&) {
289 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
290 } catch (const std::bad_cast&) {
291 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
296 static inline Value FunctionWrapper(const std::vector<Value>& arguments,
297 const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
299 if (arguments.size() < funcargs.size())
300 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
302 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
303 ScriptFrame frame(vframe->Self);
306 closedVars->CopyTo(frame.Locals);
308 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
309 frame.Locals->Set(funcargs[i], arguments[i]);
313 result = expr->Evaluate(frame);
314 } catch (const InterruptExecutionError& iee) {
315 result = iee.GetResult();
321 static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
323 Dictionary::Ptr locals;
326 locals = new Dictionary();
328 typedef std::pair<String, Expression *> ClosedVar;
329 BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
330 locals->Set(cvar.first, cvar.second->Evaluate(frame));