]> granicus.if.org Git - icinga2/blob - lib/base/function-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / function-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/function.hpp"
4 #include "base/functionwrapper.hpp"
5 #include "base/scriptframe.hpp"
6 #include "base/objectlock.hpp"
7 #include "base/exception.hpp"
8
9 using namespace icinga;
10
11 static Value FunctionCall(const std::vector<Value>& args)
12 {
13         if (args.size() < 1)
14                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for call()"));
15
16         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
17         Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
18         REQUIRE_NOT_NULL(self);
19
20         std::vector<Value> uargs(args.begin() + 1, args.end());
21         return self->InvokeThis(args[0], uargs);
22 }
23
24 static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
25 {
26         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
27         Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
28         REQUIRE_NOT_NULL(self);
29
30         std::vector<Value> uargs;
31
32         {
33                 ObjectLock olock(args);
34                 uargs = std::vector<Value>(args->Begin(), args->End());
35         }
36
37         return self->InvokeThis(thisArg, uargs);
38 }
39
40
41 Object::Ptr Function::GetPrototype()
42 {
43         static Dictionary::Ptr prototype = new Dictionary({
44                 { "call", new Function("Function#call", FunctionCall) },
45                 { "callv", new Function("Function#callv", FunctionCallV) }
46         });
47
48         return prototype;
49 }
50