Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from cout/gist:8441150
Created March 1, 2018 14:12
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/e161b687627816c6951982694343ea37 to your computer and use it in GitHub Desktop.
Save dsisnero/e161b687627816c6951982694343ea37 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