Skip to content

Commit

Permalink
Add support for getting argument count from Callables
Browse files Browse the repository at this point in the history
  • Loading branch information
AThousandShips committed Jan 28, 2024
1 parent 36847f6 commit 575e827
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
70 changes: 70 additions & 0 deletions gdextension/extension_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -18716,6 +18716,14 @@
"is_static": false,
"hash": 1825232092
},
{
"name": "get_argument_count",
"return_type": "int",
"is_vararg": false,
"is_const": true,
"is_static": false,
"hash": 3173160232
},
{
"name": "get_bound_arguments_count",
"return_type": "int",
Expand Down Expand Up @@ -61270,6 +61278,33 @@
}
]
},
{
"name": "class_get_method_argument_count",
"is_const": true,
"is_vararg": false,
"is_static": false,
"is_virtual": false,
"hash": 3885694822,
"return_value": {
"type": "int",
"meta": "int32"
},
"arguments": [
{
"name": "class",
"type": "StringName"
},
{
"name": "method",
"type": "StringName"
},
{
"name": "no_inheritance",
"type": "bool",
"default_value": "false"
}
]
},
{
"name": "class_get_method_list",
"is_const": true,
Expand Down Expand Up @@ -151474,6 +151509,24 @@
}
]
},
{
"name": "get_method_argument_count",
"is_const": true,
"is_vararg": false,
"is_static": false,
"is_virtual": false,
"hash": 2458036349,
"return_value": {
"type": "int",
"meta": "int32"
},
"arguments": [
{
"name": "method",
"type": "StringName"
}
]
},
{
"name": "has_signal",
"is_const": true,
Expand Down Expand Up @@ -215510,6 +215563,23 @@
}
]
},
{
"name": "_get_method_argument_count",
"is_const": true,
"is_static": false,
"is_vararg": false,
"is_virtual": true,
"return_value": {
"type": "int",
"meta": "int32"
},
"arguments": [
{
"name": "method",
"type": "StringName"
}
]
},
{
"name": "_get_method_info",
"is_const": true,
Expand Down
8 changes: 8 additions & 0 deletions gdextension/gdextension_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ typedef GDExtensionBool (*GDExtensionCallableCustomLessThan)(void *callable_user

typedef void (*GDExtensionCallableCustomToString)(void *callable_userdata, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out);

typedef GDExtensionInt (*GDExtensionCallableCustomGetArgumentCount)(void *callable_userdata);

typedef struct {
/* Only `call_func` and `token` are strictly required, however, `object_id` should be passed if its not a static method.
*
Expand Down Expand Up @@ -420,6 +422,8 @@ typedef struct {
GDExtensionCallableCustomLessThan less_than_func;

GDExtensionCallableCustomToString to_string_func;

GDExtensionCallableCustomGetArgumentCount get_argument_count_func;
} GDExtensionCallableCustomInfo;

/* SCRIPT INSTANCE EXTENSION */
Expand Down Expand Up @@ -447,6 +451,8 @@ typedef void (*GDExtensionScriptInstanceFreeMethodList)(GDExtensionScriptInstanc

typedef GDExtensionBool (*GDExtensionScriptInstanceHasMethod)(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name);

typedef GDExtensionInt (*GDExtensionScriptInstanceGetMethodArgumentCount)(GDExtensionScriptInstanceDataPtr p_instance, GDExtensionConstStringNamePtr p_name);

typedef void (*GDExtensionScriptInstanceCall)(GDExtensionScriptInstanceDataPtr p_self, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error);
typedef void (*GDExtensionScriptInstanceNotification)(GDExtensionScriptInstanceDataPtr p_instance, int32_t p_what); // Deprecated. Use GDExtensionScriptInstanceNotification2 instead.
typedef void (*GDExtensionScriptInstanceNotification2)(GDExtensionScriptInstanceDataPtr p_instance, int32_t p_what, GDExtensionBool p_reversed);
Expand Down Expand Up @@ -525,6 +531,8 @@ typedef struct {

GDExtensionScriptInstanceHasMethod has_method_func;

GDExtensionScriptInstanceGetMethodArgumentCount get_method_argument_count_func;

GDExtensionScriptInstanceCall call_func;
GDExtensionScriptInstanceNotification2 notification_func;

Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/variant/callable_custom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Object;
class CallableCustomBase {
public:
virtual ObjectID get_object() const = 0;
virtual int get_argument_count() const;
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const = 0;
virtual ~CallableCustomBase() {}
};
Expand Down
20 changes: 20 additions & 0 deletions include/godot_cpp/variant/callable_method_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
return ObjectID(data.instance->get_instance_id());
}

virtual int get_argument_count() const override {
return sizeof...(P);
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
}
Expand Down Expand Up @@ -110,6 +114,10 @@ class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
return ObjectID(data.instance->get_instance_id());
}

virtual int get_argument_count() const override {
return sizeof...(P);
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
}
Expand Down Expand Up @@ -147,6 +155,10 @@ class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
return ObjectID(data.instance->get_instance_id());
}

virtual int get_argument_count() const override {
return sizeof...(P);
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
}
Expand Down Expand Up @@ -182,6 +194,10 @@ class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase
return ObjectID();
}

virtual int get_argument_count() const override {
return sizeof...(P);
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
r_return_value = Variant();
Expand Down Expand Up @@ -218,6 +234,10 @@ class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerB
return ObjectID();
}

virtual int get_argument_count() const override {
return sizeof...(P);
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
}
Expand Down
10 changes: 10 additions & 0 deletions src/variant/callable_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

namespace godot {

int CallableCustomBase::get_argument_count() const {
return 0;
}

static void callable_custom_call(void *p_userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) {
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
callable_custom->call((const Variant **)p_args, p_argument_count, *(Variant *)r_return, *r_error);
Expand Down Expand Up @@ -84,6 +88,11 @@ static GDExtensionBool callable_custom_less_than_func(void *p_a, void *p_b) {
return func_a(a, b);
}

static GDExtensionInt custom_callable_get_argument_count_func(void *p_userdata) {
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
return callable_custom->get_argument_count();
}

bool CallableCustom::is_valid() const {
// The same default implementation as in Godot.
return ObjectDB::get_instance(get_object());
Expand All @@ -101,6 +110,7 @@ Callable::Callable(CallableCustom *p_callable_custom) {
info.equal_func = &callable_custom_equal_func;
info.less_than_func = &callable_custom_less_than_func;
info.to_string_func = &callable_custom_to_string;
info.get_argument_count_func = &custom_callable_get_argument_count_func;

::godot::internal::gdextension_interface_callable_custom_create(_native_ptr(), &info);
}
Expand Down
6 changes: 6 additions & 0 deletions src/variant/callable_method_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ static GDExtensionBool custom_callable_mp_less_than_func(void *p_a, void *p_b) {
return memcmp(a->get_comp_ptr(), b->get_comp_ptr(), a->get_comp_size() * 4) < 0;
}

static GDExtensionInt custom_callable_mp_get_argument_count_func(void *p_userdata) {
CallableCustomMethodPointerBase *callable_method_pointer = (CallableCustomMethodPointerBase *)p_userdata;
return callable_method_pointer->get_argument_count();
}

void CallableCustomMethodPointerBase::_setup(uint32_t *p_base_ptr, uint32_t p_ptr_size) {
comp_ptr = p_base_ptr;
comp_size = p_ptr_size / 4;
Expand All @@ -103,6 +108,7 @@ Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_m
info.hash_func = &custom_callable_mp_hash;
info.equal_func = &custom_callable_mp_equal_func;
info.less_than_func = &custom_callable_mp_less_than_func;
info.get_argument_count_func = &custom_callable_mp_get_argument_count_func;

Callable callable;
::godot::internal::gdextension_interface_callable_custom_create(callable._native_ptr(), &info);
Expand Down
6 changes: 6 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func _ready():
# mp_callable() with void method.
var mp_callable: Callable = example.test_callable_mp()
assert_equal(mp_callable.is_valid(), true)
assert_equal(mp_callable.get_argument_count(), 3)
mp_callable.call(example, "void", 36)
assert_equal(custom_signal_emitted, ["unbound_method1: Example - void", 36])

Expand All @@ -117,14 +118,17 @@ func _ready():

# mp_callable() with return value.
var mp_callable_ret: Callable = example.test_callable_mp_ret()
assert_equal(mp_callable_ret.get_argument_count(), 3)
assert_equal(mp_callable_ret.call(example, "test", 77), "unbound_method2: Example - test - 77")

# mp_callable() with const method and return value.
var mp_callable_retc: Callable = example.test_callable_mp_retc()
assert_equal(mp_callable_retc.get_argument_count(), 3)
assert_equal(mp_callable_retc.call(example, "const", 101), "unbound_method3: Example - const - 101")

# mp_callable_static() with void method.
var mp_callable_static: Callable = example.test_callable_mp_static()
assert_equal(mp_callable_static.get_argument_count(), 3)
mp_callable_static.call(example, "static", 83)
assert_equal(custom_signal_emitted, ["unbound_static_method1: Example - static", 83])

Expand All @@ -140,6 +144,7 @@ func _ready():

# mp_callable_static() with return value.
var mp_callable_static_ret: Callable = example.test_callable_mp_static_ret()
assert_equal(mp_callable_static_ret.get_argument_count(), 3)
assert_equal(mp_callable_static_ret.call(example, "static-ret", 84), "unbound_static_method2: Example - static-ret - 84")

# CallableCustom.
Expand All @@ -150,6 +155,7 @@ func _ready():
assert_equal(custom_callable.hash(), 27);
assert_equal(custom_callable.get_object(), null);
assert_equal(custom_callable.get_method(), "");
assert_equal(custom_callable.get_argument_count(), 2)
assert_equal(str(custom_callable), "<MyCallableCustom>");

# PackedArray iterators
Expand Down
4 changes: 4 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class MyCallableCustom : public CallableCustom {
return ObjectID();
}

virtual int get_argument_count() const {
return 2;
}

virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const {
r_return_value = "Hi";
r_call_error.error = GDEXTENSION_CALL_OK;
Expand Down

0 comments on commit 575e827

Please sign in to comment.