Created
March 29, 2012 15:21
-
-
Save judofyr/2238438 to your computer and use it in GitHub Desktop.
Experimenting with MRI internals in FFI
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' | |
class Object | |
def memory_location | |
object_id * 2 | |
end | |
def to_pointer | |
FFI::Pointer.new(memory_location) | |
end | |
def class=(klass) | |
to_pointer.put_ulong(8, klass.memory_location) | |
end | |
def unfreeze | |
to_pointer.tap do |p| | |
p.put_ulong(0, p.get_ulong(0) &~ (1<<11)) | |
end | |
self | |
end | |
end | |
if $0 == __FILE__ | |
a = Object.new | |
a.class = Array | |
p a.class | |
a.freeze | |
p a.frozen? | |
a.unfreeze | |
p a.frozen? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment