Skip to content

Instantly share code, notes, and snippets.

@undees
Created November 15, 2011 05:39
Show Gist options
  • Save undees/1366266 to your computer and use it in GitHub Desktop.
Save undees/1366266 to your computer and use it in GitHub Desktop.
Rubinius experiment: generate bytecode for a method call
klass = Object.const_set :Baz, Class.new
method = klass.dynamic_method :main do |generator|
generator.push_self
generator.push 42
generator.allow_private
generator.send_stack :puts, 1
generator.ret
end
Baz.new.main
# Error validating bytecode: corrupt instruction sequence
# An exception occurred running quux.rb
# corrupt instruction sequence - at main+6 (Rubinius::InvalidBytecode)
# Backtrace:
# Object#__script__ at quux.rb:11
# Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:65
# Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:107
# Rubinius::Loader#script at kernel/loader.rb:618
# Rubinius::Loader#main at kernel/loader.rb:772
@undees
Copy link
Author

undees commented Apr 23, 2012

Hi, E.J.

Thanks for this. You can also call send, a helper method whose existence I wasn't aware of when I wrote this Gist, but which Brian Ford pointed out to me:

    generator.send :puts, 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment