Skip to content

Commit

Permalink
Don't cache null forever if a singleton isn't available yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jul 17, 2023
1 parent 749b0b9 commit 1ffa645
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,13 +1453,16 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us

if is_singleton:
result.append(f"{class_name} *{class_name}::get_singleton() {{")
result.append(f"\tconst StringName _gde_class_name = {class_name}::get_class_static();")
result.append("\tstatic GDExtensionObjectPtr singleton_obj = nullptr;")
result.append("\tif (unlikely(singleton_obj == nullptr)) {")
result.append(f"\t\tconst StringName _gde_class_name = {class_name}::get_class_static();")
result.append(
"\tstatic GDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton(_gde_class_name._native_ptr());"
"\t\tsingleton_obj = internal::gdextension_interface_global_get_singleton(_gde_class_name._native_ptr());"
)
result.append("#ifdef DEBUG_ENABLED")
result.append("\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
result.append("\t\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
result.append("#endif // DEBUG_ENABLED")
result.append("\t}")
result.append(
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));"
)
Expand Down

0 comments on commit 1ffa645

Please sign in to comment.