2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2015-12-07 14:01:29 -05:00
|
|
|
|
2015-12-08 08:58:50 -05:00
|
|
|
begin
|
|
|
|
require '-test-/iseq_load/iseq_load'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2015-12-07 14:01:29 -05:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class RubyVM::InstructionSequence
|
|
|
|
def disasm_if_possible
|
|
|
|
begin
|
|
|
|
self.disasm
|
|
|
|
rescue Encoding::CompatibilityError, EncodingError, SecurityError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compare_dump_and_load i1, dumper, loader
|
|
|
|
dump = dumper.call(i1)
|
|
|
|
return i1 unless dump
|
|
|
|
i2 = loader.call(dump)
|
|
|
|
|
|
|
|
# compare disassembled result
|
|
|
|
d1 = i1.disasm_if_possible
|
|
|
|
d2 = i2.disasm_if_possible
|
|
|
|
|
|
|
|
if d1 != d2
|
|
|
|
STDERR.puts "expected:"
|
|
|
|
STDERR.puts d1
|
|
|
|
STDERR.puts "actual:"
|
|
|
|
STDERR.puts d2
|
|
|
|
|
|
|
|
t1 = Tempfile.new("expected"); t1.puts d1; t1.close
|
|
|
|
t2 = Tempfile.new("actual"); t2.puts d2; t2.close
|
|
|
|
system("diff -u #{t1.path} #{t2.path}") # use diff if available
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
i2
|
|
|
|
end
|
|
|
|
|
2015-12-08 08:58:50 -05:00
|
|
|
CHECK_TO_A = ENV['RUBY_ISEQ_DUMP_DEBUG'] == 'to_a'
|
|
|
|
CHECK_TO_BINARY = ENV['RUBY_ISEQ_DUMP_DEBUG'] == 'to_binary'
|
|
|
|
|
2015-12-07 14:01:29 -05:00
|
|
|
def self.translate i1
|
|
|
|
# check to_a/load_iseq
|
2016-01-29 10:21:52 -05:00
|
|
|
compare_dump_and_load(i1,
|
2015-12-08 08:58:50 -05:00
|
|
|
proc{|iseq|
|
|
|
|
ary = iseq.to_a
|
|
|
|
ary[9] == :top ? ary : nil
|
|
|
|
},
|
|
|
|
proc{|ary|
|
|
|
|
RubyVM::InstructionSequence.iseq_load(ary)
|
|
|
|
}) if CHECK_TO_A && defined?(RubyVM::InstructionSequence.iseq_load)
|
|
|
|
|
2015-12-09 10:10:57 -05:00
|
|
|
# check to_binary
|
2015-12-08 08:58:50 -05:00
|
|
|
i2_bin = compare_dump_and_load(i1,
|
|
|
|
proc{|iseq|
|
|
|
|
begin
|
2015-12-09 10:10:57 -05:00
|
|
|
iseq.to_binary
|
2016-01-29 10:21:52 -05:00
|
|
|
rescue RuntimeError # not a toplevel
|
2015-12-08 08:58:50 -05:00
|
|
|
# STDERR.puts [:failed, e, iseq].inspect
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
},
|
|
|
|
proc{|bin|
|
2015-12-09 10:10:57 -05:00
|
|
|
iseq = RubyVM::InstructionSequence.load_from_binary(bin)
|
2015-12-08 08:58:50 -05:00
|
|
|
# STDERR.puts iseq.inspect
|
|
|
|
iseq
|
|
|
|
}) if CHECK_TO_BINARY
|
2015-12-07 14:01:29 -05:00
|
|
|
# return value
|
2015-12-08 08:58:50 -05:00
|
|
|
i2_bin if CHECK_TO_BINARY
|
|
|
|
end if CHECK_TO_A || CHECK_TO_BINARY
|
2015-12-07 14:01:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#require_relative 'x'; exit(1)
|