Skip to content

FFI::Library#enum_value difference between MRI and JRuby #1954

@Burgestrand

Description

@Burgestrand

Here's the definition of FFI::Library#enum_value:

def enum_value(symbol)

When an enum value does not exist, MRI will return nil for FFI::Library#enum_value, but JRuby will raise an error. Here's code to reproduce the error:

require "ffi"

module MyLibrary
  extend FFI::Library

  enum :something, [:one, :two]
end

p MyLibrary.enum_value(:nonexistant)

MRI prints nil, JRuby raises an error. Here's the JRuby error stacktrace:

NoMethodError: undefined method `default' for #<FFI::Enums:0x3e19d688>
            [] at org/jruby/RubyHash.java:1087
  __map_symbol at org/jruby/ext/ffi/Enums.java:125
    enum_value at /Users/Kim/.rvm/rubies/jruby-1.7.12/lib/ruby/shared/ffi/library.rb:456
        (root) at repro.rb:9

Looking at the stack trace I believe it's because Enums (Enums.java) inherits from Hash, and when the lookup fails a default value is attempted to be retrieved but for some reason that method does not exist in FFI::Enums, and an error occurs.

I've worked around it locally with the following monkey patch:

unless FFI::Enums.method_defined?(:default)
  FFI::Enums.send(:attr_accessor, :default)
end

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions