]> granicus.if.org Git - icinga2/commitdiff
Re-implement WrapFunction() using C++11 features 5816/head
authorGunnar Beutner <gunnar.beutner@icinga.com>
Wed, 29 Nov 2017 10:53:45 +0000 (11:53 +0100)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Wed, 29 Nov 2017 10:53:45 +0000 (11:53 +0100)
19 files changed:
lib/base/CMakeLists.txt
lib/base/array-script.cpp
lib/base/boolean-script.cpp
lib/base/configobject-script.cpp
lib/base/datetime-script.cpp
lib/base/dictionary-script.cpp
lib/base/function-script.cpp
lib/base/function.hpp
lib/base/functionwrapper.cpp [deleted file]
lib/base/functionwrapper.hpp
lib/base/json-script.cpp
lib/base/math-script.cpp
lib/base/number-script.cpp
lib/base/object-script.cpp
lib/base/string-script.cpp
lib/base/typetype-script.cpp
lib/config/vmops.hpp
lib/icinga/checkable-script.cpp
test/livestatus-fixture.cpp

index 049226e9cf0fd9dc2d016d08e5880a99f336888a..97b9209ea5a65e3e198bb3213d368826fc7f8ff0 100644 (file)
@@ -34,7 +34,7 @@ set(base_SOURCES
   json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp
   netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp
   object-script.cpp objecttype.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp
-  function.cpp function.thpp function-script.cpp functionwrapper.cpp
+  function.cpp function.thpp function-script.cpp
   perfdatavalue.cpp perfdatavalue.thpp scriptglobal.cpp
   scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp socketevents-epoll.cpp socketevents-poll.cpp stacktrace.cpp
   statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp
index 1e9fd9965b4bf9c315d0601a546a8cc5fc698947..dd9d5628d8b6aa95141e4b7000f8a6730ae50a5f 100644 (file)
@@ -265,23 +265,23 @@ Object::Ptr Array::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("len", new Function("Array#len", WrapFunction(ArrayLen), {}, true));
-               prototype->Set("set", new Function("Array#set", WrapFunction(ArraySet), { "index", "value" }));
-               prototype->Set("get", new Function("Array#get", WrapFunction(ArrayGet), { "index" }));
-               prototype->Set("add", new Function("Array#add", WrapFunction(ArrayAdd), { "value" }));
-               prototype->Set("remove", new Function("Array#remove", WrapFunction(ArrayRemove), { "index" }));
-               prototype->Set("contains", new Function("Array#contains", WrapFunction(ArrayContains), { "value" }, true));
-               prototype->Set("clear", new Function("Array#clear", WrapFunction(ArrayClear)));
-               prototype->Set("sort", new Function("Array#sort", WrapFunction(ArraySort), { "less_cmp" }, true));
-               prototype->Set("shallow_clone", new Function("Array#shallow_clone", WrapFunction(ArrayShallowClone), {}, true));
-               prototype->Set("join", new Function("Array#join", WrapFunction(ArrayJoin), { "separator" }, true));
-               prototype->Set("reverse", new Function("Array#reverse", WrapFunction(ArrayReverse), {}, true));
-               prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), { "func" }, true));
-               prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, true));
-               prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), { "func" }, true));
-               prototype->Set("any", new Function("Array#any", WrapFunction(ArrayAny), { "func" }, true));
-               prototype->Set("all", new Function("Array#all", WrapFunction(ArrayAll), { "func" }, true));
-               prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), {}, true));
+               prototype->Set("len", new Function("Array#len", ArrayLen, {}, true));
+               prototype->Set("set", new Function("Array#set", ArraySet, { "index", "value" }));
+               prototype->Set("get", new Function("Array#get", ArrayGet, { "index" }));
+               prototype->Set("add", new Function("Array#add", ArrayAdd, { "value" }));
+               prototype->Set("remove", new Function("Array#remove", ArrayRemove, { "index" }));
+               prototype->Set("contains", new Function("Array#contains", ArrayContains, { "value" }, true));
+               prototype->Set("clear", new Function("Array#clear", ArrayClear));
+               prototype->Set("sort", new Function("Array#sort", ArraySort, { "less_cmp" }, true));
+               prototype->Set("shallow_clone", new Function("Array#shallow_clone", ArrayShallowClone, {}, true));
+               prototype->Set("join", new Function("Array#join", ArrayJoin, { "separator" }, true));
+               prototype->Set("reverse", new Function("Array#reverse", ArrayReverse, {}, true));
+               prototype->Set("map", new Function("Array#map", ArrayMap, { "func" }, true));
+               prototype->Set("reduce", new Function("Array#reduce", ArrayReduce, { "reduce" }, true));
+               prototype->Set("filter", new Function("Array#filter", ArrayFilter, { "func" }, true));
+               prototype->Set("any", new Function("Array#any", ArrayAny, { "func" }, true));
+               prototype->Set("all", new Function("Array#all", ArrayAll, { "func" }, true));
+               prototype->Set("unique", new Function("Array#unique", ArrayUnique, {}, true));
        }
 
        return prototype;
