mirror of
https://github.com/Icinga/icinga2.git
synced 2025-04-08 17:05:25 +02:00
parent
913bfe4eb2
commit
3008d5ef31
@ -23,6 +23,8 @@
|
||||
#include "base/primitivetype.hpp"
|
||||
#include "base/dictionary.hpp"
|
||||
#include "base/configwriter.hpp"
|
||||
#include "base/convert.hpp"
|
||||
#include "base/exception.hpp"
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
using namespace icinga;
|
||||
@ -208,3 +210,31 @@ String Array::ToString(void) const
|
||||
ConfigWriter::EmitArray(msgbuf, 1, const_cast<Array *>(this));
|
||||
return msgbuf.str();
|
||||
}
|
||||
|
||||
Value Array::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
|
||||
{
|
||||
int index;
|
||||
|
||||
try {
|
||||
index = Convert::ToLong(field);
|
||||
} catch (...) {
|
||||
return Object::GetFieldByName(field, sandboxed, debugInfo);
|
||||
}
|
||||
|
||||
ObjectLock olock(this);
|
||||
|
||||
if (index < 0 || index >= GetLength())
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
|
||||
|
||||
return Get(index);
|
||||
}
|
||||
|
||||
void Array::SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo)
|
||||
{
|
||||
ObjectLock olock(this);
|
||||
|
||||
int index = Convert::ToLong(field);
|
||||
if (index >= GetLength())
|
||||
Resize(index + 1);
|
||||
Set(index, value);
|
||||
}
|
||||
|
@ -124,6 +124,9 @@ public:
|
||||
|
||||
virtual String ToString(void) const override;
|
||||
|
||||
virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
|
||||
virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo) override;
|
||||
|
||||
private:
|
||||
std::vector<Value> m_Data; /**< The data for the array. */
|
||||
};
|
||||
|
@ -197,3 +197,23 @@ String Dictionary::ToString(void) const
|
||||
ConfigWriter::EmitScope(msgbuf, 1, const_cast<Dictionary *>(this));
|
||||
return msgbuf.str();
|
||||
}
|
||||
|
||||
Value Dictionary::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
|
||||
{
|
||||
Value value;
|
||||
|
||||
if (Get(field, &value))
|
||||
return value;
|
||||
else
|
||||
return GetPrototypeField(const_cast<Dictionary *>(this), field, false, debugInfo);
|
||||
}
|
||||
|
||||
void Dictionary::SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo)
|
||||
{
|
||||
Set(field, value);
|
||||
}
|
||||
|
||||
bool Dictionary::HasOwnField(const String& field) const
|
||||
{
|
||||
return Contains(field);
|
||||
}
|
||||
|
@ -117,6 +117,10 @@ public:
|
||||
|
||||
virtual String ToString(void) const override;
|
||||
|
||||
virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
|
||||
virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo) override;
|
||||
virtual bool HasOwnField(const String& field) const override;
|
||||
|
||||
private:
|
||||
std::map<String, Value> m_Data; /**< The data for the dictionary. */
|
||||
};
|
||||
|
@ -24,7 +24,9 @@
|
||||
#include "base/utility.hpp"
|
||||
#include "base/timer.hpp"
|
||||
#include "base/logger.hpp"
|
||||
#include "base/exception.hpp"
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/lexical_cast/bad_lexical_cast.hpp>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
@ -98,6 +100,63 @@ Value Object::GetField(int id) const
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
|
||||
}
|
||||
|
||||
bool Object::HasOwnField(const String& field) const
|
||||
{
|
||||
Type::Ptr type = GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
return false;
|
||||
|
||||
return type->GetFieldId(field) != -1;
|
||||
}
|
||||
|
||||
Value Object::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
|
||||
{
|
||||
Type::Ptr type = GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
return Empty;
|
||||
|
||||
int fid = type->GetFieldId(field);
|
||||
|
||||
if (fid == -1)
|
||||
return GetPrototypeField(const_cast<Object *>(this), field, true, debugInfo);
|
||||
|
||||
if (sandboxed) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
|
||||
if (fieldInfo.Attributes & FANoUserView)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode.", debugInfo));
|
||||
}
|
||||
|
||||
return GetField(fid);
|
||||
}
|
||||
|
||||
void Object::SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo)
|
||||
{
|
||||
Type::Ptr type = GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
|
||||
|
||||
int fid = type->GetFieldId(field);
|
||||
|
||||
if (fid == -1)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
|
||||
|
||||
try {
|
||||
SetField(fid, value);
|
||||
} catch (const boost::bad_lexical_cast&) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
|
||||
} catch (const std::bad_cast&) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
|
||||
}
|
||||
}
|
||||
|
||||
void Object::Validate(int types, const ValidationUtils& utils)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
@ -128,6 +187,26 @@ Type::Ptr Object::GetReflectionType(void) const
|
||||
return Object::TypeInstance;
|
||||
}
|
||||
|
||||
Value icinga::GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo)
|
||||
{
|
||||
Type::Ptr ctype = context.GetReflectionType();
|
||||
Type::Ptr type = ctype;
|
||||
|
||||
do {
|
||||
Object::Ptr object = type->GetPrototype();
|
||||
|
||||
if (object && object->HasOwnField(field))
|
||||
return object->GetFieldByName(field, false, debugInfo);
|
||||
|
||||
type = type->GetBaseType();
|
||||
} while (type);
|
||||
|
||||
if (not_found_error)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
|
||||
else
|
||||
return Empty;
|
||||
}
|
||||
|
||||
#ifdef I2_LEAK_DEBUG
|
||||
void icinga::TypeAddObject(Object *object)
|
||||
{
|
||||
|
@ -41,6 +41,7 @@ class Value;
|
||||
class Object;
|
||||
class Type;
|
||||
class String;
|
||||
class DebugInfo;
|
||||
class ValidationUtils;
|
||||
|
||||
extern I2_BASE_API Value Empty;
|
||||
@ -123,6 +124,9 @@ public:
|
||||
|
||||
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty);
|
||||
virtual Value GetField(int id) const;
|
||||
virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const;
|
||||
virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo);
|
||||
virtual bool HasOwnField(const String& field) const;
|
||||
virtual void ValidateField(int id, const Value& value, const ValidationUtils& utils);
|
||||
virtual void NotifyField(int id, const Value& cookie = Empty);
|
||||
virtual Object::Ptr NavigateField(int id) const;
|
||||
@ -158,6 +162,8 @@ private:
|
||||
friend void intrusive_ptr_release(Object *object);
|
||||
};
|
||||
|
||||
I2_BASE_API Value GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo);
|
||||
|
||||
void TypeAddObject(Object *object);
|
||||
void TypeRemoveObject(Object *object);
|
||||
|
||||
|
@ -122,7 +122,7 @@ ExpressionResult VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *d
|
||||
|
||||
if (frame.Locals && frame.Locals->Get(m_Variable, &value))
|
||||
return value;
|
||||
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && VMOps::HasField(frame.Self, m_Variable))
|
||||
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(m_Variable))
|
||||
return VMOps::GetField(frame.Self, m_Variable, frame.Sandboxed, m_DebugInfo);
|
||||
else
|
||||
return ScriptGlobal::Get(m_Variable);
|
||||
@ -137,7 +137,7 @@ bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value
|
||||
|
||||
if (dhint)
|
||||
*dhint = NULL;
|
||||
} else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && VMOps::HasField(frame.Self, m_Variable)) {
|
||||
} else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(m_Variable)) {
|
||||
*parent = frame.Self;
|
||||
|
||||
if (dhint && *dhint)
|
||||
|
@ -49,7 +49,7 @@ public:
|
||||
Value value;
|
||||
if (frame.Locals && frame.Locals->Get(name, &value))
|
||||
return value;
|
||||
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
|
||||
else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && static_cast<Object::Ptr>(frame.Self)->HasOwnField(name))
|
||||
return GetField(frame.Self, name, frame.Sandboxed, debugInfo);
|
||||
else
|
||||
return ScriptGlobal::Get(name);
|
||||
@ -191,42 +191,6 @@ public:
|
||||
return Empty;
|
||||
}
|
||||
|
||||
static inline bool HasField(const Object::Ptr& context, const String& field)
|
||||
{
|
||||
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
|
||||
|
||||
if (dict)
|
||||
return dict->Contains(field);
|
||||
else {
|
||||
Type::Ptr type = context->GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
return false;
|
||||
|
||||
return type->GetFieldId(field) != -1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
|
||||
{
|
||||
Type::Ptr ctype = context.GetReflectionType();
|
||||
Type::Ptr type = ctype;
|
||||
|
||||
do {
|
||||
Object::Ptr object = type->GetPrototype();
|
||||
|
||||
if (object && HasField(object, field))
|
||||
return GetField(object, field, false, debugInfo);
|
||||
|
||||
type = type->GetBaseType();
|
||||
} while (type);
|
||||
|
||||
if (not_found_error)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
|
||||
else
|
||||
return Empty;
|
||||
}
|
||||
|
||||
static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
|
||||
{
|
||||
if (context.IsEmpty() && !context.IsString())
|
||||
@ -237,51 +201,7 @@ public:
|
||||
|
||||
Object::Ptr object = context;
|
||||
|
||||
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
|
||||
|
||||
if (dict) {
|
||||
Value value;
|
||||
if (dict->Get(field, &value))
|
||||
return value;
|
||||
else
|
||||
return GetPrototypeField(context, field, false, debugInfo);
|
||||
}
|
||||
|
||||
Array::Ptr arr = dynamic_pointer_cast<Array>(object);
|
||||
|
||||
if (arr) {
|
||||
int index;
|
||||
|
||||
try {
|
||||
index = Convert::ToLong(field);
|
||||
} catch (...) {
|
||||
return GetPrototypeField(context, field, true, debugInfo);
|
||||
}
|
||||
|
||||
if (index < 0 || index >= arr->GetLength())
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
|
||||
|
||||
return arr->Get(index);
|
||||
}
|
||||
|
||||
Type::Ptr type = object->GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
return Empty;
|
||||
|
||||
int fid = type->GetFieldId(field);
|
||||
|
||||
if (fid == -1)
|
||||
return GetPrototypeField(context, field, true, debugInfo);
|
||||
|
||||
if (sandboxed) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
|
||||
if (fieldInfo.Attributes & FANoUserView)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode."));
|
||||
}
|
||||
|
||||
return object->GetField(fid);
|
||||
return object->GetFieldByName(field, sandboxed, debugInfo);
|
||||
}
|
||||
|
||||
static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
|
||||
@ -289,44 +209,7 @@ public:
|
||||
if (!context)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Cannot set field '" + field + "' on a value that is not an object.", debugInfo));
|
||||
|
||||
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
|
||||
|
||||
if (dict) {
|
||||
dict->Set(field, value);
|
||||
return;
|
||||
}
|
||||
|
||||
Array::Ptr arr = dynamic_pointer_cast<Array>(context);
|
||||
|
||||
if (arr) {
|
||||
int index = Convert::ToLong(field);
|
||||
if (index >= arr->GetLength())
|
||||
arr->Resize(index + 1);
|
||||
arr->Set(index, value);
|
||||
return;
|
||||
}
|
||||
|
||||
Type::Ptr type = context->GetReflectionType();
|
||||
|
||||
if (!type)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
|
||||
|
||||
int fid = type->GetFieldId(field);
|
||||
|
||||
if (fid == -1)
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
|
||||
|
||||
try {
|
||||
context->SetField(fid, value);
|
||||
} catch (const boost::bad_lexical_cast&) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
|
||||
} catch (const std::bad_cast&) {
|
||||
Field fieldInfo = type->GetFieldInfo(fid);
|
||||
Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
|
||||
BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
|
||||
}
|
||||
return context->SetFieldByName(field, value, debugInfo);
|
||||
}
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user