Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve check to detect if a module has a #find_type method suitable for FFI #777

Merged
merged 1 commit into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ffi/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ def aligned(alignment = 1)
def enclosing_module
begin
mod = self.name.split("::")[0..-2].inject(Object) { |obj, c| obj.const_get(c) }
(mod < FFI::Library || mod < FFI::Struct || mod.respond_to?(:find_type)) ? mod : nil
if mod.respond_to?(:find_type) && (mod.is_a?(FFI::Library) || mod < FFI::Struct)
mod
end
rescue Exception
nil
end
Expand Down
17 changes: 17 additions & 0 deletions spec/ffi/struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,23 @@ class TestStruct < FFI::Struct
instance[:number] = 0xA1
expect(FFISpecs::LibTest.ptr_ret_int32_t(instance, 0)).to eq(0xA1)
end

it "ignores a module which does not extend FFI::Library or subclass FFI::Struct" do
module FFISpecs::UnrelatedFindTypeTest
# MakeMakefile from 'mkmf' defines such a method
def self.find_type(*args)
raise "should not be called"
end

class TestStruct < FFI::Struct
layout :number, :int
end
end

instance = FFISpecs::UnrelatedFindTypeTest::TestStruct.new
instance[:number] = 123
expect(FFISpecs::LibTest.ptr_ret_int32_t(instance, 0)).to eq(123)
end
end
end

Expand Down