I've been reading about the new ruby 2.0 features, and found that it will support bytecode import / export:
Ruby 2.0 is expected to make it simple to save pre-compiled Ruby scripts to bytecode representations and to then run these directly.
I've installed ruby-2.0.0-p0, but I didn't find any information on how to export the bytecode (or generally documentation on that matter). Is this feature already implemented, and if so, how do I use it?
I'm also wondering about some of the details. Is YARV-bytecode supposed to be platform-independent? Are all gems automatically included in the bytecode?
Until someone with better information looks at this question, I did some research:
It is implemented, but it doesn't seem to be exposed (e.g
ruby --dump-bytecode
doesn't exist). Also there's not very much documentation. As far as I can tell, what you are looking for is something like:seq.disassemble
will give you a nicely formatted string that you could dump to a file, orseq.to_a
will generate an an array that looks like:If you want to persist this to a file, you can do something like:
And then to load again:
Unfortunately I can't seem to figure out how to create a new
InstructionSequence
given the array loaded above.In the example above, gems are not included. Your
InstructionSequence
would include the bytecode equivalent of arequire 'active_record'
or what have you. I suspect that if dumping and loading of bytecode were provided directly by aruby
executable, this behavior would stay the same.If anyone else has more information I'd love to see it!
Unfortunately it looks like the verifier didn't get implemented in 2.0-p0, and as a result the load functionality is still commented out (from iseq.c, line 2260):