Skip to content

Instantly share code, notes, and snippets.

@orzFly
Created May 5, 2014 14:45
Show Gist options
  • Save orzFly/18bad83973614abb03f3 to your computer and use it in GitHub Desktop.
Save orzFly/18bad83973614abb03f3 to your computer and use it in GitHub Desktop.
block-and-lambda-disasm.rb
def foo(&block)
p block
end
puts RubyVM::InstructionSequence.disasm lambda {
foo(&lambda {})
}
=begin
== disasm: <RubyVM::InstructionSequence:block in <main>@test.rb>========
== catch table
| catch type: break st: 0003 ed: 0010 sp: 0001 cont: 0010
| catch type: redo st: 0000 ed: 0016 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0016 sp: 0000 cont: 0016
|------------------------------------------------------------------------
0000 trace 1 ( 6)
0002 putself
0003 putself
0004 send :lambda, 0, block (2 levels) in <main>, 8, <ic:0>
0010 send :foo, 0, nil, 12, <ic:1>
0016 leave
== disasm: <RubyVM::InstructionSequence:block (2 levels) in <main>@test.rb>
== catch table
| catch type: redo st: 0000 ed: 0001 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0001 sp: 0000 cont: 0001
|------------------------------------------------------------------------
0000 putnil ( 6)
0001 leave
=end
puts RubyVM::InstructionSequence.disasm lambda {
foo() &lambda {}
}
=begin
== disasm: <RubyVM::InstructionSequence:block in <main>@test.rb>========
== catch table
| catch type: break st: 0009 ed: 0016 sp: 0001 cont: 0016
| catch type: redo st: 0000 ed: 0022 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0022 sp: 0000 cont: 0022
|------------------------------------------------------------------------
0000 trace 1 ( 10)
0002 putself
0003 send :foo, 0, nil, 8, <ic:0>
0009 putself
0010 send :lambda, 0, block (2 levels) in <main>, 8, <ic:1>
0016 send :&, 1, nil, 0, <ic:2>
0022 leave
== disasm: <RubyVM::InstructionSequence:block (2 levels) in <main>@test.rb>
== catch table
| catch type: redo st: 0000 ed: 0001 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0001 sp: 0000 cont: 0001
|------------------------------------------------------------------------
0000 putnil ( 10)
0001 leave
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment