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

* See ffi/ffi#776
  • Loading branch information
eregon committed May 15, 2020
1 parent 981f372 commit c281640
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ Compatibility:
* Implement `UnboundMethod#bind_call`.
* Implemented `ObjectSpace::WeakMap` (#1385, #1958).
* Implemented `strtod` and `ruby_strtod` (#2007).
* Fix detection of `#find_type` in FFI to ignore `MakeMakefile#find_type` from `mkmf` (#1896, #2010).

Performance:

Expand Down
4 changes: 3 additions & 1 deletion lib/truffle/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 @@ -587,6 +587,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 c281640

Please sign in to comment.