index 75c979ada254e7e24193323c21a13c71796cf29a..5741c06ad8da16f1557c7cf1d5f47620ec3f503b 100644 (file)
@@ -38,7 +38,7 @@ Object::Ptr Boolean::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("to_string", new Function("Boolean#to_string", WrapFunction(BooleanToString), {}, true));
+               prototype->Set("to_string", new Function("Boolean#to_string", BooleanToString, {}, true));
        }
 
        return prototype;
index dfe4455b6dbaeca0d2cdbfe6309aad6a5402cb21..d413c4ada25209a0c15415f5d45cd2c7b314d59e 100644 (file)
@@ -45,8 +45,8 @@ Object::Ptr ConfigObject::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", WrapFunction(ConfigObjectModifyAttribute), { "attr", "value" }, false));
-               prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", WrapFunction(ConfigObjectRestoreAttribute), { "attr", "value" }, false));
+               prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", ConfigObjectModifyAttribute, { "attr", "value" }, false));
+               prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", ConfigObjectRestoreAttribute, { "attr", "value" }, false));
        }
 
        return prototype;
index 7d9a14cc2e3ba2c45d606eff72d68a0c9d9e2a9a..d838790b56bb0002d5d07169b8c879efe6763216 100644 (file)
@@ -39,7 +39,7 @@ Object::Ptr DateTime::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("format", new Function("DateTime#format", WrapFunction(DateTimeFormat), { "format" }));
+               prototype->Set("format", new Function("DateTime#format", DateTimeFormat, { "format" }));
        }
 
        return prototype;
index 66aa26cbd7249876c24b1971c115c542cfbe68cc..0e976c097496cd45a51f26f4f6bcf9c29bef7a2b 100644 (file)
@@ -97,14 +97,14 @@ Object::Ptr Dictionary::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("len", new Function("Dictionary#len", WrapFunction(DictionaryLen), {}, true));
-               prototype->Set("set", new Function("Dictionary#set", WrapFunction(DictionarySet), { "key", "value" }));
-               prototype->Set("get", new Function("Dictionary#get", WrapFunction(DictionaryGet), { "key" }));
-               prototype->Set("remove", new Function("Dictionary#remove", WrapFunction(DictionaryRemove), { "key" }));
-               prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), { "key" }, true));
-               prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), {}, true));
-               prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), {}, true));
-               prototype->Set("values", new Function("Dictionary#values", WrapFunction(DictionaryValues), {}, true));
+               prototype->Set("len", new Function("Dictionary#len", DictionaryLen, {}, true));
+               prototype->Set("set", new Function("Dictionary#set", DictionarySet, { "key", "value" }));
+               prototype->Set("get", new Function("Dictionary#get", DictionaryGet, { "key" }));
+               prototype->Set("remove", new Function("Dictionary#remove", DictionaryRemove, { "key" }));
+               prototype->Set("contains", new Function("Dictionary#contains", DictionaryContains, { "key" }, true));
+               prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", DictionaryShallowClone, {}, true));
+               prototype->Set("keys", new Function("Dictionary#keys", DictionaryKeys, {}, true));
+               prototype->Set("values", new Function("Dictionary#values", DictionaryValues, {}, true));
        }
 
        return prototype;
index dab4255dee2d4b91b07ce1fa7eb0fc95c37ffbaa..09e85939d03cb9f7c3618e1646a801c7fbc6b299 100644 (file)
@@ -59,8 +59,8 @@ Object::Ptr Function::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("call", new Function("Function#call", WrapFunction(FunctionCall)));
-               prototype->Set("callv", new Function("Function#callv", WrapFunction(FunctionCallV)));
+               prototype->Set("call", new Function("Function#call", FunctionCall));
+               prototype->Set("callv", new Function("Function#callv", FunctionCallV));
        }
 
        return prototype;
index ed88db18b60d42d398794a018f6e4c588d5340ad..6f257863f7de54642a703dc24084bfdd49a7ec47 100644 (file)
@@ -42,8 +42,11 @@ public:
 
        typedef std::function<Value (const std::vector<Value>& arguments)> Callback;
 
-       Function(const String& name, const Callback& function, const std::vector<String>& args = std::vector<String>(),
-           bool side_effect_free = false, bool deprecated = false);
+       template<typename F>
+       Function(const String& name, F function, const std::vector<String>& args = std::vector<String>(),
+           bool side_effect_free = false, bool deprecated = false)
+           : Function(name, WrapFunction(function), args, side_effect_free, deprecated)
+       { }
 
        Value Invoke(const std::vector<Value>& arguments = std::vector<Value>());
        Value Invoke(const Value& otherThis, const std::vector<Value>& arguments = std::vector<Value>());
@@ -64,17 +67,20 @@ public:
 
 private:
        Callback m_Callback;
