Created
January 15, 2014 18:04
-
-
Save cout/8441150 to your computer and use it in GitHub Desktop.
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 Foo | |
extend FFI::Library | |
ffi_lib 'ruby-1.9.1' | |
attach_function :rb_intern, [:string], :long | |
attach_function :rb_obj_id, [:long], :long | |
attach_function :rb_funcall2, [:long, :long, :int, :pointer], :long | |
end | |
def id2ref(id) | |
# TODO: only works for non-special objects | |
return id ^ 1 | |
end | |
def ref(obj) | |
return id2ref(obj.__id__) | |
end | |
obj = Object | |
ptr = FFI::MemoryPointer.new(:pointer, 0) | |
ret = Foo.rb_funcall2(ref(obj), Foo.rb_intern("to_s"), 0, ptr) | |
p ret | |
objid = Foo.rb_obj_id(ret) | |
p objid | |
s = ObjectSpace._id2ref(objid) # TODO: not sure why this line isn't doing the job... | |
p s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment