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

3c04a326a4183d2423e30d68d996dfd6?d=identicon&s=25 Nikodem Solarz (narzew)
on 2013-09-03 23:45
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.
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2013-09-04 07:49
(Received via mailing list)
Subject: RubyVM::InstructionSequence compile to a file and read from a
file
  Date: mar 03 set 13 11:45:12 +0200

Quoting Nikodem Solarz (lists@ruby-forum.com):

> 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
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2013-09-04 09:00
(Received via mailing list)
On Sep 3, 2013, at 22:49 , Carlo E. Prelz <fluido@fluido.as> 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)
Cb6bbc826cd7d9238a2fae344958f7ec?d=identicon&s=25 "Sandor Szücs" <sandor.szuecs@fu-berlin.de> (Guest)
on 2013-09-04 12:14
(Received via mailing list)
On 9/4/13 12:45 AM, Nikodem Solarz 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}, "<main>", "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.
3c04a326a4183d2423e30d68d996dfd6?d=identicon&s=25 Nikodem Solarz (narzew)
on 2013-09-04 23:54
Is the way to convert the Array I get to a InstructionSequence object ?
Cb6bbc826cd7d9238a2fae344958f7ec?d=identicon&s=25 "Sandor Szücs" <sandor.szuecs@fu-berlin.de> (Guest)
on 2013-09-05 08:36
(Received via mailing list)
On 9/5/13 12:54 AM, Nikodem Solarz 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.