Ruby ソースコードの歩き方

Ruby ソースコードの歩き方

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

2b4f9f20b554d4c77c46fe6a9930d6fe?s=128

Nao Minami

June 21, 2017
Tweet

Transcript

  1. 3VCZιʔείʔυͷา͖ํ  /BP.JOBNJ !TPVUI

  2. ࣗݾ঺հ

  3. /BP.JOBNJ!TPVUI !NJOBNJP 4PGUXBSFFOHJOFFS !8BOUFEMZ *OD

  4. 3VCZΛιʔείʔυ͔Βཧղ͍ͨ͠ʂ

  5. None
  6. τοϓϨϕϧͰϑΝΠϧɺσΟϨΫτϦ

  7. ʜͲ͔͜ΒಡΜͩΒྑ͍͔͕෼͔Βͳ͍

  8. Ͱ΋ɺߏ੒΍֓ཁΛ஌͍ͬͯΕ͹ ͣͬͱಡΈ΍͘͢ͳΔ͸ͣ

  9. 3VCZιʔείʔυͷ֓ཁΛ஌Δ Ͱ΋ɺߏ੒΍֓ཁΛ஌͍ͬͯΕ͹ ͣͬͱಡΈ΍͘͢ͳΔ͸ͣ

  10. w 3VCZιʔείʔυͷ֓ཁΛ஌Δ w 3VCZ࣮ߦ࣌ͷڍಈ w ·ͱΊ ࠓ೔࿩͢಺༰

  11. w 3VCZιʔείʔυͷ֓ཁΛ஌Δ w 3VCZ࣮ߦ࣌ͷڍಈ w ·ͱΊ ࠓ೔࿩͢಺༰

  12. 3VCZιʔείʔυͷ֓ཁΛ஌Δ w 3VCZϨϙδτϦ w τοϓϨϕϧͰϑΝΠϧɺσΟϨΫτϦ w NBLFͰϏϧυɺNBLFJOTUBMMͰΠϯετʔϧ w ओཁͳϑΝΠϧ͸࣮͸ଟ͘ͳ͍ w

    ಛʹɺQBSTFZ DPNQJMFD JOTOTEFG͕ॏཁ
  13. 3VCZॲཧܥͷ֓ཁ w QBSTFʜSVCZίʔυ͔Β"45Λੜ੒ w DPNQJMFʜ"45͔Β:"37໋ྩྻΛੜ੒ w SVOUJNFʜ:"37໋ྩΛ࣮ߦ 3VCZ΋ଟ͘ͷݴޠॲཧܥͱಉ༷ɺεςοϓ͕ଘࡏ ͦΕͧΕͷεςοϓ͸Ͳ͜ͰͲ͏࣮૷͞Ε͍ͯΔͷ͔ʁ

  14. QBSTF w QBSTFʜSVCZίʔυ͔Β"45Λੜ੒ w DPNQJMFʜ"45͔Β:"37໋ྩྻΛੜ੒ w SVOUJNFʜ:"37໋ྩΛ࣮ߦ

  15. 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 ந৅ߏจ໦
  16. QBSTF w QBSTFZͰఆٛ w  ߦ͋Δ௒ڊେϑΝΠϧ w CJTPOͷ༷ͳQBSTFSHFOFSBUPSʹ৯Θ͢QBSTFD w #/'Ͱهड़ɺBDUJPO΍ͦͷଞͷ෦෼͸$Ͱهड़

    w UPLFO͸EFGTLFZXPSETͰఆٛ 3VCZͷจ๏Λ࿔Γ͔ͨͬͨΒQBSTFZΛݟΕ͹ྑ͍
  17. DPNQJMF w QBSTFʜSVCZίʔυ͔Β"45Λੜ੒ w DPNQJMFʜ"45͔Β:"37໋ྩྻΛੜ੒ w SVOUJNFʜ:"37໋ྩΛ࣮ߦ

  18. 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໋ྩྻ
  19. DPNQJMF w DPNQJMFDͰఆٛ w  ߦ͋Δ௒ڊେϑΝΠϧ w "45Λ૸ࠪͯ͠:"37໋ྩ JTFR Λੜ੒

    w ASC@JTFR@DPNQJMF@OPEF SC@JTFR@U JTFR /0%& OPEF A͕ݺ͹ΕΔ "45͕มΘͬͨΒɺDPNQJMFD΋मਖ਼
  20. SVOUJNF w QBSTFʜSVCZίʔυ͔Β"45Λੜ੒ w DPNQJMFʜ"45͔Β:"37໋ྩྻΛੜ੒ w SVOUJNFʜ:"37໋ྩΛ࣮ߦ

  21. putself putobject 2 putobject 2 opt_plus opt_send_simple <callinfo!mid:puts, argc:2…> :"37໋ྩྻΛॱ൪ʹ࣮ߦ

    SVOUJNF 4UBDL1PJOUFS 41 :"37ͷ಺෦ελοΫ 1SPHSBN$PVOUFS 1$
  22. SVOUJNF:"37໋ྩͷఆٛ :"37໋ྩͷఆٛ͸Ͳ͜ʁ

  23. SVOUJNF:"37໋ྩͷఆٛ w JOTOTEFGͰ:"37໋ྩΛఆٛ w QVUTFMG QVUPCKFDU TFOE FUDͷఆٛΛهࡌ w !@LPʹΑΔ%4-

    w ΄΅$͕ͩɺڞ௨෦෼͸ল͍ͯΔ w UPPMJOTOTWNSCͱ͍͏SVCZεΫϦϓτͰ JOTOTEFG͔Βίʔυੜ੒Λߦ͏WNJOD :"37໋ྩΛग़ൃ఺ʹಡΉ৔߹͸JOTOTEFGΛݟΔͱྑ͍
  24. ͦͷଞͷϑΝΠϧ QBSTFZ DPNQJMFD JOTOTEFGҎ֎ͷ ϑΝΠϧ͸Կʹ࢖ΘΕΔʁ

  25. ͦͷଞͷϑΝΠϧ w QBSTFZ DPNQJMFD JOTOTEFG͔Βݺͼग़͞ΕΔॲཧ w QBSTF DPNQJMFͷݺͼग़͠ɺWNͷॲཧ w ϏϧτΠϯͷΫϥε

    0CKFDU "SSBZ 4USJOH FUDʜ  w υΩϡϝϯτ େମҎԼͷͭͷͲΕ͔
  26. ಡΈํ͕෼͔Ε͹ ɹ3VCZͷιʔείʔυ͸ා͘ͳ͍ʂ

  27. w 3VCZιʔείʔυͷߏ੒Λ஌Δ w 3VCZ࣮ߦ࣌ͷڍಈ w ·ͱΊ ࠓ೔࿩͢಺༰

  28. $ ruby -e ‘print “Hello World!”’ Hellow World! 3VCZΛUFSNJOBM͔Β࣮ߦ 3VCZΛUFSNJOBM͔Β࣮ߦ

  29. 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ؔ਺
  30. 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Λར༻
  31. 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 ʹḷΓண͍ͨʂ
  32. 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(); . . . }
  33. 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ʹḷΓண͍ͨʂ
  34. ͔֬ʹɺQBSTFZ DPNQJMFD JOTOTEFG Λԡ͑͞Ε͹3VCZΛཧղͰ͖ͦ͏

  35. w 3VCZιʔείʔυͷߏ੒Λ஌Δ w 3VCZ࣮ߦ࣌ͷڍಈ w ·ͱΊ ࠓ೔࿩͢಺༰

  36. ·ͱΊ w 3VCZͷιʔείʔυ͸ා͘ͳ͍ w QBSTF DPNQJMF SVOUJNFͷͦΕͧΕͷεςοϓͷ ओཁͳϑΝΠϧΛೖΓޱʹ͢ΔͱಡΈ΍͍͢ w NBJOؔ਺͔ΒಡΈ࢝ΊΔͷ΋ָ͍͠

    3VCZΛιʔείʔυ͔Βཧղͯ͠ɺ ָ͍͠3VCZϥΠϑΛૹΖ͏ʂ