diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index f5924791d..a56cb4567 100644 --- a/lib/base/array-script.cpp +++ b/lib/base/array-script.cpp @@ -155,7 +155,6 @@ static Array::Ptr ArrayMap(const Function::Ptr& function) ObjectLock olock(self); BOOST_FOREACH(const Value& item, self) { - ScriptFrame uframe; std::vector args; args.push_back(item); result->Add(function->Invoke(args)); @@ -179,7 +178,6 @@ static Value ArrayReduce(const Function::Ptr& function) ObjectLock olock(self); for (size_t i = 1; i < self->GetLength(); i++) { - ScriptFrame uframe; std::vector args; args.push_back(result); args.push_back(self->Get(i)); @@ -201,7 +199,6 @@ static Array::Ptr ArrayFilter(const Function::Ptr& function) ObjectLock olock(self); BOOST_FOREACH(const Value& item, self) { - ScriptFrame uframe; std::vector args; args.push_back(item); if (function->Invoke(args)) @@ -250,5 +247,4 @@ Object::Ptr Array::GetPrototype(void) } return prototype; -} - +} \ No newline at end of file diff --git a/lib/base/function-script.cpp b/lib/base/function-script.cpp index 5f7201393..f0bd61118 100644 --- a/lib/base/function-script.cpp +++ b/lib/base/function-script.cpp @@ -33,9 +33,8 @@ static Value FunctionCall(const std::vector& args) ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); Function::Ptr self = static_cast(vframe->Self); - ScriptFrame uframe(args[0]); std::vector 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) @@ -43,7 +42,6 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args) ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); Function::Ptr self = static_cast(vframe->Self); - ScriptFrame uframe(thisArg); std::vector uargs; { @@ -51,7 +49,7 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args) uargs = std::vector(args->Begin(), args->End()); } - return self->Invoke(uargs); + return self->Invoke(thisArg, uargs); } diff --git a/lib/base/function.cpp b/lib/base/function.cpp index 662d1127a..d7ef94270 100644 --- a/lib/base/function.cpp +++ b/lib/base/function.cpp @@ -20,6 +20,7 @@ #include "base/function.hpp" #include "base/primitivetype.hpp" #include "base/dictionary.hpp" +#include "base/scriptframe.hpp" using namespace icinga; @@ -31,6 +32,14 @@ Function::Function(const Callback& function, bool side_effect_free) Value Function::Invoke(const std::vector& arguments) { + ScriptFrame frame; + return m_Callback(arguments); +} + +Value Function::Invoke(const Value& otherThis, const std::vector& arguments) +{ + ScriptFrame frame; + frame.Self = otherThis; return m_Callback(arguments); } diff --git a/lib/base/function.hpp b/lib/base/function.hpp index 69d717671..f08a6fdc7 100644 --- a/lib/base/function.hpp +++ b/lib/base/function.hpp @@ -45,6 +45,7 @@ public: Function(const Callback& function, bool side_effect_free = false); Value Invoke(const std::vector& arguments = std::vector()); + Value Invoke(const Value& otherThis, const std::vector& arguments = std::vector()); bool IsSideEffectFree(void) const; static Object::Ptr GetPrototype(void); diff --git a/lib/base/typetype-script.cpp b/lib/base/typetype-script.cpp index bd420e659..01a9950cf 100644 --- a/lib/base/typetype-script.cpp +++ b/lib/base/typetype-script.cpp @@ -30,8 +30,6 @@ static void InvokeAttributeHandlerHelper(const Function::Ptr& callback, { std::vector arguments; arguments.push_back(object); - - ScriptFrame frame; callback->Invoke(arguments); } diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 8c8370e5e..35a717f2e 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -619,10 +619,7 @@ bool ConfigItem::RunWithActivationContext(const Function::Ptr& function) if (!function) BOOST_THROW_EXCEPTION(ScriptError("'function' argument must not be null.")); - { - ScriptFrame frame; - function->Invoke(); - } + function->Invoke(); WorkQueue upq(25000, Application::GetConcurrency()); upq.SetName("ConfigItem::RunWithActivationContext"); diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 1479da91f..1dc5b2b94 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -89,9 +89,10 @@ public: ScriptFrame vframe; 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& args, diff --git a/lib/icinga/macroprocessor.cpp b/lib/icinga/macroprocessor.cpp index 11ab8338e..a985fa933 100644 --- a/lib/icinga/macroprocessor.cpp +++ b/lib/icinga/macroprocessor.cpp @@ -223,8 +223,8 @@ Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const Resolver _1, boost::cref(resolvers), cr, resolvedMacros, useResolvedMacros, recursionLevel + 1))); - ScriptFrame frame(resolvers_this); - return func->Invoke(); + std::vector args; + return func->Invoke(resolvers_this, args); } Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,