object.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  1. /**************************************************************************/
  2. /* object.cpp */
  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. #include "object.h"
  31. #include "core/extension/gdextension_manager.h"
  32. #include "core/io/resource.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/os.h"
  37. #include "core/string/print_string.h"
  38. #include "core/string/translation_server.h"
  39. #include "core/variant/typed_array.h"
  40. #ifdef DEBUG_ENABLED
  41. struct _ObjectDebugLock {
  42. ObjectID obj_id;
  43. _ObjectDebugLock(Object *p_obj) {
  44. obj_id = p_obj->get_instance_id();
  45. p_obj->_lock_index.ref();
  46. }
  47. ~_ObjectDebugLock() {
  48. Object *obj_ptr = ObjectDB::get_instance(obj_id);
  49. if (likely(obj_ptr)) {
  50. obj_ptr->_lock_index.unref();
  51. }
  52. }
  53. };
  54. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  55. #else
  56. #define OBJ_DEBUG_LOCK
  57. #endif
  58. PropertyInfo::operator Dictionary() const {
  59. Dictionary d;
  60. d["name"] = name;
  61. d["class_name"] = class_name;
  62. d["type"] = type;
  63. d["hint"] = hint;
  64. d["hint_string"] = hint_string;
  65. d["usage"] = usage;
  66. return d;
  67. }
  68. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  69. PropertyInfo pi;
  70. if (p_dict.has("type")) {
  71. pi.type = Variant::Type(int(p_dict["type"]));
  72. }
  73. if (p_dict.has("name")) {
  74. pi.name = p_dict["name"];
  75. }
  76. if (p_dict.has("class_name")) {
  77. pi.class_name = p_dict["class_name"];
  78. }
  79. if (p_dict.has("hint")) {
  80. pi.hint = PropertyHint(int(p_dict["hint"]));
  81. }
  82. if (p_dict.has("hint_string")) {
  83. pi.hint_string = p_dict["hint_string"];
  84. }
  85. if (p_dict.has("usage")) {
  86. pi.usage = p_dict["usage"];
  87. }
  88. return pi;
  89. }
  90. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
  91. TypedArray<Dictionary> va;
  92. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  93. va.push_back(Dictionary(E->get()));
  94. }
  95. return va;
  96. }
  97. TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector) {
  98. TypedArray<Dictionary> va;
  99. for (const PropertyInfo &E : p_vector) {
  100. va.push_back(Dictionary(E));
  101. }
  102. return va;
  103. }
  104. MethodInfo::operator Dictionary() const {
  105. Dictionary d;
  106. d["name"] = name;
  107. d["args"] = convert_property_list(arguments);
  108. Array da;
  109. for (int i = 0; i < default_arguments.size(); i++) {
  110. da.push_back(default_arguments[i]);
  111. }
  112. d["default_args"] = da;
  113. d["flags"] = flags;
  114. d["id"] = id;
  115. Dictionary r = return_val;
  116. d["return"] = r;
  117. return d;
  118. }
  119. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  120. MethodInfo mi;
  121. if (p_dict.has("name")) {
  122. mi.name = p_dict["name"];
  123. }
  124. Array args;
  125. if (p_dict.has("args")) {
  126. args = p_dict["args"];
  127. }
  128. for (const Variant &arg : args) {
  129. Dictionary d = arg;
  130. mi.arguments.push_back(PropertyInfo::from_dict(d));
  131. }
  132. Array defargs;
  133. if (p_dict.has("default_args")) {
  134. defargs = p_dict["default_args"];
  135. }
  136. for (const Variant &defarg : defargs) {
  137. mi.default_arguments.push_back(defarg);
  138. }
  139. if (p_dict.has("return")) {
  140. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  141. }
  142. if (p_dict.has("flags")) {
  143. mi.flags = p_dict["flags"];
  144. }
  145. return mi;
  146. }
  147. uint32_t MethodInfo::get_compatibility_hash() const {
  148. bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  149. uint32_t hash = hash_murmur3_one_32(has_return);
  150. hash = hash_murmur3_one_32(arguments.size(), hash);
  151. if (has_return) {
  152. hash = hash_murmur3_one_32(return_val.type, hash);
  153. if (return_val.class_name != StringName()) {
  154. hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
  155. }
  156. }
  157. for (const PropertyInfo &arg : arguments) {
  158. hash = hash_murmur3_one_32(arg.type, hash);
  159. if (arg.class_name != StringName()) {
  160. hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
  161. }
  162. }
  163. hash = hash_murmur3_one_32(default_arguments.size(), hash);
  164. for (const Variant &v : default_arguments) {
  165. hash = hash_murmur3_one_32(v.hash(), hash);
  166. }
  167. hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
  168. hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);
  169. return hash_fmix32(hash);
  170. }
  171. Object::Connection::operator Variant() const {
  172. Dictionary d;
  173. d["signal"] = signal;
  174. d["callable"] = callable;
  175. d["flags"] = flags;
  176. return d;
  177. }
  178. bool Object::Connection::operator<(const Connection &p_conn) const {
  179. if (signal == p_conn.signal) {
  180. return callable < p_conn.callable;
  181. } else {
  182. return signal < p_conn.signal;
  183. }
  184. }
  185. Object::Connection::Connection(const Variant &p_variant) {
  186. Dictionary d = p_variant;
  187. if (d.has("signal")) {
  188. signal = d["signal"];
  189. }
  190. if (d.has("callable")) {
  191. callable = d["callable"];
  192. }
  193. if (d.has("flags")) {
  194. flags = d["flags"];
  195. }
  196. }
  197. bool Object::_predelete() {
  198. _predelete_ok = 1;
  199. notification(NOTIFICATION_PREDELETE, true);
  200. if (_predelete_ok) {
  201. _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
  202. notification(NOTIFICATION_PREDELETE_CLEANUP, true);
  203. }
  204. return _predelete_ok;
  205. }
  206. void Object::cancel_free() {
  207. _predelete_ok = false;
  208. }
  209. void Object::_initialize() {
  210. _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after _initialize.
  211. _initialize_classv();
  212. _class_name_ptr = nullptr; // May have been called from a constructor.
  213. }
  214. void Object::_postinitialize() {
  215. notification(NOTIFICATION_POSTINITIALIZE);
  216. }
  217. void Object::get_valid_parents_static(List<String> *p_parents) {
  218. }
  219. void Object::_get_valid_parents_static(List<String> *p_parents) {
  220. }
  221. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  222. #ifdef TOOLS_ENABLED
  223. _edited = true;
  224. #endif
  225. if (script_instance) {
  226. if (script_instance->set(p_name, p_value)) {
  227. if (r_valid) {
  228. *r_valid = true;
  229. }
  230. return;
  231. }
  232. }
  233. if (_extension && _extension->set) {
  234. if (_extension->set(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value)) {
  235. if (r_valid) {
  236. *r_valid = true;
  237. }
  238. return;
  239. }
  240. }
  241. // Try built-in setter.
  242. {
  243. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  244. return;
  245. }
  246. }
  247. if (p_name == CoreStringName(script)) {
  248. set_script(p_value);
  249. if (r_valid) {
  250. *r_valid = true;
  251. }
  252. return;
  253. } else {
  254. Variant **V = metadata_properties.getptr(p_name);
  255. if (V) {
  256. **V = p_value;
  257. if (r_valid) {
  258. *r_valid = true;
  259. }
  260. return;
  261. } else if (p_name.operator String().begins_with("metadata/")) {
  262. // Must exist, otherwise duplicate() will not work.
  263. set_meta(p_name.operator String().replace_first("metadata/", ""), p_value);
  264. if (r_valid) {
  265. *r_valid = true;
  266. }
  267. return;
  268. }
  269. }
  270. #ifdef TOOLS_ENABLED
  271. if (script_instance) {
  272. bool valid;
  273. script_instance->property_set_fallback(p_name, p_value, &valid);
  274. if (valid) {
  275. if (r_valid) {
  276. *r_valid = true;
  277. }
  278. return;
  279. }
  280. }
  281. #endif
  282. // Something inside the object... :|
  283. bool success = _setv(p_name, p_value);
  284. if (success) {
  285. if (r_valid) {
  286. *r_valid = true;
  287. }
  288. return;
  289. }
  290. if (r_valid) {
  291. *r_valid = false;
  292. }
  293. }
  294. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  295. Variant ret;
  296. if (script_instance) {
  297. if (script_instance->get(p_name, ret)) {
  298. if (r_valid) {
  299. *r_valid = true;
  300. }
  301. return ret;
  302. }
  303. }
  304. if (_extension && _extension->get) {
  305. if (_extension->get(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  306. if (r_valid) {
  307. *r_valid = true;
  308. }
  309. return ret;
  310. }
  311. }
  312. // Try built-in getter.
  313. {
  314. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  315. if (r_valid) {
  316. *r_valid = true;
  317. }
  318. return ret;
  319. }
  320. }
  321. if (p_name == CoreStringName(script)) {
  322. ret = get_script();
  323. if (r_valid) {
  324. *r_valid = true;
  325. }
  326. return ret;
  327. }
  328. const Variant *const *V = metadata_properties.getptr(p_name);
  329. if (V) {
  330. ret = **V;
  331. if (r_valid) {
  332. *r_valid = true;
  333. }
  334. return ret;
  335. } else {
  336. #ifdef TOOLS_ENABLED
  337. if (script_instance) {
  338. bool valid;
  339. ret = script_instance->property_get_fallback(p_name, &valid);
  340. if (valid) {
  341. if (r_valid) {
  342. *r_valid = true;
  343. }
  344. return ret;
  345. }
  346. }
  347. #endif
  348. // Something inside the object... :|
  349. bool success = _getv(p_name, ret);
  350. if (success) {
  351. if (r_valid) {
  352. *r_valid = true;
  353. }
  354. return ret;
  355. }
  356. if (r_valid) {
  357. *r_valid = false;
  358. }
  359. return Variant();
  360. }
  361. }
  362. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  363. if (p_names.is_empty()) {
  364. if (r_valid) {
  365. *r_valid = false;
  366. }
  367. return;
  368. }
  369. if (p_names.size() == 1) {
  370. set(p_names[0], p_value, r_valid);
  371. return;
  372. }
  373. bool valid = false;
  374. if (!r_valid) {
  375. r_valid = &valid;
  376. }
  377. List<Variant> value_stack;
  378. value_stack.push_back(get(p_names[0], r_valid));
  379. if (!*r_valid) {
  380. value_stack.clear();
  381. return;
  382. }
  383. for (int i = 1; i < p_names.size() - 1; i++) {
  384. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid));
  385. if (r_valid) {
  386. *r_valid = valid;
  387. }
  388. if (!valid) {
  389. value_stack.clear();
  390. return;
  391. }
  392. }
  393. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  394. for (int i = p_names.size() - 1; i > 0; i--) {
  395. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid);
  396. value_stack.pop_back();
  397. if (r_valid) {
  398. *r_valid = valid;
  399. }
  400. if (!valid) {
  401. value_stack.clear();
  402. return;
  403. }
  404. }
  405. set(p_names[0], value_stack.back()->get(), r_valid);
  406. value_stack.pop_back();
  407. ERR_FAIL_COND(!value_stack.is_empty());
  408. }
  409. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  410. if (p_names.is_empty()) {
  411. if (r_valid) {
  412. *r_valid = false;
  413. }
  414. return Variant();
  415. }
  416. bool valid = false;
  417. Variant current_value = get(p_names[0], &valid);
  418. for (int i = 1; i < p_names.size(); i++) {
  419. current_value = current_value.get_named(p_names[i], valid);
  420. if (!valid) {
  421. break;
  422. }
  423. }
  424. if (r_valid) {
  425. *r_valid = valid;
  426. }
  427. return current_value;
  428. }
  429. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  430. if (script_instance && p_reversed) {
  431. script_instance->get_property_list(p_list);
  432. }
  433. if (_extension) {
  434. const ObjectGDExtension *current_extension = _extension;
  435. while (current_extension) {
  436. p_list->push_back(PropertyInfo(Variant::NIL, current_extension->class_name, PROPERTY_HINT_NONE, current_extension->class_name, PROPERTY_USAGE_CATEGORY));
  437. ClassDB::get_property_list(current_extension->class_name, p_list, true, this);
  438. if (current_extension->get_property_list) {
  439. #ifdef TOOLS_ENABLED
  440. // If this is a placeholder, we can't call into the GDExtension on the parent class,
  441. // because we don't have a real instance of the class to give it.
  442. if (likely(!_extension->is_placeholder)) {
  443. #endif
  444. uint32_t pcount;
  445. const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
  446. for (uint32_t i = 0; i < pcount; i++) {
  447. p_list->push_back(PropertyInfo(pinfo[i]));
  448. }
  449. if (current_extension->free_property_list2) {
  450. current_extension->free_property_list2(_extension_instance, pinfo, pcount);
  451. }
  452. #ifndef DISABLE_DEPRECATED
  453. else if (current_extension->free_property_list) {
  454. current_extension->free_property_list(_extension_instance, pinfo);
  455. }
  456. #endif // DISABLE_DEPRECATED
  457. #ifdef TOOLS_ENABLED
  458. }
  459. #endif
  460. }
  461. current_extension = current_extension->parent;
  462. }
  463. }
  464. _get_property_listv(p_list, p_reversed);
  465. if (!is_class("Script")) { // can still be set, but this is for user-friendliness
  466. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NEVER_DUPLICATE));
  467. }
  468. if (script_instance && !p_reversed) {
  469. script_instance->get_property_list(p_list);
  470. }
  471. for (const KeyValue<StringName, Variant> &K : metadata) {
  472. PropertyInfo pi = PropertyInfo(K.value.get_type(), "metadata/" + K.key.operator String());
  473. if (K.value.get_type() == Variant::OBJECT) {
  474. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  475. Object *obj = K.value;
  476. if (Object::cast_to<Script>(obj)) {
  477. pi.hint_string = "Script";
  478. pi.usage |= PROPERTY_USAGE_NEVER_DUPLICATE;
  479. } else {
  480. pi.hint_string = "Resource";
  481. }
  482. }
  483. p_list->push_back(pi);
  484. }
  485. }
  486. void Object::validate_property(PropertyInfo &p_property) const {
  487. _validate_propertyv(p_property);
  488. if (_extension && _extension->validate_property) {
  489. // GDExtension uses a StringName rather than a String for property name.
  490. StringName prop_name = p_property.name;
  491. GDExtensionPropertyInfo gdext_prop = {
  492. (GDExtensionVariantType)p_property.type,
  493. &prop_name,
  494. &p_property.class_name,
  495. (uint32_t)p_property.hint,
  496. &p_property.hint_string,
  497. p_property.usage,
  498. };
  499. if (_extension->validate_property(_extension_instance, &gdext_prop)) {
  500. p_property.type = (Variant::Type)gdext_prop.type;
  501. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  502. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  503. p_property.hint = (PropertyHint)gdext_prop.hint;
  504. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  505. p_property.usage = gdext_prop.usage;
  506. };
  507. }
  508. if (script_instance) { // Call it last to allow user altering already validated properties.
  509. script_instance->validate_property(p_property);
  510. }
  511. }
  512. bool Object::property_can_revert(const StringName &p_name) const {
  513. if (script_instance) {
  514. if (script_instance->property_can_revert(p_name)) {
  515. return true;
  516. }
  517. }
  518. if (_extension && _extension->property_can_revert) {
  519. if (_extension->property_can_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name)) {
  520. return true;
  521. }
  522. }
  523. return _property_can_revertv(p_name);
  524. }
  525. Variant Object::property_get_revert(const StringName &p_name) const {
  526. Variant ret;
  527. if (script_instance) {
  528. if (script_instance->property_get_revert(p_name, ret)) {
  529. return ret;
  530. }
  531. }
  532. if (_extension && _extension->property_get_revert) {
  533. if (_extension->property_get_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  534. return ret;
  535. }
  536. }
  537. if (_property_get_revertv(p_name, ret)) {
  538. return ret;
  539. }
  540. return Variant();
  541. }
  542. void Object::get_method_list(List<MethodInfo> *p_list) const {
  543. ClassDB::get_method_list(get_class_name(), p_list);
  544. if (script_instance) {
  545. script_instance->get_method_list(p_list);
  546. }
  547. }
  548. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  549. if (p_argcount < 1) {
  550. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  551. r_error.expected = 1;
  552. return Variant();
  553. }
  554. if (!p_args[0]->is_string()) {
  555. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  556. r_error.argument = 0;
  557. r_error.expected = Variant::STRING_NAME;
  558. return Variant();
  559. }
  560. StringName method = *p_args[0];
  561. return callp(method, &p_args[1], p_argcount - 1, r_error);
  562. }
  563. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  564. if (p_argcount < 1) {
  565. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  566. r_error.expected = 1;
  567. return Variant();
  568. }
  569. if (!p_args[0]->is_string()) {
  570. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  571. r_error.argument = 0;
  572. r_error.expected = Variant::STRING_NAME;
  573. return Variant();
  574. }
  575. r_error.error = Callable::CallError::CALL_OK;
  576. StringName method = *p_args[0];
  577. MessageQueue::get_singleton()->push_callp(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  578. return Variant();
  579. }
  580. bool Object::has_method(const StringName &p_method) const {
  581. if (p_method == CoreStringName(free_)) {
  582. return true;
  583. }
  584. if (script_instance && script_instance->has_method(p_method)) {
  585. return true;
  586. }
  587. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  588. if (method != nullptr) {
  589. return true;
  590. }
  591. const Script *scr = Object::cast_to<Script>(this);
  592. if (scr != nullptr) {
  593. return scr->has_static_method(p_method);
  594. }
  595. return false;
  596. }
  597. int Object::_get_method_argument_count_bind(const StringName &p_method) const {
  598. return get_method_argument_count(p_method);
  599. }
  600. int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
  601. if (p_method == CoreStringName(free_)) {
  602. if (r_is_valid) {
  603. *r_is_valid = true;
  604. }
  605. return 0;
  606. }
  607. if (script_instance) {
  608. bool valid = false;
  609. int ret = script_instance->get_method_argument_count(p_method, &valid);
  610. if (valid) {
  611. if (r_is_valid) {
  612. *r_is_valid = true;
  613. }
  614. return ret;
  615. }
  616. }
  617. {
  618. bool valid = false;
  619. int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid);
  620. if (valid) {
  621. if (r_is_valid) {
  622. *r_is_valid = true;
  623. }
  624. return ret;
  625. }
  626. }
  627. const Script *scr = Object::cast_to<Script>(this);
  628. while (scr != nullptr) {
  629. bool valid = false;
  630. int ret = scr->get_script_method_argument_count(p_method, &valid);
  631. if (valid) {
  632. if (r_is_valid) {
  633. *r_is_valid = true;
  634. }
  635. return ret;
  636. }
  637. scr = scr->get_base_script().ptr();
  638. }
  639. if (r_is_valid) {
  640. *r_is_valid = false;
  641. }
  642. return 0;
  643. }
  644. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  645. if (r_valid) {
  646. *r_valid = false;
  647. }
  648. if (p_key.is_string()) {
  649. return get(p_key, r_valid);
  650. }
  651. return Variant();
  652. }
  653. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  654. if (r_valid) {
  655. *r_valid = false;
  656. }
  657. if (p_key.is_string()) {
  658. return set(p_key, p_value, r_valid);
  659. }
  660. }
  661. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  662. const Variant **argptrs = nullptr;
  663. if (p_args.size() > 0) {
  664. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  665. for (int i = 0; i < p_args.size(); i++) {
  666. argptrs[i] = &p_args[i];
  667. }
  668. }
  669. Callable::CallError ce;
  670. const Variant ret = callp(p_method, argptrs, p_args.size(), ce);
  671. if (ce.error != Callable::CallError::CALL_OK) {
  672. ERR_FAIL_V_MSG(Variant(), vformat("Error calling method from 'callv': %s.", Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce)));
  673. }
  674. return ret;
  675. }
  676. Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  677. r_error.error = Callable::CallError::CALL_OK;
  678. if (p_method == CoreStringName(free_)) {
  679. //free must be here, before anything, always ready
  680. #ifdef DEBUG_ENABLED
  681. if (p_argcount != 0) {
  682. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  683. r_error.expected = 0;
  684. return Variant();
  685. }
  686. if (is_ref_counted()) {
  687. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  688. ERR_FAIL_V_MSG(Variant(), "Can't free a RefCounted object.");
  689. }
  690. if (_lock_index.get() > 1) {
  691. r_error.argument = 0;
  692. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  693. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  694. }
  695. #endif
  696. //must be here, must be before everything,
  697. memdelete(this);
  698. r_error.error = Callable::CallError::CALL_OK;
  699. return Variant();
  700. }
  701. Variant ret;
  702. OBJ_DEBUG_LOCK
  703. if (script_instance) {
  704. ret = script_instance->callp(p_method, p_args, p_argcount, r_error);
  705. // Force jump table.
  706. switch (r_error.error) {
  707. case Callable::CallError::CALL_OK:
  708. return ret;
  709. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  710. break;
  711. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  712. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  713. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  714. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  715. return ret;
  716. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  717. }
  718. }
  719. }
  720. //extension does not need this, because all methods are registered in MethodBind
  721. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  722. if (method) {
  723. ret = method->call(this, p_args, p_argcount, r_error);
  724. } else {
  725. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  726. }
  727. return ret;
  728. }
  729. Variant Object::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  730. r_error.error = Callable::CallError::CALL_OK;
  731. if (p_method == CoreStringName(free_)) {
  732. // Free is not const, so fail.
  733. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  734. return Variant();
  735. }
  736. Variant ret;
  737. OBJ_DEBUG_LOCK
  738. if (script_instance) {
  739. ret = script_instance->call_const(p_method, p_args, p_argcount, r_error);
  740. //force jumptable
  741. switch (r_error.error) {
  742. case Callable::CallError::CALL_OK:
  743. return ret;
  744. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  745. break;
  746. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  747. break;
  748. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  749. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  750. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  751. return ret;
  752. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  753. }
  754. }
  755. }
  756. //extension does not need this, because all methods are registered in MethodBind
  757. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  758. if (method) {
  759. if (!method->is_const()) {
  760. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  761. return ret;
  762. }
  763. ret = method->call(this, p_args, p_argcount, r_error);
  764. } else {
  765. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  766. }
  767. return ret;
  768. }
  769. void Object::notification(int p_notification, bool p_reversed) {
  770. if (p_reversed) {
  771. if (script_instance) {
  772. script_instance->notification(p_notification, p_reversed);
  773. }
  774. } else {
  775. _notificationv(p_notification, p_reversed);
  776. }
  777. if (_extension) {
  778. if (_extension->notification2) {
  779. _extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(p_reversed));
  780. #ifndef DISABLE_DEPRECATED
  781. } else if (_extension->notification) {
  782. _extension->notification(_extension_instance, p_notification);
  783. #endif // DISABLE_DEPRECATED
  784. }
  785. }
  786. if (p_reversed) {
  787. _notificationv(p_notification, p_reversed);
  788. } else {
  789. if (script_instance) {
  790. script_instance->notification(p_notification, p_reversed);
  791. }
  792. }
  793. }
  794. String Object::to_string() {
  795. // Keep this method in sync with `Node::to_string`.
  796. if (script_instance) {
  797. bool valid;
  798. String ret = script_instance->to_string(&valid);
  799. if (valid) {
  800. return ret;
  801. }
  802. }
  803. if (_extension && _extension->to_string) {
  804. String ret;
  805. GDExtensionBool is_valid;
  806. _extension->to_string(_extension_instance, &is_valid, &ret);
  807. return ret;
  808. }
  809. return "<" + get_class() + "#" + itos(get_instance_id()) + ">";
  810. }
  811. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  812. //this function is not meant to be used in any of these ways
  813. ERR_FAIL_COND(p_script.is_null());
  814. ERR_FAIL_NULL(p_instance);
  815. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  816. script = p_script;
  817. script_instance = p_instance;
  818. }
  819. void Object::set_script(const Variant &p_script) {
  820. if (script == p_script) {
  821. return;
  822. }
  823. Ref<Script> s = p_script;
  824. if (!p_script.is_null()) {
  825. ERR_FAIL_COND_MSG(s.is_null(), "Cannot set object script. Parameter should be null or a reference to a valid script.");
  826. ERR_FAIL_COND_MSG(s->is_abstract(), vformat("Cannot set object script. Script '%s' should not be abstract.", s->get_path()));
  827. }
  828. script = p_script;
  829. if (script_instance) {
  830. memdelete(script_instance);
  831. script_instance = nullptr;
  832. }
  833. if (s.is_valid()) {
  834. if (s->can_instantiate()) {
  835. OBJ_DEBUG_LOCK
  836. script_instance = s->instance_create(this);
  837. } else if (Engine::get_singleton()->is_editor_hint()) {
  838. OBJ_DEBUG_LOCK
  839. script_instance = s->placeholder_instance_create(this);
  840. }
  841. }
  842. notify_property_list_changed(); //scripts may add variables, so refresh is desired
  843. emit_signal(CoreStringName(script_changed));
  844. }
  845. void Object::set_script_instance(ScriptInstance *p_instance) {
  846. if (script_instance == p_instance) {
  847. return;
  848. }
  849. if (script_instance) {
  850. memdelete(script_instance);
  851. }
  852. script_instance = p_instance;
  853. if (p_instance) {
  854. script = p_instance->get_script();
  855. } else {
  856. script = Variant();
  857. }
  858. }
  859. Variant Object::get_script() const {
  860. return script;
  861. }
  862. bool Object::has_meta(const StringName &p_name) const {
  863. return metadata.has(p_name);
  864. }
  865. void Object::set_meta(const StringName &p_name, const Variant &p_value) {
  866. if (p_value.get_type() == Variant::NIL) {
  867. if (metadata.has(p_name)) {
  868. metadata.erase(p_name);
  869. const String &sname = p_name;
  870. metadata_properties.erase("metadata/" + sname);
  871. if (!sname.begins_with("_")) {
  872. // Metadata starting with _ don't show up in the inspector, so no need to update.
  873. notify_property_list_changed();
  874. }
  875. }
  876. return;
  877. }
  878. HashMap<StringName, Variant>::Iterator E = metadata.find(p_name);
  879. if (E) {
  880. E->value = p_value;
  881. } else {
  882. ERR_FAIL_COND_MSG(!p_name.operator String().is_valid_ascii_identifier(), vformat("Invalid metadata identifier: '%s'.", p_name));
  883. Variant *V = &metadata.insert(p_name, p_value)->value;
  884. const String &sname = p_name;
  885. metadata_properties["metadata/" + sname] = V;
  886. if (!sname.begins_with("_")) {
  887. notify_property_list_changed();
  888. }
  889. }
  890. }
  891. Variant Object::get_meta(const StringName &p_name, const Variant &p_default) const {
  892. if (!metadata.has(p_name)) {
  893. if (p_default != Variant()) {
  894. return p_default;
  895. } else {
  896. ERR_FAIL_V_MSG(Variant(), vformat("The object does not have any 'meta' values with the key '%s'.", p_name));
  897. }
  898. }
  899. return metadata[p_name];
  900. }
  901. void Object::remove_meta(const StringName &p_name) {
  902. set_meta(p_name, Variant());
  903. }
  904. void Object::merge_meta_from(const Object *p_src) {
  905. List<StringName> meta_keys;
  906. p_src->get_meta_list(&meta_keys);
  907. for (const StringName &key : meta_keys) {
  908. set_meta(key, p_src->get_meta(key));
  909. }
  910. }
  911. TypedArray<Dictionary> Object::_get_property_list_bind() const {
  912. List<PropertyInfo> lpi;
  913. get_property_list(&lpi);
  914. return convert_property_list(&lpi);
  915. }
  916. TypedArray<Dictionary> Object::_get_method_list_bind() const {
  917. List<MethodInfo> ml;
  918. get_method_list(&ml);
  919. TypedArray<Dictionary> ret;
  920. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  921. Dictionary d = E->get();
  922. //va.push_back(d);
  923. ret.push_back(d);
  924. }
  925. return ret;
  926. }
  927. TypedArray<StringName> Object::_get_meta_list_bind() const {
  928. TypedArray<StringName> _metaret;
  929. for (const KeyValue<StringName, Variant> &K : metadata) {
  930. _metaret.push_back(K.key);
  931. }
  932. return _metaret;
  933. }
  934. void Object::get_meta_list(List<StringName> *p_list) const {
  935. for (const KeyValue<StringName, Variant> &K : metadata) {
  936. p_list->push_back(K.key);
  937. }
  938. }
  939. void Object::add_user_signal(const MethodInfo &p_signal) {
  940. ERR_FAIL_COND_MSG(p_signal.name.is_empty(), "Signal name cannot be empty.");
  941. ERR_FAIL_COND_MSG(ClassDB::has_signal(get_class_name(), p_signal.name), vformat("User signal's name conflicts with a built-in signal of '%s'.", get_class_name()));
  942. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), vformat("Trying to add already existing signal '%s'.", p_signal.name));
  943. SignalData s;
  944. s.user = p_signal;
  945. signal_map[p_signal.name] = s;
  946. }
  947. bool Object::_has_user_signal(const StringName &p_name) const {
  948. if (!signal_map.has(p_name)) {
  949. return false;
  950. }
  951. return signal_map[p_name].user.name.length() > 0;
  952. }
  953. void Object::_remove_user_signal(const StringName &p_name) {
  954. SignalData *s = signal_map.getptr(p_name);
  955. ERR_FAIL_NULL_MSG(s, "Provided signal does not exist.");
  956. ERR_FAIL_COND_MSG(!s->removable, "Signal is not removable (not added with add_user_signal).");
  957. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  958. Object *target = slot_kv.key.get_object();
  959. if (likely(target)) {
  960. target->connections.erase(slot_kv.value.cE);
  961. }
  962. }
  963. signal_map.erase(p_name);
  964. }
  965. Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  966. if (unlikely(p_argcount < 1)) {
  967. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  968. r_error.expected = 1;
  969. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  970. }
  971. if (unlikely(!p_args[0]->is_string())) {
  972. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  973. r_error.argument = 0;
  974. r_error.expected = Variant::STRING_NAME;
  975. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  976. }
  977. r_error.error = Callable::CallError::CALL_OK;
  978. StringName signal = *p_args[0];
  979. const Variant **args = nullptr;
  980. int argc = p_argcount - 1;
  981. if (argc) {
  982. args = &p_args[1];
  983. }
  984. return emit_signalp(signal, args, argc);
  985. }
  986. Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
  987. if (_block_signals) {
  988. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  989. }
  990. SignalData *s = signal_map.getptr(p_name);
  991. if (!s) {
  992. #ifdef DEBUG_ENABLED
  993. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  994. //check in script
  995. ERR_FAIL_COND_V_MSG(!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name), ERR_UNAVAILABLE, vformat("Can't emit non-existing signal \"%s\".", p_name));
  996. #endif
  997. //not connected? just return
  998. return ERR_UNAVAILABLE;
  999. }
  1000. // If this is a ref-counted object, prevent it from being destroyed during signal emission,
  1001. // which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
  1002. Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));
  1003. // Ensure that disconnecting the signal or even deleting the object
  1004. // will not affect the signal calling.
  1005. Callable *slot_callables = (Callable *)alloca(sizeof(Callable) * s->slot_map.size());
  1006. uint32_t *slot_flags = (uint32_t *)alloca(sizeof(uint32_t) * s->slot_map.size());
  1007. uint32_t slot_count = 0;
  1008. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1009. memnew_placement(&slot_callables[slot_count], Callable(slot_kv.value.conn.callable));
  1010. slot_flags[slot_count] = slot_kv.value.conn.flags;
  1011. ++slot_count;
  1012. }
  1013. DEV_ASSERT(slot_count == s->slot_map.size());
  1014. // Disconnect all one-shot connections before emitting to prevent recursion.
  1015. for (uint32_t i = 0; i < slot_count; ++i) {
  1016. bool disconnect = slot_flags[i] & CONNECT_ONE_SHOT;
  1017. #ifdef TOOLS_ENABLED
  1018. if (disconnect && (slot_flags[i] & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  1019. // This signal was connected from the editor, and is being edited. Just don't disconnect for now.
  1020. disconnect = false;
  1021. }
  1022. #endif
  1023. if (disconnect) {
  1024. _disconnect(p_name, slot_callables[i]);
  1025. }
  1026. }
  1027. OBJ_DEBUG_LOCK
  1028. Error err = OK;
  1029. for (uint32_t i = 0; i < slot_count; ++i) {
  1030. const Callable &callable = slot_callables[i];
  1031. const uint32_t &flags = slot_flags[i];
  1032. if (!callable.is_valid()) {
  1033. // Target might have been deleted during signal callback, this is expected and OK.
  1034. continue;
  1035. }
  1036. const Variant **args = p_args;
  1037. int argc = p_argcount;
  1038. if (flags & CONNECT_DEFERRED) {
  1039. MessageQueue::get_singleton()->push_callablep(callable, args, argc, true);
  1040. } else {
  1041. Callable::CallError ce;
  1042. _emitting = true;
  1043. Variant ret;
  1044. callable.callp(args, argc, ret, ce);
  1045. _emitting = false;
  1046. if (ce.error != Callable::CallError::CALL_OK) {
  1047. #ifdef DEBUG_ENABLED
  1048. if (flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  1049. continue;
  1050. }
  1051. #endif
  1052. Object *target = callable.get_object();
  1053. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && target && !ClassDB::class_exists(target->get_class_name())) {
  1054. //most likely object is not initialized yet, do not throw error.
  1055. } else {
  1056. ERR_PRINT(vformat("Error calling from signal '%s' to callable: %s.", String(p_name), Variant::get_callable_error_text(callable, args, argc, ce)));
  1057. err = ERR_METHOD_NOT_FOUND;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. for (uint32_t i = 0; i < slot_count; ++i) {
  1063. slot_callables[i].~Callable();
  1064. }
  1065. return err;
  1066. }
  1067. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  1068. // this version of add_user_signal is meant to be used from scripts or external apis
  1069. // without access to ADD_SIGNAL in bind_methods
  1070. // added events are per instance, as opposed to the other ones, which are global
  1071. MethodInfo mi;
  1072. mi.name = p_name;
  1073. for (const Variant &arg : p_args) {
  1074. Dictionary d = arg;
  1075. PropertyInfo param;
  1076. if (d.has("name")) {
  1077. param.name = d["name"];
  1078. }
  1079. if (d.has("type")) {
  1080. param.type = (Variant::Type)(int)d["type"];
  1081. }
  1082. mi.arguments.push_back(param);
  1083. }
  1084. add_user_signal(mi);
  1085. if (signal_map.has(p_name)) {
  1086. signal_map.getptr(p_name)->removable = true;
  1087. }
  1088. }
  1089. TypedArray<Dictionary> Object::_get_signal_list() const {
  1090. List<MethodInfo> signal_list;
  1091. get_signal_list(&signal_list);
  1092. TypedArray<Dictionary> ret;
  1093. for (const MethodInfo &E : signal_list) {
  1094. ret.push_back(Dictionary(E));
  1095. }
  1096. return ret;
  1097. }
  1098. TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const {
  1099. List<Connection> conns;
  1100. get_all_signal_connections(&conns);
  1101. TypedArray<Dictionary> ret;
  1102. for (const Connection &c : conns) {
  1103. if (c.signal.get_name() == p_signal) {
  1104. ret.push_back(c);
  1105. }
  1106. }
  1107. return ret;
  1108. }
  1109. TypedArray<Dictionary> Object::_get_incoming_connections() const {
  1110. TypedArray<Dictionary> ret;
  1111. for (const Object::Connection &connection : connections) {
  1112. ret.push_back(connection);
  1113. }
  1114. return ret;
  1115. }
  1116. bool Object::has_signal(const StringName &p_name) const {
  1117. if (!script.is_null()) {
  1118. Ref<Script> scr = script;
  1119. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1120. return true;
  1121. }
  1122. }
  1123. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1124. return true;
  1125. }
  1126. if (_has_user_signal(p_name)) {
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1132. if (!script.is_null()) {
  1133. Ref<Script> scr = script;
  1134. if (scr.is_valid()) {
  1135. scr->get_script_signal_list(p_signals);
  1136. }
  1137. }
  1138. ClassDB::get_signal_list(get_class_name(), p_signals);
  1139. //find maybe usersignals?
  1140. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1141. if (!E.value.user.name.is_empty()) {
  1142. //user signal
  1143. p_signals->push_back(E.value.user);
  1144. }
  1145. }
  1146. }
  1147. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1148. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1149. const SignalData *s = &E.value;
  1150. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1151. p_connections->push_back(slot_kv.value.conn);
  1152. }
  1153. }
  1154. }
  1155. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1156. const SignalData *s = signal_map.getptr(p_signal);
  1157. if (!s) {
  1158. return; //nothing
  1159. }
  1160. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1161. p_connections->push_back(slot_kv.value.conn);
  1162. }
  1163. }
  1164. int Object::get_persistent_signal_connection_count() const {
  1165. int count = 0;
  1166. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1167. const SignalData *s = &E.value;
  1168. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1169. if (slot_kv.value.conn.flags & CONNECT_PERSIST) {
  1170. count += 1;
  1171. }
  1172. }
  1173. }
  1174. return count;
  1175. }
  1176. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1177. for (const Connection &E : connections) {
  1178. p_connections->push_back(E);
  1179. }
  1180. }
  1181. Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  1182. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is null.", p_signal));
  1183. if (p_callable.is_standard()) {
  1184. // FIXME: This branch should probably removed in favor of the `is_valid()` branch, but there exist some classes
  1185. // that call `connect()` before they are fully registered with ClassDB. Until all such classes can be found
  1186. // and registered soon enough this branch is needed to allow `connect()` to succeed.
  1187. ERR_FAIL_NULL_V_MSG(p_callable.get_object(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s' to callable '%s': the callable object is null.", p_signal, p_callable));
  1188. } else {
  1189. ERR_FAIL_COND_V_MSG(!p_callable.is_valid(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is not valid: '%s'.", p_signal, p_callable));
  1190. }
  1191. SignalData *s = signal_map.getptr(p_signal);
  1192. if (!s) {
  1193. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1194. //check in script
  1195. if (!signal_is_valid && !script.is_null()) {
  1196. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1197. signal_is_valid = true;
  1198. }
  1199. #ifdef TOOLS_ENABLED
  1200. else {
  1201. //allow connecting signals anyway if script is invalid, see issue #17070
  1202. if (!Ref<Script>(script)->is_valid()) {
  1203. signal_is_valid = true;
  1204. }
  1205. }
  1206. #endif
  1207. }
  1208. ERR_FAIL_COND_V_MSG(!signal_is_valid, ERR_INVALID_PARAMETER, vformat("In Object of type '%s': Attempt to connect nonexistent signal '%s' to callable '%s'.", String(get_class()), p_signal, p_callable));
  1209. signal_map[p_signal] = SignalData();
  1210. s = &signal_map[p_signal];
  1211. }
  1212. //compare with the base callable, so binds can be ignored
  1213. if (s->slot_map.has(*p_callable.get_base_comparator())) {
  1214. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1215. s->slot_map[*p_callable.get_base_comparator()].reference_count++;
  1216. return OK;
  1217. } else {
  1218. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Signal '%s' is already connected to given callable '%s' in that object.", p_signal, p_callable));
  1219. }
  1220. }
  1221. Object *target_object = p_callable.get_object();
  1222. SignalData::Slot slot;
  1223. Connection conn;
  1224. conn.callable = p_callable;
  1225. conn.signal = ::Signal(this, p_signal);
  1226. conn.flags = p_flags;
  1227. slot.conn = conn;
  1228. if (target_object) {
  1229. slot.cE = target_object->connections.push_back(conn);
  1230. }
  1231. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1232. slot.reference_count = 1;
  1233. }
  1234. //use callable version as key, so binds can be ignored
  1235. s->slot_map[*p_callable.get_base_comparator()] = slot;
  1236. return OK;
  1237. }
  1238. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1239. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot determine if connected to '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1240. const SignalData *s = signal_map.getptr(p_signal);
  1241. if (!s) {
  1242. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1243. if (signal_is_valid) {
  1244. return false;
  1245. }
  1246. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1247. return false;
  1248. }
  1249. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1250. }
  1251. return s->slot_map.has(*p_callable.get_base_comparator());
  1252. }
  1253. bool Object::has_connections(const StringName &p_signal) const {
  1254. const SignalData *s = signal_map.getptr(p_signal);
  1255. if (!s) {
  1256. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1257. if (signal_is_valid) {
  1258. return false;
  1259. }
  1260. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1261. return false;
  1262. }
  1263. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1264. }
  1265. return !s->slot_map.is_empty();
  1266. }
  1267. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1268. _disconnect(p_signal, p_callable);
  1269. }
  1270. bool Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1271. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot disconnect from '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1272. SignalData *s = signal_map.getptr(p_signal);
  1273. if (!s) {
  1274. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1275. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1276. ERR_FAIL_COND_V_MSG(signal_is_valid, false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1277. }
  1278. ERR_FAIL_NULL_V_MSG(s, false, vformat("Disconnecting nonexistent signal '%s' in '%s'.", p_signal, to_string()));
  1279. ERR_FAIL_COND_V_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1280. SignalData::Slot *slot = &s->slot_map[*p_callable.get_base_comparator()];
  1281. if (!p_force) {
  1282. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1283. if (slot->reference_count > 0) {
  1284. return false;
  1285. }
  1286. }
  1287. if (slot->cE) {
  1288. Object *target_object = p_callable.get_object();
  1289. if (target_object) {
  1290. target_object->connections.erase(slot->cE);
  1291. }
  1292. }
  1293. s->slot_map.erase(*p_callable.get_base_comparator());
  1294. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1295. //not user signal, delete
  1296. signal_map.erase(p_signal);
  1297. }
  1298. return true;
  1299. }
  1300. void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
  1301. set(p_set, p_value);
  1302. }
  1303. Variant Object::_get_bind(const StringName &p_name) const {
  1304. return get(p_name);
  1305. }
  1306. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1307. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1308. }
  1309. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1310. return get_indexed(p_name.get_as_property_path().get_subnames());
  1311. }
  1312. void Object::initialize_class() {
  1313. static bool initialized = false;
  1314. if (initialized) {
  1315. return;
  1316. }
  1317. ClassDB::_add_class<Object>();
  1318. _bind_methods();
  1319. _bind_compatibility_methods();
  1320. initialized = true;
  1321. }
  1322. StringName Object::get_translation_domain() const {
  1323. return _translation_domain;
  1324. }
  1325. void Object::set_translation_domain(const StringName &p_domain) {
  1326. _translation_domain = p_domain;
  1327. }
  1328. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1329. if (!_can_translate || !TranslationServer::get_singleton()) {
  1330. return p_message;
  1331. }
  1332. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1333. return domain->translate(p_message, p_context);
  1334. }
  1335. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1336. if (!_can_translate || !TranslationServer::get_singleton()) {
  1337. // Return message based on English plural rule if translation is not possible.
  1338. if (p_n == 1) {
  1339. return p_message;
  1340. }
  1341. return p_message_plural;
  1342. }
  1343. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1344. return domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  1345. }
  1346. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1347. switch (p_var.get_type()) {
  1348. case Variant::OBJECT: {
  1349. Ref<Resource> r = p_var;
  1350. if (r.is_null()) {
  1351. return;
  1352. }
  1353. if (!r->is_built_in()) {
  1354. return; //not an internal resource
  1355. }
  1356. Object *object = p_var;
  1357. if (!object) {
  1358. return;
  1359. }
  1360. r->set_path("");
  1361. r->clear_internal_resource_paths();
  1362. } break;
  1363. case Variant::ARRAY: {
  1364. Array a = p_var;
  1365. for (const Variant &var : a) {
  1366. _clear_internal_resource_paths(var);
  1367. }
  1368. } break;
  1369. case Variant::DICTIONARY: {
  1370. Dictionary d = p_var;
  1371. List<Variant> keys;
  1372. d.get_key_list(&keys);
  1373. for (const Variant &E : keys) {
  1374. _clear_internal_resource_paths(E);
  1375. _clear_internal_resource_paths(d[E]);
  1376. }
  1377. } break;
  1378. default: {
  1379. }
  1380. }
  1381. }
  1382. #ifdef TOOLS_ENABLED
  1383. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded, bool p_initializing) {
  1384. if (!p_initializing) {
  1385. set_edited(true);
  1386. }
  1387. if (p_unfolded) {
  1388. editor_section_folding.insert(p_section);
  1389. } else {
  1390. editor_section_folding.erase(p_section);
  1391. }
  1392. }
  1393. bool Object::editor_is_section_unfolded(const String &p_section) {
  1394. return editor_section_folding.has(p_section);
  1395. }
  1396. #endif
  1397. void Object::clear_internal_resource_paths() {
  1398. List<PropertyInfo> pinfo;
  1399. get_property_list(&pinfo);
  1400. for (const PropertyInfo &E : pinfo) {
  1401. _clear_internal_resource_paths(get(E.name));
  1402. }
  1403. }
  1404. void Object::notify_property_list_changed() {
  1405. emit_signal(CoreStringName(property_list_changed));
  1406. }
  1407. void Object::_bind_methods() {
  1408. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1409. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1410. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1411. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1412. ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind);
  1413. ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind);
  1414. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1415. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1416. ClassDB::bind_method(D_METHOD("property_can_revert", "property"), &Object::property_can_revert);
  1417. ClassDB::bind_method(D_METHOD("property_get_revert", "property"), &Object::property_get_revert);
  1418. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1419. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1420. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1421. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1422. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1423. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1424. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1425. ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant()));
  1426. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1427. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1428. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1429. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1430. ClassDB::bind_method(D_METHOD("remove_user_signal", "signal"), &Object::_remove_user_signal);
  1431. {
  1432. MethodInfo mi;
  1433. mi.name = "emit_signal";
  1434. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1435. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1436. }
  1437. {
  1438. MethodInfo mi;
  1439. mi.name = "call";
  1440. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1441. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1442. }
  1443. {
  1444. MethodInfo mi;
  1445. mi.name = "call_deferred";
  1446. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1447. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1448. }
  1449. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1450. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1451. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1452. ClassDB::bind_method(D_METHOD("get_method_argument_count", "method"), &Object::_get_method_argument_count_bind);
  1453. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1454. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1455. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1456. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1457. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
  1458. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1459. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1460. ClassDB::bind_method(D_METHOD("has_connections", "signal"), &Object::has_connections);
  1461. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1462. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1463. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1464. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1465. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1466. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
  1467. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
  1468. ClassDB::bind_method(D_METHOD("get_translation_domain"), &Object::get_translation_domain);
  1469. ClassDB::bind_method(D_METHOD("set_translation_domain", "domain"), &Object::set_translation_domain);
  1470. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1471. ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);
  1472. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1473. ADD_SIGNAL(MethodInfo("script_changed"));
  1474. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1475. #define BIND_OBJ_CORE_METHOD(m_method) \
  1476. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1477. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1478. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1479. {
  1480. MethodInfo mi("_notification");
  1481. mi.arguments.push_back(PropertyInfo(Variant::INT, "what"));
  1482. mi.arguments_metadata.push_back(GodotTypeInfo::Metadata::METADATA_INT_IS_INT32);
  1483. BIND_OBJ_CORE_METHOD(mi);
  1484. }
  1485. {
  1486. MethodInfo mi("_set");
  1487. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1488. mi.arguments.push_back(PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1489. mi.return_val.type = Variant::BOOL;
  1490. BIND_OBJ_CORE_METHOD(mi);
  1491. }
  1492. #ifdef TOOLS_ENABLED
  1493. {
  1494. MethodInfo mi("_get");
  1495. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1496. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1497. BIND_OBJ_CORE_METHOD(mi);
  1498. }
  1499. {
  1500. MethodInfo mi("_get_property_list");
  1501. mi.return_val.type = Variant::ARRAY;
  1502. mi.return_val.hint = PROPERTY_HINT_ARRAY_TYPE;
  1503. mi.return_val.hint_string = "Dictionary";
  1504. BIND_OBJ_CORE_METHOD(mi);
  1505. }
  1506. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::NIL, "_validate_property", PropertyInfo(Variant::DICTIONARY, "property")));
  1507. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property")));
  1508. {
  1509. MethodInfo mi("_property_get_revert");
  1510. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1511. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1512. BIND_OBJ_CORE_METHOD(mi);
  1513. }
  1514. // These are actually `Variant` methods, but that doesn't matter since scripts can't inherit built-in types.
  1515. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_init", PropertyInfo(Variant::ARRAY, "iter")));
  1516. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_next", PropertyInfo(Variant::ARRAY, "iter")));
  1517. {
  1518. MethodInfo mi("_iter_get");
  1519. mi.arguments.push_back(PropertyInfo(Variant::NIL, "iter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1520. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1521. BIND_OBJ_CORE_METHOD(mi);
  1522. }
  1523. #endif
  1524. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1525. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1526. BIND_CONSTANT(NOTIFICATION_EXTENSION_RELOADED);
  1527. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1528. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1529. BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT);
  1530. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1531. }
  1532. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1533. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1534. }
  1535. void Object::set_block_signals(bool p_block) {
  1536. _block_signals = p_block;
  1537. }
  1538. bool Object::is_blocking_signals() const {
  1539. return _block_signals;
  1540. }
  1541. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1542. bool valid;
  1543. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1544. if (valid) {
  1545. if (r_valid) {
  1546. *r_valid = true;
  1547. }
  1548. return t;
  1549. }
  1550. if (get_script_instance()) {
  1551. return get_script_instance()->get_property_type(p_property, r_valid);
  1552. }
  1553. if (r_valid) {
  1554. *r_valid = false;
  1555. }
  1556. return Variant::NIL;
  1557. }
  1558. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1559. if (p_path.size() == 0) {
  1560. if (r_valid) {
  1561. *r_valid = false;
  1562. }
  1563. return Variant::NIL;
  1564. }
  1565. bool valid = false;
  1566. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1567. if (!valid) {
  1568. if (r_valid) {
  1569. *r_valid = false;
  1570. }
  1571. return Variant::NIL;
  1572. }
  1573. Callable::CallError ce;
  1574. Variant check;
  1575. Variant::construct(t, check, nullptr, 0, ce);
  1576. for (int i = 1; i < p_path.size(); i++) {
  1577. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1578. // We cannot be sure about the type of properties this type can have
  1579. if (r_valid) {
  1580. *r_valid = false;
  1581. }
  1582. return Variant::NIL;
  1583. }
  1584. check = check.get_named(p_path[i], valid);
  1585. if (!valid) {
  1586. if (r_valid) {
  1587. *r_valid = false;
  1588. }
  1589. return Variant::NIL;
  1590. }
  1591. }
  1592. if (r_valid) {
  1593. *r_valid = true;
  1594. }
  1595. return check.get_type();
  1596. }
  1597. bool Object::is_queued_for_deletion() const {
  1598. return _is_queued_for_deletion;
  1599. }
  1600. #ifdef TOOLS_ENABLED
  1601. void Object::set_edited(bool p_edited) {
  1602. _edited = p_edited;
  1603. _edited_version++;
  1604. }
  1605. bool Object::is_edited() const {
  1606. return _edited;
  1607. }
  1608. uint32_t Object::get_edited_version() const {
  1609. return _edited_version;
  1610. }
  1611. #endif
  1612. StringName Object::get_class_name_for_extension(const GDExtension *p_library) const {
  1613. #ifdef TOOLS_ENABLED
  1614. // If this is the library this extension comes from and it's a placeholder, we
  1615. // have to return the closest native parent's class name, so that it doesn't try to
  1616. // use this like the real object.
  1617. if (unlikely(_extension && _extension->library == p_library && _extension->is_placeholder)) {
  1618. const StringName *class_name = _get_class_namev();
  1619. return *class_name;
  1620. }
  1621. #endif
  1622. // Only return the class name per the extension if it matches the given p_library.
  1623. if (_extension && _extension->library == p_library) {
  1624. return _extension->class_name;
  1625. }
  1626. // Extensions only have wrapper classes for classes exposed in ClassDB.
  1627. const StringName *class_name = _get_class_namev();
  1628. if (ClassDB::is_class_exposed(*class_name)) {
  1629. return *class_name;
  1630. }
  1631. // Find the nearest parent class that's exposed.
  1632. StringName parent_class = ClassDB::get_parent_class(*class_name);
  1633. while (parent_class != StringName()) {
  1634. if (ClassDB::is_class_exposed(parent_class)) {
  1635. return parent_class;
  1636. }
  1637. parent_class = ClassDB::get_parent_class(parent_class);
  1638. }
  1639. return SNAME("Object");
  1640. }
  1641. void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1642. // This is only meant to be used on creation by the binder, but we also
  1643. // need to account for reloading (where the 'binding' will be cleared).
  1644. ERR_FAIL_COND(_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr);
  1645. if (_instance_bindings == nullptr) {
  1646. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1647. _instance_binding_count = 1;
  1648. }
  1649. _instance_bindings[0].binding = p_binding;
  1650. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1651. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1652. _instance_bindings[0].token = p_token;
  1653. }
  1654. void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1655. void *binding = nullptr;
  1656. MutexLock instance_binding_lock(_instance_binding_mutex);
  1657. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1658. if (_instance_bindings[i].token == p_token) {
  1659. binding = _instance_bindings[i].binding;
  1660. break;
  1661. }
  1662. }
  1663. if (unlikely(!binding && p_callbacks)) {
  1664. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1665. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1666. if (current_size == 0 || new_size > current_size) {
  1667. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1668. }
  1669. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1670. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1671. _instance_bindings[_instance_binding_count].token = p_token;
  1672. binding = p_callbacks->create_callback(p_token, this);
  1673. _instance_bindings[_instance_binding_count].binding = binding;
  1674. #ifdef TOOLS_ENABLED
  1675. if (!_extension && Engine::get_singleton()->is_extension_reloading_enabled()) {
  1676. GDExtensionManager::get_singleton()->track_instance_binding(p_token, this);
  1677. }
  1678. #endif
  1679. _instance_binding_count++;
  1680. }
  1681. return binding;
  1682. }
  1683. bool Object::has_instance_binding(void *p_token) {
  1684. bool found = false;
  1685. MutexLock instance_binding_lock(_instance_binding_mutex);
  1686. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1687. if (_instance_bindings[i].token == p_token) {
  1688. found = true;
  1689. break;
  1690. }
  1691. }
  1692. return found;
  1693. }
  1694. void Object::free_instance_binding(void *p_token) {
  1695. bool found = false;
  1696. MutexLock instance_binding_lock(_instance_binding_mutex);
  1697. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1698. if (!found && _instance_bindings[i].token == p_token) {
  1699. if (_instance_bindings[i].free_callback) {
  1700. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1701. }
  1702. found = true;
  1703. }
  1704. if (found) {
  1705. if (i + 1 < _instance_binding_count) {
  1706. _instance_bindings[i] = _instance_bindings[i + 1];
  1707. } else {
  1708. _instance_bindings[i] = { nullptr };
  1709. }
  1710. }
  1711. }
  1712. if (found) {
  1713. _instance_binding_count--;
  1714. }
  1715. }
  1716. #ifdef TOOLS_ENABLED
  1717. void Object::clear_internal_extension() {
  1718. ERR_FAIL_NULL(_extension);
  1719. // Free the instance inside the GDExtension.
  1720. if (_extension->free_instance) {
  1721. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1722. }
  1723. _extension = nullptr;
  1724. _extension_instance = nullptr;
  1725. // Clear the instance bindings.
  1726. _instance_binding_mutex.lock();
  1727. if (_instance_bindings) {
  1728. if (_instance_bindings[0].free_callback) {
  1729. _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
  1730. }
  1731. _instance_bindings[0].binding = nullptr;
  1732. _instance_bindings[0].token = nullptr;
  1733. _instance_bindings[0].free_callback = nullptr;
  1734. _instance_bindings[0].reference_callback = nullptr;
  1735. }
  1736. _instance_binding_mutex.unlock();
  1737. // Clear the virtual methods.
  1738. while (virtual_method_list) {
  1739. (*virtual_method_list->method) = nullptr;
  1740. (*virtual_method_list->initialized) = false;
  1741. virtual_method_list = virtual_method_list->next;
  1742. }
  1743. }
  1744. void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
  1745. ERR_FAIL_COND(_extension != nullptr);
  1746. if (p_extension) {
  1747. _extension_instance = p_extension->recreate_instance ? p_extension->recreate_instance(p_extension->class_userdata, (GDExtensionObjectPtr)this) : nullptr;
  1748. ERR_FAIL_NULL_MSG(_extension_instance, "Unable to recreate GDExtension instance - does this extension support hot reloading?");
  1749. _extension = p_extension;
  1750. }
  1751. }
  1752. #endif
  1753. void Object::_construct_object(bool p_reference) {
  1754. type_is_reference = p_reference;
  1755. _instance_id = ObjectDB::add_instance(this);
  1756. #ifdef DEBUG_ENABLED
  1757. _lock_index.init(1);
  1758. #endif
  1759. }
  1760. Object::Object(bool p_reference) {
  1761. _construct_object(p_reference);
  1762. }
  1763. Object::Object() {
  1764. _construct_object(false);
  1765. }
  1766. void Object::detach_from_objectdb() {
  1767. if (_instance_id != ObjectID()) {
  1768. ObjectDB::remove_instance(this);
  1769. _instance_id = ObjectID();
  1770. }
  1771. }
  1772. Object::~Object() {
  1773. if (script_instance) {
  1774. memdelete(script_instance);
  1775. }
  1776. script_instance = nullptr;
  1777. if (_extension) {
  1778. #ifdef TOOLS_ENABLED
  1779. if (_extension->untrack_instance) {
  1780. _extension->untrack_instance(_extension->tracking_userdata, this);
  1781. }
  1782. #endif
  1783. if (_extension->free_instance) {
  1784. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1785. }
  1786. _extension = nullptr;
  1787. _extension_instance = nullptr;
  1788. }
  1789. #ifdef TOOLS_ENABLED
  1790. else if (_instance_bindings != nullptr) {
  1791. Engine *engine = Engine::get_singleton();
  1792. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  1793. if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) {
  1794. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1795. gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this);
  1796. }
  1797. }
  1798. }
  1799. #endif
  1800. if (_emitting) {
  1801. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1802. ERR_PRINT(vformat("Object '%s' was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.", to_string()));
  1803. }
  1804. // Drop all connections to the signals of this object.
  1805. while (signal_map.size()) {
  1806. // Avoid regular iteration so erasing is safe.
  1807. KeyValue<StringName, SignalData> &E = *signal_map.begin();
  1808. SignalData *s = &E.value;
  1809. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1810. Object *target = slot_kv.value.conn.callable.get_object();
  1811. if (likely(target)) {
  1812. target->connections.erase(slot_kv.value.cE);
  1813. }
  1814. }
  1815. signal_map.erase(E.key);
  1816. }
  1817. // Disconnect signals that connect to this object.
  1818. while (connections.size()) {
  1819. Connection c = connections.front()->get();
  1820. Object *obj = c.callable.get_object();
  1821. bool disconnected = false;
  1822. if (likely(obj)) {
  1823. disconnected = c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1824. }
  1825. if (unlikely(!disconnected)) {
  1826. // If the disconnect has failed, abandon the connection to avoid getting trapped in an infinite loop here.
  1827. connections.pop_front();
  1828. }
  1829. }
  1830. if (_instance_id != ObjectID()) {
  1831. ObjectDB::remove_instance(this);
  1832. _instance_id = ObjectID();
  1833. }
  1834. _predelete_ok = 2;
  1835. if (_instance_bindings != nullptr) {
  1836. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1837. if (_instance_bindings[i].free_callback) {
  1838. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1839. }
  1840. }
  1841. memfree(_instance_bindings);
  1842. }
  1843. }
  1844. bool predelete_handler(Object *p_object) {
  1845. return p_object->_predelete();
  1846. }
  1847. void postinitialize_handler(Object *p_object) {
  1848. p_object->_initialize();
  1849. p_object->_postinitialize();
  1850. }
  1851. void ObjectDB::debug_objects(DebugFunc p_func) {
  1852. spin_lock.lock();
  1853. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1854. if (object_slots[i].validator) {
  1855. p_func(object_slots[i].object);
  1856. count--;
  1857. }
  1858. }
  1859. spin_lock.unlock();
  1860. }
  1861. #ifdef TOOLS_ENABLED
  1862. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1863. const String pf = p_function;
  1864. if (p_idx == 0) {
  1865. if (pf == "connect" || pf == "is_connected" || pf == "disconnect" || pf == "emit_signal" || pf == "has_signal") {
  1866. List<MethodInfo> signals;
  1867. get_signal_list(&signals);
  1868. for (const MethodInfo &E : signals) {
  1869. r_options->push_back(E.name.quote());
  1870. }
  1871. } else if (pf == "call" || pf == "call_deferred" || pf == "callv" || pf == "has_method") {
  1872. List<MethodInfo> methods;
  1873. get_method_list(&methods);
  1874. for (const MethodInfo &E : methods) {
  1875. if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) {
  1876. continue;
  1877. }
  1878. r_options->push_back(E.name.quote());
  1879. }
  1880. } else if (pf == "set" || pf == "set_deferred" || pf == "get") {
  1881. List<PropertyInfo> properties;
  1882. get_property_list(&properties);
  1883. for (const PropertyInfo &E : properties) {
  1884. if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) {
  1885. r_options->push_back(E.name.quote());
  1886. }
  1887. }
  1888. } else if (pf == "set_meta" || pf == "get_meta" || pf == "has_meta" || pf == "remove_meta") {
  1889. for (const KeyValue<StringName, Variant> &K : metadata) {
  1890. r_options->push_back(String(K.key).quote());
  1891. }
  1892. }
  1893. } else if (p_idx == 2) {
  1894. if (pf == "connect") {
  1895. // Ideally, the constants should be inferred by the parameter.
  1896. // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now.
  1897. List<StringName> constants;
  1898. ClassDB::get_enum_constants("Object", "ConnectFlags", &constants);
  1899. for (const StringName &E : constants) {
  1900. r_options->push_back(String(E));
  1901. }
  1902. }
  1903. }
  1904. }
  1905. #endif
  1906. SpinLock ObjectDB::spin_lock;
  1907. uint32_t ObjectDB::slot_count = 0;
  1908. uint32_t ObjectDB::slot_max = 0;
  1909. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1910. uint64_t ObjectDB::validator_counter = 0;
  1911. int ObjectDB::get_object_count() {
  1912. return slot_count;
  1913. }
  1914. ObjectID ObjectDB::add_instance(Object *p_object) {
  1915. spin_lock.lock();
  1916. if (unlikely(slot_count == slot_max)) {
  1917. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1918. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1919. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1920. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1921. object_slots[i].object = nullptr;
  1922. object_slots[i].is_ref_counted = false;
  1923. object_slots[i].next_free = i;
  1924. object_slots[i].validator = 0;
  1925. }
  1926. slot_max = new_slot_max;
  1927. }
  1928. uint32_t slot = object_slots[slot_count].next_free;
  1929. if (object_slots[slot].object != nullptr) {
  1930. spin_lock.unlock();
  1931. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1932. }
  1933. object_slots[slot].object = p_object;
  1934. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1935. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1936. if (unlikely(validator_counter == 0)) {
  1937. validator_counter = 1;
  1938. }
  1939. object_slots[slot].validator = validator_counter;
  1940. uint64_t id = validator_counter;
  1941. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1942. id |= uint64_t(slot);
  1943. if (p_object->is_ref_counted()) {
  1944. id |= OBJECTDB_REFERENCE_BIT;
  1945. }
  1946. slot_count++;
  1947. spin_lock.unlock();
  1948. return ObjectID(id);
  1949. }
  1950. void ObjectDB::remove_instance(Object *p_object) {
  1951. uint64_t t = p_object->get_instance_id();
  1952. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1953. spin_lock.lock();
  1954. #ifdef DEBUG_ENABLED
  1955. if (object_slots[slot].object != p_object) {
  1956. spin_lock.unlock();
  1957. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1958. }
  1959. {
  1960. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1961. if (object_slots[slot].validator != validator) {
  1962. spin_lock.unlock();
  1963. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1964. }
  1965. }
  1966. #endif
  1967. //decrease slot count
  1968. slot_count--;
  1969. //set the free slot properly
  1970. object_slots[slot_count].next_free = slot;
  1971. //invalidate, so checks against it fail
  1972. object_slots[slot].validator = 0;
  1973. object_slots[slot].is_ref_counted = false;
  1974. object_slots[slot].object = nullptr;
  1975. spin_lock.unlock();
  1976. }
  1977. void ObjectDB::setup() {
  1978. //nothing to do now
  1979. }
  1980. void ObjectDB::cleanup() {
  1981. spin_lock.lock();
  1982. if (slot_count > 0) {
  1983. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1984. if (OS::get_singleton()->is_stdout_verbose()) {
  1985. // Ensure calling the native classes because if a leaked instance has a script
  1986. // that overrides any of those methods, it'd not be OK to call them at this point,
  1987. // now the scripting languages have already been terminated.
  1988. MethodBind *node_get_path = ClassDB::get_method("Node", "get_path");
  1989. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1990. Callable::CallError call_error;
  1991. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1992. if (object_slots[i].validator) {
  1993. Object *obj = object_slots[i].object;
  1994. String extra_info;
  1995. if (obj->is_class("Node")) {
  1996. extra_info = " - Node path: " + String(node_get_path->call(obj, nullptr, 0, call_error));
  1997. }
  1998. if (obj->is_class("Resource")) {
  1999. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  2000. }
  2001. uint64_t id = uint64_t(i) | (uint64_t(object_slots[i].validator) << OBJECTDB_SLOT_MAX_COUNT_BITS) | (object_slots[i].is_ref_counted ? OBJECTDB_REFERENCE_BIT : 0);
  2002. DEV_ASSERT(id == (uint64_t)obj->get_instance_id()); // We could just use the id from the object, but this check may help catching memory corruption catastrophes.
  2003. print_line("Leaked instance: " + String(obj->get_class()) + ":" + uitos(id) + extra_info);
  2004. count--;
  2005. }
  2006. }
  2007. print_line("Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).");
  2008. }
  2009. }
  2010. if (object_slots) {
  2011. memfree(object_slots);
  2012. }
  2013. spin_lock.unlock();
  2014. }