Skip to content

Commit

Permalink
Implement support for marking functions as deprecated
Browse files Browse the repository at this point in the history
fixes #12393
  • Loading branch information
gunnarbeutner committed Aug 10, 2016
1 parent 9517abb commit 54bbaf9
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 102 deletions.
3 changes: 2 additions & 1 deletion lib/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mkclass_target(application.ti application.tcpp application.thpp)
mkclass_target(configobject.ti configobject.tcpp configobject.thpp)
mkclass_target(datetime.ti datetime.tcpp datetime.thpp)
mkclass_target(filelogger.ti filelogger.tcpp filelogger.thpp)
mkclass_target(function.ti function.tcpp function.thpp)
mkclass_target(logger.ti logger.tcpp logger.thpp)
mkclass_target(streamlogger.ti streamlogger.tcpp streamlogger.thpp)
mkclass_target(sysloglogger.ti sysloglogger.tcpp sysloglogger.thpp)
Expand All @@ -32,7 +33,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-script.cpp functionwrapper.cpp scriptglobal.cpp
function.cpp function.thpp function-script.cpp functionwrapper.cpp 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
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp threadpool.cpp timer.cpp
Expand Down
32 changes: 16 additions & 16 deletions lib/base/array-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,22 @@ Object::Ptr Array::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("len", new Function(WrapFunction(ArrayLen), true));
prototype->Set("set", new Function(WrapFunction(ArraySet)));
prototype->Set("get", new Function(WrapFunction(ArrayGet)));
prototype->Set("add", new Function(WrapFunction(ArrayAdd)));
prototype->Set("remove", new Function(WrapFunction(ArrayRemove)));
prototype->Set("contains", new Function(WrapFunction(ArrayContains), true));
prototype->Set("clear", new Function(WrapFunction(ArrayClear)));
prototype->Set("sort", new Function(WrapFunction(ArraySort), true));
prototype->Set("shallow_clone", new Function(WrapFunction(ArrayShallowClone), true));
prototype->Set("join", new Function(WrapFunction(ArrayJoin), true));
prototype->Set("reverse", new Function(WrapFunction(ArrayReverse), true));
prototype->Set("map", new Function(WrapFunction(ArrayMap), true));
prototype->Set("reduce", new Function(WrapFunction(ArrayReduce), true));
prototype->Set("filter", new Function(WrapFunction(ArrayFilter), true));
prototype->Set("unique", new Function(WrapFunction(ArrayUnique), true));
prototype->Set("len", new Function("Array#len", WrapFunction(ArrayLen), true));
prototype->Set("set", new Function("Array#set", WrapFunction(ArraySet)));
prototype->Set("get", new Function("Array#get", WrapFunction(ArrayGet)));
prototype->Set("add", new Function("Array#add", WrapFunction(ArrayAdd)));
prototype->Set("remove", new Function("Array#remove", WrapFunction(ArrayRemove)));
prototype->Set("contains", new Function("Array#contains", WrapFunction(ArrayContains), true));
prototype->Set("clear", new Function("Array#clear", WrapFunction(ArrayClear)));
prototype->Set("sort", new Function("Array#sort", WrapFunction(ArraySort), true));
prototype->Set("shallow_clone", new Function("Array#shallow_clone", WrapFunction(ArrayShallowClone), true));
prototype->Set("join", new Function("Array#join", WrapFunction(ArrayJoin), true));
prototype->Set("reverse", new Function("Array#reverse", WrapFunction(ArrayReverse), true));
prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), true));
prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), true));
prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), true));
prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), true));
}

return prototype;
}
}
2 changes: 1 addition & 1 deletion lib/base/boolean-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Object::Ptr Boolean::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("to_string", new Function(WrapFunction(BooleanToString), true));
prototype->Set("to_string", new Function("Boolean#to_string", WrapFunction(BooleanToString), true));
}

return prototype;
Expand Down
4 changes: 2 additions & 2 deletions lib/base/configobject-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Object::Ptr ConfigObject::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("modify_attribute", new Function(WrapFunction(ConfigObjectModifyAttribute), false));
prototype->Set("restore_attribute", new Function(WrapFunction(ConfigObjectRestoreAttribute), false));
prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", WrapFunction(ConfigObjectModifyAttribute), false));
prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", WrapFunction(ConfigObjectRestoreAttribute), false));
}

