| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/core_bind.h" |
| 34 |
#include "core/core_constants.h" |
| 35 |
#include "core/object/class_db.h" |
| 36 |
|
| 37 |
#include "tests/test_macros.h" |
| 38 |
|
| 39 |
namespace TestClassDB { |
| 40 |
|
| 41 |
struct TypeReference { |
| 42 |
StringName name; |
| 43 |
bool is_enum = false; |
| 44 |
}; |
| 45 |
|
| 46 |
struct ConstantData { |
| 47 |
String name; |
| 48 |
int64_t value = 0; |
| 49 |
}; |
| 50 |
|
| 51 |
struct EnumData { |
| 52 |
StringName name; |
| 53 |
List<ConstantData> constants; |
| 54 |
|
| 55 |
_FORCE_INLINE_ bool operator==(const EnumData &p_enum) const { |
| 56 |
return p_enum.name == name; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
struct PropertyData { |
| 61 |
StringName name; |
| 62 |
int index = 0; |
| 63 |
|
| 64 |
StringName getter; |
| 65 |
StringName setter; |
| 66 |
}; |
| 67 |
|
| 68 |
struct ArgumentData { |
| 69 |
TypeReference type; |
| 70 |
String name; |
| 71 |
bool has_defval = false; |
| 72 |
Variant defval; |
| 73 |
int position; |
| 74 |
}; |
| 75 |
|
| 76 |
struct MethodData { |
| 77 |
StringName name; |
| 78 |
TypeReference return_type; |
| 79 |
List<ArgumentData> arguments; |
| 80 |
bool is_virtual = false; |
| 81 |
bool is_vararg = false; |
| 82 |
}; |
| 83 |
|
| 84 |
struct SignalData { |
| 85 |
StringName name; |
| 86 |
List<ArgumentData> arguments; |
| 87 |
}; |
| 88 |
|
| 89 |
struct ExposedClass { |
| 90 |
StringName name; |
| 91 |
StringName base; |
| 92 |
|
| 93 |
bool is_singleton = false; |
| 94 |
bool is_instantiable = false; |
| 95 |
bool is_ref_counted = false; |
| 96 |
|
| 97 |
ClassDB::APIType api_type; |
| 98 |
|
| 99 |
List<ConstantData> constants; |
| 100 |
List<EnumData> enums; |
| 101 |
List<PropertyData> properties; |
| 102 |
List<MethodData> methods; |
| 103 |
List<SignalData> signals_; |
| 104 |
|
| 105 |
const PropertyData *find_property_by_name(const StringName &p_name) const { |
| 106 |
for (const PropertyData &E : properties) { |
| 107 |
if (E.name == p_name) { |
| 108 |
return &E; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
const MethodData *find_method_by_name(const StringName &p_name) const { |
| 116 |
for (const MethodData &E : methods) { |
| 117 |
if (E.name == p_name) { |
| 118 |
return &E; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
struct NamesCache { |
| 127 |
StringName variant_type = StringName("Variant"); |
| 128 |
StringName object_class = StringName("Object"); |
| 129 |
StringName ref_counted_class = StringName("RefCounted"); |
| 130 |
StringName string_type = StringName("String"); |
| 131 |
StringName string_name_type = StringName("StringName"); |
| 132 |
StringName node_path_type = StringName("NodePath"); |
| 133 |
StringName bool_type = StringName("bool"); |
| 134 |
StringName int_type = StringName("int"); |
| 135 |
StringName float_type = StringName("float"); |
| 136 |
StringName void_type = StringName("void"); |
| 137 |
StringName vararg_stub_type = StringName("@VarArg@"); |
| 138 |
StringName vector2_type = StringName("Vector2"); |
| 139 |
StringName rect2_type = StringName("Rect2"); |
| 140 |
StringName vector3_type = StringName("Vector3"); |
| 141 |
StringName vector4_type = StringName("Vector4"); |
| 142 |
|
| 143 |
|
| 144 |
static constexpr int nullable_types_count = 18; |
| 145 |
StringName nullable_types[nullable_types_count] = { |
| 146 |
string_type, |
| 147 |
string_name_type, |
| 148 |
node_path_type, |
| 149 |
|
| 150 |
StringName(_STR(Array)), |
| 151 |
StringName(_STR(Dictionary)), |
| 152 |
StringName(_STR(Callable)), |
| 153 |
StringName(_STR(Signal)), |
| 154 |
|
| 155 |
StringName(_STR(PackedByteArray)), |
| 156 |
StringName(_STR(PackedInt32Array)), |
| 157 |
StringName(_STR(PackedInt64rray)), |
| 158 |
StringName(_STR(PackedFloat32Array)), |
| 159 |
StringName(_STR(PackedFloat64Array)), |
| 160 |
StringName(_STR(PackedStringArray)), |
| 161 |
StringName(_STR(PackedVector2Array)), |
| 162 |
StringName(_STR(PackedVector3Array)), |
| 163 |
StringName(_STR(PackedColorArray)), |
| 164 |
StringName(_STR(PackedVector4Array)), |
| 165 |
}; |
| 166 |
|
| 167 |
bool is_nullable_type(const StringName &p_type) const { |
| 168 |
for (int i = 0; i < nullable_types_count; i++) { |
| 169 |
if (p_type == nullable_types[i]) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return false; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
typedef HashMap<StringName, ExposedClass> ExposedClasses; |
| 179 |
|
| 180 |
struct Context { |
| 181 |
Vector<StringName> enum_types; |
| 182 |
Vector<StringName> builtin_types; |
| 183 |
ExposedClasses exposed_classes; |
| 184 |
List<EnumData> global_enums; |
| 185 |
NamesCache names_cache; |
| 186 |
|
| 187 |
const ExposedClass *find_exposed_class(const StringName &p_name) const { |
| 188 |
ExposedClasses::ConstIterator elem = exposed_classes.find(p_name); |
| 189 |
return elem ? &elem->value : nullptr; |
| 190 |
} |
| 191 |
|
| 192 |
const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const { |
| 193 |
ExposedClasses::ConstIterator elem = exposed_classes.find(p_type_ref.name); |
| 194 |
return elem ? &elem->value : nullptr; |
| 195 |
} |
| 196 |
|
| 197 |
bool has_type(const TypeReference &p_type_ref) const { |
| 198 |
if (builtin_types.has(p_type_ref.name)) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
if (p_type_ref.is_enum) { |
| 203 |
if (enum_types.has(p_type_ref.name)) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
return builtin_types.find(names_cache.int_type); |
| 209 |
} |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type, String *r_err_msg = nullptr) { |
| 216 |
if (p_arg_type.name == p_context.names_cache.variant_type) { |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
switch (p_val.get_type()) { |
| 222 |
case Variant::NIL: |
| 223 |
return p_context.find_exposed_class(p_arg_type) || |
| 224 |
p_context.names_cache.is_nullable_type(p_arg_type.name); |
| 225 |
case Variant::BOOL: |
| 226 |
return p_arg_type.name == p_context.names_cache.bool_type; |
| 227 |
case Variant::INT: |
| 228 |
return p_arg_type.name == p_context.names_cache.int_type || |
| 229 |
p_arg_type.name == p_context.names_cache.float_type || |
| 230 |
p_arg_type.is_enum; |
| 231 |
case Variant::FLOAT: |
| 232 |
return p_arg_type.name == p_context.names_cache.float_type; |
| 233 |
case Variant::STRING: |
| 234 |
case Variant::STRING_NAME: |
| 235 |
return p_arg_type.name == p_context.names_cache.string_type || |
| 236 |
p_arg_type.name == p_context.names_cache.string_name_type || |
| 237 |
p_arg_type.name == p_context.names_cache.node_path_type; |
| 238 |
case Variant::NODE_PATH: |
| 239 |
return p_arg_type.name == p_context.names_cache.node_path_type; |
| 240 |
case Variant::TRANSFORM3D: |
| 241 |
case Variant::TRANSFORM2D: |
| 242 |
case Variant::BASIS: |
| 243 |
case Variant::QUATERNION: |
| 244 |
case Variant::PLANE: |
| 245 |
case Variant::AABB: |
| 246 |
case Variant::COLOR: |
| 247 |
case Variant::VECTOR2: |
| 248 |
case Variant::RECT2: |
| 249 |
case Variant::VECTOR3: |
| 250 |
case Variant::VECTOR4: |
| 251 |
case Variant::PROJECTION: |
| 252 |
case Variant::RID: |
| 253 |
case Variant::ARRAY: |
| 254 |
case Variant::DICTIONARY: |
| 255 |
case Variant::PACKED_BYTE_ARRAY: |
| 256 |
case Variant::PACKED_INT32_ARRAY: |
| 257 |
case Variant::PACKED_INT64_ARRAY: |
| 258 |
case Variant::PACKED_FLOAT32_ARRAY: |
| 259 |
case Variant::PACKED_FLOAT64_ARRAY: |
| 260 |
case Variant::PACKED_STRING_ARRAY: |
| 261 |
case Variant::PACKED_VECTOR2_ARRAY: |
| 262 |
case Variant::PACKED_VECTOR3_ARRAY: |
| 263 |
case Variant::PACKED_COLOR_ARRAY: |
| 264 |
case Variant::PACKED_VECTOR4_ARRAY: |
| 265 |
case Variant::CALLABLE: |
| 266 |
case Variant::SIGNAL: |
| 267 |
return p_arg_type.name == Variant::get_type_name(p_val.get_type()); |
| 268 |
case Variant::OBJECT: |
| 269 |
return p_context.find_exposed_class(p_arg_type); |
| 270 |
case Variant::VECTOR2I: |
| 271 |
return p_arg_type.name == p_context.names_cache.vector2_type || |
| 272 |
p_arg_type.name == Variant::get_type_name(p_val.get_type()); |
| 273 |
case Variant::RECT2I: |
| 274 |
return p_arg_type.name == p_context.names_cache.rect2_type || |
| 275 |
p_arg_type.name == Variant::get_type_name(p_val.get_type()); |
| 276 |
case Variant::VECTOR3I: |
| 277 |
return p_arg_type.name == p_context.names_cache.vector3_type || |
| 278 |
p_arg_type.name == Variant::get_type_name(p_val.get_type()); |
| 279 |
case Variant::VECTOR4I: |
| 280 |
return p_arg_type.name == p_context.names_cache.vector4_type || |
| 281 |
p_arg_type.name == Variant::get_type_name(p_val.get_type()); |
| 282 |
case Variant::VARIANT_MAX: |
| 283 |
break; |
| 284 |
} |
| 285 |
if (r_err_msg) { |
| 286 |
*r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type()); |
| 287 |
} |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
bool arg_default_value_is_valid_data(const Variant &p_val, String *r_err_msg = nullptr) { |
| 292 |
switch (p_val.get_type()) { |
| 293 |
case Variant::RID: |
| 294 |
case Variant::ARRAY: |
| 295 |
case Variant::DICTIONARY: |
| 296 |
case Variant::PACKED_BYTE_ARRAY: |
| 297 |
case Variant::PACKED_INT32_ARRAY: |
| 298 |
case Variant::PACKED_INT64_ARRAY: |
| 299 |
case Variant::PACKED_FLOAT32_ARRAY: |
| 300 |
case Variant::PACKED_FLOAT64_ARRAY: |
| 301 |
case Variant::PACKED_STRING_ARRAY: |
| 302 |
case Variant::PACKED_VECTOR2_ARRAY: |
| 303 |
case Variant::PACKED_VECTOR3_ARRAY: |
| 304 |
case Variant::PACKED_COLOR_ARRAY: |
| 305 |
case Variant::PACKED_VECTOR4_ARRAY: |
| 306 |
case Variant::CALLABLE: |
| 307 |
case Variant::SIGNAL: |
| 308 |
case Variant::OBJECT: |
| 309 |
if (p_val.is_zero()) { |
| 310 |
return true; |
| 311 |
} |
| 312 |
if (r_err_msg) { |
| 313 |
*r_err_msg = "Must be zero."; |
| 314 |
} |
| 315 |
break; |
| 316 |
default: |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) { |
| 324 |
const MethodData *setter = p_class.find_method_by_name(p_prop.setter); |
| 325 |
|
| 326 |
|
| 327 |
const ExposedClass *top = &p_class; |
| 328 |
while (!setter && top->base != StringName()) { |
| 329 |
top = p_context.find_exposed_class(top->base); |
| 330 |
TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'."); |
| 331 |
setter = top->find_method_by_name(p_prop.setter); |
| 332 |
} |
| 333 |
|
| 334 |
const MethodData *getter = p_class.find_method_by_name(p_prop.getter); |
| 335 |
|
| 336 |
|
| 337 |
top = &p_class; |
| 338 |
while (!getter && top->base != StringName()) { |
| 339 |
top = p_context.find_exposed_class(top->base); |
| 340 |
TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'."); |
| 341 |
getter = top->find_method_by_name(p_prop.getter); |
| 342 |
} |
| 343 |
|
| 344 |
TEST_FAIL_COND((!setter && !getter), |
| 345 |
"Couldn't find neither the setter nor the getter for property: '", p_class.name, ".", String(p_prop.name), "'."); |
| 346 |
|
| 347 |
if (setter) { |
| 348 |
int setter_argc = p_prop.index != -1 ? 2 : 1; |
| 349 |
TEST_FAIL_COND(setter->arguments.size() != setter_argc, |
| 350 |
"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'."); |
| 351 |
} |
| 352 |
|
| 353 |
if (getter) { |
| 354 |
int getter_argc = p_prop.index != -1 ? 1 : 0; |
| 355 |
TEST_FAIL_COND(getter->arguments.size() != getter_argc, |
| 356 |
"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'."); |
| 357 |
} |
| 358 |
|
| 359 |
if (getter && setter) { |
| 360 |
const ArgumentData &setter_first_arg = setter->arguments.back()->get(); |
| 361 |
if (getter->return_type.name != setter_first_arg.type.name) { |
| 362 |
TEST_FAIL( |
| 363 |
"Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'."); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type; |
| 368 |
|
| 369 |
const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref); |
| 370 |
if (prop_class) { |
| 371 |
TEST_COND(prop_class->is_singleton, |
| 372 |
"Property type is a singleton: '", p_class.name, ".", String(p_prop.name), "'."); |
| 373 |
|
| 374 |
if (p_class.api_type == ClassDB::API_CORE) { |
| 375 |
TEST_COND(prop_class->api_type == ClassDB::API_EDITOR, |
| 376 |
"Property '", p_class.name, ".", p_prop.name, "' has type '", prop_class->name, |
| 377 |
"' from the editor API. Core API cannot have dependencies on the editor API."); |
| 378 |
} |
| 379 |
} else { |
| 380 |
|
| 381 |
TEST_FAIL_COND(!p_context.has_type(prop_type_ref), |
| 382 |
"Property type '", prop_type_ref.name, "' not found: '", p_class.name, ".", String(p_prop.name), "'."); |
| 383 |
} |
| 384 |
|
| 385 |
if (getter) { |
| 386 |
if (p_prop.index != -1) { |
| 387 |
const ArgumentData &idx_arg = getter->arguments.front()->get(); |
| 388 |
if (idx_arg.type.name != p_context.names_cache.int_type) { |
| 389 |
|
| 390 |
TEST_COND(!p_context.enum_types.has(idx_arg.type.name), |
| 391 |
"Invalid type '", idx_arg.type.name, "' for index argument of property getter: '", p_class.name, ".", String(p_prop.name), "'."); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
if (setter) { |
| 397 |
if (p_prop.index != -1) { |
| 398 |
const ArgumentData &idx_arg = setter->arguments.front()->get(); |
| 399 |
if (idx_arg.type.name != p_context.names_cache.int_type) { |
| 400 |
|
| 401 |
|
| 402 |
TEST_COND(!p_context.enum_types.has(idx_arg.type.name), |
| 403 |
"Invalid type '", idx_arg.type.name, "' for index argument of property setter: '", p_class.name, ".", String(p_prop.name), "'."); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void validate_argument(const Context &p_context, const ExposedClass &p_class, const String &p_owner_name, const String &p_owner_type, const ArgumentData &p_arg) { |
| 410 |
#ifdef DEBUG_ENABLED |
| 411 |
TEST_COND((p_arg.name.is_empty() || p_arg.name.begins_with("_unnamed_arg")), |
| 412 |
vformat("Unnamed argument in position %d of %s '%s.%s'.", p_arg.position, p_owner_type, p_class.name, p_owner_name)); |
| 413 |
|
| 414 |
TEST_FAIL_COND((p_arg.name != "@varargs@" && !p_arg.name.is_valid_ascii_identifier()), |
| 415 |
vformat("Invalid argument name '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name)); |
| 416 |
#endif |
| 417 |
|
| 418 |
const ExposedClass *arg_class = p_context.find_exposed_class(p_arg.type); |
| 419 |
if (arg_class) { |
| 420 |
TEST_COND(arg_class->is_singleton, |
| 421 |
vformat("Argument type is a singleton: '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name)); |
| 422 |
|
| 423 |
if (p_class.api_type == ClassDB::API_CORE) { |
| 424 |
TEST_COND(arg_class->api_type == ClassDB::API_EDITOR, |
| 425 |
vformat("Argument '%s' of %s '%s.%s' has type '%s' from the editor API. Core API cannot have dependencies on the editor API.", |
| 426 |
p_arg.name, p_owner_type, p_class.name, p_owner_name, arg_class->name)); |
| 427 |
} |
| 428 |
} else { |
| 429 |
|
| 430 |
TEST_FAIL_COND(!p_context.has_type(p_arg.type), |
| 431 |
vformat("Argument type '%s' not found: '%s' of %s '%s.%s'.", p_arg.type.name, p_arg.name, p_owner_type, p_class.name, p_owner_name)); |
| 432 |
} |
| 433 |
|
| 434 |
if (p_arg.has_defval) { |
| 435 |
String type_error_msg; |
| 436 |
bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, p_arg.defval, p_arg.type, &type_error_msg); |
| 437 |
|
| 438 |
String err_msg = vformat("Invalid default value for parameter '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name); |
| 439 |
if (!type_error_msg.is_empty()) { |
| 440 |
err_msg += " " + type_error_msg; |
| 441 |
} |
| 442 |
|
| 443 |
TEST_COND(!arg_defval_assignable_to_type, err_msg); |
| 444 |
|
| 445 |
bool arg_defval_valid_data = arg_default_value_is_valid_data(p_arg.defval, &type_error_msg); |
| 446 |
|
| 447 |
if (!type_error_msg.is_empty()) { |
| 448 |
err_msg += " " + type_error_msg; |
| 449 |
} |
| 450 |
|
| 451 |
TEST_COND(!arg_defval_valid_data, err_msg); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) { |
| 456 |
if (p_method.return_type.name != StringName()) { |
| 457 |
const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); |
| 458 |
if (return_class) { |
| 459 |
if (p_class.api_type == ClassDB::API_CORE) { |
| 460 |
TEST_COND(return_class->api_type == ClassDB::API_EDITOR, |
| 461 |
"Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name, |
| 462 |
"' from the editor API. Core API cannot have dependencies on the editor API."); |
| 463 |
} |
| 464 |
} else { |
| 465 |
|
| 466 |
TEST_FAIL_COND(!p_context.has_type(p_method.return_type), |
| 467 |
"Method return type '", p_method.return_type.name, "' not found: '", p_class.name, ".", p_method.name, "'."); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
for (const ArgumentData &F : p_method.arguments) { |
| 472 |
const ArgumentData &arg = F; |
| 473 |
validate_argument(p_context, p_class, p_method.name, "method", arg); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) { |
| 478 |
for (const ArgumentData &F : p_signal.arguments) { |
| 479 |
const ArgumentData &arg = F; |
| 480 |
validate_argument(p_context, p_class, p_signal.name, "signal", arg); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) { |
| 485 |
bool is_derived_type = p_exposed_class.base != StringName(); |
| 486 |
|
| 487 |
if (!is_derived_type) { |
| 488 |
|
| 489 |
TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class, |
| 490 |
"Class '", p_exposed_class.name, "' has no base class."); |
| 491 |
TEST_FAIL_COND(!p_exposed_class.is_instantiable, |
| 492 |
"Object class is not instantiable."); |
| 493 |
TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE, |
| 494 |
"Object class is API is not API_CORE."); |
| 495 |
TEST_FAIL_COND(p_exposed_class.is_singleton, |
| 496 |
"Object class is registered as a singleton."); |
| 497 |
} |
| 498 |
|
| 499 |
TEST_FAIL_COND((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class), |
| 500 |
"Singleton base class '", String(p_exposed_class.base), "' is not Object, for class '", p_exposed_class.name, "'."); |
| 501 |
|
| 502 |
TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)), |
| 503 |
"Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'."); |
| 504 |
|
| 505 |
for (const PropertyData &F : p_exposed_class.properties) { |
| 506 |
validate_property(p_context, p_exposed_class, F); |
| 507 |
} |
| 508 |
|
| 509 |
for (const MethodData &F : p_exposed_class.methods) { |
| 510 |
validate_method(p_context, p_exposed_class, F); |
| 511 |
} |
| 512 |
|
| 513 |
for (const SignalData &F : p_exposed_class.signals_) { |
| 514 |
validate_signal(p_context, p_exposed_class, F); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
void add_exposed_classes(Context &r_context) { |
| 519 |
LocalVector<StringName> class_list; |
| 520 |
ClassDB::get_class_list(class_list); |
| 521 |
|
| 522 |
for (uint32_t class_list_idx = 0; class_list_idx < class_list.size(); class_list_idx++) { |
| 523 |
StringName class_name = class_list[class_list_idx]; |
| 524 |
|
| 525 |
ClassDB::APIType api_type = ClassDB::get_api_type(class_name); |
| 526 |
|
| 527 |
if (api_type == ClassDB::API_NONE) { |
| 528 |
continue; |
| 529 |
} |
| 530 |
|
| 531 |
if (!ClassDB::is_class_exposed(class_name)) { |
| 532 |
INFO(vformat("Ignoring class '%s' because it's not exposed.", class_name)); |
| 533 |
continue; |
| 534 |
} |
| 535 |
|
| 536 |
if (!ClassDB::is_class_enabled(class_name)) { |
| 537 |
INFO(vformat("Ignoring class '%s' because it's not enabled.", class_name)); |
| 538 |
continue; |
| 539 |
} |
| 540 |
|
| 541 |
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name); |
| 542 |
|
| 543 |
ExposedClass exposed_class; |
| 544 |
exposed_class.name = class_name; |
| 545 |
exposed_class.api_type = api_type; |
| 546 |
exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name); |
| 547 |
exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton; |
| 548 |
exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted"); |
| 549 |
exposed_class.base = ClassDB::get_parent_class(class_name); |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
List<PropertyInfo> property_list; |
| 554 |
ClassDB::get_property_list(class_name, &property_list, true); |
| 555 |
|
| 556 |
HashMap<StringName, StringName> accessor_methods; |
| 557 |
|
| 558 |
for (const PropertyInfo &property : property_list) { |
| 559 |
if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY || (property.type == Variant::NIL && property.usage & PROPERTY_USAGE_ARRAY)) { |
| 560 |
continue; |
| 561 |
} |
| 562 |
|
| 563 |
PropertyData prop; |
| 564 |
prop.name = property.name; |
| 565 |
prop.setter = ClassDB::get_property_setter(class_name, prop.name); |
| 566 |
prop.getter = ClassDB::get_property_getter(class_name, prop.name); |
| 567 |
|
| 568 |
if (prop.setter != StringName()) { |
| 569 |
accessor_methods[prop.setter] = prop.name; |
| 570 |
} |
| 571 |
if (prop.getter != StringName()) { |
| 572 |
accessor_methods[prop.getter] = prop.name; |
| 573 |
} |
| 574 |
|
| 575 |
bool valid = false; |
| 576 |
prop.index = ClassDB::get_property_index(class_name, prop.name, &valid); |
| 577 |
TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'."); |
| 578 |
|
| 579 |
exposed_class.properties.push_back(prop); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
List<MethodInfo> virtual_method_list; |
| 585 |
ClassDB::get_virtual_methods(class_name, &virtual_method_list, true); |
| 586 |
|
| 587 |
List<MethodInfo> method_list; |
| 588 |
ClassDB::get_method_list(class_name, &method_list, true); |
| 589 |
method_list.sort(); |
| 590 |
|
| 591 |
for (const MethodInfo &E : method_list) { |
| 592 |
const MethodInfo &method_info = E; |
| 593 |
|
| 594 |
if (method_info.name.is_empty()) { |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
MethodData method; |
| 599 |
method.name = method_info.name; |
| 600 |
TEST_FAIL_COND(!String(method.name).is_valid_ascii_identifier(), |
| 601 |
"Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'."); |
| 602 |
|
| 603 |
if (method_info.flags & METHOD_FLAG_VIRTUAL) { |
| 604 |
method.is_virtual = true; |
| 605 |
} |
| 606 |
|
| 607 |
PropertyInfo return_info = method_info.return_val; |
| 608 |
|
| 609 |
MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name); |
| 610 |
|
| 611 |
method.is_vararg = m && m->is_vararg(); |
| 612 |
|
| 613 |
if (!m && !method.is_virtual) { |
| 614 |
TEST_FAIL_COND(!virtual_method_list.find(method_info), |
| 615 |
"Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'."); |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
method.return_type.name = r_context.names_cache.void_type; |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
String warn_msg = vformat( |
| 627 |
"Notification: New unexpected virtual non-overridable method found. " |
| 628 |
"We only expected Object.free, but found '%s.%s'.", |
| 629 |
exposed_class.name, method.name); |
| 630 |
TEST_FAIL_COND_WARN( |
| 631 |
(exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"), |
| 632 |
warn_msg); |
| 633 |
|
| 634 |
} else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) { |
| 635 |
method.return_type.name = return_info.class_name; |
| 636 |
method.return_type.is_enum = true; |
| 637 |
} else if (return_info.class_name != StringName()) { |
| 638 |
method.return_type.name = return_info.class_name; |
| 639 |
|
| 640 |
bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE && |
| 641 |
ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class); |
| 642 |
TEST_COND(bad_reference_hint, "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'.", " Are you returning a reference type by pointer? Method: '", |
| 643 |
exposed_class.name, ".", method.name, "'."); |
| 644 |
} else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { |
| 645 |
method.return_type.name = return_info.hint_string; |
| 646 |
} else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { |
| 647 |
method.return_type.name = r_context.names_cache.variant_type; |
| 648 |
} else if (return_info.type == Variant::NIL) { |
| 649 |
method.return_type.name = r_context.names_cache.void_type; |
| 650 |
} else { |
| 651 |
|
| 652 |
method.return_type.name = Variant::get_type_name(return_info.type); |
| 653 |
} |
| 654 |
|
| 655 |
for (int64_t i = 0; i < method_info.arguments.size(); ++i) { |
| 656 |
const PropertyInfo &arg_info = method_info.arguments[i]; |
| 657 |
|
| 658 |
String orig_arg_name = arg_info.name; |
| 659 |
|
| 660 |
ArgumentData arg; |
| 661 |
arg.name = orig_arg_name; |
| 662 |
arg.position = i; |
| 663 |
|
| 664 |
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) { |
| 665 |
arg.type.name = arg_info.class_name; |
| 666 |
arg.type.is_enum = true; |
| 667 |
} else if (arg_info.class_name != StringName()) { |
| 668 |
arg.type.name = arg_info.class_name; |
| 669 |
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { |
| 670 |
arg.type.name = arg_info.hint_string; |
| 671 |
} else if (arg_info.type == Variant::NIL) { |
| 672 |
arg.type.name = r_context.names_cache.variant_type; |
| 673 |
} else { |
| 674 |
|
| 675 |
arg.type.name = Variant::get_type_name(arg_info.type); |
| 676 |
} |
| 677 |
|
| 678 |
if (m && m->has_default_argument(i)) { |
| 679 |
arg.has_defval = true; |
| 680 |
arg.defval = m->get_default_argument(i); |
| 681 |
} |
| 682 |
|
| 683 |
method.arguments.push_back(arg); |
| 684 |
} |
| 685 |
|
| 686 |
if (method.is_vararg) { |
| 687 |
ArgumentData vararg; |
| 688 |
vararg.type.name = r_context.names_cache.vararg_stub_type; |
| 689 |
vararg.name = "@varargs@"; |
| 690 |
method.arguments.push_back(vararg); |
| 691 |
} |
| 692 |
|
| 693 |
TEST_COND(exposed_class.find_property_by_name(method.name), |
| 694 |
"Method name conflicts with property: '", String(class_name), ".", String(method.name), "'."); |
| 695 |
|
| 696 |
|
| 697 |
if (!method.is_virtual && String(method.name)[0] == '_') { |
| 698 |
for (const PropertyData &F : exposed_class.properties) { |
| 699 |
const PropertyData &prop = F; |
| 700 |
|
| 701 |
if (prop.setter == method.name || prop.getter == method.name) { |
| 702 |
exposed_class.methods.push_back(method); |
| 703 |
break; |
| 704 |
} |
| 705 |
} |
| 706 |
} else { |
| 707 |
exposed_class.methods.push_back(method); |
| 708 |
} |
| 709 |
|
| 710 |
if (method.is_virtual) { |
| 711 |
TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore."); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
const AHashMap<StringName, MethodInfo> &signal_map = class_info->signal_map; |
| 718 |
|
| 719 |
for (const KeyValue<StringName, MethodInfo> &K : signal_map) { |
| 720 |
SignalData signal; |
| 721 |
|
| 722 |
const MethodInfo &method_info = signal_map.get(K.key); |
| 723 |
|
| 724 |
signal.name = method_info.name; |
| 725 |
TEST_FAIL_COND(!String(signal.name).is_valid_ascii_identifier(), |
| 726 |
"Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'."); |
| 727 |
|
| 728 |
for (int64_t i = 0; i < method_info.arguments.size(); ++i) { |
| 729 |
const PropertyInfo &arg_info = method_info.arguments[i]; |
| 730 |
|
| 731 |
String orig_arg_name = arg_info.name; |
| 732 |
|
| 733 |
ArgumentData arg; |
| 734 |
arg.name = orig_arg_name; |
| 735 |
arg.position = i; |
| 736 |
|
| 737 |
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) { |
| 738 |
arg.type.name = arg_info.class_name; |
| 739 |
arg.type.is_enum = true; |
| 740 |
} else if (arg_info.class_name != StringName()) { |
| 741 |
arg.type.name = arg_info.class_name; |
| 742 |
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { |
| 743 |
arg.type.name = arg_info.hint_string; |
| 744 |
} else if (arg_info.type == Variant::NIL) { |
| 745 |
arg.type.name = r_context.names_cache.variant_type; |
| 746 |
} else { |
| 747 |
|
| 748 |
arg.type.name = Variant::get_type_name(arg_info.type); |
| 749 |
} |
| 750 |
|
| 751 |
signal.arguments.push_back(arg); |
| 752 |
} |
| 753 |
|
| 754 |
bool method_conflict = exposed_class.find_property_by_name(signal.name); |
| 755 |
|
| 756 |
String warn_msg = vformat( |
| 757 |
"Signal name conflicts with %s: '%s.%s.", |
| 758 |
method_conflict ? "method" : "property", class_name, signal.name); |
| 759 |
TEST_FAIL_COND((method_conflict || exposed_class.find_method_by_name(signal.name)), |
| 760 |
warn_msg); |
| 761 |
|
| 762 |
exposed_class.signals_.push_back(signal); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
List<String> constants; |
| 768 |
ClassDB::get_integer_constant_list(class_name, &constants, true); |
| 769 |
|
| 770 |
const HashMap<StringName, ClassDB::ClassInfo::EnumInfo> &enum_map = class_info->enum_map; |
| 771 |
|
| 772 |
for (const KeyValue<StringName, ClassDB::ClassInfo::EnumInfo> &K : enum_map) { |
| 773 |
EnumData enum_; |
| 774 |
enum_.name = K.key; |
| 775 |
|
| 776 |
for (const StringName &E : K.value.constants) { |
| 777 |
const StringName &constant_name = E; |
| 778 |
TEST_FAIL_COND(String(constant_name).contains("::"), |
| 779 |
"Enum constant contains '::', check bindings to remove the scope: '", |
| 780 |
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'."); |
| 781 |
int64_t *value = class_info->constant_map.getptr(constant_name); |
| 782 |
TEST_FAIL_COND(!value, "Missing enum constant value: '", |
| 783 |
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'."); |
| 784 |
constants.erase(constant_name); |
| 785 |
|
| 786 |
ConstantData constant; |
| 787 |
constant.name = constant_name; |
| 788 |
constant.value = *value; |
| 789 |
|
| 790 |
enum_.constants.push_back(constant); |
| 791 |
} |
| 792 |
|
| 793 |
exposed_class.enums.push_back(enum_); |
| 794 |
|
| 795 |
r_context.enum_types.push_back(String(class_name) + "." + String(K.key)); |
| 796 |
} |
| 797 |
|
| 798 |
for (const String &E : constants) { |
| 799 |
const String &constant_name = E; |
| 800 |
TEST_FAIL_COND(constant_name.contains("::"), |
| 801 |
"Constant contains '::', check bindings to remove the scope: '", |
| 802 |
String(class_name), ".", constant_name, "'."); |
| 803 |
int64_t *value = class_info->constant_map.getptr(StringName(E)); |
| 804 |
TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'."); |
| 805 |
|
| 806 |
ConstantData constant; |
| 807 |
constant.name = constant_name; |
| 808 |
constant.value = *value; |
| 809 |
|
| 810 |
exposed_class.constants.push_back(constant); |
| 811 |
} |
| 812 |
|
| 813 |
r_context.exposed_classes.insert(class_name, exposed_class); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
void add_builtin_types(Context &r_context) { |
| 818 |
|
| 819 |
for (int i = 0; i < Variant::VARIANT_MAX; i++) { |
| 820 |
r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i))); |
| 821 |
} |
| 822 |
|
| 823 |
r_context.builtin_types.push_back(_STR(Variant)); |
| 824 |
r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type); |
| 825 |
r_context.builtin_types.push_back("void"); |
| 826 |
} |
| 827 |
|
| 828 |
void add_global_enums(Context &r_context) { |
| 829 |
int global_constants_count = CoreConstants::get_global_constant_count(); |
| 830 |
|
| 831 |
if (global_constants_count > 0) { |
| 832 |
for (int i = 0; i < global_constants_count; i++) { |
| 833 |
StringName enum_name = CoreConstants::get_global_constant_enum(i); |
| 834 |
|
| 835 |
if (enum_name != StringName()) { |
| 836 |
ConstantData constant; |
| 837 |
constant.name = CoreConstants::get_global_constant_name(i); |
| 838 |
constant.value = CoreConstants::get_global_constant_value(i); |
| 839 |
|
| 840 |
EnumData enum_; |
| 841 |
enum_.name = enum_name; |
| 842 |
List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_); |
| 843 |
if (enum_match) { |
| 844 |
enum_match->get().constants.push_back(constant); |
| 845 |
} else { |
| 846 |
enum_.constants.push_back(constant); |
| 847 |
r_context.global_enums.push_back(enum_); |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
for (const EnumData &E : r_context.global_enums) { |
| 853 |
r_context.enum_types.push_back(E.name); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
for (int i = 0; i < Variant::VARIANT_MAX; i++) { |
| 858 |
if (i == Variant::OBJECT) { |
| 859 |
continue; |
| 860 |
} |
| 861 |
|
| 862 |
const Variant::Type type = Variant::Type(i); |
| 863 |
|
| 864 |
List<StringName> enum_names; |
| 865 |
Variant::get_enums_for_type(type, &enum_names); |
| 866 |
|
| 867 |
for (const StringName &enum_name : enum_names) { |
| 868 |
r_context.enum_types.push_back(Variant::get_type_name(type) + "." + enum_name); |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
TEST_SUITE("[ClassDB]") { |
| 874 |
TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") { |
| 875 |
Context context; |
| 876 |
|
| 877 |
add_exposed_classes(context); |
| 878 |
add_builtin_types(context); |
| 879 |
add_global_enums(context); |
| 880 |
|
| 881 |
SUBCASE("[ClassDB] Validate exposed classes") { |
| 882 |
const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class); |
| 883 |
TEST_FAIL_COND(!object_class, "Object class not found."); |
| 884 |
TEST_FAIL_COND(object_class->base != StringName(), |
| 885 |
"Object class derives from another class: '", object_class->base, "'."); |
| 886 |
|
| 887 |
for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) { |
| 888 |
validate_class(context, E.value); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
} |
| 893 |
} |