]> granicus.if.org Git - icinga2/blobdiff - lib/config/vmops.hpp
Workaround for GCC bug 61321
[icinga2] / lib / config / vmops.hpp
index 91663a1c5d6dbd4d12971a43d186486a4c3afd11..323ba51ee47e58b30c05b994e257b47662f9b0ed 100644 (file)
@@ -1,21 +1,21 @@
 /******************************************************************************
-* Icinga 2                                                                   *
-* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
-*                                                                            *
-* This program is free software; you can redistribute it and/or              *
-* modify it under the terms of the GNU General Public License                *
-* as published by the Free Software Foundation; either version 2             *
-* of the License, or (at your option) any later version.                     *
-*                                                                            *
-* This program is distributed in the hope that it will be useful,            *
-* but WITHOUT ANY WARRANTY; without even the implied warranty of             *
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
-* GNU General Public License for more details.                               *
-*                                                                            *
-* You should have received a copy of the GNU General Public License          *
-* along with this program; if not, write to the Free Software Foundation     *
-* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
-******************************************************************************/
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
 
 #ifndef VMOPS_H
 #define VMOPS_H
 #include "base/debuginfo.hpp"
 #include "base/array.hpp"
 #include "base/dictionary.hpp"
-#include "base/scriptfunction.hpp"
+#include "base/function.hpp"
 #include "base/scriptglobal.hpp"
 #include "base/exception.hpp"
 #include "base/convert.hpp"
 #include "base/objectlock.hpp"
-#include <boost/foreach.hpp>
-#include <boost/smart_ptr/make_shared.hpp>
 #include <map>
 #include <vector>
 
@@ -44,92 +42,157 @@ namespace icinga
 class VMOps
 {
 public:
-       static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
+       static inline bool FindVarImportRef(ScriptFrame& frame, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
        {
-               if (frame.Locals && frame.Locals->Contains(name))
-                       return frame.Locals->Get(name);
-               else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
-                       return GetField(frame.Self, name, debugInfo);
-               else
-                       return ScriptGlobal::Get(name);
+               Array::Ptr imports = ScriptFrame::GetImports();
+
+               ObjectLock olock(imports);
+               for (const Value& import : imports) {
+                       Object::Ptr obj = import;
+                       if (obj->HasOwnField(name)) {
+                               *result = import;
+                               return true;
+                       }
+               }
+
+               return false;
        }
 
-       static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const ScriptFunction::Ptr& func, const std::vector<Value>& arguments)
+       static inline bool FindVarImport(ScriptFrame& frame, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
        {
-               boost::shared_ptr<ScriptFrame> vframe;
+               Value parent;
+
+               if (FindVarImportRef(frame, name, &parent, debugInfo)) {
+                       *result = GetField(parent, name, frame.Sandboxed, debugInfo);
+                       return true;
+               }
+
+               return false;
+       }
+
+       static inline Value ConstructorCall(const Type::Ptr& type, const std::vector<Value>& args, const DebugInfo& debugInfo = DebugInfo())
+       {
+               if (type->GetName() == "String") {
+                       if (args.empty())
+                               return "";
+                       else if (args.size() == 1)
+                               return Convert::ToString(args[0]);
+                       else
+                               BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
+               } else if (type->GetName() == "Number") {
+                       if (args.empty())
+                               return 0;
+                       else if (args.size() == 1)
+                               return Convert::ToDouble(args[0]);
+                       else
+                               BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
+               } else if (type->GetName() == "Boolean") {
+                       if (args.empty())
+                               return 0;
+                       else if (args.size() == 1)
+                               return Convert::ToBool(args[0]);
+                       else
+                               BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor."));
+               } else if (args.size() == 1 && type->IsAssignableFrom(args[0].GetReflectionType()))
+                       return args[0];
+               else
+                       return type->Instantiate(args);
+       }
 
-               if (!self.IsEmpty())
-                       vframe = boost::make_shared<ScriptFrame>(self); /* passes self to the callee using a TLS variable */
+       static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
+       {
+               if (!self.IsEmpty() || self.IsString())
+                       return func->InvokeThis(self, arguments);
                else
-                       vframe = boost::make_shared<ScriptFrame>();
+                       return func->Invoke(arguments);
 
-               return func->Invoke(arguments);
        }
 
-       static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args,
-           std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
+       static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames,
+           const std::map<String, std::unique_ptr<Expression> >& closedVars, const std::shared_ptr<Expression>& expression)
        {
-               return new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
-                   EvaluateClosedVars(frame, closedVars), expression));
+               auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
+
+               auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) -> Value {
+                       if (arguments.size() < argNames.size())
+                               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
+
+                       ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
+
+                       frame->Locals = new Dictionary();
+
+                       if (evaluatedClosedVars)
+                               evaluatedClosedVars->CopyTo(frame->Locals);
+
+                       for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), argNames.size()); i++)
+                               frame->Locals->Set(argNames[i], arguments[i]);
+
+                       return expression->Evaluate(*frame);
+               };
+
+               return new Function(name, wrapper, argNames);
        }
 
