Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Dec 30, 2024
1 parent 5524e38 commit 1c202de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/godot/builtins_pyx/conversion.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ cdef object gd_variant_steal_into_pyobj(const gd_variant_t *gdvar):
cdef inline object _gd_variant_steal_into_pyobj_{{ builtin.cy_type }}(const gd_variant_t *gdvar):
cdef {{ builtin.cy_type }} ret = {{ builtin.cy_type }}.__new__({{ builtin.cy_type }})
ret._gd_data = {{ builtin.c_name_prefix }}_from_variant(<gd_variant_t *>gdvar)
gd_variant_del(gdvar)
return ret
{% endfor %}

Expand All @@ -111,6 +112,7 @@ cdef object gd_variant_copy_into_pyobj(const gd_variant_t *gdvar):
#########################################################################


# TODO rename given stealing is not possible with GDExtension C++ orientated API
cdef bint gd_variant_steal_from_pyobj(object pyobj, gd_variant_t *gdvar):
if pyobj is None:
pythonscript_gdextension.variant_new_nil(gdvar)
Expand Down Expand Up @@ -138,5 +140,6 @@ cdef bint gd_variant_steal_from_pyobj(object pyobj, gd_variant_t *gdvar):
cdef inline void _gd_variant_steal_from_pyobj_pystr(object pyobj, gd_variant_t *gdvar):
cdef gd_string_t gdstr = gd_string_from_unchecked_pystr(pyobj)
gdvar[0] = gd_string_into_variant(&gdstr)
gd_string_del(&gdstr)
# into conversion steals the ownership, so don't need to call `gd_string_del(gdstr)`
{% endmacro %}
1 change: 1 addition & 0 deletions src/godot/classes.pxd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ cdef class {{ cls.cy_type }}({{ cls.inherits.cy_type if cls.inherits else "" }})

cdef object _load_class(str name)
cdef object _load_singleton(str name)
cdef void _cleanup_loaded_classes_and_singletons()
cdef object _object_call(gd_object_t obj, str meth, args)
14 changes: 12 additions & 2 deletions src/godot/classes.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ cdef object _loaded_singletons = {}
cdef object _loaded_classes = {}


cdef void _cleanup_loaded_classes_and_singletons():
_loaded_singletons.clear()
_loaded_classes.clear()


cdef object _load_singleton(str name):
try:
return _loaded_singletons[name]
Expand Down Expand Up @@ -186,6 +191,7 @@ cdef object _load_class(str name):


cdef object _object_call(GDExtensionObjectPtr obj, str meth, args):
cdef object pyret
cdef gd_variant_t ret
cdef GDExtensionCallError call_error

Expand All @@ -206,6 +212,8 @@ cdef object _object_call(GDExtensionObjectPtr obj, str meth, args):
# TODO: provide a helper for string name from Python str creation
cdef gd_string_name_t meth_gdstrname = gd_string_name_from_unchecked_pystr(meth)
variant_args[0] = gd_string_name_into_variant(&meth_gdstrname)
gd_string_name_del(&meth_gdstrname)
# TODO: rename !
# Into conversion steals the owneship, so no need to delete meth_gdstrname

for i, arg in enumerate(args, 1):
Expand All @@ -222,10 +230,12 @@ cdef object _object_call(GDExtensionObjectPtr obj, str meth, args):
&ret,
&call_error,
)
gd_variant_del(&variant_args[0]) # Only param we created without stealing ownership
for i in range(args_with_meth_len):
gd_variant_del(&variant_args[i])
# gd_variant_del(&variant_args[0]) # Only param we created without stealing ownership
if call_error.error == GDEXTENSION_CALL_OK:
return gd_variant_steal_into_pyobj(&ret)
# No need to destroy ret given the conversion has stolen ownership on data !
return gd_variant_steal_into_pyobj(&ret)

# TODO: improve ret error raised exception type ?
elif call_error.error == GDEXTENSION_CALL_ERROR_INVALID_METHOD:
Expand Down

0 comments on commit 1c202de

Please sign in to comment.