$30 off During Our Annual Pro Sale. View Details »

How is magic formed

How is magic formed

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

Rin Raeuber

July 03, 2014
Tweet

More Decks by Rin Raeuber

Other Decks in Programming

Transcript

  1. How is !
    magic !
    formed

    View Slide

  2. Rin!
    @rinpaku @bitcrowd

    View Slide

  3. View Slide

  4. WHY?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. Tokens
    9 . t i m e s …

    View Slide

  10. 9 . times do puts ""
    Tokens
    9 . t i m e s …
    integer identifier identifier
    period keyword strin

    View Slide

  11. Syntax Tree
    do_block
    period
    method_add_block
    integer!
    9
    identifier!
    times
    call
    command
    identifier!
    puts

    View Slide

  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 0006 leave
    == disasm: @== 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"

    View Slide

  13. I MIGHT!
    HAVE MADE
    THIS ALL UP

    View Slide

  14. Tokenizing
    Ripper.lex(your_code)

    View Slide

  15. parse.c

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  19. What !
    is an!
    Object?

    View Slide

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

    View Slide

  21. union location {
    char address[120];
    struct position {
    int latitude;
    int longitude;
    }
    };
    Union

    View Slide

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

    View Slide

  23. Let’s look at some
    source code

    View Slide

  24. RObject
    struct RObject {
    struct RBasic basic;

    };

    View Slide

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

    View Slide

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

    View Slide

  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;
    };

    View Slide

  28. RFloat
    struct RFloat {
    struct RBasic basic;
    double float_value;
    };

    View Slide

  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

    View Slide

  30. WAIT! Where’s the bacon?

    View Slide

  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

    View Slide