Skip to content

Commit fa95e89

Browse files
committed
Improve gdapi.pxd & rename templates in src/godot/_builtins_pyx for better syntax highlighting
1 parent 531b15a commit fa95e89

File tree

8 files changed

+60
-21
lines changed

8 files changed

+60
-21
lines changed

scripts/extension_api_parser/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Dict
1+
from typing import List, Dict, Tuple
22
from dataclasses import dataclass
33
from pathlib import Path
44
import json
@@ -57,7 +57,7 @@ class UtilityFunctionSpec:
5757
category: str
5858
is_vararg: bool
5959
hash: int
60-
arguments: Dict[str, TypeInUse]
60+
arguments: List[Tuple[str, TypeInUse]]
6161

6262
@classmethod
6363
def parse(cls, item: dict) -> "UtilityFunctionSpec":
@@ -72,7 +72,7 @@ def parse(cls, item: dict) -> "UtilityFunctionSpec":
7272
category=item["category"],
7373
is_vararg=item["is_vararg"],
7474
hash=item["hash"],
75-
arguments={x["name"]: TypeInUse(x["type"]) for x in item["arguments"]},
75+
arguments=[(correct_name(x["name"]), TypeInUse(x["type"])) for x in item["arguments"]],
7676
)
7777

7878

src/godot/_builtins.pyx.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- from '_builtins_pyx/class.j2' import render_spec with context -%}
1+
{%- from '_builtins_pyx/class.pxi.j2' import render_spec with context -%}
22
# /!\ Autogenerated code, modifications will be lost /!\
33
# see `scripts/generate_tmpl.py`
44

src/godot/_builtins_pyx/class.j2 renamed to src/godot/_builtins_pyx/class.pxi.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{%- from '_builtins_pyx/constructor.j2' import render_constructor with context -%}
2-
{%- from '_builtins_pyx/gdapi.j2' import render_gdapi with context -%}
1+
{%- from '_builtins_pyx/constructor.pxi.j2' import render_constructor with context -%}
2+
{%- from '_builtins_pyx/gdapi.pxi.j2' import render_gdapi with context -%}
33

44
{% macro render_spec(spec) -%}
55

src/godot/_hazmat/gdapi.pxd.j2

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,47 @@ cdef extern from * nogil:
2020
cdef const GDNativeInterface *pythonscript_gdapi
2121

2222

23-
# Following stuff are all the Godot builtins exposed as:
24-
# - a C structure (with typed fields if the structure is not opaque)
25-
# - the function pointer of all the builtin's constructors/destructors/methods/operators
26-
# - Inlines functions for a nicer way to call the constructors/destructors/methods/operators
23+
# The following is pretty verbose, here is the tl;dr:
24+
# - Utility functions exposed as both function pointer and nicer inline function
25+
# - Builtins as C structure (with typed fields if the structure is not opaque)
26+
# - Builtins' constructors/destructor/methods/operators exposed as function pointer
27+
# and nicer inline function
2728

2829

30+
##############################################################################
31+
# Utility functions #
32+
##############################################################################
33+
{% for spec in api["utility_functions"] %}
34+
35+
# {{ spec.name }}
36+
cdef GDNativePtrUtilityFunction __utility_{{ spec.name }}
37+
cdef inline {{ "void" if spec.return_type.is_nil else spec.return_type.c_type }} {{ spec.name }}(
38+
{% for arg_name, arg_type in spec.arguments %}
39+
{{ arg_type.c_type }}* {{ arg_name }},
40+
{% endfor %}
41+
):
42+
cdef GDNativeTypePtr[{{ spec.arguments | length }}] p_args = [{% for arg_name, _ in spec.arguments %}{{ arg_name }},{% endfor %}]
43+
{% if not spec.return_type.is_nil %}
44+
cdef {{ spec.return_type.c_type }} r_return
45+
{% endif %}
46+
__utility_{{ spec.name }}(
47+
{# GDNativeTypePtr r_return #}
48+
{{ "NULL" if spec.return_type.is_nil else "&r_return" }},
49+
{# const GDNativeTypePtr *p_arguments #}
50+
p_args,
51+
{# int p_argument_count #}
52+
{{ spec.arguments | length }}
53+
)
54+
{% if not spec.return_type.is_nil %}
55+
return r_return
56+
{% endif %}
57+
{% endfor %}
58+
59+
60+
##############################################################################
61+
# Builtins #
62+
##############################################################################
63+
2964
cdef struct Variant:
3065
char _gd_opaque[{{ api.variant_size }}]
3166

src/godot/_hazmat/gdapi_ptrs.pxi.j2

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
from .gdnative_interface cimport *
55

66

7-
{% macro render_arg(arg) -%}
8-
{{arg.name}}: {{ arg.type.py_type if arg.type.py_type != "GDString" else "GDString | str"}}{{ "={}".format(arg.default_value.value) if arg.default_value is not none }}
9-
{%- endmacro %}
10-
117
# Builtins
128
{% for spec in api["builtins"] if not spec.is_scalar %}
139

14-
# {{ spec.name }}
10+
##############################################################################
11+
#{{ "{:^76}".format(spec.name) }}#
12+
##############################################################################
1513

1614
{% for c in spec.constructors %}
1715
cdef GDNativePtrConstructor __{{ spec.name }}_constructor_{{ c.index }} = pythonscript_gdapi.variant_get_ptr_constructor(
@@ -39,12 +37,18 @@ cdef GDNativePtrOperatorEvaluator __{{ spec.name }}_op_{{ o.name }} = pythonscri
3937

4038
{% endfor %}
4139

42-
# Utility functions
43-
{% for spec in api["utility_functions"] %}
40+
##############################################################################
41+
# Utility functions #
42+
##############################################################################
4443

44+
{% for spec in api["utility_functions"] %}
45+
# {{ spec.name }}
46+
cdef GDNativePtrUtilityFunction __utility_{{ spec.name }} = pythonscript_gdapi.variant_get_ptr_utility_function("{{ spec.original_name }}", {{ spec.hash }})
4547
{% endfor %}
4648

47-
# Classes
49+
##############################################################################
50+
# Classes #
51+
##############################################################################
4852
{# TODO: do lazy function pointer initialization ? #}
4953
{% for spec in api["classes"] %}
5054

src/godot/meson.build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ pyx_builtins = custom_target(
6767
generate_tmpl_base_input,
6868
files(
6969
'_builtins.pyx.j2',
70-
'_builtins_pyx/class.j2',
71-
'_builtins_pyx/constructor.j2',
72-
'_builtins_pyx/gdapi.j2',
70+
'_builtins_pyx/class.pxi.j2',
71+
'_builtins_pyx/constructor.pxi.j2',
72+
'_builtins_pyx/gdapi.pxi.j2',
7373
),
7474
],
7575
command: generate_tmpl_cmd,

0 commit comments

Comments
 (0)