Bug #3841

RubyVM::InstructionSequence.to_a() and disasm() do not work properly for "for <var> in <list>"

Added by Sean O'Neil over 5 years ago. Updated about 3 years ago.

ruby -v:
ruby 1.9.2p0 (2010-08-18 revision 29036) [i386-mswin32_90]
2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN


I have been playing with the concept of caching compiled Ruby instruction sequences and reloading them later. For now the simplest way to do it is to use RubyVM::InstructionSequence.to_a() and Marshal.dump() to convert the resulting array to a smaller binary byte sequence. While testing this, I found that some Ruby scripts worked fine when an instruction sequence was reloaded, but others broke.

As a test, I decided to try compiling irb.rb and its dependencies. When I try to reload the compiled sequences, the first file that breaks is irb/extend-command.rb. I tracked it down to the way "for in " code block and parameters are handled, but I am not sure how to fix it. When I converted each "for in " loop to ".each {||", it started working perfectly. Here's an example of how the disassembly differs between the two loops:

def self.install_extend_commands
@EXTEND_COMMANDS.each {|args|
== disasm:
== catch table
| catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1] s3)
[ 2] args

0000 trace 1 ( 110)
0002 putnil

0003 getdynamic args, 0
0006 send :def_extend_command, 1, nil, 10, ic:0
0012 leave

def self.install_extend_commands
for args in @EXTEND_COMMANDS
== disasm:
== catch table
| catch type: redo st: 0005 ed: 0016 sp: 0000 cont: 0005
| catch type: next st: 0005 ed: 0016 sp: 0000 cont: 0016
local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1] s3)
[ 2] ?

0000 getdynamic *, 0 ( 206)
0003 setlocal args ( 204)
0005 trace 1 ( 205)
0007 putnil

0008 getlocal args
0010 send :def_extend_command, 1, nil, 10, ic:0
0016 leave

In this example, rb_id2name() returns NULL for the name of the argument passed to the code block. That is why it puts a "?" in the local table and a "*" in the getdynamic command. The to_a() method has a similar problem, and it appears to put a number there instead. When I use the load() method to convert the array back into an instruction sequence and try to execute it, the code block ends up receiving a number as a parameter instead of the entry in the list it is supposed to get.

I imagine this is not a high priority for the Ruby core team, and I don't mind putting in some work to fix it. However, it looks like it would take me a while to figure out, so I would appreciate any tips you could give me to point me in the right direction.

Thank you,
Sean O'Neil


#1 Updated by Shyouhei Urabe over 5 years ago

  • Status changed from Open to Assigned
  • Assignee set to Koichi Sasada



#2 [ruby-core:36981] Updated by Koichi Sasada over 4 years ago

  • Priority changed from Normal to 3
  • Target version set to 2.0.0

If I have a time, I'll check it.

#3 [ruby-core:50116] Updated by Koichi Sasada about 3 years ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF