[icinga-checkins] icinga.org: icinga2/master: Register a new script frame in Function:: Invoke

git at icinga.org git at icinga.org
Mon Aug 8 14:14:54 CEST 2016


Module: icinga2
Branch: master
Commit: 8deeb73bb8c9f6b7e4ce594ccec102554ac205c7
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=8deeb73bb8c9f6b7e4ce594ccec102554ac205c7

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Mon Aug  8 13:53:45 2016 +0200

Register a new script frame in Function::Invoke

fixes #9848

---

 lib/base/array-script.cpp     |    6 +-----
 lib/base/function-script.cpp  |    6 ++----
 lib/base/function.cpp         |    9 +++++++++
 lib/base/function.hpp         |    1 +
 lib/base/typetype-script.cpp  |    2 --
 lib/config/configitem.cpp     |    5 +----
 lib/config/vmops.hpp          |    5 +++--
 lib/icinga/macroprocessor.cpp |    4 ++--
 8 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp
index f592479..a56cb45 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<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
diff --git a/lib/base/function-script.cpp b/lib/base/function-script.cpp
index 5f72013..f0bd611 100644
--- a/lib/base/function-script.cpp
+++ b/lib/base/function-script.cpp
@@ -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);
 }
 
 
diff --git a/lib/base/function.cpp b/lib/base/function.cpp
index 662d112..d7ef942 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<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);
 }
 
diff --git a/lib/base/function.hpp b/lib/base/function.hpp
index 69d7176..f08a6fd 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<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);
diff --git a/lib/base/typetype-script.cpp b/lib/base/typetype-script.cpp
index bd420e6..01a9950 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<Value> arguments;
 	arguments.push_back(object);
-	
-	ScriptFrame frame;
 	callback->Invoke(arguments);
 }
 
diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp
index 8c8370e..35a717f 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 1479da9..1dc5b2b 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<String>& args,
diff --git a/lib/icinga/macroprocessor.cpp b/lib/icinga/macroprocessor.cpp
index 11ab833..a985fa9 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<Value> args;
+	return func->Invoke(resolvers_this, args);
 }
 
 Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,



More information about the icinga-checkins mailing list