Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from teleological/ffi6.rb
Created February 28, 2018 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/06fb994637747f1a328d7fbe0659438e to your computer and use it in GitHub Desktop.
Save dsisnero/06fb994637747f1a328d7fbe0659438e 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