Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread start with no user data when target has no default argument #51093

Merged
merged 1 commit into from
Aug 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,36 @@ void _Thread::_start_func(void *ud) {
memdelete(tud);
Callable::CallError ce;
const Variant *arg[1] = { &t->userdata };
int argc = (int)(arg[0]->get_type() != Variant::NIL);
int argc = 0;
if (arg[0]->get_type() != Variant::NIL) {
// Just pass to the target function whatever came as user data
argc = 1;
} else {
// There are two cases of null user data:
// a) The target function has zero parameters and the caller is just honoring that.
// b) The target function has at least one parameter with no default and the caller is
// leveraging the fact that user data defaults to null in Thread.start().
// We care about the case of more than one parameter because, even if a thread
// function can have one at most, out mindset here is to do our best with the
// only/first one and let the call handle any other error conditions, like too
// much arguments.
// We must check if we are in case b).
int target_param_count = 0;
int target_default_arg_count = 0;
Ref<Script> script = t->target_instance->get_script();
if (script.is_valid()) {
MethodInfo mi = script->get_method_info(t->target_method);
target_param_count = mi.arguments.size();
target_default_arg_count = mi.default_arguments.size();
} else {
MethodBind *method = ClassDB::get_method(t->target_instance->get_class_name(), t->target_method);
target_param_count = method->get_argument_count();
target_default_arg_count = method->get_default_argument_count();
}
if (target_param_count >= 1 && target_default_arg_count == target_param_count) {
argc = 1;
}
}

Thread::set_name(t->target_method);

Expand Down