return prototype;
Expand Down
2 changes: 1 addition & 1 deletion lib/base/datetime-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Object::Ptr DateTime::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("format", new Function(WrapFunction(DateTimeFormat)));
prototype->Set("format", new Function("DateTime#format", WrapFunction(DateTimeFormat)));
}

return prototype;
Expand Down
14 changes: 7 additions & 7 deletions lib/base/dictionary-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ Object::Ptr Dictionary::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("len", new Function(WrapFunction(DictionaryLen), true));
prototype->Set("set", new Function(WrapFunction(DictionarySet)));
prototype->Set("get", new Function(WrapFunction(DictionaryGet)));
prototype->Set("remove", new Function(WrapFunction(DictionaryRemove)));
prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true));
prototype->Set("shallow_clone", new Function(WrapFunction(DictionaryShallowClone), true));
prototype->Set("keys", new Function(WrapFunction(DictionaryKeys), true));
prototype->Set("len", new Function("Dictionary#len", WrapFunction(DictionaryLen), true));
prototype->Set("set", new Function("Dictionary#set", WrapFunction(DictionarySet)));
prototype->Set("get", new Function("Dictionary#get", WrapFunction(DictionaryGet)));
prototype->Set("remove", new Function("Dictionary#remove", WrapFunction(DictionaryRemove)));
prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), true));
prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), true));
prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), true));
}

return prototype;
Expand Down
4 changes: 2 additions & 2 deletions lib/base/function-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Object::Ptr Function::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("call", new Function(WrapFunction(FunctionCall)));
prototype->Set("callv", new Function(WrapFunction(FunctionCallV)));
prototype->Set("call", new Function("Function#call", WrapFunction(FunctionCall)));
prototype->Set("callv", new Function("Function#callv", WrapFunction(FunctionCallV)));
}

return prototype;
Expand Down
20 changes: 9 additions & 11 deletions lib/base/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
******************************************************************************/

#include "base/function.hpp"
#include "base/primitivetype.hpp"
#include "base/dictionary.hpp"
#include "base/function.tcpp"
#include "base/scriptframe.hpp"

using namespace icinga;

REGISTER_PRIMITIVE_TYPE_NOINST(Function, Object, Function::GetPrototype());
REGISTER_TYPE_WITH_PROTOTYPE(Function, Function::GetPrototype());

Function::Function(const Callback& function, bool side_effect_free)
: m_Callback(function), m_SideEffectFree(side_effect_free)
{ }
Function::Function(const String& name, const Callback& function, bool side_effect_free, bool deprecated)
: m_Callback(function)
{
SetName(name, true);
SetSideEffectFree(side_effect_free, true);
SetDeprecated(deprecated, true);
}

Value Function::Invoke(const std::vector<Value>& arguments)
{
Expand All @@ -43,11 +46,6 @@ Value Function::Invoke(const Value& otherThis, const std::vector<Value>& argumen
return m_Callback(arguments);
}

bool Function::IsSideEffectFree(void) const
{
return m_SideEffectFree;
}

Object::Ptr Function::Clone(void) const
{
return const_cast<Function *>(this);
Expand Down
29 changes: 21 additions & 8 deletions lib/base/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define SCRIPTFUNCTION_H

#include "base/i2-base.hpp"
#include "base/function.thpp"
#include "base/value.hpp"
#include "base/functionwrapper.hpp"
#include "base/scriptglobal.hpp"
Expand All @@ -35,32 +36,40 @@ namespace icinga
*
* @ingroup base
*/
class I2_BASE_API Function : public Object
class I2_BASE_API Function : public ObjectImpl<Function>
{
public:
DECLARE_OBJECT(Function);

typedef boost::function<Value (const std::vector<Value>& arguments)> Callback;

Function(const Callback& function, bool side_effect_free = false);
Function(const String& name, const Callback& function, bool side_effect_free = false, bool deprecated = 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;

inline bool IsSideEffectFree(void) const
{
return GetSideEffectFree();
}

inline bool IsDeprecated(void) const
{
return GetDeprecated();
}

static Object::Ptr GetPrototype(void);

virtual Object::Ptr Clone(void) const override;

private:
Callback m_Callback;
bool m_SideEffectFree;
};

#define REGISTER_SCRIPTFUNCTION(name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback)); \
Function::Ptr sf = new icinga::Function(#name, WrapFunction(callback)); \
ScriptGlobal::Set(#name, sf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
Expand All @@ -69,16 +78,18 @@ class I2_BASE_API Function : public Object
#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback)); \
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function(#name " (deprecated)", WrapFunction(callback), false, true); \
ScriptGlobal::Set(#name, dsf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }

#define REGISTER_SAFE_SCRIPTFUNCTION(name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback), true); \
Function::Ptr sf = new icinga::Function(#name, WrapFunction(callback), true); \
ScriptGlobal::Set(#name, sf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
Expand All @@ -87,8 +98,10 @@ class I2_BASE_API Function : public Object
#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback) \
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
void RegisterFunction(void) { \
Function::Ptr sf = new icinga::Function(WrapFunction(callback), true); \
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function(#name " (deprecated)", WrapFunction(callback), true, true); \
ScriptGlobal::Set(#name, dsf); \
} \
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
} } }
Expand Down
34 changes: 34 additions & 0 deletions lib/base/function.ti
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* 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/configobject.hpp"

library base;

namespace icinga
{

abstract class Function
{
String "name";
bool side_effect_free;
bool deprecated;
};

}
4 changes: 2 additions & 2 deletions lib/base/json-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void InitializeJsonObj(void)
Dictionary::Ptr jsonObj = new Dictionary();

/* Methods */
jsonObj->Set("encode", new Function(WrapFunction(JsonEncodeShim), true));
jsonObj->Set("decode", new Function(WrapFunction(JsonDecode), true));
jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), true));
jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), true));

ScriptGlobal::Set("Json", jsonObj);
}
Expand Down
42 changes: 21 additions & 21 deletions lib/base/math-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,27 @@ static void InitializeMathObj(void)
mathObj->Set("SQRT2", 1.41421356237309504880);

/* Methods */
mathObj->Set("abs", new Function(WrapFunction(MathAbs), true));
mathObj->Set("acos", new Function(WrapFunction(MathAcos), true));
mathObj->Set("asin", new Function(WrapFunction(MathAsin), true));
mathObj->Set("atan", new Function(WrapFunction(MathAtan), true));
mathObj->Set("atan2", new Function(WrapFunction(MathAtan2), true));
mathObj->Set("ceil", new Function(WrapFunction(MathCeil), true));
mathObj->Set("cos", new Function(WrapFunction(MathCos), true));
mathObj->Set("exp", new Function(WrapFunction(MathExp), true));
mathObj->Set("floor", new Function(WrapFunction(MathFloor), true));
mathObj->Set("log", new Function(WrapFunction(MathLog), true));
mathObj->Set("max", new Function(WrapFunction(MathMax), true));
mathObj->Set("min", new Function(WrapFunction(MathMin), true));
mathObj->Set("pow", new Function(WrapFunction(MathPow), true));
mathObj->Set("random", new Function(WrapFunction(MathRandom), true));
mathObj->Set("round", new Function(WrapFunction(MathRound), true));
mathObj->Set("sin", new Function(WrapFunction(MathSin), true));
mathObj->Set("sqrt", new Function(WrapFunction(MathSqrt), true));
mathObj->Set("tan", new Function(WrapFunction(MathTan), true));
mathObj->Set("isnan", new Function(WrapFunction(MathIsnan), true));
mathObj->Set("isinf", new Function(WrapFunction(MathIsinf), true));
mathObj->Set("sign", new Function(WrapFunction(MathSign), true));
mathObj->Set("abs", new Function("Math#abs", WrapFunction(MathAbs), true));
mathObj->Set("acos", new Function("Math#acos", WrapFunction(MathAcos), true));
mathObj->Set("asin", new Function("Math#asin", WrapFunction(MathAsin), true));
mathObj->Set("atan", new Function("Math#atan", WrapFunction(MathAtan), true));
mathObj->Set("atan2", new Function("Math#atan2", WrapFunction(MathAtan2), true));
mathObj->Set("ceil", new Function("Math#ceil", WrapFunction(MathCeil), true));
mathObj->Set("cos", new Function("Math#cos", WrapFunction(MathCos), true));
mathObj->Set("exp", new Function("Math#exp", WrapFunction(MathExp), true));
mathObj->Set("floor", new Function("Math#floor", WrapFunction(MathFloor), true));
mathObj->Set("log", new Function("Math#log", WrapFunction(MathLog), 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), true));
mathObj->Set("random", new Function("Math#random", WrapFunction(MathRandom), true));
mathObj->Set("round", new Function("Math#round", WrapFunction(MathRound), true));
mathObj->Set("sin", new Function("Math#sin", WrapFunction(MathSin), true));
mathObj->Set("sqrt", new Function("Math#sqrt", WrapFunction(MathSqrt), true));
mathObj->Set("tan", new Function("Math#tan", WrapFunction(MathTan), true));
mathObj->Set("isnan", new Function("Math#isnan", WrapFunction(MathIsnan), true));
mathObj->Set("isinf", new Function("Math#isinf", WrapFunction(MathIsinf), true));
mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), true));

ScriptGlobal::Set("Math", mathObj);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/base/number-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Object::Ptr Number::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("to_string", new Function(WrapFunction(NumberToString), true));
prototype->Set("to_string", new Function("Number#to_string", WrapFunction(NumberToString), true));
}

return prototype;
Expand Down
6 changes: 3 additions & 3 deletions lib/base/object-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ Object::Ptr Object::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("to_string", new Function(WrapFunction(ObjectToString), true));
prototype->Set("notify_attribute", new Function(WrapFunction(ObjectNotifyAttribute), false));
prototype->Set("clone", new Function(WrapFunction(ObjectClone), true));
prototype->Set("to_string", new Function("Object#to_string", WrapFunction(ObjectToString), true));
prototype->Set("notify_attribute", new Function("Object#notify_attribute", WrapFunction(ObjectNotifyAttribute), false));
prototype->Set("clone", new Function("Object#clone", WrapFunction(ObjectClone), true));
}

return prototype;
Expand Down
22 changes: 11 additions & 11 deletions lib/base/string-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ Object::Ptr String::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("len", new Function(WrapFunction(StringLen), true));
prototype->Set("to_string", new Function(WrapFunction(StringToString), true));
prototype->Set("substr", new Function(WrapFunction(StringSubstr), true));
prototype->Set("upper", new Function(WrapFunction(StringUpper), true));
prototype->Set("lower", new Function(WrapFunction(StringLower), true));
prototype->Set("split", new Function(WrapFunction(StringSplit), true));
prototype->Set("find", new Function(WrapFunction(StringFind), true));
prototype->Set("contains", new Function(WrapFunction(StringContains), true));
prototype->Set("replace", new Function(WrapFunction(StringReplace), true));
prototype->Set("reverse", new Function(WrapFunction(StringReverse), true));
prototype->Set("trim", new Function(WrapFunction(StringTrim), true));
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), 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), true));
prototype->Set("find", new Function("String#find", WrapFunction(StringFind), true));
prototype->Set("contains", new Function("String#contains", WrapFunction(StringContains), true));
prototype->Set("replace", new Function("String#replace", WrapFunction(StringReplace), true));
prototype->Set("reverse", new Function("String#reverse", WrapFunction(StringReverse), true));
prototype->Set("trim", new Function("String#trim", WrapFunction(StringTrim), true));
}

return prototype;
Expand Down
2 changes: 1 addition & 1 deletion lib/base/typetype-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Object::Ptr TypeType::GetPrototype(void)

if (!prototype) {
prototype = new Dictionary();
prototype->Set("register_attribute_handler", new Function(WrapFunction(TypeRegisterAttributeHandler), false));
prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", WrapFunction(TypeRegisterAttributeHandler), false));
}

return prototype;
Expand Down
Loading

0 comments on commit 54bbaf9

Please sign in to comment.