1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/array.hpp"
21 #include "base/function.hpp"
22 #include "base/functionwrapper.hpp"
23 #include "base/scriptframe.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/exception.hpp"
27 using namespace icinga;
29 static double ArrayLen()
31 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
32 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
33 REQUIRE_NOT_NULL(self);
34 return self->GetLength();
37 static void ArraySet(int index, const Value& value)
39 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
40 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
41 REQUIRE_NOT_NULL(self);
42 self->Set(index, value);
45 static Value ArrayGet(int index)
47 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
48 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
49 REQUIRE_NOT_NULL(self);
50 return self->Get(index);
53 static void ArrayAdd(const Value& value)
55 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
56 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
57 REQUIRE_NOT_NULL(self);
61 static void ArrayRemove(int index)
63 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
64 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
65 REQUIRE_NOT_NULL(self);
69 static bool ArrayContains(const Value& value)
71 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
72 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
73 REQUIRE_NOT_NULL(self);
74 return self->Contains(value);
77 static void ArrayClear()
79 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
80 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
81 REQUIRE_NOT_NULL(self);
85 static bool ArraySortCmp(const Function::Ptr& cmp, const Value& a, const Value& b)
87 return cmp->Invoke({ a, b });
90 static Array::Ptr ArraySort(const std::vector<Value>& args)
92 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
93 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
94 REQUIRE_NOT_NULL(self);
96 Array::Ptr arr = self->ShallowClone();
99 ObjectLock olock(arr);
100 std::sort(arr->Begin(), arr->End());
102 Function::Ptr function = args[0];
104 if (vframe->Sandboxed && !function->IsSideEffectFree())
105 BOOST_THROW_EXCEPTION(ScriptError("Sort function must be side-effect free."));
107 ObjectLock olock(arr);
108 std::sort(arr->Begin(), arr->End(), std::bind(ArraySortCmp, args[0], _1, _2));
114 static Array::Ptr ArrayShallowClone()
116 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
117 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
118 REQUIRE_NOT_NULL(self);
119 return self->ShallowClone();
122 static Value ArrayJoin(const Value& separator)
124 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
125 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
126 REQUIRE_NOT_NULL(self);
131 ObjectLock olock(self);
132 for (const Value& item : self) {
136 result = result + separator;
139 result = result + item;
145 static Array::Ptr ArrayReverse()
147 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
148 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
149 REQUIRE_NOT_NULL(self);
150 return self->Reverse();
153 static Array::Ptr ArrayMap(const Function::Ptr& function)
155 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
156 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
157 REQUIRE_NOT_NULL(self);
159 if (vframe->Sandboxed && !function->IsSideEffectFree())
160 BOOST_THROW_EXCEPTION(ScriptError("Map function must be side-effect free."));
164 ObjectLock olock(self);
165 for (const Value& item : self) {
166 result.push_back(function->Invoke({ item }));
169 return new Array(std::move(result));
172 static Value ArrayReduce(const Function::Ptr& function)
174 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
175 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
176 REQUIRE_NOT_NULL(self);
178 if (vframe->Sandboxed && !function->IsSideEffectFree())
179 BOOST_THROW_EXCEPTION(ScriptError("Reduce function must be side-effect free."));
181 if (self->GetLength() == 0)
184 Value result = self->Get(0);
186 ObjectLock olock(self);
187 for (size_t i = 1; i < self->GetLength(); i++) {
188 result = function->Invoke({ result, self->Get(i) });
194 static Array::Ptr ArrayFilter(const Function::Ptr& function)
196 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
197 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
198 REQUIRE_NOT_NULL(self);
200 if (vframe->Sandboxed && !function->IsSideEffectFree())
201 BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
205 ObjectLock olock(self);
206 for (const Value& item : self) {
207 if (function->Invoke({ item }))
208 result.push_back(item);
211 return new Array(std::move(result));
214 static bool ArrayAny(const Function::Ptr& function)
216 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
217 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
218 REQUIRE_NOT_NULL(self);
220 if (vframe->Sandboxed && !function->IsSideEffectFree())
221 BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
223 ObjectLock olock(self);
224 for (const Value& item : self) {
225 if (function->Invoke({ item }))
232 static bool ArrayAll(const Function::Ptr& function)
234 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
235 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
236 REQUIRE_NOT_NULL(self);
238 if (vframe->Sandboxed && !function->IsSideEffectFree())
239 BOOST_THROW_EXCEPTION(ScriptError("Filter function must be side-effect free."));
241 ObjectLock olock(self);
242 for (const Value& item : self) {
243 if (!function->Invoke({ item }))
249 static Array::Ptr ArrayUnique()
251 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
252 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
253 REQUIRE_NOT_NULL(self);
254 return self->Unique();
257 static void ArrayFreeze()
259 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
260 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
264 Object::Ptr Array::GetPrototype()
266 static Dictionary::Ptr prototype = new Dictionary({
267 { "len", new Function("Array#len", ArrayLen, {}, true) },
268 { "set", new Function("Array#set", ArraySet, { "index", "value" }) },
269 { "get", new Function("Array#get", ArrayGet, { "index" }) },
270 { "add", new Function("Array#add", ArrayAdd, { "value" }) },
271 { "remove", new Function("Array#remove", ArrayRemove, { "index" }) },
272 { "contains", new Function("Array#contains", ArrayContains, { "value" }, true) },
273 { "clear", new Function("Array#clear", ArrayClear) },
274 { "sort", new Function("Array#sort", ArraySort, { "less_cmp" }, true) },
275 { "shallow_clone", new Function("Array#shallow_clone", ArrayShallowClone, {}, true) },
276 { "join", new Function("Array#join", ArrayJoin, { "separator" }, true) },
277 { "reverse", new Function("Array#reverse", ArrayReverse, {}, true) },
278 { "map", new Function("Array#map", ArrayMap, { "func" }, true) },
279 { "reduce", new Function("Array#reduce", ArrayReduce, { "reduce" }, true) },
280 { "filter", new Function("Array#filter", ArrayFilter, { "func" }, true) },
281 { "any", new Function("Array#any", ArrayAny, { "func" }, true) },
282 { "all", new Function("Array#all", ArrayAll, { "func" }, true) },
283 { "unique", new Function("Array#unique", ArrayUnique, {}, true) },
284 { "freeze", new Function("Array#freeze", ArrayFreeze, {}) }