2014-12-12 15:19:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2014-12-12 15:19:23 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/array.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
|
|
|
#include "base/functionwrapper.hpp"
|
2014-12-12 15:33:02 +01:00
|
|
|
#include "base/scriptframe.hpp"
|
2015-01-20 16:56:08 +01:00
|
|
|
#include "base/objectlock.hpp"
|
2016-07-29 10:47:13 +02:00
|
|
|
#include "base/exception.hpp"
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static double ArrayLen()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
return self->GetLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ArraySet(int index, const Value& value)
|
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
self->Set(index, value);
|
|
|
|
}
|
|
|
|
|
2015-07-30 20:58:52 +02:00
|
|
|
static Value ArrayGet(int index)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
return self->Get(index);
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
static void ArrayAdd(const Value& value)
|
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
self->Add(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ArrayRemove(int index)
|
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
self->Remove(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ArrayContains(const Value& value)
|
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
return self->Contains(value);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static void ArrayClear()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
self->Clear();
|
|
|
|
}
|
|
|
|
|
2015-01-21 08:47:45 +01:00
|
|
|
static bool ArraySortCmp(const Function::Ptr& cmp, const Value& a, const Value& b)
|
2015-01-20 16:56:08 +01:00
|
|
|
{
|
2017-11-30 08:19:58 +01:00
|
|
|
return cmp->Invoke({ a, b });
|
2015-01-20 16:56:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Array::Ptr ArraySort(const std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
Array::Ptr arr = self->ShallowClone();
|
|
|
|
|
|
|
|
if (args.empty()) {
|
|
|
|
ObjectLock olock(arr);
|
|
|
|
std::sort(arr->Begin(), arr->End());
|
|
|
|
} else {
|
2016-07-29 10:47:13 +02:00
|
|
|
Function::Ptr function = args[0];
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Sort function must be side-effect free."));
|
|
|
|
|
2015-01-20 16:56:08 +01:00
|
|
|
ObjectLock olock(arr);
|
2017-11-21 11:52:55 +01:00
|
|
|
std::sort(arr->Begin(), arr->End(), std::bind(ArraySortCmp, args[0], _1, _2));
|
2015-01-20 16:56:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static Array::Ptr ArrayShallowClone()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
2014-12-12 15:38:06 +01:00
|
|
|
return self->ShallowClone();
|
2014-12-12 15:19:23 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 08:37:55 +01:00
|
|
|
static Value ArrayJoin(const Value& separator)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
Value result;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
ObjectLock olock(self);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : self) {
|
2015-02-02 08:37:55 +01:00
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
result = result + separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = result + item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static Array::Ptr ArrayReverse()
|
2015-09-23 09:06:15 +02:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
return self->Reverse();
|
|
|
|
}
|
|
|
|
|
2016-07-29 10:47:13 +02:00
|
|
|
static Array::Ptr ArrayMap(const Function::Ptr& function)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Map function must be side-effect free."));
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData result;
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
ObjectLock olock(self);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : self) {
|
2018-01-11 11:17:38 +01:00
|
|
|
result.push_back(function->Invoke({ item }));
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Array(std::move(result));
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Value ArrayReduce(const Function::Ptr& function)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Reduce function must be side-effect free."));
|
|
|
|
|
2016-07-29 11:09:46 +02:00
|
|
|
if (self->GetLength() == 0)
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
Value result = self->Get(0);
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
ObjectLock olock(self);
|
2016-07-29 11:09:46 +02:00
|
|
|
for (size_t i = 1; i < self->GetLength(); i++) {
|
2017-11-30 08:19:58 +01:00
|
|
|
result = function->Invoke({ result, self->Get(i) });
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Array::Ptr ArrayFilter(const Function::Ptr& function)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData result;
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
ObjectLock olock(self);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : self) {
|
2017-11-30 08:19:58 +01:00
|
|
|
if (function->Invoke({ item }))
|
2018-01-11 11:17:38 +01:00
|
|
|
result.push_back(item);
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Array(std::move(result));
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:59:44 +02:00
|
|
|
static bool ArrayAny(const Function::Ptr& function)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
|
|
|
|
|
|
|
|
ObjectLock olock(self);
|
|
|
|
for (const Value& item : self) {
|
2017-11-30 08:19:58 +01:00
|
|
|
if (function->Invoke({ item }))
|
2017-05-15 15:59:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ArrayAll(const Function::Ptr& function)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
if (vframe->Sandboxed && !function->IsSideEffectFree())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
|
|
|
|
|
|
|
|
ObjectLock olock(self);
|
|
|
|
for (const Value& item : self) {
|
2017-11-30 08:19:58 +01:00
|
|
|
if (!function->Invoke({ item }))
|
2017-05-15 15:59:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-04 04:25:35 +01:00
|
|
|
static Array::Ptr ArrayUnique()
|
2016-07-29 10:47:13 +02:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
|
|
|
|
std::set<Value> result;
|
|
|
|
|
|
|
|
ObjectLock olock(self);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : self) {
|
2016-07-29 10:47:13 +02:00
|
|
|
result.insert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array::FromSet(result);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Object::Ptr Array::GetPrototype()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
static Dictionary::Ptr prototype = new Dictionary({
|
|
|
|
{ "len", new Function("Array#len", ArrayLen, {}, true) },
|
|
|
|
{ "set", new Function("Array#set", ArraySet, { "index", "value" }) },
|
|
|
|
{ "get", new Function("Array#get", ArrayGet, { "index" }) },
|
|
|
|
{ "add", new Function("Array#add", ArrayAdd, { "value" }) },
|
|
|
|
{ "remove", new Function("Array#remove", ArrayRemove, { "index" }) },
|
|
|
|
{ "contains", new Function("Array#contains", ArrayContains, { "value" }, true) },
|
|
|
|
{ "clear", new Function("Array#clear", ArrayClear) },
|
|
|
|
{ "sort", new Function("Array#sort", ArraySort, { "less_cmp" }, true) },
|
|
|
|
{ "shallow_clone", new Function("Array#shallow_clone", ArrayShallowClone, {}, true) },
|
|
|
|
{ "join", new Function("Array#join", ArrayJoin, { "separator" }, true) },
|
|
|
|
{ "reverse", new Function("Array#reverse", ArrayReverse, {}, true) },
|
|
|
|
{ "map", new Function("Array#map", ArrayMap, { "func" }, true) },
|
|
|
|
{ "reduce", new Function("Array#reduce", ArrayReduce, { "reduce" }, true) },
|
|
|
|
{ "filter", new Function("Array#filter", ArrayFilter, { "func" }, true) },
|
|
|
|
{ "any", new Function("Array#any", ArrayAny, { "func" }, true) },
|
|
|
|
{ "all", new Function("Array#all", ArrayAll, { "func" }, true) },
|
|
|
|
{ "unique", new Function("Array#unique", ArrayUnique, {}, true) }
|
|
|
|
});
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
return prototype;
|
2016-08-10 15:40:02 +02:00
|
|
|
}
|