RubyVM::InstructionSequence compile to a file and read from a file

Hello. I used RubyVM::InstructionSequence to compile file test.rb and I
used .to_a function to save it to an array and Marshal.dump to dump the
array to a file.

Is the way to eval the sequence I have in the file ?

Code I used to compile a file:

file = File.open(‘test.rbc’,‘wb’)
Marshal.dump(RubyVM::InstructionSequence.compile_file(“test.rb”).to_a,file)
file.close

I tried this, but it doesn’t work.

file = File.open(‘test.rbc’,‘rb’)
x = Marshal.load(file)
puts RubyVM::InstructionSequence.new(x.to_s).eval
file.close

Please, help.

Subject: RubyVM::InstructionSequence compile to a file and read from a
file
Date: mar 03 set 13 11:45:12 +0200

Quoting Nikodem S. ([email protected]):

Hello. I used RubyVM::InstructionSequence to compile file test.rb and I
used .to_a function to save it to an array and Marshal.dump to dump the
array to a file.

I know nothing about RubyVM, but you are using Marshal the wrong
way. Marshal::dump returns a string, which you have to write to your
file. For example:

File::open(‘yourfile’,‘wb’) do |f|
f.write(Marshal::dump(WHATEVER-YOU-DUMP))
end

You recover your data this way:

WHATEVER-YOU-HAVE-DUMPED=Marshal::restore(File::read(‘yourfile’))

HTH

Carlo

On Sep 3, 2013, at 22:49 , Carlo E. Prelz [email protected] wrote:

I know nothing about RubyVM, but you are using Marshal the wrong
way. Marshal::dump returns a string, which you have to write to your
file. For example:

No. Marshal.dump also takes an optional IO:

% ri Marshal.dump

= Marshal.dump

(from ruby core)

Is the way to convert the Array I get to a InstructionSequence object ?

On 9/4/13 12:45 AM, Nikodem S. wrote:

file = File.open(‘test.rbc’,‘wb’)
x = Marshal.load(file)
x is an array of instructions

puts RubyVM::InstructionSequence.new(x.to_s).eval

x.to_s returns a string representation of that array which you convert
to an RubyVM::InstructionSequence. It is not the same!

irb>
RubyVM::InstructionSequence.new(RubyVM::InstructionSequence.compile_file(‘test_compile.rb’).to_a.to_s).eval
=> [“YARVInstructionSequence/SimpleDataFormat”, 2, 0, 1, {:arg_size=>0,
:local_size=>1, :stack_max=>2}, “”, “test_compile.rb”,
“test_compile.rb”, 1, :top, [], 0, [], [1, [:trace, 1], [:putself],
[:putstring, “Yeaha VM”], [:opt_send_simple, {:mid=>:puts, :flag=>264,
:orig_argc=>1, :blockptr=>nil}], [:leave]]]
irb>
RubyVM::InstructionSequence.new(RubyVM::InstructionSequence.compile_file(‘Programmierung/ruby/vm/test_compile.rb’).to_a.to_s).eval.class
=> Array

You have to write a converter for that or maybe there is something in
YARV which you can use to let it do this for you.

On 9/5/13 12:54 AM, Nikodem S. wrote:

Is the way to convert the Array I get to a InstructionSequence object ?

Not that I am aware of.
For me it looks like you have to build one of these methods:

  • RubyVM::InstructionSequence::to_instr_seq =>
    RubyVM::InstructionSequence
  • RubyVM::InstructionSequence::to_src(ary) => String

or dig into CRuby, where you maybe get other possibilities.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs