有没有办法将 RubyVM::InstructionSequence 存储到文件中并稍后读取?
我试过Marshal.dump
没有成功。我收到以下错误:
`dump': no _dump_data is defined for class RubyVM::InstructionSequence (TypeError)
是的,有办法。
首先,您需要 make 的可访问load
方法InstructionSequence
,默认情况下禁用该方法:
require 'fiddle'
class RubyVM::InstructionSequence
# Retrieve Ruby Core's C-ext `iseq_load' function address
load_fn_addr = Fiddle::Handle::DEFAULT['rb_iseq_load']
# Retrieve `iseq_load' C function representation
load_fn = Fiddle::Function.new(load_fn_addr,
[Fiddle::TYPE_VOIDP] * 3,
Fiddle::TYPE_VOIDP)
# Make `iseq_load' accessible as `load' class method
define_singleton_method(:load) do |data, parent = nil, opt = nil|
load_fn.call(Fiddle.dlwrap(data), parent, opt).to_value
end
end
由于该RubyVM::InstructionSequence.load
方法可以将编译后的 VM 指令加载为数组,因此您可以自由地将其用于(反)序列化目的:
irb> # compile simple ruby program into its instruction sequence
irb> seq = RubyVM::InstructionSequence.new <<-EOS
irb: p 'Hello, world !'
irb: EOS
=> <RubyVM::InstructionSequence:<compiled>@<compiled>
irb> # serialize sequence as Array instance representation
irb> data = Marshal.dump seq.to_a
=> "\x04\b[\x13\"-YARVInstructionSequence/SimpleDataFormat … ]"
irb> # de-serialize previously serialized sequence
irb> seq_loaded = Marshal.load data
=> ["YARVInstructionSequence/SimpleDataFormat", 2, 2, 1, { … ]
irb> # load deserialized Array back into instruction sequence
irb> new_iseq = RubyVM::InstructionSequence.load seq_loaded
=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
irb> # execute instruction sequence in current context
irb> new_iseq.eval
"Hello, world !"
=> "Hello, world !"
这就是所有人;)
鉴于该类的方法有限,因此您可以尝试的事情有限。可能您唯一能做的就是将其实例保存为字符串:
puts RubyVM::InstructionSequence.disasm(proc{puts "foo"})
结果:
== disasm: <RubyVM::InstructionSequence:block in irb_binding@(irb)>=====
== catch table
| catch type: redo st: 0002 ed: 0009 sp: 0000 cont: 0002
| catch type: next st: 0002 ed: 0009 sp: 0000 cont: 0009
|------------------------------------------------------------------------
0000 trace 256 ( 1)
0002 trace 1
0004 putself
0005 putstring \"foo\"
0007 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>
0009 trace 512
0011 leave
当你想反序列化它时,你需要解析这个字符串。
这很简单。
iseq = RubyVM::InstructionSequence.compile("a = 1 + 2") # just an example
File.open('iseq.bin', 'wb') { |file| file.write iseq.to_binary }
# later
RubyVM::InstructionSequence.load_from_binary(File.read('iseq.bin')).eval # returns 3