-
-
Save dsisnero/06fb994637747f1a328d7fbe0659438e to your computer and use it in GitHub Desktop.
FFI internals 6
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) | |
attach_variable :ptr_vm_core, :rb_mRubyVMFrozenCore, :pointer | |
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) | |
def define_iseq_singleton_method(mod,sel,iseq) | |
mod.module_eval do | |
RubyCore.send("core#define_singleton_method",mod,sel,iseq) | |
end | |
end | |
end | |
module Greeter | |
extend RubyInternals | |
iseq = RubyVM::InstructionSequence.compile <<-RUBY | |
puts "Hello, world." | |
RUBY | |
define_iseq_singleton_method(self,:greet,iseq) | |
end | |
Greeter.greet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment