Skip to content

Commit 47d5eef

Browse files
committed
Don't cache null forever if a singleton isn't available yet
# Conflicts: # binding_generator.py
1 parent 3162be2 commit 47d5eef

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

binding_generator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,15 +1453,23 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
14531453

14541454
if is_singleton:
14551455
result.append(f"{class_name} *{class_name}::get_singleton() {{")
1456+
# We assume multi-threaded access is OK because each assignment will assign the same value every time
1457+
result.append(f"\tstatic {class_name} *singleton = nullptr;")
1458+
result.append("\tif (unlikely(singleton == nullptr)) {")
1459+
result.append(f"\t\tconst StringName _gde_class_name = {class_name}::get_class_static();")
14561460
result.append(
1457-
f"\tstatic GDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton({class_name}::get_class_static()._native_ptr());"
1461+
"\t\tGDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton(_gde_class_name._native_ptr());"
14581462
)
14591463
result.append("#ifdef DEBUG_ENABLED")
1460-
result.append("\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
1464+
result.append("\t\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
14611465
result.append("#endif // DEBUG_ENABLED")
14621466
result.append(
1463-
f"\tstatic {class_name} *singleton = reinterpret_cast<{class_name} *>(internal::gdextension_interface_object_get_instance_binding(singleton_obj, internal::token, &{class_name}::_gde_binding_callbacks));"
1467+
f"\t\tsingleton = reinterpret_cast<{class_name} *>(internal::gdextension_interface_object_get_instance_binding(singleton_obj, internal::token, &{class_name}::_gde_binding_callbacks));"
14641468
)
1469+
result.append("#ifdef DEBUG_ENABLED")
1470+
result.append("\t\tERR_FAIL_COND_V(singleton == nullptr, nullptr);")
1471+
result.append("#endif // DEBUG_ENABLED")
1472+
result.append("\t}")
14651473
result.append("\treturn singleton;")
14661474
result.append("}")
14671475
result.append("")

0 commit comments

Comments
 (0)