+
+       Function(const String& name, const Callback& function, const std::vector<String>& args,
+           bool side_effect_free, bool deprecated);
 };
 
 #define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
                ScriptGlobal::Set(#ns "." #name, sf); \
        }, 10)
 
 #define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
                ScriptGlobal::Set(#ns "." #name, sf); \
                Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \
                ScriptGlobal::Set("Deprecated.__" #name, dsf); \
@@ -82,7 +88,7 @@ private:
 
 #define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
                ScriptGlobal::Set(#ns "." #name, sf); \
                Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \
                ScriptGlobal::Set("Deprecated." #name, dsf); \
@@ -90,13 +96,13 @@ private:
 
 #define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
                ScriptGlobal::Set(#ns "." #name, sf); \
        }, 10)
 
 #define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
                ScriptGlobal::Set(#ns "." #name, sf); \
                Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \
                ScriptGlobal::Set("Deprecated.__" #name, dsf); \
@@ -104,7 +110,7 @@ private:
 
 #define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \
        INITIALIZE_ONCE_WITH_PRIORITY([]() { \
-               Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+               Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
                ScriptGlobal::Set(#ns "." #name, sf); \
                Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \
                ScriptGlobal::Set("Deprecated." #name, dsf); \
diff --git a/lib/base/functionwrapper.cpp b/lib/base/functionwrapper.cpp
deleted file mode 100644 (file)
index f99a0f7..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
- *                                                                            *
- * This program is free software; you can redistribute it and/or              *
- * modify it under the terms of the GNU General Public License                *
- * as published by the Free Software Foundation; either version 2             *
- * of the License, or (at your option) any later version.                     *
- *                                                                            *
- * This program is distributed in the hope that it will be useful,            *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
- * GNU General Public License for more details.                               *
- *                                                                            *
- * You should have received a copy of the GNU General Public License          *
- * along with this program; if not, write to the Free Software Foundation     *
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
- ******************************************************************************/
-
-#include "base/functionwrapper.hpp"
-
-using namespace icinga;
-
-Value icinga::FunctionWrapperVV(void (*function)(void), const std::vector<Value>&)
-{
-       function();
-
-       return Empty;
-}
-
-Value icinga::FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments)
-{
-       function(arguments);
-
-       return Empty;
-}
-
-std::function<Value (const std::vector<Value>& arguments)> icinga::WrapFunction(void (*function)(void))
-{
-       return std::bind(&FunctionWrapperVV, function, _1);
-}
index 835a79961739377497fa0c7a5817c65b15a8b80d..a87efeeafff6e7878e661a599b9cf7af50ba25eb 100644 (file)
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
 
-#ifndef SCRIPTFUNCTIONWRAPPER_H
-#define SCRIPTFUNCTIONWRAPPER_H
+#ifndef FUNCTIONWRAPPER_H
+#define FUNCTIONWRAPPER_H
 
 #include "base/i2-base.hpp"
 #include "base/value.hpp"
 #include <vector>
+#include <type_traits>
 
 using namespace std::placeholders;
 
 namespace icinga
 {
 
-Value FunctionWrapperVV(void (*function)(void), const std::vector<Value>& arguments);
-Value FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments);
-
-std::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapFunction(void (*function)(void));
-
-template<typename TR>
-Value FunctionWrapperR(TR (*function)(void), const std::vector<Value>&)
-{
-       return function();
-}
-
-template<typename TR>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(void))
-{
-       return std::bind(&FunctionWrapperR<TR>, function, _1);
-}
-
-template<typename T0>
-Value FunctionWrapperV(void (*function)(T0), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 1)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 1)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]));
-
-       return Empty;
-}
-
-template<typename T0>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0))
-{
-       return std::bind(&FunctionWrapperV<T0>, function, _1);
-}
-
-template<typename TR, typename T0>
-Value FunctionWrapperR(TR (*function)(T0), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 1)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 1)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]));
-}
-
-template<typename TR, typename T0>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0))
-{
-       return std::bind(&FunctionWrapperR<TR, T0>, function, _1);
-}
-
-template<typename T0, typename T1>
-Value FunctionWrapperV(void (*function)(T0, T1), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 2)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 2)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]));
-
-       return Empty;
-}
-
-template<typename T0, typename T1>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1))
+inline std::function<Value (const std::vector<Value>&)> WrapFunction(const std::function<Value(const std::vector<Value>&)>& function)
 {
-       return std::bind(&FunctionWrapperV<T0, T1>, function, _1);
+       return function;
 }
 
-template<typename TR, typename T0, typename T1>
-Value FunctionWrapperR(TR (*function)(T0, T1), const std::vector<Value>& arguments)
+inline std::function<Value (const std::vector<Value>&)> WrapFunction(void (*function)(const std::vector<Value>&))
 {
-       if (arguments.size() < 2)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 2)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]));
+       return [function](const std::vector<Value>& arguments) {
+               function(arguments);
+               return Empty;
+       };
 }
-
-template<typename TR, typename T0, typename T1>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1))
+template<typename Return>
+std::function<Value (const std::vector<Value>&)> WrapFunction(Return (*function)(const std::vector<Value>&))
 {
-       return std::bind(&FunctionWrapperR<TR, T0, T1>, function, _1);
+       return [function](const std::vector<Value>& arguments) {
+               return static_cast<Value>(function(arguments));
+       };
 }
 
-template<typename T0, typename T1, typename T2>
-Value FunctionWrapperV(void (*function)(T0, T1, T2), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 3)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 3)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]));
-
-       return Empty;
-}
+template <std::size_t... Indices>
+struct indices {
+       using next = indices<Indices..., sizeof...(Indices)>;
+};
 
-template<typename T0, typename T1, typename T2>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2))
-{
-       return std::bind(&FunctionWrapperV<T0, T1, T2>, function, _1);
-}
+template <std::size_t N>
+struct build_indices {
+       using type = typename build_indices<N-1>::type::next;
+};
 
-template<typename TR, typename T0, typename T1, typename T2>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 3)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 3)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+template <>
+struct build_indices<0> {
+       using type = indices<>;
+};
 
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]));
-}
+template <std::size_t N>
+using BuildIndices = typename build_indices<N>::type;
 
-template<typename TR, typename T0, typename T1, typename T2>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2))
+struct unpack_caller
 {
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2>, function, _1);
-}
+private:
+       template <typename FuncType, size_t... I>
+       auto call(FuncType f, const std::vector<Value>& args, indices<I...>) -> decltype(f(args[I]...))
+       {
+               return f(args[I]...);
+       }
 
-template<typename T0, typename T1, typename T2, typename T3>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 4)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 4)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+public:
+       template <typename FuncType>
+       auto operator () (FuncType f, const std::vector<Value>& args)
+           -> decltype(call(f, args, BuildIndices<boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity>{}))
+       {
+               return call(f, args, BuildIndices<boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity>{});
+       }
+};
 
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]));
+enum class enabler_t {};
 
-       return Empty;
-}
+template<bool T>
+using EnableIf = typename std::enable_if<T, enabler_t>::type;
 
-template<typename T0, typename T1, typename T2, typename T3>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3))
+template<typename FuncType>
+std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
+    EnableIf<std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
 {
-       return std::bind(&FunctionWrapperV<T0, T1, T2, T3>, function, _1);
-}
+       return [function](const std::vector<Value>& arguments) {
+               constexpr int arity = boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity;
 
-template<typename TR, typename T0, typename T1, typename T2, typename T3>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 4)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 4)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+               if (arguments.size() < arity)
+                       BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
+               else if (arguments.size() > arity)
+                       BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
 
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]));
+               unpack_caller()(function, arguments);
+               return Empty;
+       };
 }
 
-template<typename TR, typename T0, typename T1, typename T2, typename T3>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3))
+template<typename FuncType>
+std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
+               EnableIf<!std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
 {
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3>, function, _1);
-}
+       return [function](const std::vector<Value>& arguments) {
+               constexpr int arity = boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity;
 
-template<typename T0, typename T1, typename T2, typename T3, typename T4>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 5)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 5)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]));
-
-       return Empty;
-}
+               if (arity > 0) {
+                       if (arguments.size() < arity)
+                               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
+                       else if (arguments.size() > arity)
+                               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+               }
 
-template<typename T0, typename T1, typename T2, typename T3, typename T4>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4))
-{
-       return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 5)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 5)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4))
-{
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 6)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 6)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]));
-
-       return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5))
-{
-       return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 6)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 6)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5))
-{
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 7)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 7)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]),
-           static_cast<T6>(arguments[6]));
-
-       return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6))
-{
-       return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 7)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 7)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]),
-           static_cast<T6>(arguments[6]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6))
-{
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 8)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 8)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]),
-           static_cast<T6>(arguments[6]),
-           static_cast<T7>(arguments[7]));
-
-       return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
-{
-       return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
-{
-       if (arguments.size() < 8)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
-       else if (arguments.size() > 8)
-               BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
-       return function(static_cast<T0>(arguments[0]),
-           static_cast<T1>(arguments[1]),
-           static_cast<T2>(arguments[2]),
-           static_cast<T3>(arguments[3]),
-           static_cast<T4>(arguments[4]),
-           static_cast<T5>(arguments[5]),
-           static_cast<T6>(arguments[6]),
-           static_cast<T7>(arguments[7]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
-{
-       return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
-}
-
-template<typename TR>
-std::function<TR (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(const std::vector<Value>&))
-{
-       return std::bind<TR>(function, _1);
-}
-
-inline std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(const std::vector<Value>&))
-{
-       return std::bind(&FunctionWrapperVA, function, _1);
+               return unpack_caller()(function, arguments);
+       };
 }
 
 }
 
-#endif /* SCRIPTFUNCTION_H */
+#endif /* FUNCTIONWRAPPER_H */
index e342f5ee2ab54660a37c7cf7358d0b295141a488..3776b499df23f161ef108e481dc3b1b4810766d5 100644 (file)
@@ -35,8 +35,8 @@ INITIALIZE_ONCE([]() {
        Dictionary::Ptr jsonObj = new Dictionary();
 
        /* Methods */
-       jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), { "value" }, true));
-       jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), { "value" }, true));
+       jsonObj->Set("encode", new Function("Json#encode", JsonEncodeShim, { "value" }, true));
+       jsonObj->Set("decode", new Function("Json#decode", JsonDecode, { "value" }, true));
 
        ScriptGlobal::Set("Json", jsonObj);
 });
