diff options
author | Aaron Patterson <tenderlove@ruby-lang.org> | 2023-01-20 10:57:23 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2023-01-20 14:11:05 -0800 |
commit | 06b62cbbdd560c6210d48a50ba99cfdc2f110dfe (patch) | |
tree | 2e6a7d617448295b4f3f3cbfcb7430f7649b4026 /lib/ruby_vm | |
parent | 887d21613cb62f7e964474131dcdf73034f879af (diff) | |
download | ruby-06b62cbbdd560c6210d48a50ba99cfdc2f110dfe.tar.gz |
Add `offsetof` so we can get the offset of members
I want to get the offset of fields inside structs, but I don't want to
instantiate the struct. I need to embed the offsets inside machine
code, and I can't get the offsets without calling `new` on the struct.
This commit adds an `offset` method so you can get the offset of a
member without instantiating anything. You can do:
```ruby
C.rb_control_frame_t.offsetof(:sp) #=> 8
```
I don't think this implementation is perfect, you can only get immediate
fields. But it is better than nothing!
Diffstat (limited to 'lib/ruby_vm')
-rw-r--r-- | lib/ruby_vm/mjit/c_pointer.rb | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/ruby_vm/mjit/c_pointer.rb b/lib/ruby_vm/mjit/c_pointer.rb index a92c2140ae..de45c171c3 100644 --- a/lib/ruby_vm/mjit/c_pointer.rb +++ b/lib/ruby_vm/mjit/c_pointer.rb @@ -54,6 +54,12 @@ module RubyVM::MJIT # :nodoc: all # Return the size of this type define_singleton_method(:sizeof) { sizeof } + # Get the offset of a member named +name+ + define_singleton_method(:offsetof) { |name| + _, offset = members.fetch(name) + offset / 8 + } + define_method(:initialize) do |addr = nil| if addr.nil? # TODO: get rid of this feature later addr = Fiddle.malloc(sizeof) |