How is magic formed

How is magic formed

Short excursion into Ruby Internals, Ruby Usergroup Berlin, June 3rd 2014

Fdc75e4cf5aa4bf67af27f5ebe19d261?s=128

Rin Raeuber

July 03, 2014
Tweet

Transcript

  1. How is ! magic ! formed

  2. Rin! @rinpaku @bitcrowd

  3. None
  4. WHY?

  5. TIL: There’s bacon in! the Ruby source code

  6. What happens when Ruby runs my code?! ! What is

    an Object?
  7. What happens ! when Ruby ! runs my code?

  8. Ruby code 9.times do puts "Ente" end

  9. Tokens 9 . t i m e s …

  10. 9 . times do puts "" Tokens 9 . t

    i m e s … integer identifier identifier period keyword strin
  11. Syntax Tree do_block period method_add_block integer! 9 identifier! times call

    command identifier! puts …
  12. YARV instructions “Yet Another Ruby Virtual Machine” == catch table

    | catch type: break st: 0002 ed: 0006 sp: 0000 cont: 0006 |-------------------------------------------------------------- 0000 trace 1 0002 putobject 9 0004 send <callinfo!mid:times, argc:0, block:block 0006 leave == disasm: <RubyVM::InstructionSequence:block in <compiled>@<co == catch table | catch type: redo st: 0000 ed: 0009 sp: 0000 cont: 0000 | catch type: next st: 0000 ed: 0009 sp: 0000 cont: 0009 |-------------------------------------------------------------- 0000 trace 256 0002 trace 1 0004 putself 0005 putstring "Ente"
  13. I MIGHT! HAVE MADE THIS ALL UP

  14. Tokenizing Ripper.lex(your_code)

  15. parse.c

  16. with Ripper: Ripper.sexp(your_code) ! Parsing

  17. with Ripper: Ripper.sexp(your_code) parser debug information: ruby -y example.rb Parsing

  18. RubyVM::InstructionSequence. compile(code).disasm Compiling

  19. What ! is an! Object?

  20. struct meetup { char title[120]; time_t starts_at; bool has_talks; };

    Structs
  21. union location { char address[120]; struct position { int latitude;

    int longitude; } }; Union
  22. struct meetup { char title[120]; time_t starts_at; bool has_talks; union

    { char address[120]; struct position { int latitude; int longitude; } } };
  23. Let’s look at some source code

  24. RObject struct RObject { struct RBasic basic; … };

  25. RBasic struct RBasic { VALUE flags; const VALUE klass; };

  26. RObject struct RObject { struct RBasic basic; struct { long

    numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; }; simplified
  27. RObject struct RObject { struct RBasic basic; union { struct

    { long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; VALUE ary[ROBJECT_EMBED_LEN_MAX]; } as; };
  28. RFloat struct RFloat { struct RBasic basic; double float_value; };

  29. - Reading C is not that bad.* 
 *(If you

    hate it, you can still look at the Standard Library or Rubinius.)! ! - Check out the Ruby source code
 and give a talk about it. ;) Things to take home
  30. WAIT! Where’s the bacon?

  31. Never create Ruby Strings longer than 23 characters ! Ruby

    under a microscope (the book) ! The Ruby Hacking Guide (Warning: Ruby 1.7.3!) ! A Tour of the Ruby MRI Source Code with Pat Shaughnessy Some Links