summaryrefslogtreecommitdiff
path: root/core/variant
diff options
context:
space:
mode:
Diffstat (limited to 'core/variant')
-rw-r--r--core/variant/array.cpp2
-rw-r--r--core/variant/array.h2
-rw-r--r--core/variant/callable.cpp39
-rw-r--r--core/variant/callable.h1
-rw-r--r--core/variant/type_info.h8
-rw-r--r--core/variant/variant.cpp54
-rw-r--r--core/variant/variant_call.cpp9
-rw-r--r--core/variant/variant_construct.cpp1
-rw-r--r--core/variant/variant_construct.h76
-rw-r--r--core/variant/variant_internal.h25
-rw-r--r--core/variant/variant_parser.cpp2
-rw-r--r--core/variant/variant_setget.cpp2
-rw-r--r--core/variant/variant_setget.h8
-rw-r--r--core/variant/variant_utility.cpp12
14 files changed, 159 insertions, 82 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp
index c1bdd6a6bc..8b958814db 100644
--- a/core/variant/array.cpp
+++ b/core/variant/array.cpp
@@ -402,6 +402,7 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const {
Array result;
+ result._p->typed = _p->typed;
ERR_FAIL_COND_V_MSG(p_step == 0, result, "Slice step cannot be zero.");
@@ -433,6 +434,7 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const {
Array Array::filter(const Callable &p_callable) const {
Array new_arr;
new_arr.resize(size());
+ new_arr._p->typed = _p->typed;
int accepted_count = 0;
const Variant *argptrs[1];
diff --git a/core/variant/array.h b/core/variant/array.h
index c007376734..3d9a794969 100644
--- a/core/variant/array.h
+++ b/core/variant/array.h
@@ -47,7 +47,6 @@ class Array {
void _unref() const;
protected:
- Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
bool _assign(const Array &p_array);
public:
@@ -131,6 +130,7 @@ public:
void set_read_only(bool p_enable);
bool is_read_only() const;
+ Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
Array(const Array &p_from);
Array();
~Array();
diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp
index 28efb43fc5..79532d9aca 100644
--- a/core/variant/callable.cpp
+++ b/core/variant/callable.cpp
@@ -63,6 +63,21 @@ void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_ret
}
}
+Variant Callable::callv(const Array &p_arguments) const {
+ int argcount = p_arguments.size();
+ const Variant **argptrs = nullptr;
+ if (argcount) {
+ argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
+ for (int i = 0; i < argcount; i++) {
+ argptrs[i] = &p_arguments[i];
+ }
+ }
+ CallError ce;
+ Variant ret;
+ callp(argptrs, argcount, ret, ce);
+ return ret;
+}
+
Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
if (is_null()) {
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
@@ -387,33 +402,33 @@ Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
}
Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
- Object *object = get_object();
- ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);
+ Object *obj = get_object();
+ ERR_FAIL_COND_V(!obj, ERR_UNCONFIGURED);
- return object->connect(name, p_callable, p_flags);
+ return obj->connect(name, p_callable, p_flags);
}
void Signal::disconnect(const Callable &p_callable) {
- Object *object = get_object();
- ERR_FAIL_COND(!object);
- object->disconnect(name, p_callable);
+ Object *obj = get_object();
+ ERR_FAIL_COND(!obj);
+ obj->disconnect(name, p_callable);
}
bool Signal::is_connected(const Callable &p_callable) const {
- Object *object = get_object();
- ERR_FAIL_COND_V(!object, false);
+ Object *obj = get_object();
+ ERR_FAIL_COND_V(!obj, false);
- return object->is_connected(name, p_callable);
+ return obj->is_connected(name, p_callable);
}
Array Signal::get_connections() const {
- Object *object = get_object();
- if (!object) {
+ Object *obj = get_object();
+ if (!obj) {
return Array();
}
List<Object::Connection> connections;
- object->get_signal_connection_list(name, &connections);
+ obj->get_signal_connection_list(name, &connections);
Array arr;
for (const Object::Connection &E : connections) {
diff --git a/core/variant/callable.h b/core/variant/callable.h
index 1f1c983eb3..0305dc55c3 100644
--- a/core/variant/callable.h
+++ b/core/variant/callable.h
@@ -71,6 +71,7 @@ public:
void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
void call_deferredp(const Variant **p_arguments, int p_argcount) const;
+ Variant callv(const Array &p_arguments) const;
Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;
diff --git a/core/variant/type_info.h b/core/variant/type_info.h
index 7372c60754..e355053296 100644
--- a/core/variant/type_info.h
+++ b/core/variant/type_info.h
@@ -276,7 +276,7 @@ inline String enum_qualified_name_to_class_info_name(const String &p_qualified_n
template <typename T>
inline StringName __constant_get_enum_name(T param, const String &p_constant) {
- if (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL) {
+ if constexpr (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL) {
ERR_PRINT("Missing VARIANT_ENUM_CAST for constant's enum: " + p_constant);
}
return GetTypeInfo<T>::get_class_info().class_name;
@@ -284,14 +284,14 @@ inline StringName __constant_get_enum_name(T param, const String &p_constant) {
template <class T>
class BitField {
- uint32_t value = 0;
+ int64_t value = 0;
public:
_FORCE_INLINE_ void set_flag(T p_flag) { value |= p_flag; }
_FORCE_INLINE_ bool has_flag(T p_flag) const { return value & p_flag; }
_FORCE_INLINE_ void clear_flag(T p_flag) { return value &= ~p_flag; }
- _FORCE_INLINE_ BitField(uint32_t p_value) { value = p_value; }
- _FORCE_INLINE_ operator uint32_t() const { return value; }
+ _FORCE_INLINE_ BitField(int64_t p_value) { value = p_value; }
+ _FORCE_INLINE_ operator int64_t() const { return value; }
_FORCE_INLINE_ operator Variant() const { return value; }
};
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp
index f24ffeb1a9..4eae23b0fb 100644
--- a/core/variant/variant.cpp
+++ b/core/variant/variant.cpp
@@ -1101,8 +1101,6 @@ bool Variant::is_one() const {
return !is_zero();
}
}
-
- return false;
}
bool Variant::is_null() const {
@@ -1835,11 +1833,13 @@ String Variant::stringify(int recursion_count) const {
case DICTIONARY: {
const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
if (recursion_count > MAX_RECURSION) {
- ERR_PRINT("Max recursion reached");
- return "{...}";
+ ERR_PRINT("Maximum dictionary recursion reached!");
+ return "{ ... }";
}
- String str("{");
+ // Add leading and trailing space to Dictionary printing. This distinguishes it
+ // from array printing on fonts that have similar-looking {} and [] characters.
+ String str("{ ");
List<Variant> keys;
d.get_key_list(&keys);
@@ -1858,9 +1858,9 @@ String Variant::stringify(int recursion_count) const {
if (i > 0) {
str += ", ";
}
- str += pairs[i].key + ":" + pairs[i].value;
+ str += pairs[i].key + ": " + pairs[i].value;
}
- str += "}";
+ str += " }";
return str;
} break;
@@ -1894,7 +1894,7 @@ String Variant::stringify(int recursion_count) const {
case ARRAY: {
Array arr = operator Array();
if (recursion_count > MAX_RECURSION) {
- ERR_PRINT("Max recursion reached");
+ ERR_PRINT("Maximum array recursion reached!");
return "[...]";
}
@@ -1929,8 +1929,6 @@ String Variant::stringify(int recursion_count) const {
return "<" + get_type_name(type) + ">";
}
}
-
- return "";
}
String Variant::to_json_string() const {
@@ -3121,22 +3119,22 @@ uint32_t Variant::recursive_hash(int recursion_count) const {
case PROJECTION: {
uint32_t h = HASH_MURMUR3_SEED;
const Projection &t = *_data._projection;
- h = hash_murmur3_one_real(t.matrix[0].x, h);
- h = hash_murmur3_one_real(t.matrix[0].y, h);
- h = hash_murmur3_one_real(t.matrix[0].z, h);
- h = hash_murmur3_one_real(t.matrix[0].w, h);
- h = hash_murmur3_one_real(t.matrix[1].x, h);
- h = hash_murmur3_one_real(t.matrix[1].y, h);
- h = hash_murmur3_one_real(t.matrix[1].z, h);
- h = hash_murmur3_one_real(t.matrix[1].w, h);
- h = hash_murmur3_one_real(t.matrix[2].x, h);
- h = hash_murmur3_one_real(t.matrix[2].y, h);
- h = hash_murmur3_one_real(t.matrix[2].z, h);
- h = hash_murmur3_one_real(t.matrix[2].w, h);
- h = hash_murmur3_one_real(t.matrix[3].x, h);
- h = hash_murmur3_one_real(t.matrix[3].y, h);
- h = hash_murmur3_one_real(t.matrix[3].z, h);
- h = hash_murmur3_one_real(t.matrix[3].w, h);
+ h = hash_murmur3_one_real(t.columns[0].x, h);
+ h = hash_murmur3_one_real(t.columns[0].y, h);
+ h = hash_murmur3_one_real(t.columns[0].z, h);
+ h = hash_murmur3_one_real(t.columns[0].w, h);
+ h = hash_murmur3_one_real(t.columns[1].x, h);
+ h = hash_murmur3_one_real(t.columns[1].y, h);
+ h = hash_murmur3_one_real(t.columns[1].z, h);
+ h = hash_murmur3_one_real(t.columns[1].w, h);
+ h = hash_murmur3_one_real(t.columns[2].x, h);
+ h = hash_murmur3_one_real(t.columns[2].y, h);
+ h = hash_murmur3_one_real(t.columns[2].z, h);
+ h = hash_murmur3_one_real(t.columns[2].w, h);
+ h = hash_murmur3_one_real(t.columns[3].x, h);
+ h = hash_murmur3_one_real(t.columns[3].y, h);
+ h = hash_murmur3_one_real(t.columns[3].z, h);
+ h = hash_murmur3_one_real(t.columns[3].w, h);
return hash_fmix32(h);
} break;
// misc types
@@ -3507,7 +3505,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Projection *r = p_variant._data._projection;
for (int i = 0; i < 4; i++) {
- if (!(hash_compare_vector4(l->matrix[i], r->matrix[i]))) {
+ if (!(hash_compare_vector4(l->columns[i], r->columns[i]))) {
return false;
}
}
@@ -3571,8 +3569,6 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
evaluate(OP_EQUAL, *this, p_variant, r, v);
return r;
}
-
- return false;
}
bool Variant::is_ref_counted() const {
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index f09885b325..1831f7b72a 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1859,6 +1859,7 @@ static void _register_variant_builtin_methods() {
/* Callable */
+ bind_method(Callable, callv, sarray("arguments"), varray());
bind_method(Callable, is_null, sarray(), varray());
bind_method(Callable, is_custom, sarray(), varray());
bind_method(Callable, is_standard, sarray(), varray());
@@ -2065,6 +2066,14 @@ static void _register_variant_builtin_methods() {
bind_method(Array, all, sarray("method"), varray());
bind_method(Array, max, sarray(), varray());
bind_method(Array, min, sarray(), varray());
+ bind_method(Array, typed_assign, sarray("array"), varray());
+ bind_method(Array, set_typed, sarray("type", "class_name", "script"), varray());
+ bind_method(Array, is_typed, sarray(), varray());
+ bind_method(Array, get_typed_builtin, sarray(), varray());
+ bind_method(Array, get_typed_class_name, sarray(), varray());
+ bind_method(Array, get_typed_script, sarray(), varray());
+ bind_method(Array, set_read_only, sarray("enable"), varray());
+ bind_method(Array, is_read_only, sarray(), varray());
/* Byte Array */
bind_method(PackedByteArray, size, sarray(), varray());
diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp
index d048f45737..3b88dc11ca 100644
--- a/core/variant/variant_construct.cpp
+++ b/core/variant/variant_construct.cpp
@@ -200,6 +200,7 @@ void Variant::_register_variant_constructors() {
add_constructor<VariantConstructNoArgs<Array>>(sarray());
add_constructor<VariantConstructor<Array, Array>>(sarray("from"));
+ add_constructor<VariantConstructorTypedArray>(sarray("base", "type", "class_name", "script"));
add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from"));
add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from"));
add_constructor<VariantConstructorToArray<PackedInt64Array>>(sarray("from"));
diff --git a/core/variant/variant_construct.h b/core/variant/variant_construct.h
index 58a0f34c1e..34d228f4d2 100644
--- a/core/variant/variant_construct.h
+++ b/core/variant/variant_construct.h
@@ -336,6 +336,82 @@ public:
}
};
+class VariantConstructorTypedArray {
+public:
+ static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
+ if (p_args[0]->get_type() != Variant::ARRAY) {
+ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument = 0;
+ r_error.expected = Variant::ARRAY;
+ return;
+ }
+
+ if (p_args[1]->get_type() != Variant::INT) {
+ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument = 1;
+ r_error.expected = Variant::INT;
+ return;
+ }
+
+ if (p_args[2]->get_type() != Variant::STRING_NAME) {
+ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument = 2;
+ r_error.expected = Variant::STRING_NAME;
+ return;
+ }
+
+ const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
+ const uint32_t type = p_args[1]->operator uint32_t();
+ const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
+ r_ret = Array(base_arr, type, class_name, *p_args[3]);
+ }
+
+ static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
+ const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
+ const uint32_t type = p_args[1]->operator uint32_t();
+ const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
+ *r_ret = Array(base_arr, type, class_name, *p_args[3]);
+ }
+
+ static void ptr_construct(void *base, const void **p_args) {
+ const Array &base_arr = PtrToArg<Array>::convert(p_args[0]);
+ const uint32_t type = PtrToArg<uint32_t>::convert(p_args[1]);
+ const StringName &class_name = PtrToArg<StringName>::convert(p_args[2]);
+ const Variant &script = PtrToArg<Variant>::convert(p_args[3]);
+ Array dst_arr = Array(base_arr, type, class_name, script);
+
+ PtrConstruct<Array>::construct(dst_arr, base);
+ }
+
+ static int get_argument_count() {
+ return 4;
+ }
+
+ static Variant::Type get_argument_type(int p_arg) {
+ switch (p_arg) {
+ case 0: {
+ return Variant::ARRAY;
+ } break;
+ case 1: {
+ return Variant::INT;
+ } break;
+ case 2: {
+ return Variant::STRING_NAME;
+ } break;
+ case 3: {
+ return Variant::NIL;
+ } break;
+ default: {
+ return Variant::NIL;
+ } break;
+ }
+ }
+
+ static Variant::Type get_base_type() {
+ return Variant::ARRAY;
+ }
+};
+
template <class T>
class VariantConstructorToArray {
public:
diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h
index 874a183d29..7ae2368fe4 100644
--- a/core/variant/variant_internal.h
+++ b/core/variant/variant_internal.h
@@ -1047,7 +1047,7 @@ struct VariantInternalAccessor<PackedColorArray> {
template <>
struct VariantInternalAccessor<Object *> {
static _FORCE_INLINE_ Object *get(const Variant *v) { return const_cast<Object *>(*VariantInternal::get_object(v)); }
- static _FORCE_INLINE_ void set(Variant *v, const Object *p_value) { *VariantInternal::get_object(v) = const_cast<Object *>(p_value); }
+ static _FORCE_INLINE_ void set(Variant *v, const Object *p_value) { VariantInternal::object_assign(v, p_value); }
};
template <>
@@ -1532,27 +1532,4 @@ struct VariantTypeConstructor {
}
};
-template <>
-struct VariantTypeConstructor<Object *> {
- _FORCE_INLINE_ static void variant_from_type(void *p_variant, void *p_value) {
- Variant *variant = reinterpret_cast<Variant *>(p_variant);
- VariantInitializer<Object *>::init(variant);
- Object *object = *(reinterpret_cast<Object **>(p_value));
- if (object) {
- if (object->is_ref_counted()) {
- if (!VariantInternal::initialize_ref(object)) {
- return;
- }
- }
- VariantInternalAccessor<Object *>::set(variant, object);
- VariantInternalAccessor<ObjectID>::set(variant, object->get_instance_id());
- }
- }
-
- _FORCE_INLINE_ static void type_from_variant(void *p_value, void *p_variant) {
- Object **value = reinterpret_cast<Object **>(p_value);
- *value = VariantInternalAccessor<Object *>::get(reinterpret_cast<Variant *>(p_variant));
- }
-};
-
#endif // VARIANT_INTERNAL_H
diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp
index 8151ff2102..d2e4d752a4 100644
--- a/core/variant/variant_parser.cpp
+++ b/core/variant/variant_parser.cpp
@@ -1651,7 +1651,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
if (i != 0 || j != 0) {
s += ", ";
}
- s += rtos_fix(t.matrix[i][j]);
+ s += rtos_fix(t.columns[i][j]);
}
}
diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp
index ff67b187ef..188103ee5e 100644
--- a/core/variant/variant_setget.cpp
+++ b/core/variant/variant_setget.cpp
@@ -826,7 +826,7 @@ INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Color, double, float, 4)
INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Transform2D, Vector2, .columns, 3)
INDEXED_SETGET_STRUCT_BULTIN_FUNC(Basis, Vector3, set_column, get_column, 3)
-INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Projection, Vector4, .matrix, 4)
+INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Projection, Vector4, .columns, 4)
INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedByteArray, int64_t, uint8_t)
INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedInt32Array, int64_t, int32_t)
diff --git a/core/variant/variant_setget.h b/core/variant/variant_setget.h
index 570277dc7a..d151a85a6e 100644
--- a/core/variant/variant_setget.h
+++ b/core/variant/variant_setget.h
@@ -325,10 +325,10 @@ SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_column, get_column, 2)
SETGET_STRUCT(Transform3D, Basis, basis)
SETGET_STRUCT(Transform3D, Vector3, origin)
-SETGET_STRUCT_CUSTOM(Projection, Vector4, x, matrix[0])
-SETGET_STRUCT_CUSTOM(Projection, Vector4, y, matrix[1])
-SETGET_STRUCT_CUSTOM(Projection, Vector4, z, matrix[2])
-SETGET_STRUCT_CUSTOM(Projection, Vector4, w, matrix[3])
+SETGET_STRUCT_CUSTOM(Projection, Vector4, x, columns[0])
+SETGET_STRUCT_CUSTOM(Projection, Vector4, y, columns[1])
+SETGET_STRUCT_CUSTOM(Projection, Vector4, z, columns[2])
+SETGET_STRUCT_CUSTOM(Projection, Vector4, w, columns[3])
SETGET_NUMBER_STRUCT(Color, double, r)
SETGET_NUMBER_STRUCT(Color, double, g)
diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp
index 7ff64c88f0..670b66d53e 100644
--- a/core/variant/variant_utility.cpp
+++ b/core/variant/variant_utility.cpp
@@ -128,8 +128,8 @@ struct VariantUtilityFunctions {
return Math::floor(x);
}
- static inline int floori(double x) {
- return int(x);
+ static inline int64_t floori(double x) {
+ return int64_t(Math::floor(x));
}
static inline Variant ceil(Variant x, Callable::CallError &r_error) {
@@ -161,8 +161,8 @@ struct VariantUtilityFunctions {
return Math::ceil(x);
}
- static inline int ceili(double x) {
- return int(Math::ceil(x));
+ static inline int64_t ceili(double x) {
+ return int64_t(Math::ceil(x));
}
static inline Variant round(Variant x, Callable::CallError &r_error) {
@@ -194,8 +194,8 @@ struct VariantUtilityFunctions {
return Math::round(x);
}
- static inline int roundi(double x) {
- return int(Math::round(x));
+ static inline int64_t roundi(double x) {
+ return int64_t(Math::round(x));
}
static inline Variant abs(const Variant &x, Callable::CallError &r_error) {