test_class_db.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /**************************************************************************/
  2. /* test_class_db.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_CLASS_DB_H
  31. #define TEST_CLASS_DB_H
  32. #include "core/core_bind.h"
  33. #include "core/core_constants.h"
  34. #include "core/object/class_db.h"
  35. #include "tests/test_macros.h"
  36. namespace TestClassDB {
  37. struct TypeReference {
  38. StringName name;
  39. bool is_enum = false;
  40. };
  41. struct ConstantData {
  42. String name;
  43. int64_t value = 0;
  44. };
  45. struct EnumData {
  46. StringName name;
  47. List<ConstantData> constants;
  48. _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
  49. return p_enum.name == name;
  50. }
  51. };
  52. struct PropertyData {
  53. StringName name;
  54. int index = 0;
  55. StringName getter;
  56. StringName setter;
  57. };
  58. struct ArgumentData {
  59. TypeReference type;
  60. String name;
  61. bool has_defval = false;
  62. Variant defval;
  63. int position;
  64. };
  65. struct MethodData {
  66. StringName name;
  67. TypeReference return_type;
  68. List<ArgumentData> arguments;
  69. bool is_virtual = false;
  70. bool is_vararg = false;
  71. };
  72. struct SignalData {
  73. StringName name;
  74. List<ArgumentData> arguments;
  75. };
  76. struct ExposedClass {
  77. StringName name;
  78. StringName base;
  79. bool is_singleton = false;
  80. bool is_instantiable = false;
  81. bool is_ref_counted = false;
  82. ClassDB::APIType api_type;
  83. List<ConstantData> constants;
  84. List<EnumData> enums;
  85. List<PropertyData> properties;
  86. List<MethodData> methods;
  87. List<SignalData> signals_;
  88. const PropertyData *find_property_by_name(const StringName &p_name) const {
  89. for (const PropertyData &E : properties) {
  90. if (E.name == p_name) {
  91. return &E;
  92. }
  93. }
  94. return nullptr;
  95. }
  96. const MethodData *find_method_by_name(const StringName &p_name) const {
  97. for (const MethodData &E : methods) {
  98. if (E.name == p_name) {
  99. return &E;
  100. }
  101. }
  102. return nullptr;
  103. }
  104. };
  105. struct NamesCache {
  106. StringName variant_type = StaticCString::create("Variant");
  107. StringName object_class = StaticCString::create("Object");
  108. StringName ref_counted_class = StaticCString::create("RefCounted");
  109. StringName string_type = StaticCString::create("String");
  110. StringName string_name_type = StaticCString::create("StringName");
  111. StringName node_path_type = StaticCString::create("NodePath");
  112. StringName bool_type = StaticCString::create("bool");
  113. StringName int_type = StaticCString::create("int");
  114. StringName float_type = StaticCString::create("float");
  115. StringName void_type = StaticCString::create("void");
  116. StringName vararg_stub_type = StaticCString::create("@VarArg@");
  117. StringName vector2_type = StaticCString::create("Vector2");
  118. StringName rect2_type = StaticCString::create("Rect2");
  119. StringName vector3_type = StaticCString::create("Vector3");
  120. StringName vector4_type = StaticCString::create("Vector4");
  121. // Object not included as it must be checked for all derived classes
  122. static constexpr int nullable_types_count = 18;
  123. StringName nullable_types[nullable_types_count] = {
  124. string_type,
  125. string_name_type,
  126. node_path_type,
  127. StaticCString::create(_STR(Array)),
  128. StaticCString::create(_STR(Dictionary)),
  129. StaticCString::create(_STR(Callable)),
  130. StaticCString::create(_STR(Signal)),
  131. StaticCString::create(_STR(PackedByteArray)),
  132. StaticCString::create(_STR(PackedInt32Array)),
  133. StaticCString::create(_STR(PackedInt64rray)),
  134. StaticCString::create(_STR(PackedFloat32Array)),
  135. StaticCString::create(_STR(PackedFloat64Array)),
  136. StaticCString::create(_STR(PackedStringArray)),
  137. StaticCString::create(_STR(PackedVector2Array)),
  138. StaticCString::create(_STR(PackedVector3Array)),
  139. StaticCString::create(_STR(PackedColorArray)),
  140. StaticCString::create(_STR(PackedVector4Array)),
  141. };
  142. bool is_nullable_type(const StringName &p_type) const {
  143. for (int i = 0; i < nullable_types_count; i++) {
  144. if (p_type == nullable_types[i]) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. };
  151. typedef HashMap<StringName, ExposedClass> ExposedClasses;
  152. struct Context {
  153. Vector<StringName> enum_types;
  154. Vector<StringName> builtin_types;
  155. ExposedClasses exposed_classes;
  156. List<EnumData> global_enums;
  157. NamesCache names_cache;
  158. const ExposedClass *find_exposed_class(const StringName &p_name) const {
  159. ExposedClasses::ConstIterator elem = exposed_classes.find(p_name);
  160. return elem ? &elem->value : nullptr;
  161. }
  162. const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
  163. ExposedClasses::ConstIterator elem = exposed_classes.find(p_type_ref.name);
  164. return elem ? &elem->value : nullptr;
  165. }
  166. bool has_type(const TypeReference &p_type_ref) const {
  167. if (builtin_types.has(p_type_ref.name)) {
  168. return true;
  169. }
  170. if (p_type_ref.is_enum) {
  171. if (enum_types.has(p_type_ref.name)) {
  172. return true;
  173. }
  174. // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
  175. return builtin_types.find(names_cache.int_type);
  176. }
  177. return false;
  178. }
  179. };
  180. 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) {
  181. if (p_arg_type.name == p_context.names_cache.variant_type) {
  182. // Variant can take anything
  183. return true;
  184. }
  185. switch (p_val.get_type()) {
  186. case Variant::NIL:
  187. return p_context.find_exposed_class(p_arg_type) ||
  188. p_context.names_cache.is_nullable_type(p_arg_type.name);
  189. case Variant::BOOL:
  190. return p_arg_type.name == p_context.names_cache.bool_type;
  191. case Variant::INT:
  192. return p_arg_type.name == p_context.names_cache.int_type ||
  193. p_arg_type.name == p_context.names_cache.float_type ||
  194. p_arg_type.is_enum;
  195. case Variant::FLOAT:
  196. return p_arg_type.name == p_context.names_cache.float_type;
  197. case Variant::STRING:
  198. case Variant::STRING_NAME:
  199. return p_arg_type.name == p_context.names_cache.string_type ||
  200. p_arg_type.name == p_context.names_cache.string_name_type ||
  201. p_arg_type.name == p_context.names_cache.node_path_type;
  202. case Variant::NODE_PATH:
  203. return p_arg_type.name == p_context.names_cache.node_path_type;
  204. case Variant::TRANSFORM3D:
  205. case Variant::TRANSFORM2D:
  206. case Variant::BASIS:
  207. case Variant::QUATERNION:
  208. case Variant::PLANE:
  209. case Variant::AABB:
  210. case Variant::COLOR:
  211. case Variant::VECTOR2:
  212. case Variant::RECT2:
  213. case Variant::VECTOR3:
  214. case Variant::VECTOR4:
  215. case Variant::PROJECTION:
  216. case Variant::RID:
  217. case Variant::ARRAY:
  218. case Variant::DICTIONARY:
  219. case Variant::PACKED_BYTE_ARRAY:
  220. case Variant::PACKED_INT32_ARRAY:
  221. case Variant::PACKED_INT64_ARRAY:
  222. case Variant::PACKED_FLOAT32_ARRAY:
  223. case Variant::PACKED_FLOAT64_ARRAY:
  224. case Variant::PACKED_STRING_ARRAY:
  225. case Variant::PACKED_VECTOR2_ARRAY:
  226. case Variant::PACKED_VECTOR3_ARRAY:
  227. case Variant::PACKED_COLOR_ARRAY:
  228. case Variant::PACKED_VECTOR4_ARRAY:
  229. case Variant::CALLABLE:
  230. case Variant::SIGNAL:
  231. return p_arg_type.name == Variant::get_type_name(p_val.get_type());
  232. case Variant::OBJECT:
  233. return p_context.find_exposed_class(p_arg_type);
  234. case Variant::VECTOR2I:
  235. return p_arg_type.name == p_context.names_cache.vector2_type ||
  236. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  237. case Variant::RECT2I:
  238. return p_arg_type.name == p_context.names_cache.rect2_type ||
  239. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  240. case Variant::VECTOR3I:
  241. return p_arg_type.name == p_context.names_cache.vector3_type ||
  242. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  243. case Variant::VECTOR4I:
  244. return p_arg_type.name == p_context.names_cache.vector4_type ||
  245. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  246. case Variant::VARIANT_MAX:
  247. break;
  248. }
  249. if (r_err_msg) {
  250. *r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type());
  251. }
  252. return false;
  253. }
  254. bool arg_default_value_is_valid_data(const Variant &p_val, String *r_err_msg = nullptr) {
  255. switch (p_val.get_type()) {
  256. case Variant::RID:
  257. case Variant::ARRAY:
  258. case Variant::DICTIONARY:
  259. case Variant::PACKED_BYTE_ARRAY:
  260. case Variant::PACKED_INT32_ARRAY:
  261. case Variant::PACKED_INT64_ARRAY:
  262. case Variant::PACKED_FLOAT32_ARRAY:
  263. case Variant::PACKED_FLOAT64_ARRAY:
  264. case Variant::PACKED_STRING_ARRAY:
  265. case Variant::PACKED_VECTOR2_ARRAY:
  266. case Variant::PACKED_VECTOR3_ARRAY:
  267. case Variant::PACKED_COLOR_ARRAY:
  268. case Variant::PACKED_VECTOR4_ARRAY:
  269. case Variant::CALLABLE:
  270. case Variant::SIGNAL:
  271. case Variant::OBJECT:
  272. if (p_val.is_zero()) {
  273. return true;
  274. }
  275. if (r_err_msg) {
  276. *r_err_msg = "Must be zero.";
  277. }
  278. break;
  279. default:
  280. return true;
  281. }
  282. return false;
  283. }
  284. void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
  285. const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
  286. // Search it in base classes too
  287. const ExposedClass *top = &p_class;
  288. while (!setter && top->base != StringName()) {
  289. top = p_context.find_exposed_class(top->base);
  290. TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
  291. setter = top->find_method_by_name(p_prop.setter);
  292. }
  293. const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
  294. // Search it in base classes too
  295. top = &p_class;
  296. while (!getter && top->base != StringName()) {
  297. top = p_context.find_exposed_class(top->base);
  298. TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
  299. getter = top->find_method_by_name(p_prop.getter);
  300. }
  301. TEST_FAIL_COND((!setter && !getter),
  302. "Couldn't find neither the setter nor the getter for property: '", p_class.name, ".", String(p_prop.name), "'.");
  303. if (setter) {
  304. int setter_argc = p_prop.index != -1 ? 2 : 1;
  305. TEST_FAIL_COND(setter->arguments.size() != setter_argc,
  306. "Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
  307. }
  308. if (getter) {
  309. int getter_argc = p_prop.index != -1 ? 1 : 0;
  310. TEST_FAIL_COND(getter->arguments.size() != getter_argc,
  311. "Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
  312. }
  313. if (getter && setter) {
  314. const ArgumentData &setter_first_arg = setter->arguments.back()->get();
  315. if (getter->return_type.name != setter_first_arg.type.name) {
  316. // Special case for Node::set_name
  317. bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
  318. setter_first_arg.type.name == p_context.names_cache.string_type;
  319. TEST_FAIL_COND(!whitelisted,
  320. "Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'.");
  321. }
  322. }
  323. const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
  324. const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
  325. if (prop_class) {
  326. TEST_COND(prop_class->is_singleton,
  327. "Property type is a singleton: '", p_class.name, ".", String(p_prop.name), "'.");
  328. if (p_class.api_type == ClassDB::API_CORE) {
  329. TEST_COND(prop_class->api_type == ClassDB::API_EDITOR,
  330. "Property '", p_class.name, ".", p_prop.name, "' has type '", prop_class->name,
  331. "' from the editor API. Core API cannot have dependencies on the editor API.");
  332. }
  333. } else {
  334. // Look for types that don't inherit Object
  335. TEST_FAIL_COND(!p_context.has_type(prop_type_ref),
  336. "Property type '", prop_type_ref.name, "' not found: '", p_class.name, ".", String(p_prop.name), "'.");
  337. }
  338. if (getter) {
  339. if (p_prop.index != -1) {
  340. const ArgumentData &idx_arg = getter->arguments.front()->get();
  341. if (idx_arg.type.name != p_context.names_cache.int_type) {
  342. // If not an int, it can be an enum
  343. TEST_COND(!p_context.enum_types.has(idx_arg.type.name),
  344. "Invalid type '", idx_arg.type.name, "' for index argument of property getter: '", p_class.name, ".", String(p_prop.name), "'.");
  345. }
  346. }
  347. }
  348. if (setter) {
  349. if (p_prop.index != -1) {
  350. const ArgumentData &idx_arg = setter->arguments.front()->get();
  351. if (idx_arg.type.name != p_context.names_cache.int_type) {
  352. // Assume the index parameter is an enum
  353. // If not an int, it can be an enum
  354. TEST_COND(!p_context.enum_types.has(idx_arg.type.name),
  355. "Invalid type '", idx_arg.type.name, "' for index argument of property setter: '", p_class.name, ".", String(p_prop.name), "'.");
  356. }
  357. }
  358. }
  359. }
  360. 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) {
  361. #ifdef DEBUG_METHODS_ENABLED
  362. TEST_COND((p_arg.name.is_empty() || p_arg.name.begins_with("_unnamed_arg")),
  363. vformat("Unnamed argument in position %d of %s '%s.%s'.", p_arg.position, p_owner_type, p_class.name, p_owner_name));
  364. #endif // DEBUG_METHODS_ENABLED
  365. const ExposedClass *arg_class = p_context.find_exposed_class(p_arg.type);
  366. if (arg_class) {
  367. TEST_COND(arg_class->is_singleton,
  368. vformat("Argument type is a singleton: '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));
  369. if (p_class.api_type == ClassDB::API_CORE) {
  370. TEST_COND(arg_class->api_type == ClassDB::API_EDITOR,
  371. vformat("Argument '%s' of %s '%s.%s' has type '%s' from the editor API. Core API cannot have dependencies on the editor API.",
  372. p_arg.name, p_owner_type, p_class.name, p_owner_name, arg_class->name));
  373. }
  374. } else {
  375. // Look for types that don't inherit Object.
  376. TEST_FAIL_COND(!p_context.has_type(p_arg.type),
  377. 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));
  378. }
  379. if (p_arg.has_defval) {
  380. String type_error_msg;
  381. bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, p_arg.defval, p_arg.type, &type_error_msg);
  382. 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);
  383. if (!type_error_msg.is_empty()) {
  384. err_msg += " " + type_error_msg;
  385. }
  386. TEST_COND(!arg_defval_assignable_to_type, err_msg);
  387. bool arg_defval_valid_data = arg_default_value_is_valid_data(p_arg.defval, &type_error_msg);
  388. if (!type_error_msg.is_empty()) {
  389. err_msg += " " + type_error_msg;
  390. }
  391. TEST_COND(!arg_defval_valid_data, err_msg);
  392. }
  393. }
  394. void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
  395. if (p_method.return_type.name != StringName()) {
  396. const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
  397. if (return_class) {
  398. if (p_class.api_type == ClassDB::API_CORE) {
  399. TEST_COND(return_class->api_type == ClassDB::API_EDITOR,
  400. "Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,
  401. "' from the editor API. Core API cannot have dependencies on the editor API.");
  402. }
  403. } else {
  404. // Look for types that don't inherit Object
  405. TEST_FAIL_COND(!p_context.has_type(p_method.return_type),
  406. "Method return type '", p_method.return_type.name, "' not found: '", p_class.name, ".", p_method.name, "'.");
  407. }
  408. }
  409. for (const ArgumentData &F : p_method.arguments) {
  410. const ArgumentData &arg = F;
  411. validate_argument(p_context, p_class, p_method.name, "method", arg);
  412. }
  413. }
  414. void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
  415. for (const ArgumentData &F : p_signal.arguments) {
  416. const ArgumentData &arg = F;
  417. validate_argument(p_context, p_class, p_signal.name, "signal", arg);
  418. }
  419. }
  420. void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
  421. bool is_derived_type = p_exposed_class.base != StringName();
  422. if (!is_derived_type) {
  423. // Asserts about the base Object class
  424. TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,
  425. "Class '", p_exposed_class.name, "' has no base class.");
  426. TEST_FAIL_COND(!p_exposed_class.is_instantiable,
  427. "Object class is not instantiable.");
  428. TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,
  429. "Object class is API is not API_CORE.");
  430. TEST_FAIL_COND(p_exposed_class.is_singleton,
  431. "Object class is registered as a singleton.");
  432. }
  433. TEST_FAIL_COND((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class),
  434. "Singleton base class '", String(p_exposed_class.base), "' is not Object, for class '", p_exposed_class.name, "'.");
  435. TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),
  436. "Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'.");
  437. for (const PropertyData &F : p_exposed_class.properties) {
  438. validate_property(p_context, p_exposed_class, F);
  439. }
  440. for (const MethodData &F : p_exposed_class.methods) {
  441. validate_method(p_context, p_exposed_class, F);
  442. }
  443. for (const SignalData &F : p_exposed_class.signals_) {
  444. validate_signal(p_context, p_exposed_class, F);
  445. }
  446. }
  447. void add_exposed_classes(Context &r_context) {
  448. List<StringName> class_list;
  449. ClassDB::get_class_list(&class_list);
  450. class_list.sort_custom<StringName::AlphCompare>();
  451. while (class_list.size()) {
  452. StringName class_name = class_list.front()->get();
  453. ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
  454. if (api_type == ClassDB::API_NONE) {
  455. class_list.pop_front();
  456. continue;
  457. }
  458. if (!ClassDB::is_class_exposed(class_name)) {
  459. INFO(vformat("Ignoring class '%s' because it's not exposed.", class_name));
  460. class_list.pop_front();
  461. continue;
  462. }
  463. if (!ClassDB::is_class_enabled(class_name)) {
  464. INFO(vformat("Ignoring class '%s' because it's not enabled.", class_name));
  465. class_list.pop_front();
  466. continue;
  467. }
  468. ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
  469. ExposedClass exposed_class;
  470. exposed_class.name = class_name;
  471. exposed_class.api_type = api_type;
  472. exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
  473. exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
  474. exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted");
  475. exposed_class.base = ClassDB::get_parent_class(class_name);
  476. // Add properties
  477. List<PropertyInfo> property_list;
  478. ClassDB::get_property_list(class_name, &property_list, true);
  479. HashMap<StringName, StringName> accessor_methods;
  480. for (const PropertyInfo &property : property_list) {
  481. 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)) {
  482. continue;
  483. }
  484. PropertyData prop;
  485. prop.name = property.name;
  486. prop.setter = ClassDB::get_property_setter(class_name, prop.name);
  487. prop.getter = ClassDB::get_property_getter(class_name, prop.name);
  488. if (prop.setter != StringName()) {
  489. accessor_methods[prop.setter] = prop.name;
  490. }
  491. if (prop.getter != StringName()) {
  492. accessor_methods[prop.getter] = prop.name;
  493. }
  494. bool valid = false;
  495. prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
  496. TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'.");
  497. exposed_class.properties.push_back(prop);
  498. }
  499. // Add methods
  500. List<MethodInfo> virtual_method_list;
  501. ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
  502. List<MethodInfo> method_list;
  503. ClassDB::get_method_list(class_name, &method_list, true);
  504. method_list.sort();
  505. for (const MethodInfo &E : method_list) {
  506. const MethodInfo &method_info = E;
  507. if (method_info.name.is_empty()) {
  508. continue;
  509. }
  510. MethodData method;
  511. method.name = method_info.name;
  512. TEST_FAIL_COND(!String(method.name).is_valid_ascii_identifier(),
  513. "Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'.");
  514. if (method_info.flags & METHOD_FLAG_VIRTUAL) {
  515. method.is_virtual = true;
  516. }
  517. PropertyInfo return_info = method_info.return_val;
  518. MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
  519. method.is_vararg = m && m->is_vararg();
  520. if (!m && !method.is_virtual) {
  521. TEST_FAIL_COND(!virtual_method_list.find(method_info),
  522. "Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'.");
  523. // A virtual method without the virtual flag. This is a special case.
  524. // The method Object.free is registered as a virtual method, but without the virtual flag.
  525. // This is because this method is not supposed to be overridden, but called.
  526. // We assume the return type is void.
  527. method.return_type.name = r_context.names_cache.void_type;
  528. // Actually, more methods like this may be added in the future, which could return
  529. // something different. Let's put this check to notify us if that ever happens.
  530. String warn_msg = vformat(
  531. "Notification: New unexpected virtual non-overridable method found. "
  532. "We only expected Object.free, but found '%s.%s'.",
  533. exposed_class.name, method.name);
  534. TEST_FAIL_COND_WARN(
  535. (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),
  536. warn_msg);
  537. } else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  538. method.return_type.name = return_info.class_name;
  539. method.return_type.is_enum = true;
  540. } else if (return_info.class_name != StringName()) {
  541. method.return_type.name = return_info.class_name;
  542. bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
  543. ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class);
  544. 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: '",
  545. exposed_class.name, ".", method.name, "'.");
  546. } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  547. method.return_type.name = return_info.hint_string;
  548. } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  549. method.return_type.name = r_context.names_cache.variant_type;
  550. } else if (return_info.type == Variant::NIL) {
  551. method.return_type.name = r_context.names_cache.void_type;
  552. } else {
  553. // NOTE: We don't care about the size and sign of int and float in these tests
  554. method.return_type.name = Variant::get_type_name(return_info.type);
  555. }
  556. int i = 0;
  557. for (List<PropertyInfo>::ConstIterator itr = method_info.arguments.begin(); itr != method_info.arguments.end(); ++itr, ++i) {
  558. const PropertyInfo &arg_info = *itr;
  559. String orig_arg_name = arg_info.name;
  560. ArgumentData arg;
  561. arg.name = orig_arg_name;
  562. arg.position = i;
  563. if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  564. arg.type.name = arg_info.class_name;
  565. arg.type.is_enum = true;
  566. } else if (arg_info.class_name != StringName()) {
  567. arg.type.name = arg_info.class_name;
  568. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  569. arg.type.name = arg_info.hint_string;
  570. } else if (arg_info.type == Variant::NIL) {
  571. arg.type.name = r_context.names_cache.variant_type;
  572. } else {
  573. // NOTE: We don't care about the size and sign of int and float in these tests
  574. arg.type.name = Variant::get_type_name(arg_info.type);
  575. }
  576. if (m && m->has_default_argument(i)) {
  577. arg.has_defval = true;
  578. arg.defval = m->get_default_argument(i);
  579. }
  580. method.arguments.push_back(arg);
  581. }
  582. if (method.is_vararg) {
  583. ArgumentData vararg;
  584. vararg.type.name = r_context.names_cache.vararg_stub_type;
  585. vararg.name = "@varargs@";
  586. method.arguments.push_back(vararg);
  587. }
  588. TEST_COND(exposed_class.find_property_by_name(method.name),
  589. "Method name conflicts with property: '", String(class_name), ".", String(method.name), "'.");
  590. // Methods starting with an underscore are ignored unless they're virtual or used as a property setter or getter.
  591. if (!method.is_virtual && String(method.name)[0] == '_') {
  592. for (const PropertyData &F : exposed_class.properties) {
  593. const PropertyData &prop = F;
  594. if (prop.setter == method.name || prop.getter == method.name) {
  595. exposed_class.methods.push_back(method);
  596. break;
  597. }
  598. }
  599. } else {
  600. exposed_class.methods.push_back(method);
  601. }
  602. if (method.is_virtual) {
  603. TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore.");
  604. }
  605. }
  606. // Add signals
  607. const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
  608. for (const KeyValue<StringName, MethodInfo> &K : signal_map) {
  609. SignalData signal;
  610. const MethodInfo &method_info = signal_map.get(K.key);
  611. signal.name = method_info.name;
  612. TEST_FAIL_COND(!String(signal.name).is_valid_ascii_identifier(),
  613. "Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'.");
  614. int i = 0;
  615. for (List<PropertyInfo>::ConstIterator itr = method_info.arguments.begin(); itr != method_info.arguments.end(); ++itr, ++i) {
  616. const PropertyInfo &arg_info = *itr;
  617. String orig_arg_name = arg_info.name;
  618. ArgumentData arg;
  619. arg.name = orig_arg_name;
  620. arg.position = i;
  621. if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  622. arg.type.name = arg_info.class_name;
  623. arg.type.is_enum = true;
  624. } else if (arg_info.class_name != StringName()) {
  625. arg.type.name = arg_info.class_name;
  626. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  627. arg.type.name = arg_info.hint_string;
  628. } else if (arg_info.type == Variant::NIL) {
  629. arg.type.name = r_context.names_cache.variant_type;
  630. } else {
  631. // NOTE: We don't care about the size and sign of int and float in these tests
  632. arg.type.name = Variant::get_type_name(arg_info.type);
  633. }
  634. signal.arguments.push_back(arg);
  635. }
  636. bool method_conflict = exposed_class.find_property_by_name(signal.name);
  637. String warn_msg = vformat(
  638. "Signal name conflicts with %s: '%s.%s.",
  639. method_conflict ? "method" : "property", class_name, signal.name);
  640. TEST_FAIL_COND((method_conflict || exposed_class.find_method_by_name(signal.name)),
  641. warn_msg);
  642. exposed_class.signals_.push_back(signal);
  643. }
  644. // Add enums and constants
  645. List<String> constants;
  646. ClassDB::get_integer_constant_list(class_name, &constants, true);
  647. const HashMap<StringName, ClassDB::ClassInfo::EnumInfo> &enum_map = class_info->enum_map;
  648. for (const KeyValue<StringName, ClassDB::ClassInfo::EnumInfo> &K : enum_map) {
  649. EnumData enum_;
  650. enum_.name = K.key;
  651. for (const StringName &E : K.value.constants) {
  652. const StringName &constant_name = E;
  653. TEST_FAIL_COND(String(constant_name).contains("::"),
  654. "Enum constant contains '::', check bindings to remove the scope: '",
  655. String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
  656. int64_t *value = class_info->constant_map.getptr(constant_name);
  657. TEST_FAIL_COND(!value, "Missing enum constant value: '",
  658. String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
  659. constants.erase(constant_name);
  660. ConstantData constant;
  661. constant.name = constant_name;
  662. constant.value = *value;
  663. enum_.constants.push_back(constant);
  664. }
  665. exposed_class.enums.push_back(enum_);
  666. r_context.enum_types.push_back(String(class_name) + "." + String(K.key));
  667. }
  668. for (const String &E : constants) {
  669. const String &constant_name = E;
  670. TEST_FAIL_COND(constant_name.contains("::"),
  671. "Constant contains '::', check bindings to remove the scope: '",
  672. String(class_name), ".", constant_name, "'.");
  673. int64_t *value = class_info->constant_map.getptr(StringName(E));
  674. TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'.");
  675. ConstantData constant;
  676. constant.name = constant_name;
  677. constant.value = *value;
  678. exposed_class.constants.push_back(constant);
  679. }
  680. r_context.exposed_classes.insert(class_name, exposed_class);
  681. class_list.pop_front();
  682. }
  683. }
  684. void add_builtin_types(Context &r_context) {
  685. // NOTE: We don't care about the size and sign of int and float in these tests
  686. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  687. r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
  688. }
  689. r_context.builtin_types.push_back(_STR(Variant));
  690. r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
  691. r_context.builtin_types.push_back("void");
  692. }
  693. void add_global_enums(Context &r_context) {
  694. int global_constants_count = CoreConstants::get_global_constant_count();
  695. if (global_constants_count > 0) {
  696. for (int i = 0; i < global_constants_count; i++) {
  697. StringName enum_name = CoreConstants::get_global_constant_enum(i);
  698. if (enum_name != StringName()) {
  699. ConstantData constant;
  700. constant.name = CoreConstants::get_global_constant_name(i);
  701. constant.value = CoreConstants::get_global_constant_value(i);
  702. EnumData enum_;
  703. enum_.name = enum_name;
  704. List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
  705. if (enum_match) {
  706. enum_match->get().constants.push_back(constant);
  707. } else {
  708. enum_.constants.push_back(constant);
  709. r_context.global_enums.push_back(enum_);
  710. }
  711. }
  712. }
  713. for (const EnumData &E : r_context.global_enums) {
  714. r_context.enum_types.push_back(E.name);
  715. }
  716. }
  717. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  718. if (i == Variant::OBJECT) {
  719. continue;
  720. }
  721. const Variant::Type type = Variant::Type(i);
  722. List<StringName> enum_names;
  723. Variant::get_enums_for_type(type, &enum_names);
  724. for (const StringName &enum_name : enum_names) {
  725. r_context.enum_types.push_back(Variant::get_type_name(type) + "." + enum_name);
  726. }
  727. }
  728. }
  729. TEST_SUITE("[ClassDB]") {
  730. TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {
  731. Context context;
  732. add_exposed_classes(context);
  733. add_builtin_types(context);
  734. add_global_enums(context);
  735. SUBCASE("[ClassDB] Validate exposed classes") {
  736. const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
  737. TEST_FAIL_COND(!object_class, "Object class not found.");
  738. TEST_FAIL_COND(object_class->base != StringName(),
  739. "Object class derives from another class: '", object_class->base, "'.");
  740. for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) {
  741. validate_class(context, E.value);
  742. }
  743. }
  744. }
  745. }
  746. } // namespace TestClassDB
  747. #endif // TEST_CLASS_DB_H