mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-25 22:54:57 +02:00
parent
250fc54cd0
commit
ed29d06ab6
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
REGISTER_PRIMITIVE_TYPE(Function, Function::GetPrototype());
|
REGISTER_PRIMITIVE_TYPE_NOINST(Function, Function::GetPrototype());
|
||||||
|
|
||||||
Function::Function(const Callback& function)
|
Function::Function(const Callback& function)
|
||||||
: m_Callback(function)
|
: m_Callback(function)
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
PrimitiveType::PrimitiveType(const String& name)
|
PrimitiveType::PrimitiveType(const String& name, const ObjectFactory& factory)
|
||||||
: m_Name(name)
|
: m_Name(name), m_Factory(factory)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
String PrimitiveType::GetName(void) const
|
String PrimitiveType::GetName(void) const
|
||||||
@ -58,6 +58,6 @@ int PrimitiveType::GetFieldCount(void) const
|
|||||||
|
|
||||||
ObjectFactory PrimitiveType::GetFactory(void) const
|
ObjectFactory PrimitiveType::GetFactory(void) const
|
||||||
{
|
{
|
||||||
return NULL;
|
return m_Factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ namespace icinga
|
|||||||
class I2_BASE_API PrimitiveType : public Type
|
class I2_BASE_API PrimitiveType : public Type
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PrimitiveType(const String& name);
|
PrimitiveType(const String& name, const ObjectFactory& factory = ObjectFactory());
|
||||||
|
|
||||||
virtual String GetName(void) const;
|
virtual String GetName(void) const;
|
||||||
virtual Type::Ptr GetBaseType(void) const;
|
virtual Type::Ptr GetBaseType(void) const;
|
||||||
@ -44,6 +44,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
String m_Name;
|
String m_Name;
|
||||||
|
ObjectFactory m_Factory;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define REGISTER_BUILTIN_TYPE(type, prototype) \
|
#define REGISTER_BUILTIN_TYPE(type, prototype) \
|
||||||
@ -57,11 +58,11 @@ private:
|
|||||||
INITIALIZE_ONCE_WITH_PRIORITY(RegisterBuiltinType, 15); \
|
INITIALIZE_ONCE_WITH_PRIORITY(RegisterBuiltinType, 15); \
|
||||||
} } }
|
} } }
|
||||||
|
|
||||||
#define REGISTER_PRIMITIVE_TYPE(type, prototype) \
|
#define REGISTER_PRIMITIVE_TYPE_FACTORY(type, prototype, factory) \
|
||||||
namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \
|
namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \
|
||||||
void RegisterPrimitiveType(void) \
|
void RegisterPrimitiveType(void) \
|
||||||
{ \
|
{ \
|
||||||
icinga::Type::Ptr t = new PrimitiveType(#type); \
|
icinga::Type::Ptr t = new PrimitiveType(#type, factory);\
|
||||||
t->SetPrototype(prototype); \
|
t->SetPrototype(prototype); \
|
||||||
icinga::Type::Register(t); \
|
icinga::Type::Register(t); \
|
||||||
type::TypeInstance = t; \
|
type::TypeInstance = t; \
|
||||||
@ -70,6 +71,12 @@ private:
|
|||||||
} } } \
|
} } } \
|
||||||
DEFINE_TYPE_INSTANCE(type)
|
DEFINE_TYPE_INSTANCE(type)
|
||||||
|
|
||||||
|
#define REGISTER_PRIMITIVE_TYPE(type, prototype) \
|
||||||
|
REGISTER_PRIMITIVE_TYPE_FACTORY(type, prototype, DefaultObjectFactory<type>)
|
||||||
|
|
||||||
|
#define REGISTER_PRIMITIVE_TYPE_NOINST(type, prototype) \
|
||||||
|
REGISTER_PRIMITIVE_TYPE_FACTORY(type, prototype, NULL)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* PRIMITIVETYPE_H */
|
#endif /* PRIMITIVETYPE_H */
|
||||||
|
@ -395,6 +395,18 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin
|
|||||||
vfunc = vfuncres.GetValue();
|
vfunc = vfuncres.GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (vfunc.IsObjectType<Type>()) {
|
||||||
|
if (m_Args.empty())
|
||||||
|
return VMOps::ConstructorCall(vfunc, m_DebugInfo);
|
||||||
|
else if (m_Args.size() == 1) {
|
||||||
|
ExpressionResult argres = m_Args[0]->Evaluate(frame);
|
||||||
|
CHECK_RESULT(argres);
|
||||||
|
|
||||||
|
return VMOps::CopyConstructorCall(vfunc, argres.GetValue(), m_DebugInfo);
|
||||||
|
} else
|
||||||
|
BOOST_THROW_EXCEPTION(ScriptError("Too many arguments for constructor.", m_DebugInfo));
|
||||||
|
}
|
||||||
|
|
||||||
if (!vfunc.IsObjectType<Function>())
|
if (!vfunc.IsObjectType<Function>())
|
||||||
BOOST_THROW_EXCEPTION(ScriptError("Argument is not a callable object.", m_DebugInfo));
|
BOOST_THROW_EXCEPTION(ScriptError("Argument is not a callable object.", m_DebugInfo));
|
||||||
|
|
||||||
|
@ -54,6 +54,38 @@ public:
|
|||||||
return ScriptGlobal::Get(name);
|
return ScriptGlobal::Get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline Value CopyConstructorCall(const Type::Ptr& type, const Value& value, const DebugInfo& debugInfo = DebugInfo())
|
||||||
|
{
|
||||||
|
if (type->GetName() == "String")
|
||||||
|
return Convert::ToString(value);
|
||||||
|
else if (type->GetName() == "Number")
|
||||||
|
return Convert::ToDouble(value);
|
||||||
|
else if (type->GetName() == "Boolean")
|
||||||
|
return Convert::ToBool(value);
|
||||||
|
else if (!type->IsAssignableFrom(value.GetReflectionType()))
|
||||||
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid cast: Tried to cast object of type '" + value.GetReflectionType()->GetName() + "' to type '" + type->GetName() + "'", debugInfo));
|
||||||
|
else
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline Value ConstructorCall(const Type::Ptr& type, const DebugInfo& debugInfo = DebugInfo())
|
||||||
|
{
|
||||||
|
if (type->GetName() == "String")
|
||||||
|
return "";
|
||||||
|
else if (type->GetName() == "Number")
|
||||||
|
return 0;
|
||||||
|
else if (type->GetName() == "Boolean")
|
||||||
|
return false;
|
||||||
|
else {
|
||||||
|
Object::Ptr object = type->Instantiate();
|
||||||
|
|
||||||
|
if (!object)
|
||||||
|
BOOST_THROW_EXCEPTION(ScriptError("Failed to instantiate object of type '" + type->GetName() + "'", debugInfo));
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
|
static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const Function::Ptr& func, const std::vector<Value>& arguments)
|
||||||
{
|
{
|
||||||
boost::shared_ptr<ScriptFrame> vframe;
|
boost::shared_ptr<ScriptFrame> vframe;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user