Created
November 15, 2011 05:39
-
-
Save undees/1366266 to your computer and use it in GitHub Desktop.
Rubinius experiment: generate bytecode for a method call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
I found this gist on Google beause I was running into the same problem. I figured out that you can just call
generator.send :puts, 1
and that will work. Thesend
method is just shorthand for:Hope that helps!