From 59bcc2888c0c6002428ed1040ef6b36957a80e98 Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Sun, 28 Jan 2024 15:16:09 +0100 Subject: [PATCH] Add methods to get argument count of methods Added to: * `Callable`s * `Object`s * `ClassDB` * `Script(Instance)`s --- core/core_bind.cpp | 6 + core/core_bind.h | 2 + core/extension/gdextension_interface.cpp | 56 +++++++ core/extension/gdextension_interface.h | 51 ++++++- core/object/callable_method_pointer.h | 25 ++++ core/object/class_db.cpp | 25 ++++ core/object/class_db.h | 1 + core/object/object.cpp | 55 +++++++ core/object/object.h | 2 + core/object/script_instance.cpp | 22 +++ core/object/script_instance.h | 2 + core/object/script_language.cpp | 16 ++ core/object/script_language.h | 9 ++ core/object/script_language_extension.cpp | 3 + core/object/script_language_extension.h | 18 +++ core/variant/callable.cpp | 19 +++ core/variant/callable.h | 2 + core/variant/callable_bind.cpp | 16 ++ core/variant/callable_bind.h | 2 + core/variant/variant_call.cpp | 5 + core/variant/variant_callable.cpp | 9 ++ core/variant/variant_callable.h | 1 + doc/classes/Callable.xml | 6 + doc/classes/ClassDB.xml | 9 ++ doc/classes/Object.xml | 8 + doc/classes/ScriptExtension.xml | 6 + modules/gdscript/gdscript.cpp | 34 +++++ modules/gdscript/gdscript.h | 6 + modules/gdscript/gdscript_function.h | 1 + modules/gdscript/gdscript_lambda_callable.cpp | 18 +++ modules/gdscript/gdscript_lambda_callable.h | 2 + modules/gdscript/gdscript_rpc_callable.cpp | 4 + modules/gdscript/gdscript_rpc_callable.h | 1 + .../gdscript/gdscript_utility_callable.cpp | 15 ++ modules/gdscript/gdscript_utility_callable.h | 1 + .../gdscript/gdscript_utility_functions.cpp | 2 +- modules/gdscript/gdscript_utility_functions.h | 2 +- .../runtime/features/argument_count.gd | 102 +++++++++++++ .../runtime/features/argument_count.out | 27 ++++ modules/mono/csharp_script.cpp | 51 +++++++ modules/mono/csharp_script.h | 2 + .../Core/Bridge/ManagedCallbacks.cs | 2 + .../GodotSharp/Core/DelegateUtils.cs | 23 +++ modules/mono/managed_callable.cpp | 4 + modules/mono/managed_callable.h | 1 + modules/mono/mono_gd/gd_mono_cache.cpp | 1 + modules/mono/mono_gd/gd_mono_cache.h | 2 + tests/core/object/test_object.h | 6 + tests/core/variant/test_callable.h | 140 ++++++++++++++++++ tests/test_main.cpp | 1 + 50 files changed, 821 insertions(+), 3 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/features/argument_count.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/argument_count.out create mode 100644 tests/core/variant/test_callable.h diff --git a/core/core_bind.cpp b/core/core_bind.cpp index a0df1b62403d..6927db002bd2 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1434,6 +1434,10 @@ bool ClassDB::class_has_method(const StringName &p_class, const StringName &p_me return ::ClassDB::has_method(p_class, p_method, p_no_inheritance); } +int ClassDB::class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const { + return ::ClassDB::get_method_argument_count(p_class, p_method, nullptr, p_no_inheritance); +} + TypedArray ClassDB::class_get_method_list(const StringName &p_class, bool p_no_inheritance) const { List methods; ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance); @@ -1562,6 +1566,8 @@ void ClassDB::_bind_methods() { ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::class_has_method, DEFVAL(false)); + ::ClassDB::bind_method(D_METHOD("class_get_method_argument_count", "class", "method", "no_inheritance"), &ClassDB::class_get_method_argument_count, DEFVAL(false)); + ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::class_get_method_list, DEFVAL(false)); ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::class_get_integer_constant_list, DEFVAL(false)); diff --git a/core/core_bind.h b/core/core_bind.h index 64ab4dd7e242..305f616cef5e 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -447,6 +447,8 @@ public: bool class_has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false) const; + int class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false) const; + TypedArray class_get_method_list(const StringName &p_class, bool p_no_inheritance = false) const; PackedStringArray class_get_integer_constant_list(const StringName &p_class, bool p_no_inheritance = false) const; diff --git a/core/extension/gdextension_interface.cpp b/core/extension/gdextension_interface.cpp index 67ec09d76457..ca58d589bd9d 100644 --- a/core/extension/gdextension_interface.cpp +++ b/core/extension/gdextension_interface.cpp @@ -59,6 +59,8 @@ class CallableCustomExtension : public CallableCustom { GDExtensionCallableCustomToString to_string_func; + GDExtensionCallableCustomGetArgumentCount get_argument_count_func; + uint32_t _hash; static bool default_compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { @@ -143,6 +145,21 @@ public: return object; } + int get_argument_count(bool &r_is_valid) const override { + if (get_argument_count_func != nullptr) { + GDExtensionBool is_valid = false; + + GDExtensionInt ret = get_argument_count_func(userdata, &is_valid); + + if (is_valid) { + r_is_valid = true; + return ret; + } + } + r_is_valid = false; + return 0; + } + void *get_userdata(void *p_token) const { return (p_token == token) ? userdata : nullptr; } @@ -157,6 +174,7 @@ public: r_call_error.expected = error.expected; } +#ifndef DISABLE_DEPRECATED CallableCustomExtension(GDExtensionCallableCustomInfo *p_info) { userdata = p_info->callable_userdata; token = p_info->token; @@ -172,6 +190,35 @@ public: to_string_func = p_info->to_string_func; + get_argument_count_func = nullptr; + + // Pre-calculate the hash. + if (p_info->hash_func != nullptr) { + _hash = p_info->hash_func(userdata); + } else { + _hash = hash_murmur3_one_64((uint64_t)call_func); + _hash = hash_murmur3_one_64((uint64_t)userdata, _hash); + } + } +#endif + + CallableCustomExtension(GDExtensionCallableCustomInfo2 *p_info) { + userdata = p_info->callable_userdata; + token = p_info->token; + + object = p_info->object_id; + + call_func = p_info->call_func; + is_valid_func = p_info->is_valid_func; + free_func = p_info->free_func; + + equal_func = p_info->equal_func; + less_than_func = p_info->less_than_func; + + to_string_func = p_info->to_string_func; + + get_argument_count_func = p_info->get_argument_count_func; + // Pre-calculate the hash. if (p_info->hash_func != nullptr) { _hash = p_info->hash_func(userdata); @@ -1378,9 +1425,15 @@ static GDExtensionScriptInstancePtr gdextension_object_get_script_instance(GDExt return script_instance_extension->instance; } +#ifndef DISABLE_DEPRECATED static void gdextension_callable_custom_create(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_custom_callable_info) { memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info)))); } +#endif + +static void gdextension_callable_custom_create2(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo2 *p_custom_callable_info) { + memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info)))); +} static void *gdextension_callable_custom_get_userdata(GDExtensionTypePtr p_callable, void *p_token) { const Callable &callable = *reinterpret_cast(p_callable); @@ -1595,7 +1648,10 @@ void gdextension_setup_interface() { REGISTER_INTERFACE_FUNC(placeholder_script_instance_create); REGISTER_INTERFACE_FUNC(placeholder_script_instance_update); REGISTER_INTERFACE_FUNC(object_get_script_instance); +#ifndef DISABLE_DEPRECATED REGISTER_INTERFACE_FUNC(callable_custom_create); +#endif // DISABLE_DEPRECATED + REGISTER_INTERFACE_FUNC(callable_custom_create2); REGISTER_INTERFACE_FUNC(callable_custom_get_userdata); REGISTER_INTERFACE_FUNC(classdb_construct_object); REGISTER_INTERFACE_FUNC(classdb_get_method_bind); diff --git a/core/extension/gdextension_interface.h b/core/extension/gdextension_interface.h index e7497a9d4c93..c8635070197d 100644 --- a/core/extension/gdextension_interface.h +++ b/core/extension/gdextension_interface.h @@ -442,6 +442,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, GDExtensionBool *r_is_valid); + typedef struct { /* Only `call_func` and `token` are strictly required, however, `object_id` should be passed if its not a static method. * @@ -471,7 +473,40 @@ typedef struct { GDExtensionCallableCustomLessThan less_than_func; GDExtensionCallableCustomToString to_string_func; -} GDExtensionCallableCustomInfo; +} GDExtensionCallableCustomInfo; // Deprecated. Use GDExtensionCallableCustomInfo2 instead. + +typedef struct { + /* Only `call_func` and `token` are strictly required, however, `object_id` should be passed if its not a static method. + * + * `token` should point to an address that uniquely identifies the GDExtension (for example, the + * `GDExtensionClassLibraryPtr` passed to the entry symbol function. + * + * `hash_func`, `equal_func`, and `less_than_func` are optional. If not provided both `call_func` and + * `callable_userdata` together are used as the identity of the callable for hashing and comparison purposes. + * + * The hash returned by `hash_func` is cached, `hash_func` will not be called more than once per callable. + * + * `is_valid_func` is necessary if the validity of the callable can change before destruction. + * + * `free_func` is necessary if `callable_userdata` needs to be cleaned up when the callable is freed. + */ + void *callable_userdata; + void *token; + + GDObjectInstanceID object_id; + + GDExtensionCallableCustomCall call_func; + GDExtensionCallableCustomIsValid is_valid_func; + GDExtensionCallableCustomFree free_func; + + GDExtensionCallableCustomHash hash_func; + GDExtensionCallableCustomEqual equal_func; + GDExtensionCallableCustomLessThan less_than_func; + + GDExtensionCallableCustomToString to_string_func; + + GDExtensionCallableCustomGetArgumentCount get_argument_count_func; +} GDExtensionCallableCustomInfo2; /* SCRIPT INSTANCE EXTENSION */ @@ -2510,6 +2545,7 @@ typedef GDExtensionScriptInstanceDataPtr (*GDExtensionInterfaceObjectGetScriptIn /** * @name callable_custom_create * @since 4.2 + * @deprecated in Godot 4.3. Use `callable_custom_create2` instead. * * Creates a custom Callable object from a function pointer. * @@ -2520,6 +2556,19 @@ typedef GDExtensionScriptInstanceDataPtr (*GDExtensionInterfaceObjectGetScriptIn */ typedef void (*GDExtensionInterfaceCallableCustomCreate)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_callable_custom_info); +/** + * @name callable_custom_create2 + * @since 4.3 + * + * Creates a custom Callable object from a function pointer. + * + * Provided struct can be safely freed once the function returns. + * + * @param r_callable A pointer that will receive the new Callable. + * @param p_callable_custom_info The info required to construct a Callable. + */ +typedef void (*GDExtensionInterfaceCallableCustomCreate2)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo2 *p_callable_custom_info); + /** * @name callable_custom_get_userdata * @since 4.2 diff --git a/core/object/callable_method_pointer.h b/core/object/callable_method_pointer.h index f8e8c4d7e904..09fe9679f733 100644 --- a/core/object/callable_method_pointer.h +++ b/core/object/callable_method_pointer.h @@ -93,6 +93,11 @@ public: return data.instance->get_instance_id(); } + virtual int get_argument_count(bool &r_is_valid) const { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error); @@ -140,6 +145,11 @@ public: return data.instance->get_instance_id(); } + virtual int get_argument_count(bool &r_is_valid) const { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); @@ -187,6 +197,11 @@ public: return data.instance->get_instance_id(); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override { ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method."); call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error); @@ -238,6 +253,11 @@ public: return ObjectID(); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &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(); @@ -280,6 +300,11 @@ public: return ObjectID(); } + virtual int get_argument_count(bool &r_is_valid) const override { + r_is_valid = true; + return sizeof...(P); + } + virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override { call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error); } diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 231a8e4d686e..80a2703c2fae 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1666,6 +1666,31 @@ bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, return false; } +int ClassDB::get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid, bool p_no_inheritance) { + OBJTYPE_RLOCK; + + ClassInfo *type = classes.getptr(p_class); + + while (type) { + MethodBind **method = type->method_map.getptr(p_method); + if (method && *method) { + if (r_is_valid) { + *r_is_valid = true; + } + return (*method)->get_argument_count(); + } + if (p_no_inheritance) { + break; + } + type = type->inherits_ptr; + } + + if (r_is_valid) { + *r_is_valid = false; + } + return 0; +} + void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) { _bind_method_custom(p_class, p_method, false); } diff --git a/core/object/class_db.h b/core/object/class_db.h index 7f117b4a9bb3..3b146dd06eab 100644 --- a/core/object/class_db.h +++ b/core/object/class_db.h @@ -430,6 +430,7 @@ public: static void get_method_list(const StringName &p_class, List *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false); static void get_method_list_with_compatibility(const StringName &p_class, List> *p_methods_with_hash, bool p_no_inheritance = false, bool p_exclude_from_properties = false); static bool get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance = false, bool p_exclude_from_properties = false); + static int get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid = nullptr, bool p_no_inheritance = false); static MethodBind *get_method(const StringName &p_class, const StringName &p_name); static MethodBind *get_method_with_compatibility(const StringName &p_class, const StringName &p_name, uint64_t p_hash, bool *r_method_exists = nullptr, bool *r_is_deprecated = nullptr); static Vector get_method_compatibility_hashes(const StringName &p_class, const StringName &p_name); diff --git a/core/object/object.cpp b/core/object/object.cpp index 6a5a9efefa8d..e5d771844b1b 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -688,6 +688,59 @@ bool Object::has_method(const StringName &p_method) const { return false; } +int Object::_get_method_argument_count_bind(const StringName &p_method) const { + return get_method_argument_count(p_method); +} + +int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const { + if (p_method == CoreStringNames::get_singleton()->_free) { + if (r_is_valid) { + *r_is_valid = true; + } + return 0; + } + + if (script_instance) { + bool valid = false; + int ret = script_instance->get_method_argument_count(p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + } + + { + bool valid = false; + int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid); + if (valid) { + if (r_is_valid) { + *r_is_valid = true; + } + return ret; + } + } + + const Script *scr = Object::cast_to