Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Created November 16, 2020 21:38
Show Gist options
  • Save gr33n7007h/969718a8bb12e626ed9f9f865d270e5b to your computer and use it in GitHub Desktop.
Save gr33n7007h/969718a8bb12e626ed9f9f865d270e5b to your computer and use it in GitHub Desktop.
Testing nested struct/union Fiddle version 1.0.1
require 'fiddle/import'
include Fiddle::Importer
# Fiddle v:1.0.1
dlload
typealias 'VALUE', 'unsigned long long'
RBasic = struct [
'VALUE flags',
'VALUE klass'
]
Aux = union [
'long capa',
'VALUE shared'
]
Heap = struct [
'long len',
'char *ptr',
{aux: Aux}
]
As = union [
{heap: Heap},
'char ary[24]'
]
RString = struct [
{basic: RBasic},
{as: As}
]
# Heap string.
rs = RString.new dlwrap(RUBY_COPYRIGHT)
p flags: rs.basic.flags
p class: Pointer.new(rs.basic.klass).to_value
p length: rs.as.heap.len
p pointer: rs.as.heap.ptr.to_s
p capacity: rs.as.heap.aux.capa
p shared: rs.as.heap.aux.shared
p stack: rs.as.ary.pack('C*')
puts "-" * 80
# Stack string.
rs = RString.new dlwrap("ruby")
p flags: rs.basic.flags
p class: Pointer.new(rs.basic.klass).to_value
p length: rs.as.heap.len
p pointer: rs.as.heap.ptr
p capacity: rs.as.heap.aux.capa
p shared: rs.as.heap.aux.shared
p stack: rs.as.ary.pack('C*')
__END__
{:flags=>1082140773}
{:class=>String}
{:length=>49}
{:pointer=>"ruby - Copyright (C) 1993-2020 Yukihiro Matsumoto"}
{:capacity=>49}
{:shared=>49}
{:stack=>"1\x00\x00\x00\x00\x00\x00\x00\x80V\r\xAC\x91\x7F\x00\x001\x00\x00\x00\x00\x00\x00\x00"}
--------------------------------------------------------------------------------
{:flags=>5308421}
{:class=>String}
{:length=>2036495730}
{:pointer=>#<Fiddle::Pointer:0x00005648352bb110 ptr=0x0000000000000000 size=0 free=0x0000000000000000>}
{:capacity=>0}
{:shared=>0}
{:stack=>"ruby\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment