Skip to content

Commit

Permalink
Improve check to detect if a module has a #find_type method suitable …
Browse files Browse the repository at this point in the history
…for FFI

* Fixes ffi#776
  • Loading branch information
eregon committed May 15, 2020
1 parent 33fb45a commit 717fbe4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ffi/struct.rb
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
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

0 comments on commit 717fbe4

Please sign in to comment.