2014-11-15 08:20:22 +01:00
|
|
|
/******************************************************************************
|
2015-03-21 11:02:01 +01:00
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-03-21 11:02:01 +01:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
#ifndef VMOPS_H
|
|
|
|
#define VMOPS_H
|
|
|
|
|
|
|
|
#include "config/i2-config.hpp"
|
|
|
|
#include "config/expression.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "config/objectrule.hpp"
|
|
|
|
#include "base/debuginfo.hpp"
|
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2014-12-14 11:33:45 +01:00
|
|
|
#include "base/scriptglobal.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-11-15 08:20:22 +01:00
|
|
|
#include "base/convert.hpp"
|
2014-11-20 22:53:59 +01:00
|
|
|
#include "base/objectlock.hpp"
|
2014-12-11 21:12:34 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2014-11-15 08:20:22 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
class VMOps
|
|
|
|
{
|
|
|
|
public:
|
2016-08-12 11:25:36 +02:00
|
|
|
static inline bool FindVarImportRef(ScriptFrame& frame, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2016-08-12 13:13:01 +02:00
|
|
|
Array::Ptr imports = ScriptFrame::GetImports();
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2016-08-12 13:13:01 +02:00
|
|
|
ObjectLock olock(imports);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& import : imports) {
|
2016-08-12 11:25:36 +02:00
|
|
|
Object::Ptr obj = import;
|
|
|
|
if (obj->HasOwnField(name)) {
|
|
|
|
*result = import;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool FindVarImport(ScriptFrame& frame, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
|
|
|
|
{
|
|
|
|
Value parent;
|
|
|
|
|
|
|
|
if (FindVarImportRef(frame, name, &parent, debugInfo)) {
|
|
|
|
*result = GetField(parent, name, frame.Sandboxed, debugInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 12:45:22 +02:00
|
|
|
static inline Value ConstructorCall(const Type::Ptr& type, const std::vector<Value>& args, const DebugInfo& debugInfo = DebugInfo())
|
2015-03-21 22:48:23 +01:00
|
|
|
{
|
2016-03-29 12:45:22 +02:00
|
|
|
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];
|
2015-03-21 22:48:23 +01:00
|
|
|
else
|
2016-03-29 12:45:22 +02:00
|
|
|
return type->Instantiate(args);
|
2015-03-21 22:48:23 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 08:47:45 +01:00
|
|
|
static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2015-09-22 11:24:00 +02:00
|
|
|
if (!self.IsEmpty() || self.IsString())
|
2016-08-08 13:53:45 +02:00
|
|
|
return func->Invoke(self, arguments);
|
|
|
|
else
|
|
|
|
return func->Invoke(arguments);
|
2014-12-11 21:12:34 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-10 15:40:02 +02:00
|
|
|
static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& args,
|
2014-11-22 12:21:28 +01:00
|
|
|
std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2016-08-10 15:40:02 +02:00
|
|
|
return new Function(name, boost::bind(&FunctionWrapper, _1, args,
|
2017-03-27 14:26:56 +02:00
|
|
|
EvaluateClosedVars(frame, closedVars), expression), args);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2014-12-12 15:33:02 +01:00
|
|
|
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& package, const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
|
2015-10-05 12:44:11 +02:00
|
|
|
bool ignoreOnError, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
|
2015-10-05 12:44:11 +02:00
|
|
|
fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:21:30 +02:00
|
|
|
static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const boost::shared_ptr<Expression>& filter,
|
2016-08-28 10:27:43 +02:00
|
|
|
const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
|
|
|
ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
|
|
|
|
|
|
|
|
String checkName = name;
|
|
|
|
|
|
|
|
if (!abstract) {
|
2017-05-11 14:21:30 +02:00
|
|
|
NameComposer *nc = dynamic_cast<NameComposer *>(type.get());
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
if (nc)
|
|
|
|
checkName = nc->MakeName(name, Dictionary::Ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkName.IsEmpty()) {
|
2015-08-20 16:43:03 +02:00
|
|
|
ConfigItem::Ptr oldItem = ConfigItem::GetByTypeAndName(type, checkName);
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
if (oldItem) {
|
|
|
|
std::ostringstream msgbuf;
|
2017-05-11 14:21:30 +02:00
|
|
|
msgbuf << "Object '" << name << "' of type '" << type->GetName() << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:21:30 +02:00
|
|
|
if (filter && !ObjectRule::IsValidSourceType(type->GetName())) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Object '" << name << "' of type '" << type->GetName() << "' must not have 'assign where' and 'ignore where' rules: " << debugInfo;
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
|
|
|
|
}
|
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
item->SetType(type);
|
|
|
|
item->SetName(name);
|
|
|
|
|
2016-08-28 10:27:43 +02:00
|
|
|
if (!abstract)
|
|
|
|
item->AddExpression(new ImportDefaultTemplatesExpression());
|
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
item->AddExpression(new OwnedExpression(expression));
|
|
|
|
item->SetAbstract(abstract);
|
2014-11-22 12:21:28 +01:00
|
|
|
item->SetScope(EvaluateClosedVars(frame, closedVars));
|
2014-11-15 08:20:22 +01:00
|
|
|
item->SetZone(zone);
|
2015-08-28 17:58:29 +02:00
|
|
|
item->SetPackage(package);
|
2014-11-20 22:53:59 +01:00
|
|
|
item->SetFilter(filter);
|
2016-08-28 10:27:43 +02:00
|
|
|
item->SetDefaultTemplate(defaultTmpl);
|
2015-10-05 12:44:11 +02:00
|
|
|
item->SetIgnoreOnError(ignoreOnError);
|
2014-11-15 08:20:22 +01:00
|
|
|
item->Compile()->Register();
|
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2015-03-22 00:04:57 +01:00
|
|
|
static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
|
|
|
if (value.IsObjectType<Array>()) {
|
|
|
|
if (!fvvar.IsEmpty())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
Array::Ptr arr = value;
|
|
|
|
|
2015-01-16 08:40:19 +01:00
|
|
|
for (Array::SizeType i = 0; i < arr->GetLength(); i++) {
|
|
|
|
frame.Locals->Set(fkvar, arr->Get(i));
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult res = expression->Evaluate(frame);
|
|
|
|
CHECK_RESULT_LOOP(res);
|
2014-12-12 06:54:38 +01:00
|
|
|
}
|
2014-11-22 12:21:28 +01:00
|
|
|
} else if (value.IsObjectType<Dictionary>()) {
|
2014-11-15 08:20:22 +01:00
|
|
|
if (fvvar.IsEmpty())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr dict = value;
|
2015-01-16 08:40:19 +01:00
|
|
|
std::vector<String> keys;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(dict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : dict) {
|
2015-01-16 08:40:19 +01:00
|
|
|
keys.push_back(kv.first);
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& key : keys) {
|
2015-01-16 08:40:19 +01:00
|
|
|
frame.Locals->Set(fkvar, key);
|
|
|
|
frame.Locals->Set(fvvar, dict->Get(key));
|
2015-02-19 12:57:52 +01:00
|
|
|
ExpressionResult res = expression->Evaluate(frame);
|
|
|
|
CHECK_RESULT_LOOP(res);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
2014-12-12 06:54:38 +01:00
|
|
|
} else
|
2014-12-11 15:07:21 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2015-10-26 10:41:00 +01:00
|
|
|
static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2016-04-19 09:37:41 +02:00
|
|
|
if (unlikely(context.IsEmpty() && !context.IsString()))
|
2014-12-15 17:23:18 +01:00
|
|
|
return Empty;
|
|
|
|
|
2016-04-19 09:37:41 +02:00
|
|
|
if (unlikely(!context.IsObject()))
|
2014-12-15 17:23:18 +01:00
|
|
|
return GetPrototypeField(context, field, true, debugInfo);
|
2014-12-12 06:54:38 +01:00
|
|
|
|
|
|
|
Object::Ptr object = context;
|
|
|
|
|
2016-04-18 11:29:43 +02:00
|
|
|
return object->GetFieldByName(field, sandboxed, debugInfo);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:06:09 +01:00
|
|
|
static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2015-07-30 08:28:07 +02:00
|
|
|
if (!context)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Cannot set field '" + field + "' on a value that is not an object.", debugInfo));
|
|
|
|
|
2016-04-18 11:29:43 +02:00
|
|
|
return context->SetFieldByName(field, value, debugInfo);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline Value FunctionWrapper(const std::vector<Value>& arguments,
|
2014-11-22 12:21:28 +01:00
|
|
|
const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
|
|
|
if (arguments.size() < funcargs.size())
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2015-03-31 07:09:20 +02:00
|
|
|
ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
|
2014-11-22 12:21:28 +01:00
|
|
|
|
|
|
|
if (closedVars)
|
2015-03-31 07:09:20 +02:00
|
|
|
closedVars->CopyTo(frame->Locals);
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
|
2015-03-31 07:09:20 +02:00
|
|
|
frame->Locals->Set(funcargs[i], arguments[i]);
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2015-03-31 07:09:20 +02:00
|
|
|
return expr->Evaluate(*frame);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2014-12-12 15:33:02 +01:00
|
|
|
static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
|
2014-11-22 12:21:28 +01:00
|
|
|
{
|
|
|
|
Dictionary::Ptr locals;
|
|
|
|
|
|
|
|
if (closedVars) {
|
|
|
|
locals = new Dictionary();
|
|
|
|
|
|
|
|
typedef std::pair<String, Expression *> ClosedVar;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ClosedVar& cvar : *closedVars) {
|
2014-11-22 12:21:28 +01:00
|
|
|
locals->Set(cvar.first, cvar.second->Evaluate(frame));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return locals;
|
|
|
|
}
|
2014-11-15 08:20:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* VMOPS_H */
|