index eb1f4b44989ffaa0ed81af923df64be6ed88345f..994d567ac9a2f2f18d8f28869bc699fec78e06d3 100644 (file)
@@ -171,27 +171,27 @@ INITIALIZE_ONCE([]() {
        mathObj->Set("SQRT2", 1.41421356237309504880);
 
        /* Methods */
-       mathObj->Set("abs", new Function("Math#abs", WrapFunction(MathAbs), { "x" }, true));
-       mathObj->Set("acos", new Function("Math#acos", WrapFunction(MathAcos), { "x" }, true));
-       mathObj->Set("asin", new Function("Math#asin", WrapFunction(MathAsin), { "x" }, true));
-       mathObj->Set("atan", new Function("Math#atan", WrapFunction(MathAtan), { "x" }, true));
-       mathObj->Set("atan2", new Function("Math#atan2", WrapFunction(MathAtan2), { "x", "y" }, true));
-       mathObj->Set("ceil", new Function("Math#ceil", WrapFunction(MathCeil), { "x" }, true));
-       mathObj->Set("cos", new Function("Math#cos", WrapFunction(MathCos), { "x" }, true));
-       mathObj->Set("exp", new Function("Math#exp", WrapFunction(MathExp), { "x" }, true));
-       mathObj->Set("floor", new Function("Math#floor", WrapFunction(MathFloor), { "x" }, true));
-       mathObj->Set("log", new Function("Math#log", WrapFunction(MathLog), { "x" }, true));
-       mathObj->Set("max", new Function("Math#max", WrapFunction(MathMax), {}, true));
-       mathObj->Set("min", new Function("Math#min", WrapFunction(MathMin), {}, true));
-       mathObj->Set("pow", new Function("Math#pow", WrapFunction(MathPow), { "x", "y" }, true));
-       mathObj->Set("random", new Function("Math#random", WrapFunction(MathRandom), {}, true));
-       mathObj->Set("round", new Function("Math#round", WrapFunction(MathRound), { "x" }, true));
-       mathObj->Set("sin", new Function("Math#sin", WrapFunction(MathSin), { "x" }, true));
-       mathObj->Set("sqrt", new Function("Math#sqrt", WrapFunction(MathSqrt), { "x" }, true));
-       mathObj->Set("tan", new Function("Math#tan", WrapFunction(MathTan), { "x" }, true));
-       mathObj->Set("isnan", new Function("Math#isnan", WrapFunction(MathIsnan), { "x" }, true));
-       mathObj->Set("isinf", new Function("Math#isinf", WrapFunction(MathIsinf), { "x" }, true));
-       mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), { "x" }, true));
+       mathObj->Set("abs", new Function("Math#abs", MathAbs, { "x" }, true));
+       mathObj->Set("acos", new Function("Math#acos", MathAcos, { "x" }, true));
+       mathObj->Set("asin", new Function("Math#asin", MathAsin, { "x" }, true));
+       mathObj->Set("atan", new Function("Math#atan", MathAtan, { "x" }, true));
+       mathObj->Set("atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true));
+       mathObj->Set("ceil", new Function("Math#ceil", MathCeil, { "x" }, true));
+       mathObj->Set("cos", new Function("Math#cos", MathCos, { "x" }, true));
+       mathObj->Set("exp", new Function("Math#exp", MathExp, { "x" }, true));
+       mathObj->Set("floor", new Function("Math#floor", MathFloor, { "x" }, true));
+       mathObj->Set("log", new Function("Math#log", MathLog, { "x" }, true));
+       mathObj->Set("max", new Function("Math#max", MathMax, {}, true));
+       mathObj->Set("min", new Function("Math#min", MathMin, {}, true));
+       mathObj->Set("pow", new Function("Math#pow", MathPow, { "x", "y" }, true));
+       mathObj->Set("random", new Function("Math#random", MathRandom, {}, true));
+       mathObj->Set("round", new Function("Math#round", MathRound, { "x" }, true));
+       mathObj->Set("sin", new Function("Math#sin", MathSin, { "x" }, true));
+       mathObj->Set("sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true));
+       mathObj->Set("tan", new Function("Math#tan", MathTan, { "x" }, true));
+       mathObj->Set("isnan", new Function("Math#isnan", MathIsnan, { "x" }, true));
+       mathObj->Set("isinf", new Function("Math#isinf", MathIsinf, { "x" }, true));
+       mathObj->Set("sign", new Function("Math#sign", MathSign, { "x" }, true));
 
        ScriptGlobal::Set("Math", mathObj);
 });
