summaryrefslogtreecommitdiffstats
path: root/lib/base/function-script.cpp
blob: e59e84dbcfe6296684748dc18a4ad60b7390c99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/function.hpp"
#include "base/functionwrapper.hpp"
#include "base/scriptframe.hpp"
#include "base/objectlock.hpp"
#include "base/exception.hpp"

using namespace icinga;

static Value FunctionCall(const std::vector<Value>& args)
{
	if (args.size() < 1)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for call()"));

	ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
	Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
	REQUIRE_NOT_NULL(self);

	std::vector<Value> uargs(args.begin() + 1, args.end());
	return self->InvokeThis(args[0], uargs);
}

static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
{
	ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
	Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
	REQUIRE_NOT_NULL(self);

	std::vector<Value> uargs;

	{
		ObjectLock olock(args);
		uargs = std::vector<Value>(args->Begin(), args->End());
	}

	return self->InvokeThis(thisArg, uargs);
}


Object::Ptr Function::GetPrototype()
{
	static Dictionary::Ptr prototype = new Dictionary({
		{ "call", new Function("Function#call", FunctionCall) },
		{ "callv", new Function("Function#callv", FunctionCallV) }
	});

	return prototype;
}