2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
#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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2015-07-30 20:58:52 +02:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2014-12-12 15:19:23 +01:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2015-01-20 16:56:08 +01:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2015-02-02 08:37:55 +01:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2015-09-23 09:06:15 +02:00
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2016-07-29 10:47:13 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2017-05-15 15:59:44 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2017-05-15 15:59:44 +02:00
|
|
|
|
|
|
|
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);
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(self);
|
2018-05-09 16:55:14 +02:00
|
|
|
return self->Unique();
|
2016-07-29 10:47:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 12:19:34 +01:00
|
|
|
static void ArrayFreeze()
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
|
|
|
|
self->Freeze();
|
|
|
|
}
|
|
|
|
|
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) },
|
2018-01-30 12:19:34 +01:00
|
|
|
{ "unique", new Function("Array#unique", ArrayUnique, {}, true) },
|
|
|
|
{ "freeze", new Function("Array#freeze", ArrayFreeze, {}) }
|
2018-01-11 11:17:38 +01:00
|
|
|
});
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
return prototype;
|
2016-08-10 15:40:02 +02:00
|
|
|
}
|