static VALUE
umethod_bind(VALUE method, VALUE recv)
{
struct METHOD *data, *bound;
VALUE methclass, klass;
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
methclass = data->me->owner;
if (!RB_TYPE_P(methclass, T_MODULE) &&
methclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, methclass)) {
if (FL_TEST(methclass, FL_SINGLETON)) {
rb_raise(rb_eTypeError,
"singleton method called for a different object");
}
else {
rb_raise(rb_eTypeError, "bind argument must be an instance of % "PRIsVALUE,
rb_class_name(methclass));
}
}
klass = CLASS_OF(recv);
method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
RB_OBJ_WRITE(method, &bound->recv, recv);
RB_OBJ_WRITE(method, &bound->klass, data->klass);
RB_OBJ_WRITE(method, &bound->me, rb_method_entry_clone(data->me));
if (RB_TYPE_P(bound->me->owner, T_MODULE)) {
VALUE ic = rb_class_search_ancestor(klass, bound->me->owner);
if (ic) {
klass = ic;
}
else {
klass = rb_include_class_new(methclass, klass);
}
RB_OBJ_WRITE(method, &bound->me, rb_method_entry_complement_defined_class(bound->me, bound->me->called_id, klass));
}
return method;
}