This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module RubyInternals | |
extend FFI::Library | |
ffi_lib(FFI::CURRENT_PROCESS) | |
end |
Foreign? Far from it. You've now got access to all of C-ruby's innards. For example, if you're using Ruby 1.9:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RubyInternals | |
attach_variable :ptr_vm_core, :rb_mRubyVMFrozenCore, :pointer | |
end |
With this you've got a handle on the otherwise inaccessible RubyVM core object. As a pointer, it's not all that useful. But with a little more mischief...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RubyInternals | |
attach_function :const_def_pointer, :rb_define_const, | |
[:pointer, :string, :pointer], :void | |
attach_variable :ptr_object_class, :rb_cObject, :pointer | |
const_def_pointer(ptr_object_class, "RubyCore", ptr_vm_core) | |
end |
... now you can treat the RubyVM core like any other object. One interesting (if unwieldly-named) method that it offers is "core#define_singleton_method". It accepts an InstructionSequence -- compiled YARV bytecode -- and uses it to set up a singleton method in the object where it is invoked. Let's make it a little easier to use:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RubyInternals | |
def define_iseq_singleton_method(mod,sel,iseq) | |
mod.module_eval do | |
RubyCore.send("core#define_singleton_method",mod,sel,iseq) | |
end | |
end | |
end |
Now it is easy to create methods using bytecode sequences:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Greeter | |
extend RubyInternals | |
iseq = RubyVM::InstructionSequence.compile <<-RUBY | |
puts "Hello, world." | |
RUBY | |
define_iseq_singleton_method(self, :greet, iseq) | |
end | |
Greeter.greet |