2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
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"
|
2018-08-07 13:55:41 +02:00
|
|
|
#include "base/namespace.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-11-15 08:20:22 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
class VMOps
|
|
|
|
{
|
|
|
|
public:
|
2019-07-26 14:17:27 +02:00
|
|
|
static inline bool FindVarImportRef(ScriptFrame& frame, const std::vector<Expression::Ptr>& imports, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2018-08-07 13:55:41 +02:00
|
|
|
for (const auto& import : imports) {
|
|
|
|
ExpressionResult res = import->Evaluate(frame);
|
|
|
|
Object::Ptr obj = res.GetValue();
|
2016-08-12 11:25:36 +02:00
|
|
|
if (obj->HasOwnField(name)) {
|
2018-08-07 13:55:41 +02:00
|
|
|
*result = obj;
|
2016-08-12 11:25:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
static inline bool FindVarImport(ScriptFrame& frame, const std::vector<Expression::Ptr>& imports, const String& name, Value *result, const DebugInfo& debugInfo = DebugInfo())
|
2016-08-12 11:25:36 +02:00
|
|
|
{
|
|
|
|
Value parent;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (FindVarImportRef(frame, imports, name, &parent, debugInfo)) {
|
2016-08-12 11:25:36 +02:00
|
|
|
*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())
|
2017-11-30 08:19:58 +01:00
|
|
|
return func->InvokeThis(self, arguments);
|
2016-08-08 13:53:45 +02:00
|
|
|
else
|
|
|
|
return func->Invoke(arguments);
|
2014-12-11 21:12:34 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 11:53:45 +01:00
|
|
|
static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames,
|
2019-07-26 14:17:27 +02:00
|
|
|
const std::map<String, std::unique_ptr<Expression> >& closedVars, const Expression::Ptr& expression)
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2017-11-29 11:53:45 +01:00
|
|
|
auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
|
|
|
|
|
2017-12-19 08:42:24 +01:00
|
|
|
auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) -> Value {
|
2017-11-29 11:53:45 +01:00
|
|
|
if (arguments.size() < argNames.size())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
|
|
|
|
|
|
|
|
ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
|
|
|
|
|
2017-12-18 10:30:20 +01:00
|
|
|
frame->Locals = new Dictionary();
|
|
|
|
|
2017-11-29 11:53:45 +01:00
|
|
|
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);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const Expression::Ptr& filter,
|
|
|
|
const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm, const std::map<String, std::unique_ptr<Expression> >& closedVars,
|
|
|
|
bool ignoreOnError, const Expression::Ptr& 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,
|
2017-12-19 15:50:05 +01:00
|
|
|
fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const Expression::Ptr& filter,
|
|
|
|
const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, const std::map<String, std::unique_ptr<Expression> >& closedVars, const Expression::Ptr& expression, const DebugInfo& debugInfo = DebugInfo())
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2018-01-10 17:20:33 +01:00
|
|
|
ConfigItemBuilder item{debugInfo};
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
String checkName = name;
|
|
|
|
|
|
|
|
if (!abstract) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *nc = dynamic_cast<NameComposer *>(type.get());
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
if (nc)
|
2017-11-30 08:36:35 +01:00
|
|
|
checkName = nc->MakeName(name, nullptr);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
item.SetType(type);
|
|
|
|
item.SetName(name);
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2016-08-28 10:27:43 +02:00
|
|
|
if (!abstract)
|
2018-01-10 17:20:33 +01:00
|
|
|
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();
|
2014-11-15 08:20:22 +01:00
|
|
|
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
2017-12-15 05:34:46 +01:00
|
|
|
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())
|
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
|
|
|
}
|
2018-08-07 13:55:41 +02:00
|
|
|
} else if (value.IsObjectType<Namespace>()) {
|
|
|
|
if (fvvar.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for namespace.", debugInfo));
|
|
|
|
|
|
|
|
Namespace::Ptr ns = value;
|
|
|
|
std::vector<String> keys;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(ns);
|
|
|
|
for (const Namespace::Pair& kv : ns) {
|
|
|
|
keys.push_back(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const String& key : keys) {
|
|
|
|
frame.Locals->Set(fkvar, key);
|
|
|
|
frame.Locals->Set(fvvar, ns->Get(key));
|
|
|
|
ExpressionResult res = expression->Evaluate(frame);
|
|
|
|
CHECK_RESULT_LOOP(res);
|
|
|
|
}
|
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
|
|
|
{
|
2019-07-09 13:20:53 +02:00
|
|
|
if (BOOST_UNLIKELY(context.IsEmpty() && !context.IsString()))
|
2014-12-15 17:23:18 +01:00
|
|
|
return Empty;
|
|
|
|
|
2019-07-09 13:20:53 +02:00
|
|
|
if (BOOST_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
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, bool overrideFrozen, 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));
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
return context->SetFieldByName(field, value, overrideFrozen, debugInfo);
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-12-15 05:34:46 +01:00
|
|
|
static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, const std::map<String, std::unique_ptr<Expression> >& closedVars)
|
2014-11-22 12:21:28 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
if (closedVars.empty())
|
|
|
|
return nullptr;
|
2014-11-22 12:21:28 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData locals;
|
2014-11-22 12:21:28 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
for (const auto& cvar : closedVars)
|
|
|
|
locals.emplace_back(cvar.first, cvar.second->Evaluate(frame));
|
2014-11-22 12:21:28 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary(std::move(locals));
|
2014-11-22 12:21:28 +01:00
|
|
|
}
|
2014-11-15 08:20:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* VMOPS_H */
|