From: Daniel Bovensiepen Date: 2009-04-08T06:01:12+09:00 Subject: [ruby-core:23147] Re: Running ruby1.9 bytecode --000e0cd32df0ce4bfe0466fd53ca Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi James, > I just found out about the VM::InstructionSequence.compile method for > turning Ruby code strings into ruby1.9 bytecode. Is there a standard method > of de/serializing this bytecode to/from files, and is there a way of running > bytecode directly? I'm looking at generating bytecode from another language > to get it to run on Ruby. > You can at least load bytecode directly, transform ruby to bytecode and at the end evaluate everything: 1 2 3 4 5 6 7 8 9 *10* 11 12 13 14 15 16 17 18 19 *20* iseqFromCode = RubyVM::InstructionSequence.new "a = 1; a += a; puts a" # => @> iseqFromCode.eval # 2 # => nil iseqRawArr = iseqFromCode.to_a # => ["YARVInstructionSequence/SimpleDataFormat", 1, 1, 1, # {:arg_size=>0, :local_size=>2, :stack_max=>2}, "", # "", :top, [:a], 0, [], [1, [:trace, 1], # [:putobject, 1], [:setlocal, 2], [:trace, 1], [:getlocal, 2], # [:getlocal, 2], [:opt_plus], [:setlocal, 2], [:trace, 1], # [:putnil], [:getlocal, 2], [:send, :puts, 1, nil, 8, nil], # [:leave]]] iseqRawArr.class # => Array iseqFromArr = RubyVM::InstructionSequence.load iseqRawArr # => @> iseqFromArr.eval # 2 # => nil But for this you have to apply this to the source code: 1 2 3 4 5 6 7 8 9 *10* 11 12 13 14 15 boviAir:src danielbovensiepen$ svn diff iseq.c Index: iseq.c =================================================================== --- iseq.c (revision 21348) +++ iseq.c (working copy) @@ -1451,7 +1451,7 @@ rb_define_method(rb_cISeq, "eval", iseq_eval, 0); /* disable this feature because there is no verifier. */ - /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1); */ + rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1); (void)iseq_s_load; rb_define_singleton_method(rb_cISeq, "compile", iseq_s_compile, -1); boviAir:src danielbovensiepen$ Of course, the comments are very clear: you shouldn't do this! MfG Daniel --000e0cd32df0ce4bfe0466fd53ca Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi James,

I just found out about the VM::InstructionSequ= ence.compile method for turning Ruby code strings into ruby1.9 bytecode. Is= there a standard method of de/serializing this bytecode to/from files, and= is there a way of running bytecode directly? I'm looking at generating= bytecode from another language to get it to run on Ruby.
=A0
You can at least load bytecode directly, tr= ansform ruby to bytecode and at the end evaluate everything:

=
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
17
18
19
20
iseqFromCode =3D=A0RubyVM::InstructionSequence.new=A0"a =3D 1; a +=3D a; puts a"=A0
=A0=A0
# =3D&= gt; <RubyVM::InstructionSequence:<compiled>@<compiled>>= =A0
iseqFromCode.eval=A0
=A0=A0# 2=A0
=A0 # =3D> nil=A0
iseqRawArr =3D iseqFromCode.to_a=A0
=A0= =A0# =3D> ["= ;YARVInstructionSequence/SimpleDataFormat", 1, 1, 1,=A0
=A0 #=A0 = =A0 =A0{:arg_size=3D>0, :local_size=3D>2, :stack_max=3D>2}, "= <compiled>",=A0
=A0 #=A0 =A0 =A0"<compiled>", :top, [:a], 0, [], [1, [:trac= e, 1],=A0
=A0 #=A0 =A0 =A0[:putobject, 1], [:setlocal, 2], [:trace, 1], = [:getlocal, 2],=A0
=A0 #=A0 =A0 =A0[:getlocal, 2], [:opt_plus], [:setloc= al, 2], [:trace, 1],=A0
=A0 #=A0 =A0 =A0[:putnil], [:getlocal, 2], [:send, :puts, 1, nil, 8, nil],= =A0
=A0 #=A0 =A0 =A0[:leave]]]=A0
iseqRawArr.class=A0
=A0= =A0# =3D> Array= =A0
iseqFromArr =3D=A0RubyVM::InstructionSequence.load = iseqRawArr=A0
=A0=A0# =3D> <= ;RubyVM::InstructionSequence:<compiled>@<compiled>>=A0
iseqFromArr.eval=A0
=A0=A0# 2=A0
=A0 # =3D> nil

But for this you have to apply this to the source code:
<= br>
1
2
3
4
5
6
7
8
9
10
11
12
1314
15
boviAir:src danielbovensiepen$ svn diff iseq.c=A0
= Index: iseq.c=A0
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=A0=
--- iseq.c=A0 =A0 =A0 (revision 21348)=A0
+++ iseq.c=A0 =A0 =A0 (working= copy)=A0
@@ -1451,7 +1451,7 @@=A0
=A0 =A0 =A0rb_define_method(rb_cIS= eq, "eval", iseq_eval, 0);=A0
=A0=A0
=A0 =A0 =A0/* disable = this feature because there is no verifier. */=A0
-=A0 =A0 /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_l= oad, -1); */=A0
+=A0 =A0 rb_define_singleton_method(rb_cISeq, "load= ", iseq_s_load, -1);=A0
=A0 =A0 =A0(void)iseq_s_load;=A0
=A0=A0<= br>=A0 =A0 =A0rb_define_singleton_method(rb_cISeq, "compile", ise= q_s_compile, -1);=A0
boviAir:src danielbovensiepen$
= =A0
Of course, the comments are very clear: you shouldn't do = this!


MfG
Daniel
--000e0cd32df0ce4bfe0466fd53ca--