# Running ruby1.9 bytecode

**URL:** <https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840>\
**Category:** Ruby\
**Created:** [April 6, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840 "2009-04-06T14:49:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/1 "2009-04-06T14:49:38Z")

</div>

Hi all,

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.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 8:59pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/2 "2009-04-06T20:59:55Z")

</div>

Originally posted to Ruby T., trying my luck here to see if it gets a  
response:

---------- Forwarded message ----------  
From: James C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: 2009/4/6  
Subject: Running ruby1.9 bytecode  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi all,

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.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 9:29pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/3 "2009-04-06T21:29:16Z")

</div>

On Mon, Apr 6, 2009 at 2:48 PM, James C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi all,
> 
> 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.  
> Quoting from the Pickaxe book  
> no, because the bytecode verifier is not ready yet, that seems the  
> only missing bit.  
> HTH  
> Robert

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 11:04pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/4 "2009-04-06T23:04:46Z")

</div>

> Well that’s part of it. But, if by “generating bytecode from another  
> language” means, for instance, executing JVM bytecodes, I don’t think it  
> will fly. The term bytecode here is being used rather generically. The YARV  
> “bytecodes” aren’t the same as the JVM bytecodes, any more than  
> Smalltalk-80  
> bytecodes are the same as either…
> 
> Getting another language to produce YARV bytecodes would be an exercise for  
> the reader.

I meant that I’d like to try and generate YARV bytecode from non-Ruby  
source. Mostly a pie-in-the-sky idea, only just writing my first  
AST-walking  
interpreter, just wondering if it’s possible.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/5 "2009-04-06T23:48:40Z")

</div>

James C. wrote:

> I meant that I’d like to try and generate YARV bytecode from non-Ruby  
> source. Mostly a pie-in-the-sky idea, only just writing my first AST-walking  
> interpreter, just wondering if it’s possible.

It’s definitely possible. I suggest you take a look at the parrot  
interpreter. It is a one-for-all interpreter (well, that’s the aim,  
anyway), which began life as an April Fool’s joke that someone took one  
step further. It officially hit version 1.0 the other day, so it should  
be a good example for you. By one-for-all I mean, it’s a generic  
interpreter that can handle many languages, much like .NET and the clr,  
I guess.

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 6, 2009, 10:37pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/6 "2009-04-06T22:37:13Z")

</div>

On Mon, Apr 6, 2009 at 3:27 PM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> language
> 
> > to get it to run on Ruby.  
> > Quoting from the Pickaxe book  
> > no, because the bytecode verifier is not ready yet, that seems the  
> > only missing bit.  
> > HTH  
> > Robert

Well that’s part of it. But, if by “generating bytecode from another  
language” means, for instance, executing JVM bytecodes, I don’t think it  
will fly. The term bytecode here is being used rather generically. The  
YARV  
“bytecodes” aren’t the same as the JVM bytecodes, any more than  
Smalltalk-80  
bytecodes are the same as either.

It’s like the difference between an Intel and Motorola processor, both  
have  
instruction sets with binary representations, but those instruction sets  
aren’t necessarily interchangeable.

## Getting another language to produce YARV bytecodes would be an exercise for the reader.

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 7, 2009, 11:05am UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/7 "2009-04-07T11:05:22Z")

</div>

On Mon, Apr 6, 2009 at 11:03 PM, James C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I meant that I’d like to try and generate YARV bytecode from non-Ruby  
> source. Mostly a pie-in-the-sky idea, only just writing my first AST-walking  
> interpreter, just wondering if it’s possible.  
> Having YARV/Rubinius as a backend for other languages is certainly a  
> very reasonable idea.  
> And if I understood correctly it is planned to be possible in the  
> (near?) future.

Cheers  
R.

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [April 7, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/running-ruby1-9-bytecode/164840/8 "2009-04-07T23:02:55Z")

</div>

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”

# =\> \<RubyVM::InstructionSequence:@\>

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

# =\> \<RubyVM::InstructionSequence:@\>

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;

Of course, the comments are very clear: you shouldn’t do this!

MfG  
Daniel
