1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://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/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>
47 static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
50 if (frame.Locals && frame.Locals->Get(name, &value))
52 else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(name))
53 return GetField(frame.Self, name, frame.Sandboxed, debugInfo);
55 return ScriptGlobal::Get(name);
58 static inline Value ConstructorCall(const Type::Ptr& type, const std::vector<Value>& args, const DebugInfo& debugInfo = DebugInfo())
60 if (type->GetName() == "String") {
63 else if (args.size() == 1)
64 return Convert::ToString(args[0]);
66 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
67 } else if (type->GetName() == "Number") {
70 else if (args.size() == 1)
71 return Convert::ToDouble(args[0]);
73 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
74 } else if (type->GetName() == "Boolean") {
77 else if (args.size() == 1)
78 return Convert::ToBool(args[0]);
80 BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
81 } else if (args.size() == 1 && type->IsAssignableFrom(args[0].GetReflectionType()))
84 return type->Instantiate(args);
87 static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
91 if (!self.IsEmpty() || self.IsString())
92 return func->Invoke(self, arguments);
94 return func->Invoke(arguments);
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 return new Function(name, boost::bind(&FunctionWrapper, _1, args,
102 EvaluateClosedVars(frame, closedVars), expression));
105 static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
106 const String& package, const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
107 bool ignoreOnError, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
109 ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
110 fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
115 static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
116 const String& zone, const String& package, bool ignoreOnError, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
118 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
120 String checkName = name;
123 Type::Ptr ptype = Type::GetByName(type);
125 NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
128 checkName = nc->MakeName(name, Dictionary::Ptr());
131 if (!checkName.IsEmpty()) {
132 ConfigItem::Ptr oldItem = ConfigItem::GetByTypeAndName(type, checkName);
135 std::ostringstream msgbuf;
136 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
137 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
144 item->AddExpression(new OwnedExpression(expression));
145 item->SetAbstract(abstract);
146 item->SetScope(EvaluateClosedVars(frame, closedVars));
148 item->SetPackage(package);
149 item->SetFilter(filter);
150 item->SetIgnoreOnError(ignoreOnError);
151 item->Compile()->Register();
156 static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
158 if (value.IsObjectType<Array>()) {
159 if (!fvvar.IsEmpty())
160 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
162 Array::Ptr arr = value;
164 for (Array::SizeType i = 0; i < arr->GetLength(); i++) {
165 frame.Locals->Set(fkvar, arr->Get(i));
166 ExpressionResult res = expression->Evaluate(frame);
167 CHECK_RESULT_LOOP(res);
169 } else if (value.IsObjectType<Dictionary>()) {
171 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
173 Dictionary::Ptr dict = value;
174 std::vector<String> keys;
177 ObjectLock olock(dict);
178 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
179 keys.push_back(kv.first);
183 BOOST_FOREACH(const String& key, keys) {
184 frame.Locals->Set(fkvar, key);
185 frame.Locals->Set(fvvar, dict->Get(key));
186 ExpressionResult res = expression->Evaluate(frame);
187 CHECK_RESULT_LOOP(res);
190 BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
195 static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
197 if (unlikely(context.IsEmpty() && !context.IsString()))
200 if (unlikely(!context.IsObject()))
201 return GetPrototypeField(context, field, true, debugInfo);
203 Object::Ptr object = context;
205 return object->GetFieldByName(field, sandboxed, debugInfo);
208 static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
211 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field '" + field + "' on a value that is not an object.", debugInfo));
213 return context->SetFieldByName(field, value, debugInfo);
217 static inline Value FunctionWrapper(const std::vector<Value>& arguments,
218 const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
220 if (arguments.size() < funcargs.size())
221 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
223 ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
226 closedVars->CopyTo(frame->Locals);
228 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
229 frame->Locals->Set(funcargs[i], arguments[i]);
231 return expr->Evaluate(*frame);
234 static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
236 Dictionary::Ptr locals;
239 locals = new Dictionary();
241 typedef std::pair<String, Expression *> ClosedVar;
242 BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
243 locals->Set(cvar.first, cvar.second->Evaluate(frame));