-       static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
-               const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
-               const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
+       static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const std::shared_ptr<Expression>& filter,
+               const String& package, const String& fkvar, const String& fvvar, const std::shared_ptr<Expression>& fterm, const std::map<String, std::unique_ptr<Expression> >& closedVars,
+               bool ignoreOnError, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
        {
-               ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
-                   fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
+               ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
+                   fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
 
                return Empty;
        }
 
-       static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
-               const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
+       static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const std::shared_ptr<Expression>& filter,
+               const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, const std::map<String, std::unique_ptr<Expression> >& closedVars, const std::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
        {
                ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
 
                String checkName = name;
 
                if (!abstract) {
-                       Type::Ptr ptype = Type::GetByName(type);
-
-                       NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
+                       NameComposer *nc = dynamic_cast<NameComposer *>(type.get());
 
                        if (nc)
-                               checkName = nc->MakeName(name, Dictionary::Ptr());
+                               checkName = nc->MakeName(name, nullptr);
                }
 
                if (!checkName.IsEmpty()) {
-                       ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
+                       ConfigItem::Ptr oldItem = ConfigItem::GetByTypeAndName(type, checkName);
 
                        if (oldItem) {
                                std::ostringstream msgbuf;
-                               msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
+                               msgbuf << "Object '" << name << "' of type '" << type->GetName() << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
                                BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
                        }
                }
 
-               item->SetType(type);
-
-               if (name.FindFirstOf("!") != String::NPos) {
+               if (filter && !ObjectRule::IsValidSourceType(type->GetName())) {
                        std::ostringstream msgbuf;
-                       msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
+                       msgbuf << "Object '" << name << "' of type '" << type->GetName() << "' must not have 'assign where' and 'ignore where' rules: " << debugInfo;
                        BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
                }
 
+               item->SetType(type);
                item->SetName(name);
 
+               if (!abstract)
+                       item->AddExpression(new ImportDefaultTemplatesExpression());
+
                item->AddExpression(new OwnedExpression(expression));
                item->SetAbstract(abstract);
                item->SetScope(EvaluateClosedVars(frame, closedVars));
                item->SetZone(zone);
+               item->SetPackage(package);
                item->SetFilter(filter);
+               item->SetDefaultTemplate(defaultTmpl);
+               item->SetIgnoreOnError(ignoreOnError);
                item->Compile()->Register();
 
                return Empty;
        }
 
-       static inline Value For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
+       static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, const std::unique_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
        {
                if (value.IsObjectType<Array>()) {
                        if (!fvvar.IsEmpty())
@@ -137,32 +200,30 @@ public:
 
                        Array::Ptr arr = value;
 
-                       ObjectLock olock(arr);
-                       BOOST_FOREACH(const Value& value, arr) {
-                               frame.Locals->Set(fkvar, value);
-                               expression->Evaluate(frame);
-                       }
-               } else if (value.IsString()) {
-                       if (!fvvar.IsEmpty())
-                               BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for string.", debugInfo));
-
-                       String str = value;
-
-                       BOOST_FOREACH(char ch, str) {
-                               frame.Locals->Set(fkvar, String(1, ch));
-                               expression->Evaluate(frame);
+                       for (Array::SizeType i = 0; i < arr->GetLength(); i++) {
+                               frame.Locals->Set(fkvar, arr->Get(i));
+                               ExpressionResult res = expression->Evaluate(frame);
+                               CHECK_RESULT_LOOP(res);
                        }
                } else if (value.IsObjectType<Dictionary>()) {
                        if (fvvar.IsEmpty())
                                BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
 
                        Dictionary::Ptr dict = value;
+                       std::vector<String> keys;
 
-                       ObjectLock olock(dict);
-                       BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
-                               frame.Locals->Set(fkvar, kv.first);
-                               frame.Locals->Set(fvvar, kv.second);
-                               expression->Evaluate(frame);
+                       {
+                               ObjectLock olock(dict);
+                               for (const Dictionary::Pair& kv : dict) {
+                                       keys.push_back(kv.first);
+                               }
+                       }
+
+                       for (const String& key : keys) {
+                               frame.Locals->Set(fkvar, key);
+                               frame.Locals->Set(fvvar, dict->Get(key));
+                               ExpressionResult res = expression->Evaluate(frame);
+                               CHECK_RESULT_LOOP(res);
                        }
                } else
                        BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
@@ -170,163 +231,36 @@ public:
                return Empty;
        }
 
-       static inline bool HasField(const Object::Ptr& context, const String& field)
-       {
-               Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
-
-               if (dict)
-                       return dict->Contains(field);
-               else {
-                       Type::Ptr type = context->GetReflectionType();
-
-                       if (!type)
-                               return false;
-
-                       return type->GetFieldId(field) != -1;
-               }
-       }
-
-       static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
-       {
-               Type::Ptr type = context.GetReflectionType();
-
-               do {
-                       Object::Ptr object = type->GetPrototype();
-
-                       if (object && HasField(object, field))
-                               return GetField(object, field, debugInfo);
-
-                       type = type->GetBaseType();
-               } while (type);
-
-               if (not_found_error)
-                       BOOST_THROW_EXCEPTION(ScriptError("Invalid field name: '" + field + "'", debugInfo));
-               else
-                       return Empty;
-       }
-
-       static inline Value GetField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
+       static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
        {
-               if (context.IsEmpty())
+               if (unlikely(context.IsEmpty() && !context.IsString()))
                        return Empty;
 
-               if (!context.IsObject())
+               if (unlikely(!context.IsObject()))
                        return GetPrototypeField(context, field, true, debugInfo);
 
                Object::Ptr object = context;
 
-               Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
-
-               if (dict) {
-                       if (dict->Contains(field))
-                               return dict->Get(field);
-                       else
-                               return GetPrototypeField(context, field, false, debugInfo);
-               }
-
-               Array::Ptr arr = dynamic_pointer_cast<Array>(object);
-
-               if (arr) {
-                       int index;
-
-                       try {
-                               index = Convert::ToLong(field);
-                       } catch (...) {
-                               return GetPrototypeField(context, field, true, debugInfo);
-                       }
-
-                       if (index < 0 || index >= arr->GetLength())
-                               BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
-
-                       return arr->Get(index);
-               }
-
-               Type::Ptr type = object->GetReflectionType();
-
-               if (!type)
-                       return Empty;
-
-               int fid = type->GetFieldId(field);
-
-               if (fid == -1)
-                       return GetPrototypeField(context, field, true, debugInfo);
-
-               return object->GetField(fid);
+               return object->GetFieldByName(field, sandboxed, debugInfo);
        }
 
        static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
        {
-               Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
-
-               if (dict) {
-                       dict->Set(field, value);
-                       return;
-               }
-
-               Array::Ptr arr = dynamic_pointer_cast<Array>(context);
-
-               if (arr) {
-                       int index = Convert::ToLong(field);
-                       if (index >= arr->GetLength())
-                               arr->Resize(index + 1);
-                       arr->Set(index, value);
-                       return;
-               }
-
-               Type::Ptr type = context->GetReflectionType();
-
-               if (!type)
-                       BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
-
-               int fid = type->GetFieldId(field);
-
-               if (fid == -1)
-                       BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
+               if (!context)
+                       BOOST_THROW_EXCEPTION(ScriptError("Cannot set field '" + field + "' on a value that is not an object.", debugInfo));
 
-               try {
-                       context->SetField(fid, value);
-               } catch (const boost::bad_lexical_cast&) {
-                       BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
-               } catch (const std::bad_cast&) {
-                       BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
-               }
+               return context->SetFieldByName(field, value, debugInfo);
        }
 
 private:
-       static inline Value FunctionWrapper(const std::vector<Value>& arguments,
-           const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
-       {
-               if (arguments.size() < funcargs.size())
-                       BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
-
-               ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
-               ScriptFrame frame(vframe->Self);
-
-               if (closedVars)
-                       closedVars->CopyTo(frame.Locals);
-
-               for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
-                       frame.Locals->Set(funcargs[i], arguments[i]);
-
-               Value result;
-               try {
-                       result = expr->Evaluate(frame);
-               } catch (const InterruptExecutionError& iee) {
-                       result = iee.GetResult();
-               }
-
-               return result;
-       }
-
-       static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
+       static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, const std::map<String, std::unique_ptr<Expression> >& closedVars)
        {
                Dictionary::Ptr locals;
 
-               if (closedVars) {
+               if (!closedVars.empty()) {
                        locals = new Dictionary();
 
-                       typedef std::pair<String, Expression *> ClosedVar;
-                       BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
+                       for (const auto& cvar : closedVars) {
                                locals->Set(cvar.first, cvar.second->Evaluate(frame));
                        }
                }