module Docs::O_CPP_BRIDGE_ARCHITECTURE

Overview

O. C++ GDExtension Loader Bridge Architecture

The LibGodot C++ GDExtension Loader Bridge (src/bridge/) provides the low-level, native C-ABI execution bridge between the Godot Engine 4.8+ runtime and dynamically compiled Crystal shared libraries (game.dll on Windows, game.so on Linux).


Architectural Invariants & Role

In Godot's GDExtension architecture, the engine expects a dynamic library that exports crystal_library_init. While Crystal can produce dynamic libraries (--link-flags /DLL), host-driven execution (where Godot is the parent host process) poses critical runtime challenges:

  1. Boehm GC Thread Registration: Foreign engine threads (Godot main thread, audio thread, rendering thread, and WorkerThreadPool workers) allocating or accessing Crystal memory crash with EXCEPTION_ACCESS_VIOLATION (0xC0000005) unless registered with Boehm GC.
  2. Headerless Flat C-ABI Interface: Crystal cannot consume complex C++ templates or Godot C++ classes directly. The bridge translates Godot's C-API function table into flat, predictable C-ABI data structures (BridgeAPI, VariantArg, CrystalClassDesc).
  3. Windows OS DLL File-Locking & Shadow Copying: On Windows, LoadLibraryA locks the DLL file on disk. The bridge creates unique timestamped shadow copies (game_loaded_<PID>_<TS>.dll) to leave bin/game.dll unlocked for continuous background compilation while the editor stays open.
  4. Memory Pinning across Live Reloads: When Godot reloads a GDExtension, it calls FreeLibrary on the extension DLL. Because Godot's ClassDB retains function pointers and instance userdata pointers, unmapping the bridge causes immediate access violations. The bridge pins itself in memory (GET_MODULE_HANDLE_EX_FLAG_PIN / RTLD_NODELETE) to remain permanent across reloads.

File Map Overview

File Role Primary Functionality
crystal_bridge.cpp Master Entry Point Exports crystal_library_init, manages GDExtension lifecycle levels, and coordinates module unloading.
common.hpp Platform & Crash Diagnostics Win32/POSIX platform abstractions, crash interception, and backtrace generation to crash_dump.log.
gdextension_api.hpp Function Pointer Table Dynamically resolves and caches Godot C-API function pointers from p_get_proc_address.
bridge_types.hpp C-ABI Data Contracts Defines VariantArg, BridgeSignalArg, CrystalClassDesc, GenericExtensionInstance, and BridgeAPI.
gc_support.hpp Boehm GC Thread Safety Dynamically discovers gc.dll exports and registers foreign Godot threads with the garbage collector.
editor_doc.hpp XML Help Harvester Buffers XML class/property doc comments and flushes them into Godot's EditorHelp subsystem.
dispatch_signals.hpp Method & Signal Dispatch Variant marshaling, interned StringNames, CustomCallable signal wrappers, and dynamic vararg dispatch.
extension_instance.hpp Instance Lifecycle Instantiates Godot native base classes, binds Crystal objects, routes virtual methods, and handles property get/set.
classdb_registry.hpp ClassDB Reflection Registers custom classes, properties, signals, constants, and defers editor-only classes to EDITOR level.
bridge_api.hpp Exported API Assembly Populates global BridgeAPI table and defines exported C symbols for external binding.
module_loader.hpp Dynamic Library Loader Discovers candidate DLLs, creates timestamped shadow copies, preloads runtime DLLs, and calls crystal_godot_init.

Deep Dive: Detailed File Mechanics

1. crystal_bridge.cpp

The master compilation translation unit and GDExtension library entry point.

2. common.hpp

Provides OS-level header imports, compiler visibility macros, and crash diagnostics:

3. gdextension_api.hpp

Manages the dynamic function pointer table required to interact with Godot's C-API:

4. bridge_types.hpp

Specifies the binary layout of all data exchanged between C++ and Crystal:

5. gc_support.hpp

Handles multi-threading and Boehm GC integration:

6. editor_doc.hpp

Provides Godot's in-editor F1 Help and hover tooltip documentation system:

7. dispatch_signals.hpp

Implements bidirectional method calling, Variant conversion, and CustomCallable signal dispatch:

8. extension_instance.hpp

Manages instance instantiation, virtual method routing, and property access:

9. classdb_registry.hpp

Handles registration with Godot's ClassDB:

10. bridge_api.hpp

Assembles and exports the C-ABI function pointer interface:

11. module_loader.hpp

Handles library discovery, runtime dependency loading, and Windows shadow copying:

Defined in:

libgodot/docs/cpp_bridge.cr

Class Method Summary

Class Method Detail

def self.files : Array(String) #

Returns the list of all C++ bridge files documented in this module


[View source]
def self.invariants : Array(String) #

Returns architectural invariants summary


[View source]