Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby ソースコードの歩き方

Ruby ソースコードの歩き方

第4回 meguro.rb LT で Ruby のソースコードの読み方について話しました
https://megurorb.connpass.com/event/59459/

Nao Minami

June 21, 2017
Tweet

More Decks by Nao Minami

Other Decks in Programming

Transcript

  1. QBSTF puts 2 + 2 3VCZίʔυ UPLFOྻ NODE_CALL + NODE_LIT

    2 identifier puts integer 2 integer 2 op + NODE_LIT 2 NODE_FCALL puts "45 ந৅ߏจ໦
  2. DPNQJMF "45͔ΒDPNQJMFͯ͠:"37໋ྩྻੜ੒ NODE_CALL + NODE_LIT 2 NODE_LIT 2 NODE_FCALL puts

    "45 putself putobject 2 putobject 2 opt_plus opt_send_simple <callinfo!mid:puts, argc:2…> :"37໋ྩྻ
  3. DPNQJMF w DPNQJMFDͰఆٛ w  ߦ͋Δ௒ڊେϑΝΠϧ w "45Λ૸ࠪͯ͠:"37໋ྩ JTFR Λੜ੒

    w ASC@JTFR@DPNQJMF@OPEF SC@JTFR@U JTFR /0%& OPEF A͕ݺ͹ΕΔ "45͕มΘͬͨΒɺDPNQJMFD΋मਖ਼
  4. SVOUJNF:"37໋ྩͷఆٛ w JOTOTEFGͰ:"37໋ྩΛఆٛ w QVUTFMG QVUPCKFDU TFOE FUDͷఆٛΛهࡌ w !@LPʹΑΔ%4-

    w ΄΅$͕ͩɺڞ௨෦෼͸ল͍ͯΔ w UPPMJOTOTWNSCͱ͍͏SVCZεΫϦϓτͰ JOTOTEFG͔Βίʔυੜ੒Λߦ͏WNJOD :"37໋ྩΛग़ൃ఺ʹಡΉ৔߹͸JOTOTEFGΛݟΔͱྑ͍
  5. int main(int argc, char **argv) { . . . {

    . . . return ruby_run_node(ruby_options(argc, argv)); } } w 3VCZ࣮ߦ࣌͸ANBJOAؔ਺͕ΤϯτϦʔϙΠϯτ w ASVCZ@PQUJPOTAͰQBSTF DPNQJMF w ASVCZ@SVO@OPEFA͕:"37໋ྩྻΛ࣮ߦ 3VCZͷNBJOؔ਺
  6. SVCZ@PQUJPOT void * ruby_options(int argc, char **argv) { . .

    . if ((state = EXEC_TAG()) == 0) { SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv)); } else { . . iseq = (void *)INT2FIX(state); } . . . return iseq; } w JTFR :"37໋ྩྻ Λฦ͢ w ॲཧΛ௥͏ͱɺQBSTFZͰఆٛ͞ΕͨAZZQBSTFA΍ DPNQJMFDͰఆٛ͞ΕͨASC@JTFR@DPNQJMF@OPEFAΛར༻
  7. SVCZ@PQUJPOT void * ruby_options(int argc, char **argv) { . .

    . if ((state = EXEC_TAG()) == 0) { SAVE_ROOT_JMPBUF(GET_THREAD(), iseq = ruby_process_options(argc, argv)); } else { . . iseq = (void *)INT2FIX(state); } . . . return iseq; } w JTFR :"37໋ྩྻ Λฦ͢ w ॲཧΛ௥͏ͱɺQBSTFZͰఆٛ͞ΕͨAZZQBSTFA΍ DPNQJMFDͰఆٛ͞ΕͨASC@JTFR@OFX@NBJOAΛར༻ QBSTFZ DPNQJMFD ʹḷΓண͍ͨʂ
  8. SVCZ@SVO@OPEF w JTFR :"37໋ྩྻ Λ࣮ߦ͢Δ w ॲཧΛ௥͏ͱɺAWN@FYFD@DPSFA͕࣮ߦ͞ΕΔ w JOTOTEFG͔Βੜ੒ͨ͠Dίʔυ͕ຒΊࠐ·ΕͯΔ static

    VALUE vm_exec_core(rb_thread_t *th, VALUE initial) { . . . first: INSN_DISPATCH(); /*****************/ #include "vm.inc" /*****************/ END_INSNS_DISPATCH(); . . . }
  9. SVCZ@SVO@OPEF w JTFR :"37໋ྩྻ Λ࣮ߦ͢Δ w ॲཧΛ௥͏ͱɺJOTOTEFG͔Βੜ੒ͨ͠Dίʔυ͕ ຒΊࠐ·ΕͨAWN@FYFD@DPSFA͕࣮ߦ͞ΕΔ static VALUE

    vm_exec_core(rb_thread_t *th, VALUE initial) { . . . first: INSN_DISPATCH(); /*****************/ #include "vm.inc" /*****************/ END_INSNS_DISPATCH(); . . . } JOTOTEFGʹḷΓண͍ͨʂ