index 4366277c74cdb5558108c3e750d55c4791c89277..7967b0650577106a517aa28425382ac613111eab 100644 (file)
@@ -37,7 +37,7 @@ Object::Ptr Number::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("to_string", new Function("Number#to_string", WrapFunction(NumberToString), {}, true));
+               prototype->Set("to_string", new Function("Number#to_string", NumberToString, {}, true));
        }
 
        return prototype;
index c8112a402414247dfa378614e3f59bf8479aae96..3e9faff28738415e55f675b7f36d1827fcf9cede 100644 (file)
@@ -52,9 +52,9 @@ Object::Ptr Object::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("to_string", new Function("Object#to_string", WrapFunction(ObjectToString), {}, true));
-               prototype->Set("notify_attribute", new Function("Object#notify_attribute", WrapFunction(ObjectNotifyAttribute), { "attribute" }, false));
-               prototype->Set("clone", new Function("Object#clone", WrapFunction(ObjectClone), {}, true));
+               prototype->Set("to_string", new Function("Object#to_string", ObjectToString, {}, true));
+               prototype->Set("notify_attribute", new Function("Object#notify_attribute", ObjectNotifyAttribute, { "attribute" }, false));
+               prototype->Set("clone", new Function("Object#clone", ObjectClone, {}, true));
        }
 
        return prototype;
index 60af63ddab1fcc7db77d4059ec0127fc3ff729c5..e84026112c586ef830b665d5ff6fce6573dd5545 100644 (file)
@@ -145,17 +145,17 @@ Object::Ptr String::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("len", new Function("String#len", WrapFunction(StringLen), {}, true));
-               prototype->Set("to_string", new Function("String#to_string", WrapFunction(StringToString), {}, true));
-               prototype->Set("substr", new Function("String#substr", WrapFunction(StringSubstr), { "start", "len" }, true));
-               prototype->Set("upper", new Function("String#upper", WrapFunction(StringUpper), {}, true));
-               prototype->Set("lower", new Function("String#lower", WrapFunction(StringLower), {}, true));
-               prototype->Set("split", new Function("String#split", WrapFunction(StringSplit), { "delims" }, true));
-               prototype->Set("find", new Function("String#find", WrapFunction(StringFind), { "str", "start" }, true));
-               prototype->Set("contains", new Function("String#contains", WrapFunction(StringContains), { "str" }, true));
-               prototype->Set("replace", new Function("String#replace", WrapFunction(StringReplace), { "search", "replacement" }, true));
-               prototype->Set("reverse", new Function("String#reverse", WrapFunction(StringReverse), {}, true));
-               prototype->Set("trim", new Function("String#trim", WrapFunction(StringTrim), {}, true));
+               prototype->Set("len", new Function("String#len", StringLen, {}, true));
+               prototype->Set("to_string", new Function("String#to_string", StringToString, {}, true));
+               prototype->Set("substr", new Function("String#substr", StringSubstr, { "start", "len" }, true));
+               prototype->Set("upper", new Function("String#upper", StringUpper, {}, true));
+               prototype->Set("lower", new Function("String#lower", StringLower, {}, true));
+               prototype->Set("split", new Function("String#split", StringSplit, { "delims" }, true));
+               prototype->Set("find", new Function("String#find", StringFind, { "str", "start" }, true));
+               prototype->Set("contains", new Function("String#contains", StringContains, { "str" }, true));
+               prototype->Set("replace", new Function("String#replace", StringReplace, { "search", "replacement" }, true));
+               prototype->Set("reverse", new Function("String#reverse", StringReverse, {}, true));
+               prototype->Set("trim", new Function("String#trim", StringTrim, {}, true));
        }
 
        return prototype;
