gdextension_interface_json_file.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. .. _doc_gdextension_interface_json_file:
  2. The C interface JSON file
  3. =========================
  4. The ``gdextension_interface.json`` file is the "source of truth" for the C API that
  5. Godot uses to communicate with GDExtensions.
  6. You can use the Godot executable to dump the file by using the following command:
  7. .. code-block:: shell
  8. godot --headless --dump-gdextension-interface-json
  9. This file is intended to be used by GDExtension language bindings to generate code for
  10. using this API in whatever form makes the most sense for that language.
  11. .. note::
  12. This is not to be confused with the ``extension_api.json``, which is also used by
  13. GDExtension language bindings, and contains information about the classes and
  14. methods that are exposed by Godot. The ``gdextension_interface.json`` is more
  15. low-level, and is used to interact with those higher-level classes and methods.
  16. For languages that can be extended via C, or provide tools for interacting with C code,
  17. it's also possible to use the Godot executable to dump a generated C header file:
  18. .. code-block:: shell
  19. godot --headless --dump-gdextension-interface
  20. .. note::
  21. The header file is compatible with earlier versions of the header file that were included
  22. with Godot 4.5 and earlier, which means it preserves some typos in names in order to
  23. ensure compatibility.
  24. The goal of this page is to explain the JSON format for the GDExtension language bindings that
  25. would like to do their own code generation from the JSON.
  26. Overall structure
  27. -----------------
  28. The JSON file is broken up into 3 sections:
  29. - The header, which includes some miscellaneous information at the top-level of the JSON file.
  30. - The ``types`` key, which defines all the types used in the GDExtension interface.
  31. - The ``interface`` key, which defines all the function pointers that can be loaded via the
  32. ``GDExtensionInterfaceGetProcAddress`` function pointer, which is passed to all GDExtensions
  33. when they are loaded.
  34. There is a complete `JSON schema <https://github.com/godotengine/godot/blob/master/core/extension/gdextension_interface.schema.json>`__
  35. included in Godot's source code.
  36. Even though we may add new types and interface functions with each minor release of Godot, we
  37. strive to **never** change them in a backwards incompatible way, or remove them. Every
  38. interface function is labeled with the version of Godot it was introduced in (the ``since``
  39. key), so you can always use the latest version of the file, and simply refrain from using
  40. anything in versions of Godot that are newer than the version you are targeting.
  41. Header
  42. ------
  43. The "header" is made up of 3 miscellaneous keys at the top-level of the file:
  44. - ``_copyright``: The standard copyright and license text that Godot includes in all source
  45. code files.
  46. - ``$schema``: Points to the JSON schema relative to this file. It can be useful to place
  47. the schema in the same directory, if you're viewing it with a code editor that understands
  48. JSON schema.
  49. - ``format_version``: An integer for the version of the file format (meaning the schema).
  50. Right now, there is only one format version (``1``). If we ever change the file format in
  51. an incompatible way, we will increment this number. This *doesn't* reflect the version
  52. of the data in the file (so it won't change between Godot versions), only its format.
  53. Hopefully, we'll never have to use it, but it allows code generators to error early if they
  54. encounter an unexpected value here.
  55. Types
  56. -----
  57. The ``types`` section is an array of types that will be used by other types, and the interface
  58. functions that will be in the last section.
  59. The types should be evaluated in order. Later types may refer to earlier types, but earlier
  60. types will not refer to later types.
  61. There is a small set of built-in types which aren't explicitly listed in the JSON:
  62. - ``void``
  63. - ``int8_t``
  64. - ``uint8_t``
  65. - ``int16_t``
  66. - ``uint16_t``
  67. - ``int32_t``
  68. - ``uint32_t``
  69. - ``int64_t``
  70. - ``uint64_t``
  71. - ``size_t`` (``uint32_t`` on 32-bit architectures, and ``uint64_t`` on 64-bit architectures)
  72. - ``char``
  73. - ``char16_t``
  74. - ``char32_t``
  75. - ``wchar_t``
  76. - ``float``
  77. - ``double``
  78. These correspond to their equivalent C types.
  79. Additionally, types can include modifiers such as:
  80. - ``*`` (e.g. ``int8_t*``) to indicate a pointer to the type
  81. - ``const`` (e.g. ``const int8_t*``) to indicate a const type
  82. Each type defined in the JSON file falls into one of 5 "kinds":
  83. - ``enum``
  84. - ``handle``
  85. - ``alias``
  86. - ``struct``
  87. - ``function``
  88. Regardless of the "kind", all types can have the following keys:
  89. - ``kind`` (required): The type's "kind".
  90. - ``name`` (required): The name of the type, which could be used as a valid C identifier.
  91. - ``description``: An array of strings documenting the type, where each string is a line of
  92. documentation (this format for ``description`` is used throughout the JSON file).
  93. - ``deprecated``: An object with its own keys for the Godot version the type was deprecated in
  94. (``since``), a message explaining the deprecation (``message``), and optionally a replacement
  95. to use instead (``replacement``).
  96. Enums
  97. ~~~~~
  98. Enums are 32-bit integers with a fixed set of possible values. In C, they could be represented
  99. as an ``enum``.
  100. They have the following keys:
  101. - ``is_bitfield``: If true, this enum is a bitfield, where the enum values can be bitwise OR'd together.
  102. It is false by default.
  103. - ``values``: The array of fixed values for this enum, each with a ``name``, ``value``, and ``description``.
  104. An enum should be represented as an ``int32_t``, unless ``is_bitfield`` is true, in which case a ``uint32_t``
  105. should be used.
  106. Example
  107. +++++++
  108. .. code-block:: json
  109. {
  110. "name": "GDExtensionInitializationLevel",
  111. "kind": "enum",
  112. "values": [
  113. {
  114. "name": "GDEXTENSION_INITIALIZATION_CORE",
  115. "value": 0
  116. },
  117. {
  118. "name": "GDEXTENSION_INITIALIZATION_SERVERS",
  119. "value": 1
  120. },
  121. {
  122. "name": "GDEXTENSION_INITIALIZATION_SCENE",
  123. "value": 2
  124. },
  125. {
  126. "name": "GDEXTENSION_INITIALIZATION_EDITOR",
  127. "value": 3
  128. },
  129. {
  130. "name": "GDEXTENSION_MAX_INITIALIZATION_LEVEL",
  131. "value": 4
  132. }
  133. ]
  134. }
  135. Handles
  136. ~~~~~~~
  137. Handles are pointers to opaque structs. In C, they could be represented as ``void *`` or ``struct{} *``.
  138. They have the following keys:
  139. - ``is_const``: If true, this handle type is to be treated as a "const pointer", meaning its internal
  140. data will not be changed. It is false by default.
  141. - ``is_uninitialized``: If true, this handle type is to be treated as pointing to uninitialized memory
  142. (which may be initialized using interface functions). It is false by default.
  143. - ``parent``: The optional name of another handle type, if this handle type is the const or uninitialized
  144. version of the parent type. This only makes sense if either ``is_const`` or ``is_uninitialized`` is true.
  145. Handles are the size of pointers on the given architecture (so, 64-bit on x86_64 and 32-bit on x86_32,
  146. for example).
  147. Example
  148. +++++++
  149. .. code-block:: json
  150. {
  151. "name": "GDExtensionStringNamePtr",
  152. "kind": "handle"
  153. }
  154. Aliases
  155. ~~~~~~~
  156. Aliases are alternative names for a type. In C, they could be represented as a ``typedef``.
  157. They have only one additional key:
  158. - ``type``: The type the alias is an alternative name for. It may include modifiers as described above.
  159. These should be represented using the same C type as the type they refer to.
  160. Example
  161. +++++++
  162. .. code-block:: json
  163. {
  164. "name": "GDExtensionInt",
  165. "kind": "alias",
  166. "type": "int64_t"
  167. }
  168. Structs
  169. ~~~~~~~
  170. Structs represent C ``struct``\ s (aka a block of memory made up of the given members in order), and should
  171. follow all the same layout and alignment rules as C structs.
  172. They have only one additional key:
  173. - ``members``: An array of objects which have a ``name``, ``type`` (which may include modifiers), and
  174. ``description``.
  175. Example
  176. +++++++
  177. .. code-block:: json
  178. {
  179. "name": "GDExtensionCallError",
  180. "kind": "struct",
  181. "members": [
  182. {
  183. "name": "error",
  184. "type": "GDExtensionCallErrorType"
  185. },
  186. {
  187. "name": "argument",
  188. "type": "int32_t"
  189. },
  190. {
  191. "name": "expected",
  192. "type": "int32_t"
  193. }
  194. ]
  195. }
  196. Functions
  197. ~~~~~~~~~
  198. Functions represent C function pointer types, with a list of arguments and a return type, and should
  199. follow the same size and alignment requirements as C function pointers.
  200. They have the following members:
  201. - ``return_value``: An object which has a ``type`` (which may include modifiers) and ``description``.
  202. If the function has no return value, this will be omitted.
  203. - ``arguments`` (required): An array of function arguments which each has a ``type`` (which may include modifiers),
  204. ``name``, and ``description``.
  205. Example
  206. +++++++
  207. .. code-block:: json
  208. {
  209. "name": "GDExtensionPtrConstructor",
  210. "kind": "function",
  211. "arguments": [
  212. {
  213. "name": "p_base",
  214. "type": "GDExtensionUninitializedTypePtr"
  215. },
  216. {
  217. "name": "p_args",
  218. "type": "const GDExtensionConstTypePtr*"
  219. }
  220. ]
  221. }
  222. Interface
  223. ---------
  224. The ``interface`` section of the JSON file is the list of interface functions, which can be loaded
  225. by ``name`` using the ``GDExtensionInterfaceGetProcAddress`` function pointer, which is
  226. passed to all GDExtensions when they are loaded.
  227. Interface functions have some of the same keys as types, including ``name`` (required),
  228. ``deprecated``, and ``description``.
  229. And they also have ``return_value`` and ``arguments`` (required) that have the same format
  230. as the equivalent keys on function types (as described in the previous section).
  231. There are only a handful of unique keys:
  232. - ``since`` (required): The Godot version that introduced this interface function.
  233. - ``see``: An array of strings describing external references with more information, for example,
  234. names of classes or functions in the Godot source code, or URLs pointing to documentation.
  235. - ``legacy_type_name``: The legacy name used for the function pointer type in the header generated
  236. by Godot, when the legacy name doesn't match the pattern used for these type names. This field
  237. only exists so that we can generate the header in a way that is backwards compatible with the
  238. header from Godot 4.5 or earlier, and it shouldn't be used unless you also need to maintain
  239. compatibility with the old header.
  240. Example
  241. ~~~~~~~
  242. .. code-block:: json
  243. {
  244. "name": "get_godot_version",
  245. "arguments": [
  246. {
  247. "name": "r_godot_version",
  248. "type": "GDExtensionGodotVersion*",
  249. "description": [
  250. "A pointer to the structure to write the version information into."
  251. ]
  252. }
  253. ],
  254. "description": [
  255. "Gets the Godot version that the GDExtension was loaded into."
  256. ],
  257. "since": "4.1",
  258. "deprecated": {
  259. "since": "4.5",
  260. "replace_with": "get_godot_version2"
  261. }
  262. }