Mais conteúdo relacionado
Semelhante a Rubyのコードを読んでみよう(オブジェクト編) (20)
Rubyのコードを読んでみよう(オブジェクト編)
- 17. Kernel#define_methodの解説
● define_methodの定義はproc.cにあるのでそこ
から順番に手繰る
● rb_define_private_method(rb_cModule, "define_method",
rb_mod_define_method, -1); // proc.c
● rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
● rb_method_entry_set(mod, id, method->me, noex); // vm_method.c
● rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type,
rb_method_definition_t *def, rb_method_flag_t noex)
● st_insert(register st_table *table, register st_data_t key, st_data_t value)
- 19. メソッドの挿入をしているst_insert
● st_insert(register st_table *table, register st_data_t key, st_data_t value)
このメソッドの、st_tableって構造体
…何処かで見たような気が
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl; // メソッド関係一覧
struct st_table *iv_index_tbl; // インスタンス変数一覧
};
- 26. Module#prepend
● Ruby2.0で追加されたModule#prependでも似
たようなもの
● ” ” ” ”継承順のリストに 追記 ではなく 挿入 をして
いるだけ
● 関数名は書いておくので興味があったら確認
してね
● rb_define_private_method(rb_cModule, "prepend", rb_mod_prepend, -1); // ./eval.c:
rb_mod_prepend(int argc, VALUE *argv, VALUE module)
rb_prepend_module(VALUE klass, VALUE module)
- 29. method_missing解説
メソッドの呼ばれ方(rb_call0)を探せば簡単
./vm_eval.c
● static inline VALUE rb_call0(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope,
VALUE self){
rb_method_entry_t *me = rb_search_method_entry(recv, mid); // method_missingでないメソッド
rb_thread_t *th = GET_THREAD();
int call_status = rb_method_call_status(th, me, scope, self);
if (call_status != NOEX_OK) {
return method_missing(recv, mid, argc, argv, call_status); // ここでmethod_missingを読んでいる
}
stack_check();
return vm_call0(th, recv, mid, argc, argv, me);
}