index 08d616b2e1ec3629b39f997d73519e5cc0790641..66452fdcc3bf39f899dda19f6ab283dca094af33 100644 (file)
@@ -48,7 +48,7 @@ Object::Ptr TypeType::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", WrapFunction(TypeRegisterAttributeHandler), { "field", "callback" }, false));
+               prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", TypeRegisterAttributeHandler, { "field", "callback" }, false));
        }
 
        return prototype;
index c64ac1064d6b92a57fbc855411bc613b23bb4bda..25999e60b629ce23cbaaa08f5b8423b55292b1a5 100644 (file)
@@ -109,11 +109,27 @@ public:
 
        }
 
-       static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& args,
+       static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames,
            std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
        {
-               return new Function(name, std::bind(&FunctionWrapper, _1, args,
-                   EvaluateClosedVars(frame, closedVars), expression), args);
+               auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
+
+               auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) {
+                       if (arguments.size() < argNames.size())
+                               BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
+
+                       ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
+
+                       if (evaluatedClosedVars)
+                               evaluatedClosedVars->CopyTo(frame->Locals);
+
+                       for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), argNames.size()); i++)
+                               frame->Locals->Set(argNames[i], arguments[i]);
+
+                       return expression->Evaluate(*frame);
+               };
+
+               return new Function(name, wrapper, argNames);
        }
 
        static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
@@ -236,23 +252,6 @@ public:
        }
 
 private:
-       static inline Value FunctionWrapper(const std::vector<Value>& arguments,
-           const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
-       {
-               if (arguments.size() < funcargs.size())
-                       BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
-
-               ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
-
-               if (closedVars)
-                       closedVars->CopyTo(frame->Locals);
-
-               for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
-                       frame->Locals->Set(funcargs[i], arguments[i]);
-
-               return expr->Evaluate(*frame);
-       }
-
        static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
        {
                Dictionary::Ptr locals;
index 40626fef916771213347f410586d2e259d97a96c..31a58a3d408e9e7ad7f0b7cd7385b5a00e1cb873 100644 (file)
@@ -39,7 +39,7 @@ Object::Ptr Checkable::GetPrototype(void)
 
        if (!prototype) {
                prototype = new Dictionary();
-               prototype->Set("process_check_result", new Function("Checkable#process_check_result", WrapFunction(CheckableProcessCheckResult), { "cr" }, false));
+               prototype->Set("process_check_result", new Function("Checkable#process_check_result", CheckableProcessCheckResult, { "cr" }, false));
        }
 
        return prototype;
index a6bd7ea3b90dfaf112e3bbd54fcef7229d093bb5..054d05827421796a84613ff5662b630261d7968c 100644 (file)
@@ -31,7 +31,7 @@ struct LivestatusFixture
        {
                BOOST_TEST_MESSAGE("Preparing config objects...");
 
-               ConfigItem::RunWithActivationContext(new Function("CreateTestObjects", WrapFunction(CreateTestObjects)));
+               ConfigItem::RunWithActivationContext(new Function("CreateTestObjects", CreateTestObjects));
        }
 
        static void CreateTestObjects(void)