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/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>
47 static inline Value Variable(VMFrame& frame, const String& name)
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);
57 return ScriptVariable::Get(name);
60 static inline Value FunctionCall(VMFrame& frame, const Value& funcName, const std::vector<Value>& arguments)
62 ScriptFunction::Ptr func;
64 if (funcName.IsObjectType<ScriptFunction>())
67 func = ScriptFunction::GetByName(funcName);
70 BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
72 return func->Invoke(arguments);
75 static inline Value Indexer(VMFrame& frame, const std::vector<Expression *>& indexer)
77 Value result = indexer[0]->Evaluate(frame);
79 for (int i = 1; i < indexer.size(); i++) {
83 Value index = indexer[i]->Evaluate(frame);
84 result = GetField(result, index);
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)
93 ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
94 EvaluateClosedVars(frame, closedVars), expression));
97 ScriptFunction::Register(name, func);
102 static inline Value NewSlot(VMFrame& frame, const String& signal, const Value& slot)
104 ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal);
107 BOOST_THROW_EXCEPTION(ScriptError("Signal '" + signal + "' does not exist."));
109 sig->AddSlot(boost::bind(SlotWrapper, slot, _1));
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())
118 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
119 fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
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())
127 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
129 String checkName = name;
132 Type::Ptr ptype = Type::GetByName(type);
134 NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
137 checkName = nc->MakeName(name, Dictionary::Ptr());
140 if (!checkName.IsEmpty()) {
141 ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
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));
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));
160 item->AddExpression(new OwnedExpression(expression));
161 item->SetAbstract(abstract);
162 item->SetScope(EvaluateClosedVars(frame, closedVars));
164 item->SetFilter(filter);
165 item->Compile()->Register();
170 static inline Value For(VMFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
172 if (value.IsObjectType<Array>()) {
173 if (!fvvar.IsEmpty())
174 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
176 Array::Ptr arr = value;
178 ObjectLock olock(arr);
179 BOOST_FOREACH(const Value& value, arr) {
180 frame.Locals->Set(fkvar, value);
181 expression->Evaluate(frame);
183 } else if (value.IsObjectType<Dictionary>()) {
185 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
187 Dictionary::Ptr dict = value;
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);
197 BOOST_THROW_EXCEPTION(ScriptError("Invalid type in __for expression: " + value.GetTypeName(), debugInfo));
202 static inline bool HasField(const Object::Ptr& context, const String& field)
204 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
207 return dict->Contains(field);
209 Type::Ptr type = context->GetReflectionType();
214 return type->GetFieldId(field) != -1;
218 static inline Value GetField(const Object::Ptr& context, const String& field)
220 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
223 return dict->Get(field);
225 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
228 int index = Convert::ToLong(field);
229 return arr->Get(index);
232 Type::Ptr type = context->GetReflectionType();
237 int fid = type->GetFieldId(field);
242 return context->GetField(fid);
245 static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
247 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
250 dict->Set(field, value);
254 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
257 int index = Convert::ToLong(field);
258 if (index >= arr->GetLength())
259 arr->Resize(index + 1);
260 arr->Set(index, value);
264 Type::Ptr type = context->GetReflectionType();
267 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
269 int fid = type->GetFieldId(field);
272 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
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));
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)
287 if (arguments.size() < funcargs.size())
288 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
293 closedVars->CopyTo(frame.Locals);
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]);
300 result = expr->Evaluate(frame);
301 } catch (const InterruptExecutionError& iee) {
302 result = iee.GetResult();
308 static inline void SlotWrapper(const Value& funcName, const std::vector<Value>& arguments)
310 ScriptFunction::Ptr func;
312 if (funcName.IsObjectType<ScriptFunction>())
315 func = ScriptFunction::GetByName(funcName);
318 BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
320 func->Invoke(arguments);
323 static inline Dictionary::Ptr EvaluateClosedVars(VMFrame& frame, std::map<String, Expression *> *closedVars)
325 Dictionary::Ptr locals;
328 locals = new Dictionary();
330 typedef std::pair<String, Expression *> ClosedVar;
331 BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
332 locals->Set(cvar.first, cvar.second->Evaluate(frame));