?????????????????????? Module#include ??????????????????????????????????????????????????? prepend_features ?????? prepended ???????????Module#prepend_features ??????????????????????????????
static VALUE
rb_mod_prepend_features(VALUE module?? VALUE prepend)
{
switch (TYPE(prepend)) {
case T_CLASS:
case T_MODULE:
break;
default:
Check_Type(prepend?? T_CLASS);
break;
}
rb_prepend_module(prepend?? module);
return module;
}
???????????Щ?????????飬????????????? rb_prepend_module ????????????? rb_prepend_module ????????????
void
rb_prepend_module(VALUE klass?? VALUE module)
{
void rb_vm_check_redefinition_by_prepend(VALUE klass);
VALUE origin;
int changed = 0;
rb_frozen_class_p(klass);
if (!OBJ_UNTRUSTED(klass)) {
rb_secure(4);
}
Check_Type(module?? T_MODULE);
OBJ_INFECT(klass?? module);
origin = RCLASS_ORIGIN(klass);
if (origin == klass) {
origin = class_alloc(T_ICLASS?? klass);
RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
RCLASS_SUPER(klass) = origin;
RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
RCLASS_M_TBL(klass) = st_init_numtable();
st_foreach(RCLASS_M_TBL(origin)?? move_refined_method??
(st_data_t) RCLASS_M_TBL(klass));
}
changed = include_modules_at(klass?? klass?? module);
if (changed < 0)
rb_raise(rb_eArgError?? "cyclic prepend detected");
if (changed) {
rb_clear_cache();
rb_vm_check_redefinition_by_prepend(klass);
}
}
????????????????Щ????????????????????? 16 ?ж????????Щ????????????????????????? 17 ?п????????
??????????? RCLASS_ORIGIN ??? klass ?? origin ?????????????? klass ???????????? origin ??????????????????????????????棬?? klass ?? origin ???????????????????????? if ????е??????
????19 ??? klass ??????????μ????????????????????
????20 ~ 21 ?а??′????????????? klass ?? klass ??????м?
????22 ?н? klass ?? origin ????????????
????????????23 ~ 24 ?а? klass ????????????????У?????? klass ????????
??????25 ????? klass ??????????е? Refined ?????????????
?????????? if ????????????????????? 28 ?С???????????????????????????????? include_modules_at ????????????????У???????????????????????????????????顣???????????????????????????????????飬??????????????????
???????????????????????顣???????????????????????? klass ?? klass ????????????? klass ????????????????????ε??????У?????????λ?????ú????Ruby ???????任????????????????????????飬??????
??????????????????????????????????У??? C ?? prepend A?? B ??????????????
????+-----+      +--------+
????Before:  |  C  |----->| Object |
????+-----+      +--------+
????+--------------- klass ----------------+
????|                                      |
????v                                      |
????+-----+      +-----+      +-----+      +-----+      +--------+
????After:  |  C  |----->|  A  |----->|  B  |----->|  C' |+---->| Object |
????+-----+      +-----+      +-----+      +-----+      +--------+
????|                                      ^
????|                                      |
????+--------------- origin ---------------+