1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
9 using namespace icinga;
11 static Value FunctionCall(const std::vector<Value>& args)
14 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for call()"));
16 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
17 Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
18 REQUIRE_NOT_NULL(self);
20 std::vector<Value> uargs(args.begin() + 1, args.end());
21 return self->InvokeThis(args[0], uargs);
24 static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
26 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
27 Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
28 REQUIRE_NOT_NULL(self);
30 std::vector<Value> uargs;
33 ObjectLock olock(args);
34 uargs = std::vector<Value>(args->Begin(), args->End());
37 return self->InvokeThis(thisArg, uargs);
41 Object::Ptr Function::GetPrototype()
43 static Dictionary::Ptr prototype = new Dictionary({
44 { "call", new Function("Function#call", FunctionCall) },
45 { "callv", new Function("Function#callv", FunctionCallV) }