Skip to content

Instantly share code, notes, and snippets.

@teleological
Created April 29, 2011 13:47
Show Gist options
  • Save teleological/948318 to your computer and use it in GitHub Desktop.
Save teleological/948318 to your computer and use it in GitHub Desktop.
FFI internals 6
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