Register a new script frame in Function::Invoke

fixes #9848
This commit is contained in:
Gunnar Beutner 2016-08-08 13:53:45 +02:00 committed by Gunnar Beutner
parent 1cd8a25ab2
commit 8deeb73bb8
8 changed files with 19 additions and 19 deletions

View File

@ -155,7 +155,6 @@ static Array::Ptr ArrayMap(const Function::Ptr& function)
ObjectLock olock(self); ObjectLock olock(self);
BOOST_FOREACH(const Value& item, self) { BOOST_FOREACH(const Value& item, self) {
ScriptFrame uframe;
std::vector<Value> args; std::vector<Value> args;
args.push_back(item); args.push_back(item);
result->Add(function->Invoke(args)); result->Add(function->Invoke(args));
@ -179,7 +178,6 @@ static Value ArrayReduce(const Function::Ptr& function)
ObjectLock olock(self); ObjectLock olock(self);
for (size_t i = 1; i < self->GetLength(); i++) { for (size_t i = 1; i < self->GetLength(); i++) {
ScriptFrame uframe;
std::vector<Value> args; std::vector<Value> args;
args.push_back(result); args.push_back(result);
args.push_back(self->Get(i)); args.push_back(self->Get(i));
@ -201,7 +199,6 @@ static Array::Ptr ArrayFilter(const Function::Ptr& function)
ObjectLock olock(self); ObjectLock olock(self);
BOOST_FOREACH(const Value& item, self) { BOOST_FOREACH(const Value& item, self) {
ScriptFrame uframe;
std::vector<Value> args; std::vector<Value> args;
args.push_back(item); args.push_back(item);
if (function->Invoke(args)) if (function->Invoke(args))
@ -250,5 +247,4 @@ Object::Ptr Array::GetPrototype(void)
} }
return prototype; return prototype;
} }

View File

@ -33,9 +33,8 @@ static Value FunctionCall(const std::vector<Value>& args)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Function::Ptr self = static_cast<Function::Ptr>(vframe->Self); Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
ScriptFrame uframe(args[0]);
std::vector<Value> uargs(args.begin() + 1, args.end()); std::vector<Value> uargs(args.begin() + 1, args.end());
return self->Invoke(uargs); return self->Invoke(args[0], uargs);
} }
static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args) static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
@ -43,7 +42,6 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Function::Ptr self = static_cast<Function::Ptr>(vframe->Self); Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
ScriptFrame uframe(thisArg);
std::vector<Value> uargs; std::vector<Value> uargs;
{ {
@ -51,7 +49,7 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
uargs = std::vector<Value>(args->Begin(), args->End()); uargs = std::vector<Value>(args->Begin(), args->End());
} }
return self->Invoke(uargs); return self->Invoke(thisArg, uargs);
} }

View File

@ -20,6 +20,7 @@
#include "base/function.hpp" #include "base/function.hpp"
#include "base/primitivetype.hpp" #include "base/primitivetype.hpp"
#include "base/dictionary.hpp" #include "base/dictionary.hpp"
#include "base/scriptframe.hpp"
using namespace icinga; using namespace icinga;
@ -31,6 +32,14 @@ Function::Function(const Callback& function, bool side_effect_free)
Value Function::Invoke(const std::vector<Value>& arguments) Value Function::Invoke(const std::vector<Value>& arguments)
{ {
ScriptFrame frame;
return m_Callback(arguments);
}
Value Function::Invoke(const Value& otherThis, const std::vector<Value>& arguments)
{
ScriptFrame frame;
frame.Self = otherThis;
return m_Callback(arguments); return m_Callback(arguments);
} }

View File

@ -45,6 +45,7 @@ public:
Function(const Callback& function, bool side_effect_free = false); Function(const Callback& function, bool side_effect_free = false);
Value Invoke(const std::vector<Value>& arguments = std::vector<Value>()); Value Invoke(const std::vector<Value>& arguments = std::vector<Value>());
Value Invoke(const Value& otherThis, const std::vector<Value>& arguments = std::vector<Value>());
bool IsSideEffectFree(void) const; bool IsSideEffectFree(void) const;
static Object::Ptr GetPrototype(void); static Object::Ptr GetPrototype(void);

View File

@ -30,8 +30,6 @@ static void InvokeAttributeHandlerHelper(const Function::Ptr& callback,
{ {
std::vector<Value> arguments; std::vector<Value> arguments;
arguments.push_back(object); arguments.push_back(object);
ScriptFrame frame;
callback->Invoke(arguments); callback->Invoke(arguments);
} }

View File

@ -619,10 +619,7 @@ bool ConfigItem::RunWithActivationContext(const Function::Ptr& function)
if (!function) if (!function)
BOOST_THROW_EXCEPTION(ScriptError("'function' argument must not be null.")); BOOST_THROW_EXCEPTION(ScriptError("'function' argument must not be null."));
{ function->Invoke();
ScriptFrame frame;
function->Invoke();
}
WorkQueue upq(25000, Application::GetConcurrency()); WorkQueue upq(25000, Application::GetConcurrency());
upq.SetName("ConfigItem::RunWithActivationContext"); upq.SetName("ConfigItem::RunWithActivationContext");

View File

@ -89,9 +89,10 @@ public:
ScriptFrame vframe; ScriptFrame vframe;
if (!self.IsEmpty() || self.IsString()) if (!self.IsEmpty() || self.IsString())
vframe.Self = self; return func->Invoke(self, arguments);
else
return func->Invoke(arguments);
return func->Invoke(arguments);
} }
static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args, static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args,

View File

@ -223,8 +223,8 @@ Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const Resolver
_1, boost::cref(resolvers), cr, resolvedMacros, useResolvedMacros, _1, boost::cref(resolvers), cr, resolvedMacros, useResolvedMacros,
recursionLevel + 1))); recursionLevel + 1)));
ScriptFrame frame(resolvers_this); std::vector<Value> args;
return func->Invoke(); return func->Invoke(resolvers_this, args);
} }
Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers, Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,