Skip to content

Detect fake constants / platforms? #105

Description

@ahorek

moving the discussion from jruby/jruby#6843

we have a set of constants based on the platform we know
https://github.com/jnr/jnr-constants/blob/master/src/main/java/jnr/constants/platform/linux/AddressFamily.java
and some of them aren't defined

if we load them like this
https://github.com/jruby/jruby/blob/8f25a744ef7f24bbda63cdf00f72784192102d76/core/src/main/java/org/jruby/Ruby.java#L4597

require 'socket'
Socket.constants =>[AF_UNSPEC, AF_LOCAL, ....]

but for instance AF_IMPLINK won't be in the list on Linux

this is useful for feature checks like the one in Puma
https://github.com/puma/puma/blob/20dc923b0b83a829cc3e2578a2b2c0b854985c18/lib/puma/server.rb#L124

however, if there's a platform that isn't supported, we do load "fake constants"
https://github.com/jnr/jnr-constants/blob/master/src/main/java/jnr/constants/platform/fake/AddressFamily.java

in this case, suddenly Socket.constants will include all possible constants, so
Socket.const_defined?(:IPPROTO_TCP) will return true and the code that expects the feature to be supported breaks.

we're saying that we do support everything, but it's a false assumption.

I think it would be nice to be able to detect if these constants are real or not.
Note that fake constants aren't close to any platform and any code that depends on correct values for doing native calls will almost for sure break (mostly on unexpected and hard-to-track errors).
Of course, I can do a second platform check in JRuby to achieve the same goal, but it would be cleaner if jnr-constants could handle platform differences and give me the information about usability instead of trying to fake it.

this is just a topic for discussion based on the previous task I was working on. I have a workaround so feel free to close it :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions