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>
38 #include <boost/smart_ptr/make_shared.hpp>
48 static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
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);
58 return ScriptVariable::Get(name);
61 static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Value& funcName, const std::vector<Value>& arguments)
63 ScriptFunction::Ptr func;
65 if (funcName.IsObjectType<ScriptFunction>())
68 func = ScriptFunction::GetByName(funcName);
71 BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
73 boost::shared_ptr<ScriptFrame> vframe;
76 vframe = boost::make_shared<ScriptFrame>(self); /* passes self to the callee using a TLS variable */
78 vframe = boost::make_shared<ScriptFrame>();
80 return func->Invoke(arguments);
83 static inline Value Indexer(ScriptFrame& frame, const std::vector<Expression *>& indexer, const DebugInfo& debugInfo = DebugInfo())
85 Value result = indexer[0]->Evaluate(frame);
87 for (int i = 1; i < indexer.size(); i++) {
91 Value index = indexer[i]->Evaluate(frame);
92 result = GetField(result, index, debugInfo);
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)
101 ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
102 EvaluateClosedVars(frame, closedVars), expression));
105 ScriptFunction::Register(name, func);
110 static inline Value NewSlot(ScriptFrame& frame, const String& signal, const Value& slot)
112 ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal);
115 BOOST_THROW_EXCEPTION(ScriptError("Signal '" + signal + "' does not exist."));
117 sig->AddSlot(boost::bind(SlotWrapper, slot, _1));
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())
126 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
127 fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
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())
135 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
137 String checkName = name;
140 Type::Ptr ptype = Type::GetByName(type);
142 NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
145 checkName = nc->MakeName(name, Dictionary::Ptr());
148 if (!checkName.IsEmpty()) {
149 ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
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));
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));
168 item->AddExpression(new OwnedExpression(expression));
169 item->SetAbstract(abstract);
170 item->SetScope(EvaluateClosedVars(frame, closedVars));
172 item->SetFilter(filter);
173 item->Compile()->Register();
178 static inline Value For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
180 if (value.IsObjectType<Array>()) {
181 if (!fvvar.IsEmpty())
182 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
184 Array::Ptr arr = value;
186 ObjectLock olock(arr);
187 BOOST_FOREACH(const Value& value, arr) {
188 frame.Locals->Set(fkvar, value);
189 expression->Evaluate(frame);
191 } else if (value.IsString()) {
192 if (!fvvar.IsEmpty())
193 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for string.", debugInfo));
197 BOOST_FOREACH(char ch, str) {
198 frame.Locals->Set(fkvar, String(1, ch));
199 expression->Evaluate(frame);
201 } else if (value.IsObjectType<Dictionary>()) {
203 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
205 Dictionary::Ptr dict = value;
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);
214 BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
219 static inline bool HasField(const Object::Ptr& context, const String& field)
221 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
224 return dict->Contains(field);
226 Type::Ptr type = context->GetReflectionType();
231 return type->GetFieldId(field) != -1;
235 static inline Value GetPrototypeField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
237 Type::Ptr type = context.GetReflectionType();
240 Object::Ptr object = type->GetPrototype();
242 if (HasField(object, field))
243 return GetField(object, field);
245 type = type->GetBaseType();
251 static inline Value GetField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
253 if (!context.IsObject())
254 return GetPrototypeField(context, field);
256 Object::Ptr object = context;
258 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
261 if (dict->Contains(field))
262 return dict->Get(field);
264 return GetPrototypeField(context, field);
267 Array::Ptr arr = dynamic_pointer_cast<Array>(object);
273 index = Convert::ToLong(field);
275 return GetPrototypeField(context, field);
278 return arr->Get(index);
281 Type::Ptr type = object->GetReflectionType();
286 int fid = type->GetFieldId(field);
289 return GetPrototypeField(context, field);
291 return object->GetField(fid);
294 static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
296 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
299 dict->Set(field, value);
303 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
306 int index = Convert::ToLong(field);
307 if (index >= arr->GetLength())
308 arr->Resize(index + 1);
309 arr->Set(index, value);
313 Type::Ptr type = context->GetReflectionType();
316 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
318 int fid = type->GetFieldId(field);
321 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
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));
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)
336 if (arguments.size() < funcargs.size())
337 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
339 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
340 ScriptFrame frame(vframe->Self);
343 closedVars->CopyTo(frame.Locals);
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]);
350 result = expr->Evaluate(frame);
351 } catch (const InterruptExecutionError& iee) {
352 result = iee.GetResult();
358 static inline void SlotWrapper(const Value& funcName, const std::vector<Value>& arguments)
360 ScriptFunction::Ptr func;
362 if (funcName.IsObjectType<ScriptFunction>())
365 func = ScriptFunction::GetByName(funcName);
368 BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist."));
370 func->Invoke(arguments);
373 static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
375 Dictionary::Ptr locals;
378 locals = new Dictionary();
380 typedef std::pair<String, Expression *> ClosedVar;
381 BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
382 locals->Set(cvar.first, cvar.second->Evaluate(frame));