ruby - How can I store and read a RubyVM::InstructionSequence? -


is there way store rubyvm::instructionsequence file , read in later?

i tried marshal.dump without success. im getting following error:

`dump': no _dump_data defined class rubyvm::instructionsequence (typeerror) 

yes, there way.

first, need make accessible load method of instructionsequence, disabled default:

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 `load' class method   define_singleton_method(:load) |data, parent = nil, opt = nil|     load_fn.call(fiddle.dlwrap(data), parent, opt).to_value   end end 

because rubyvm::instructionsequence.load method can load compiled vm instructions array, can freely use (de)serialization purposes:

irb> # compile simple ruby program instruction sequence irb> seq = rubyvm::instructionsequence.new <<-eos irb:   p 'hello, world !' irb:   eos => <rubyvm::instructionsequence:<compiled>@<compiled>  irb> # serialize sequence array instance representation irb> data = marshal.dump seq.to_a => "\x04\b[\x13\"-yarvinstructionsequence/simpledataformat … ]"  irb> # de-serialize serialized sequence irb> seq_loaded = marshal.load data => ["yarvinstructionsequence/simpledataformat", 2, 2, 1, { … ]  irb> # load deserialized array 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 !" 

that's folks ;)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -