Skip to content

Instantly share code, notes, and snippets.

@cout
Created January 15, 2014 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cout/8441150 to your computer and use it in GitHub Desktop.
Save cout/8441150 to your computer and use it in GitHub Desktop.
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