]> granicus.if.org Git - icinga2/commitdiff
Register a new script frame in Function::Invoke
authorGunnar Beutner <gunnar@beutner.name>
Mon, 8 Aug 2016 11:53:45 +0000 (13:53 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 8 Aug 2016 12:12:08 +0000 (14:12 +0200)
fixes #9848

lib/base/array-script.cpp
lib/base/function-script.cpp
lib/base/function.cpp
lib/base/function.hpp
lib/base/typetype-script.cpp
lib/config/configitem.cpp
lib/config/vmops.hpp
lib/icinga/macroprocessor.cpp

index f5924791d3e764d2b50b0e94d5ecbc3542516982..a56cb45679a8651470efe0c6a222f86acd4c5181 100644 (file)
@@ -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<Value> 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<Value> 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<Value> 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
index 5f72013930f957b06dfef4e6f04de921b92de0ef..f0bd611180ea54f9b578be2a7a823b7022fc5a3f 100644 (file)
@@ -33,9 +33,8 @@ static Value FunctionCall(const std::vector<Value>& args)
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
        Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
 
-       ScriptFrame uframe(args[0]);
        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)
@@ -43,7 +42,6 @@ static Value FunctionCallV(const Value& thisArg, const Array::Ptr& args)
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
        Function::Ptr self = static_cast<Function::Ptr>(vframe->Self);
 
-       ScriptFrame uframe(thisArg);
        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());
        }
 
-       return self->Invoke(uargs);
+       return self->Invoke(thisArg, uargs);
 }
 
 
index 662d1127aba4fe317f10d6b228aa66a520a5c86f..d7ef94270537fec54cd38a1e68c364e55ec422d4 100644 (file)
@@ -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<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);
 }
 
index 69d71767195e1d53fc9284736e80f8826b8a123d..f08a6fdc74003c0ec9a5c27aaee2c61609e7b540 100644 (file)
@@ -45,6 +45,7 @@ public:
        Function(const Callback& function, bool side_effect_free = false);
 
        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;
 
        static Object::Ptr GetPrototype(void);
index bd420e659e7373e390b6747d302e471105f903cc..01a9950cfaee87865181a4ee5436c156ac916246 100644 (file)
@@ -30,8 +30,6 @@ static void InvokeAttributeHandlerHelper(const Function::Ptr& callback,
 {
        std::vector<Value> arguments;
        arguments.push_back(object);
-       
-       ScriptFrame frame;
        callback->Invoke(arguments);
 }
 
index 8c8370e5ea866e7da05d1eb74542e2b0f6b6ac21..35a717f2e0a4593a2571a91826d2e0c6404d8227 100644 (file)
@@ -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");
index 1479da91f909e0fdcf6e0d0e4b7783e48e70af27..1dc5b2b948337ec271f193fdf3181834accf1258 100644 (file)
@@ -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<String>& args,
index 11ab8338e02df539448a9cd7f9e83db81e2c61cc..a985fa933332c049efaafecb12e969e414078687 100644 (file)
@@ -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<Value> args;
+       return func->Invoke(resolvers_this, args);
 }
 
 Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,