class_db.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**************************************************************************/
  2. /* class_db.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 <godot_cpp/core/class_db.hpp>
  31. #include <godot_cpp/core/error_macros.hpp>
  32. #include <godot_cpp/godot.hpp>
  33. #include <godot_cpp/templates/vector.hpp>
  34. #include <godot_cpp/core/memory.hpp>
  35. namespace godot {
  36. HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
  37. AHashMap<StringName, const GDExtensionInstanceBindingCallbacks *> ClassDB::instance_binding_callbacks;
  38. LocalVector<StringName> ClassDB::class_register_order;
  39. AHashMap<StringName, Object *> ClassDB::engine_singletons;
  40. std::mutex ClassDB::engine_singletons_mutex;
  41. GDExtensionInitializationLevel ClassDB::current_level = GDEXTENSION_INITIALIZATION_CORE;
  42. MethodDefinition D_METHOD(StringName p_name) {
  43. return MethodDefinition(p_name);
  44. }
  45. MethodDefinition D_METHOD(StringName p_name, StringName p_arg1) {
  46. MethodDefinition method(p_name);
  47. method.args.push_front(p_arg1);
  48. return method;
  49. }
  50. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix) {
  51. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), String("Trying to add property '{0}{1}' to non-existing class '{2}'.").format(Array::make(p_prefix, p_name, p_class)));
  52. ::godot::gdextension_interface::classdb_register_extension_class_property_group(::godot::gdextension_interface::library, p_class._native_ptr(), p_name._native_ptr(), p_prefix._native_ptr());
  53. }
  54. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix) {
  55. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), String("Trying to add property '{0}{1}' to non-existing class '{2}'.").format(Array::make(p_prefix, p_name, p_class)));
  56. ::godot::gdextension_interface::classdb_register_extension_class_property_subgroup(::godot::gdextension_interface::library, p_class._native_ptr(), p_name._native_ptr(), p_prefix._native_ptr());
  57. }
  58. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  59. ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), String("Trying to add property '{0}' to non-existing class '{1}'.").format(Array::make(p_pinfo.name, p_class)));
  60. ClassInfo &info = classes[p_class];
  61. ERR_FAIL_COND_MSG(info.property_names.find(p_pinfo.name) != info.property_names.end(), String("Property '{0}' already exists in class '{1}'.").format(Array::make(p_pinfo.name, p_class)));
  62. MethodBind *setter = nullptr;
  63. if (p_setter != String("")) {
  64. setter = get_method(p_class, p_setter);
  65. ERR_FAIL_NULL_MSG(setter, String("Setter method '{0}::{1}()' not found for property '{2}::{3}'.").format(Array::make(p_class, p_setter, p_class, p_pinfo.name)));
  66. size_t exp_args = 1 + (p_index >= 0 ? 1 : 0);
  67. ERR_FAIL_COND_MSG((int)exp_args != setter->get_argument_count(), String("Setter method '{0}::{1}()' must take a single argument.").format(Array::make(p_class, p_setter)));
  68. }
  69. ERR_FAIL_COND_MSG(p_getter == String(""), String("Getter method must be specified for '{0}::{1}'.").format(Array::make(p_class, p_pinfo.name)));
  70. MethodBind *getter = get_method(p_class, p_getter);
  71. ERR_FAIL_NULL_MSG(getter, String("Getter method '{0}::{1}()' not found for property '{2}::{3}'.").format(Array::make(p_class, p_getter, p_class, p_pinfo.name)));
  72. {
  73. size_t exp_args = 0 + (p_index >= 0 ? 1 : 0);
  74. ERR_FAIL_COND_MSG((int)exp_args != getter->get_argument_count(), String("Getter method '{0}::{1}()' must not take any argument.").format(Array::make(p_class, p_getter)));
  75. }
  76. // register property with plugin
  77. info.property_names.insert(p_pinfo.name);
  78. // register with Godot
  79. GDExtensionPropertyInfo prop_info = {
  80. static_cast<GDExtensionVariantType>(p_pinfo.type), // GDExtensionVariantType type;
  81. p_pinfo.name._native_ptr(), // GDExtensionStringNamePtr name;
  82. p_pinfo.class_name._native_ptr(), // GDExtensionStringNamePtr class_name;
  83. p_pinfo.hint, // NONE //uint32_t hint;
  84. p_pinfo.hint_string._native_ptr(), // GDExtensionStringPtr hint_string;
  85. p_pinfo.usage, // DEFAULT //uint32_t usage;
  86. };
  87. ::godot::gdextension_interface::classdb_register_extension_class_property_indexed(::godot::gdextension_interface::library, info.name._native_ptr(), &prop_info, p_setter._native_ptr(), p_getter._native_ptr(), p_index);
  88. }
  89. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_method) {
  90. ERR_FAIL_COND_V_MSG(classes.find(p_class) == classes.end(), nullptr, String("Class '{0}' not found.").format(Array::make(p_class)));
  91. ClassInfo *type = &classes[p_class];
  92. while (type) {
  93. AHashMap<StringName, MethodBind *>::Iterator method = type->method_map.find(p_method);
  94. if (method != type->method_map.end()) {
  95. return method->value;
  96. }
  97. type = type->parent_ptr;
  98. continue;
  99. }
  100. return nullptr;
  101. }
  102. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount) {
  103. StringName instance_type = p_bind->get_instance_class();
  104. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(instance_type);
  105. if (type_it == classes.end()) {
  106. memdelete(p_bind);
  107. ERR_FAIL_V_MSG(nullptr, String("Class '{0}' doesn't exist.").format(Array::make(instance_type)));
  108. }
  109. ClassInfo &type = type_it->value;
  110. if (type.method_map.find(method_name.name) != type.method_map.end()) {
  111. memdelete(p_bind);
  112. ERR_FAIL_V_MSG(nullptr, String("Binding duplicate method: {0}::{1}().").format(Array::make(instance_type, method_name.name)));
  113. }
  114. if (type.virtual_methods.find(method_name.name) != type.virtual_methods.end()) {
  115. memdelete(p_bind);
  116. ERR_FAIL_V_MSG(nullptr, String("Method '{0}::{1}()' already bound as virtual.").format(Array::make(instance_type, method_name.name)));
  117. }
  118. p_bind->set_name(method_name.name);
  119. if ((int)method_name.args.size() > p_bind->get_argument_count()) {
  120. memdelete(p_bind);
  121. ERR_FAIL_V_MSG(nullptr, String("Method '{0}::{1}()' definition has more arguments than the actual method.").format(Array::make(instance_type, method_name.name)));
  122. }
  123. p_bind->set_hint_flags(p_flags);
  124. LocalVector<StringName> args;
  125. args.resize(method_name.args.size());
  126. size_t arg_index = 0;
  127. for (StringName arg : method_name.args) {
  128. args[arg_index++] = arg;
  129. }
  130. p_bind->set_argument_names(args);
  131. LocalVector<Variant> defvals;
  132. defvals.resize(p_defcount);
  133. for (int i = 0; i < p_defcount; i++) {
  134. defvals[i] = *static_cast<const Variant *>(p_defs[i]);
  135. }
  136. p_bind->set_default_arguments(defvals);
  137. p_bind->set_hint_flags(p_flags);
  138. // register our method bind within our plugin
  139. type.method_map[method_name.name] = p_bind;
  140. // and register with godot
  141. bind_method_godot(type.name, p_bind);
  142. return p_bind;
  143. }
  144. void ClassDB::bind_method_godot(const StringName &p_class_name, MethodBind *p_method) {
  145. LocalVector<GDExtensionVariantPtr> def_args;
  146. const LocalVector<Variant> &def_args_val = p_method->get_default_arguments();
  147. def_args.resize(def_args_val.size());
  148. for (size_t i = 0; i < def_args_val.size(); i++) {
  149. def_args[i] = (GDExtensionVariantPtr)&def_args_val[i];
  150. }
  151. LocalVector<PropertyInfo> return_value_and_arguments_info = p_method->get_arguments_info_list();
  152. LocalVector<GDExtensionClassMethodArgumentMetadata> return_value_and_arguments_metadata = p_method->get_arguments_metadata_list();
  153. LocalVector<GDExtensionPropertyInfo> return_value_and_arguments_gdextension_info;
  154. return_value_and_arguments_gdextension_info.reserve(return_value_and_arguments_info.size());
  155. for (const PropertyInfo &info : return_value_and_arguments_info) {
  156. return_value_and_arguments_gdextension_info.push_back(
  157. GDExtensionPropertyInfo{
  158. static_cast<GDExtensionVariantType>(info.type), // GDExtensionVariantType type;
  159. info.name._native_ptr(), // GDExtensionStringNamePtr name;
  160. info.class_name._native_ptr(), // GDExtensionStringNamePtr class_name;
  161. info.hint, // uint32_t hint;
  162. info.hint_string._native_ptr(), // GDExtensionStringPtr hint_string;
  163. info.usage, // uint32_t usage;
  164. });
  165. }
  166. GDExtensionPropertyInfo *return_value_info = return_value_and_arguments_gdextension_info.ptr();
  167. GDExtensionClassMethodArgumentMetadata *return_value_metadata = return_value_and_arguments_metadata.ptr();
  168. GDExtensionPropertyInfo *arguments_info = return_value_and_arguments_gdextension_info.ptr() + 1;
  169. GDExtensionClassMethodArgumentMetadata *arguments_metadata = return_value_and_arguments_metadata.ptr() + 1;
  170. StringName name = p_method->get_name();
  171. GDExtensionClassMethodInfo method_info = {
  172. name._native_ptr(), // GDExtensionStringNamePtr;
  173. p_method, // void *method_userdata;
  174. MethodBind::bind_call, // GDExtensionClassMethodCall call_func;
  175. MethodBind::bind_ptrcall, // GDExtensionClassMethodPtrCall ptrcall_func;
  176. p_method->get_hint_flags(), // uint32_t method_flags; /* GDExtensionClassMethodFlags */
  177. (GDExtensionBool)p_method->has_return(), // GDExtensionBool has_return_value;
  178. return_value_info, // GDExtensionPropertyInfo *
  179. *return_value_metadata, // GDExtensionClassMethodArgumentMetadata *
  180. (uint32_t)p_method->get_argument_count(), // uint32_t argument_count;
  181. arguments_info, // GDExtensionPropertyInfo *
  182. arguments_metadata, // GDExtensionClassMethodArgumentMetadata *
  183. (uint32_t)p_method->get_default_argument_count(), // uint32_t default_argument_count;
  184. def_args.ptr(), // GDExtensionVariantPtr *default_arguments;
  185. };
  186. ::godot::gdextension_interface::classdb_register_extension_class_method(::godot::gdextension_interface::library, p_class_name._native_ptr(), &method_info);
  187. }
  188. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  189. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(p_class);
  190. ERR_FAIL_COND_MSG(type_it == classes.end(), String("Class '{0}' doesn't exist.").format(Array::make(p_class)));
  191. ClassInfo &cl = type_it->value;
  192. // Check if this signal is already register
  193. ClassInfo *check = &cl;
  194. while (check) {
  195. ERR_FAIL_COND_MSG(check->signal_names.find(p_signal.name) != check->signal_names.end(), String("Class '{0}' already has signal '{1}'.").format(Array::make(p_class, p_signal.name)));
  196. check = check->parent_ptr;
  197. }
  198. // register our signal in our plugin
  199. cl.signal_names.insert(p_signal.name);
  200. // register our signal in godot
  201. LocalVector<GDExtensionPropertyInfo> parameters;
  202. parameters.reserve(p_signal.arguments.size());
  203. for (const PropertyInfo &par : p_signal.arguments) {
  204. parameters.push_back(GDExtensionPropertyInfo{
  205. static_cast<GDExtensionVariantType>(par.type), // GDExtensionVariantType type;
  206. par.name._native_ptr(), // GDExtensionStringNamePtr name;
  207. par.class_name._native_ptr(), // GDExtensionStringNamePtr class_name;
  208. par.hint, // NONE //uint32_t hint;
  209. par.hint_string._native_ptr(), // GDExtensionStringPtr hint_string;
  210. par.usage, // DEFAULT //uint32_t usage;
  211. });
  212. }
  213. ::godot::gdextension_interface::classdb_register_extension_class_signal(::godot::gdextension_interface::library, cl.name._native_ptr(), p_signal.name._native_ptr(), parameters.ptr(), parameters.size());
  214. }
  215. void ClassDB::bind_integer_constant(const StringName &p_class_name, const StringName &p_enum_name, const StringName &p_constant_name, GDExtensionInt p_constant_value, bool p_is_bitfield) {
  216. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(p_class_name);
  217. ERR_FAIL_COND_MSG(type_it == classes.end(), String("Class '{0}' doesn't exist.").format(Array::make(p_class_name)));
  218. ClassInfo &type = type_it->value;
  219. // check if it already exists
  220. ERR_FAIL_COND_MSG(type.constant_names.find(p_constant_name) != type.constant_names.end(), String("Constant '{0}::{1}' already registered.").format(Array::make(p_class_name, p_constant_name)));
  221. // register it with our plugin (purely to check for duplicates)
  222. type.constant_names.insert(p_constant_name);
  223. // Register it with Godot
  224. ::godot::gdextension_interface::classdb_register_extension_class_integer_constant(::godot::gdextension_interface::library, p_class_name._native_ptr(), p_enum_name._native_ptr(), p_constant_name._native_ptr(), p_constant_value, p_is_bitfield);
  225. }
  226. GDExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name, uint32_t p_hash) {
  227. // This is called by Godot the first time it calls a virtual function, and it caches the result, per object instance.
  228. // Because of this, it can happen from different threads at once.
  229. // It should be ok not using any mutex as long as we only READ data.
  230. const StringName *class_name = reinterpret_cast<const StringName *>(p_userdata);
  231. const StringName *name = reinterpret_cast<const StringName *>(p_name);
  232. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(*class_name);
  233. ERR_FAIL_COND_V_MSG(type_it == classes.end(), nullptr, String("Class '{0}' doesn't exist.").format(Array::make(*class_name)));
  234. const ClassInfo *type = &type_it->value;
  235. // Find method in current class, or any of its parent classes (Godot classes not included)
  236. while (type != nullptr) {
  237. AHashMap<StringName, ClassInfo::VirtualMethod>::ConstIterator method_it = type->virtual_methods.find(*name);
  238. if (method_it != type->virtual_methods.end() && method_it->value.hash == p_hash) {
  239. return method_it->value.func;
  240. }
  241. type = type->parent_ptr;
  242. }
  243. return nullptr;
  244. }
  245. const GDExtensionInstanceBindingCallbacks *ClassDB::get_instance_binding_callbacks(const StringName &p_class) {
  246. AHashMap<StringName, const GDExtensionInstanceBindingCallbacks *>::Iterator callbacks_it = instance_binding_callbacks.find(p_class);
  247. if (likely(callbacks_it != instance_binding_callbacks.end())) {
  248. return callbacks_it->value;
  249. }
  250. // If we don't have an instance binding callback for the given class, find the closest parent where we do.
  251. StringName class_name = p_class;
  252. do {
  253. class_name = get_parent_class(class_name);
  254. ERR_FAIL_COND_V_MSG(class_name == StringName(), nullptr, String("Cannot find instance binding callbacks for class '{0}'.").format(Array::make(p_class)));
  255. callbacks_it = instance_binding_callbacks.find(class_name);
  256. } while (callbacks_it == instance_binding_callbacks.end());
  257. return callbacks_it->value;
  258. }
  259. void ClassDB::bind_virtual_method(const StringName &p_class, const StringName &p_method, GDExtensionClassCallVirtual p_call, uint32_t p_hash) {
  260. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(p_class);
  261. ERR_FAIL_COND_MSG(type_it == classes.end(), String("Class '{0}' doesn't exist.").format(Array::make(p_class)));
  262. ClassInfo &type = type_it->value;
  263. ERR_FAIL_COND_MSG(type.method_map.find(p_method) != type.method_map.end(), String("Method '{0}::{1}()' already registered as non-virtual.").format(Array::make(p_class, p_method)));
  264. ERR_FAIL_COND_MSG(type.virtual_methods.find(p_method) != type.virtual_methods.end(), String("Virtual '{0}::{1}()' method already registered.").format(Array::make(p_class, p_method)));
  265. type.virtual_methods[p_method] = ClassInfo::VirtualMethod{
  266. p_call,
  267. p_hash,
  268. };
  269. }
  270. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, const Vector<StringName> &p_arg_names) {
  271. HashMap<StringName, ClassInfo>::Iterator type_it = classes.find(p_class);
  272. ERR_FAIL_COND_MSG(type_it == classes.end(), String("Class '{0}' doesn't exist.").format(Array::make(p_class)));
  273. GDExtensionClassVirtualMethodInfo mi;
  274. mi.name = (GDExtensionStringNamePtr)&p_method.name;
  275. mi.method_flags = p_method.flags;
  276. mi.return_value = p_method.return_val._to_gdextension();
  277. mi.return_value_metadata = p_method.return_val_metadata;
  278. mi.argument_count = p_method.arguments.size();
  279. if (mi.argument_count > 0) {
  280. mi.arguments = (GDExtensionPropertyInfo *)memalloc(sizeof(GDExtensionPropertyInfo) * mi.argument_count);
  281. mi.arguments_metadata = (GDExtensionClassMethodArgumentMetadata *)memalloc(sizeof(GDExtensionClassMethodArgumentMetadata) * mi.argument_count);
  282. if (mi.argument_count != p_method.arguments_metadata.size()) {
  283. WARN_PRINT("Mismatch argument metadata count for virtual method: " + String(p_class) + "::" + p_method.name);
  284. }
  285. for (uint32_t i = 0; i < mi.argument_count; i++) {
  286. mi.arguments[i] = p_method.arguments[i]._to_gdextension();
  287. if (i < p_method.arguments_metadata.size()) {
  288. mi.arguments_metadata[i] = p_method.arguments_metadata[i];
  289. }
  290. }
  291. } else {
  292. mi.arguments = nullptr;
  293. mi.arguments_metadata = nullptr;
  294. }
  295. if (p_arg_names.size() != mi.argument_count) {
  296. WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name);
  297. } else {
  298. for (int i = 0; i < p_arg_names.size(); i++) {
  299. mi.arguments[i].name = (GDExtensionStringNamePtr)&p_arg_names[i];
  300. }
  301. }
  302. ::godot::gdextension_interface::classdb_register_extension_class_virtual_method(::godot::gdextension_interface::library, &p_class, &mi);
  303. if (mi.arguments) {
  304. memfree(mi.arguments);
  305. }
  306. if (mi.arguments_metadata) {
  307. memfree(mi.arguments_metadata);
  308. }
  309. }
  310. void ClassDB::_editor_get_classes_used_callback(GDExtensionTypePtr p_packed_string_array) {
  311. PackedStringArray *arr = reinterpret_cast<PackedStringArray *>(p_packed_string_array);
  312. arr->resize(instance_binding_callbacks.size());
  313. int index = 0;
  314. for (const KeyValue<StringName, const GDExtensionInstanceBindingCallbacks *> &pair : instance_binding_callbacks) {
  315. (*arr)[index++] = pair.key;
  316. }
  317. }
  318. void ClassDB::initialize_class(const ClassInfo &p_cl) {
  319. }
  320. void ClassDB::initialize(GDExtensionInitializationLevel p_level) {
  321. for (const KeyValue<StringName, ClassInfo> &pair : classes) {
  322. const ClassInfo &cl = pair.value;
  323. if (cl.level != p_level) {
  324. continue;
  325. }
  326. // Nothing to do here for now...
  327. }
  328. }
  329. void ClassDB::deinitialize(GDExtensionInitializationLevel p_level) {
  330. std::set<StringName> to_erase;
  331. for (int i = class_register_order.size() - 1; i >= 0; --i) {
  332. const StringName &name = class_register_order[i];
  333. const ClassInfo &cl = classes[name];
  334. if (cl.level != p_level) {
  335. continue;
  336. }
  337. ::godot::gdextension_interface::classdb_unregister_extension_class(::godot::gdextension_interface::library, name._native_ptr());
  338. for (const KeyValue<StringName, MethodBind *> &method : cl.method_map) {
  339. memdelete(method.value);
  340. }
  341. classes.erase(name);
  342. to_erase.insert(name);
  343. }
  344. {
  345. for (const StringName &x : to_erase) {
  346. class_register_order.erase(x);
  347. }
  348. }
  349. if (p_level == GDEXTENSION_INITIALIZATION_CORE) {
  350. // Make a new list of the singleton objects, since freeing the instance bindings will lead to
  351. // elements getting removed from engine_singletons.
  352. LocalVector<Object *> singleton_objects;
  353. {
  354. std::lock_guard<std::mutex> lock(engine_singletons_mutex);
  355. singleton_objects.reserve(engine_singletons.size());
  356. for (const KeyValue<StringName, Object *> &pair : engine_singletons) {
  357. singleton_objects.push_back(pair.value);
  358. }
  359. }
  360. for (const Object *i : singleton_objects) {
  361. ::godot::gdextension_interface::object_free_instance_binding((*i)._owner, ::godot::gdextension_interface::token);
  362. }
  363. }
  364. }
  365. } // namespace godot