Skip to content

Commit 3c51eaf

Browse files
committed
Move builtins methods as inlined into .pxd
1 parent ff1d4a5 commit 3c51eaf

File tree

5 files changed

+65
-62
lines changed

5 files changed

+65
-62
lines changed

scripts/extension_api_parser/type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def register_classes_in_types_db(classes: Iterable["ClassSpec"]) -> None:
386386
gdapi_type=spec.original_name,
387387
py_type=spec.name,
388388
c_type="GDNativeObjectPtr",
389-
cy_type="GDNativeObjectPtr",
389+
cy_type="object",
390390
_variant_type_name="GDNATIVE_VARIANT_TYPE_OBJECT",
391391
)
392392
TYPES_DB[ts.gdapi_type] = ts

src/godot/builtins.macros.pxd.j2

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{% macro render_method_signature(spec, m) -%}
2+
{{ "void" if m.return_type.is_nil else m.return_type.cy_type }} {{ m.name }}(self
3+
{%- for arg in m.arguments -%}
4+
, {{ arg.type.cy_type }} {{ arg.name }}
5+
{%- endfor -%})
6+
{%- endmacro %}
7+
8+
9+
{% macro render_method(spec, m) %}
10+
cpdef inline {{ render_method_signature(spec, m) }}:
11+
{% if m.contains_unsuported_types %}
12+
raise NotImplementedError # TODO
13+
{% else %}
14+
{# Return type #}
15+
{% if m.return_type.is_nil %}
16+
{% elif m.return_type.is_scalar %}
17+
cdef {{ m.return_type.cy_type }} ret = 0
18+
{% elif m.return_type.is_builtin %}
19+
# Call to __new__ bypasses __init__ constructor
20+
cdef {{ m.return_type.cy_type }} ret = {{ m.return_type.cy_type }}.__new__({{ m.return_type.cy_type }})
21+
{% endif %}
22+
{# Arguments #}
23+
{% if (m.arguments | length) != 0 %}
24+
cdef GDNativeTypePtr[{{ m.arguments | length }}] args = [
25+
{% for arg in m.arguments %}
26+
{% if arg.type.is_scalar %}
27+
&{{ arg.name }},
28+
{% else %}
29+
&{{ arg.name }}._gd_data,
30+
{% endif %}
31+
{% endfor %}
32+
]
33+
{% endif %}
34+
{# Actual call ! #}
35+
__{{ spec.name }}_meth_{{ m.name }}(
36+
{# GDNativeTypePtr p_base #}
37+
&self._gd_data,
38+
{# const GDNativeTypePtr *p_args #}
39+
{{ "NULL" if (m.arguments | length) == 0 else "args" }},
40+
{# GDNativeTypePtr r_return #}
41+
{% if m.return_type.is_nil %}
42+
NULL,
43+
{% elif m.return_type.is_scalar %}
44+
&ret,
45+
{% elif m.return_type.is_builtin %}
46+
&ret._gd_data,
47+
{% endif %}
48+
{# int p_argument_count #}
49+
{{ m.arguments | length }},
50+
)
51+
{% if not m.return_type.is_nil %}
52+
return ret
53+
{% endif %}
54+
{% endif %}
55+
{% endmacro %}

src/godot/builtins.pxd.j2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# /!\ Autogenerated code, modifications will be lost /!\
2-
# see `scripts/generate_tmpl.py`
3-
1+
{%- from 'builtins.macros.pxd.j2' import render_method -%}
42
cimport cython
3+
54
cdef extern from "Python.h":
65
const char* PyUnicode_AsUTF8AndSize(object, size_t*)
76
object PyUnicode_DecodeUTF32(const char *s, size_t size, const char *errors, int *byteorder)
@@ -47,4 +46,7 @@ cdef class {{ spec.name }}:
4746
cdef inline gd_string_t to_pystr(self):
4847
return gdstring_to_pystr(&self._gd_data)
4948
{% endif %}
49+
{% for m in spec.methods %}
50+
{{ render_method(spec, m) | indent }}
51+
{% endfor %}
5052
{% endfor %}

src/godot/builtins_pyx/class.pxi.j2

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -121,59 +121,6 @@ def set_{{ m.name }}(self, {{ m.type.cy_type }} val not None):
121121
{% endmacro %}
122122

123123

124-
{#########################################################################
125-
# Render method
126-
#########################################################################}
127-
128-
{% macro render_method(spec, m) %}
129-
def {{ m.name }}(self, {% for arg in m.arguments %}{{ arg.type.cy_type }} {{ arg.name }}, {% endfor %}): # -> {{ "None" if m.return_type.is_nil else m.return_type.py_type }}
130-
{% if m.contains_unsuported_types %}
131-
raise NotImplementedError
132-
{% else %}
133-
{# Return type #}
134-
{% if m.return_type.is_nil %}
135-
{% elif m.return_type.is_scalar %}
136-
cdef {{ m.return_type.cy_type }} ret = 0
137-
{% elif m.return_type.is_builtin %}
138-
# Call to __new__ bypasses __init__ constructor
139-
cdef {{ m.return_type.cy_type }} ret = {{ m.return_type.cy_type }}.__new__({{ m.return_type.cy_type }})
140-
{% endif %}
141-
{# Arguments #}
142-
{% if (m.arguments | length) != 0 %}
143-
cdef GDNativeTypePtr[{{ m.arguments | length }}] args = [
144-
{% for arg in m.arguments %}
145-
{% if arg.type.is_scalar %}
146-
&{{ arg.name }},
147-
{% else %}
148-
&{{ arg.name }}._gd_data,
149-
{% endif %}
150-
{% endfor %}
151-
]
152-
{% endif %}
153-
{# Actual call ! #}
154-
__{{ spec.name }}_meth_{{ m.name }}(
155-
{# GDNativeTypePtr p_base #}
156-
&self._gd_data,
157-
{# const GDNativeTypePtr *p_args #}
158-
{{ "NULL" if (m.arguments | length) == 0 else "args" }},
159-
{# GDNativeTypePtr r_return #}
160-
{% if m.return_type.is_nil %}
161-
NULL,
162-
{% elif m.return_type.is_scalar %}
163-
&ret,
164-
{% elif m.return_type.is_builtin %}
165-
&ret._gd_data,
166-
{% endif %}
167-
{# int p_argument_count #}
168-
{{ m.arguments | length }},
169-
)
170-
{% if not m.return_type.is_nil %}
171-
return ret
172-
{% endif %}
173-
{% endif %}
174-
{% endmacro %}
175-
176-
177124
{#########################################################################
178125
# Render full class spec
179126
#########################################################################}
@@ -224,8 +171,4 @@ cdef class {{ spec.name }}:
224171
# Methods
225172

226173
{% endif %}
227-
{% for m in spec.methods %}
228-
{{ render_method(spec, m) | indent }}
229-
{% endfor %}
230-
231174
{%- endmacro %}

src/meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ pxd_godot_builtins = custom_target(
8989
output: 'godot#builtins.pxd',
9090
input: [
9191
generate_tmpl_base_input,
92-
files('godot/builtins.pxd.j2'),
92+
files(
93+
'godot/builtins.pxd.j2',
94+
'godot/builtins.macros.pxd.j2',
95+
),
9396
],
9497
command: generate_tmpl_cmd,
9598
)

0 commit comments

Comments
 (0)