Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created November 1, 2013 20:44
Show Gist options
  • Save rednaxelafx/7271652 to your computer and use it in GitHub Desktop.
Save rednaxelafx/7271652 to your computer and use it in GitHub Desktop.
Explore the actual value of VALUE for Symbol in Ruby 1.8.7, on Mac OS X 10.7.5 (64-bit)
#define SYMBOL_FLAG 0x0e
#define SYMBOL_P(x) (((VALUE)(x)&0xff)==SYMBOL_FLAG)
#define ID2SYM(x) ((VALUE)(((long)(x))<<8|SYMBOL_FLAG))
#define SYM2ID(x) RSHIFT((unsigned long)x,8)
#define T_SYMBOL 0x24
ID | ID_LOCAL | SYMBOL_FLAG
000000000101111100111 | 001 | 00001110
000000000101111101000 | 001 | 00001110
000000000101111101001 | 001 | 00001110
/*
* 32-bit VALUE space
* MSB ------------------------ LSB
* false 00000000000000000000000000000000
* true 00000000000000000000000000000010
* nil 00000000000000000000000000000100
* undef 00000000000000000000000000000110
* symbol ssssssssssssssssssssssss00001110
* object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
* fixnum fffffffffffffffffffffffffffffff1
*
* object_id space
* LSB
* false 00000000000000000000000000000000
* true 00000000000000000000000000000010
* nil 00000000000000000000000000000100
* undef 00000000000000000000000000000110
* symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
* object oooooooooooooooooooooooooooooo0 o...o % A = 0
* fixnum fffffffffffffffffffffffffffffff1 bignum if required
*
* where A = sizeof(RVALUE)/4
*
* sizeof(RVALUE) is
* 20 if 32-bit, double is 4-byte aligned
* 24 if 32-bit, double is 8-byte aligned
* 40 if 64-bit
*/
KrisMBA:/tmp
$ sudo gem install RubyInline
Successfully installed ZenTest-4.9.4
Fetching: RubyInline-3.12.2.gem (100%)
Successfully installed RubyInline-3.12.2
Installing ri documentation for RubyInline-3.12.2
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block
Installing ri documentation for ZenTest-4.9.4
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block
Done installing documentation for RubyInline, ZenTest after 3 seconds
2 gems installed
KrisMBA:/tmp
$ irb
>> require 'rubygems'
=> false
>> require 'inline'
=> true
>> class Foo
>> inline do |builder|
?> builder.c '
void pv(VALUE v) {
printf("%lp\n", v);
}'
>> end
>> end
(irb): In function ‘pv’:
(irb):9: warning: use of ‘l’ length modifier with ‘p’ type character
(irb):9: warning: use of ‘l’ length modifier with ‘p’ type character
(irb): In function ‘pv’:
(irb):9: warning: use of ‘l’ length modifier with ‘p’ type character
(irb):9: warning: use of ‘l’ length modifier with ‘p’ type character
=> true
>> f = Foo.new
=> #<Foo:0x108d04ba8>
>> f.pv 1
0x3
=> nil
>> f.pv 2
0x5
=> nil
>> f.pv 0
0x1
=> nil
>> f.pv :rednaxelafx
0x5f390e
=> nil
>> f.pv :rednaxelapx
0x5f410e
=> nil
>> f.pv :rednaxelagx
0x5f490e
=> nil
>> 0x5f390e.to_s 2
=> "10111110011100100001110"
>> 0x5f410e.to_s 2
=> "10111110100000100001110"
>> 0x5f490e.to_s 2
=> "10111110100100100001110"
>> :rednaxelafx.object_id
=> 487548
>> _.to_s 2
=> "1110111000001111100"
>> :rednaxelapx.object_id
=> 487708
>> _.to_s 2
=> "1110111000100011100"
>> :rednaxelagx.object_id
=> 487868
>> _.to_s 2
=> "1110111000110111100"
>> def id_with_scope(object_id)
>> # sizeof(RVALUE) = 40 on 64-bit platform for Ruby 1.8.7
?> ((object_id << 1) - (4 << 2)) / 40
>> end
=> nil
>> ID_LOCAL = 1
=> 1
>> ID_SHIFT = 3
=> 3
>> def to_id(sym)
>> return nil unless sym.is_a? Symbol
>> id_with_scope(sym.object_id) >> ID_SHIFT
>> end
=> nil
>> to_id :rednaxelafx
=> 3047
>> to_id :rednaxelapx
=> 3048
>> to_id :rednaxelagx
=> 3049
>> 0x5f390e >> (ID_SHIFT + 8)
=> 3047
>> 0x5f410e >> (ID_SHIFT + 8)
=> 3048
>> 0x5f490e >> (ID_SHIFT + 8)
=> 3049
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment