Skip to content

Commit

Permalink
Make freed object different than null in comparison operators
Browse files Browse the repository at this point in the history
This is so everything is consistent, as a freed object is not equivalent
to `null` in general. The booleanization of a freed object still returns
`false` to work as an easy check for validity of objects. Similarly, the
negation of a freed object returns `true`.

Also makes freed objects different from each other (if they are not the
same reference).
  • Loading branch information
vnen committed Aug 21, 2023
1 parent 970be7a commit 150b50c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ bool Variant::is_zero() const {
return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
}
case OBJECT: {
return _get_obj().obj == nullptr;
return get_validated_object() == nullptr;
}
case CALLABLE: {
return reinterpret_cast<const Callable *>(_data._mem)->is_null();
Expand Down
32 changes: 16 additions & 16 deletions core/variant/variant_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,14 @@ class OperatorEvaluatorEqual {
class OperatorEvaluatorEqualObject {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
const Object *a = p_left.get_validated_object();
const Object *b = p_right.get_validated_object();
const ObjectID &a = VariantInternal::get_object_id(&p_left);
const ObjectID &b = VariantInternal::get_object_id(&p_right);
*r_ret = a == b;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
const Object *a = left->get_validated_object();
const Object *b = right->get_validated_object();
const ObjectID &a = VariantInternal::get_object_id(left);
const ObjectID &b = VariantInternal::get_object_id(right);
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == b;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand All @@ -568,12 +568,12 @@ class OperatorEvaluatorEqualObject {
class OperatorEvaluatorEqualObjectNil {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
const Object *a = p_left.get_validated_object();
const Object *a = p_left.operator Object *();
*r_ret = a == nullptr;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
const Object *a = left->get_validated_object();
const Object *a = left->operator Object *();
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == nullptr;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand All @@ -585,12 +585,12 @@ class OperatorEvaluatorEqualObjectNil {
class OperatorEvaluatorEqualNilObject {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
const Object *b = p_right.get_validated_object();
const Object *b = p_right.operator Object *();
*r_ret = nullptr == b;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
const Object *b = right->get_validated_object();
const Object *b = right->operator Object *();
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr == b;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand Down Expand Up @@ -620,14 +620,14 @@ class OperatorEvaluatorNotEqual {
class OperatorEvaluatorNotEqualObject {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
Object *a = p_left.get_validated_object();
Object *b = p_right.get_validated_object();
const ObjectID &a = VariantInternal::get_object_id(&p_left);
const ObjectID &b = VariantInternal::get_object_id(&p_right);
*r_ret = a != b;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
Object *a = left->get_validated_object();
Object *b = right->get_validated_object();
const ObjectID &a = VariantInternal::get_object_id(left);
const ObjectID &b = VariantInternal::get_object_id(right);
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != b;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand All @@ -639,12 +639,12 @@ class OperatorEvaluatorNotEqualObject {
class OperatorEvaluatorNotEqualObjectNil {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
Object *a = p_left.get_validated_object();
Object *a = p_left.operator Object *();
*r_ret = a != nullptr;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
Object *a = left->get_validated_object();
Object *a = left->operator Object *();
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != nullptr;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand All @@ -656,12 +656,12 @@ class OperatorEvaluatorNotEqualObjectNil {
class OperatorEvaluatorNotEqualNilObject {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
Object *b = p_right.get_validated_object();
Object *b = p_right.operator Object *();
*r_ret = nullptr != b;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
Object *b = right->get_validated_object();
Object *b = right->operator Object *();
*VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr != b;
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
Expand Down

0 comments on commit 150b50c

Please sign in to comment.