Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

FFI::Platform::BYTE_ORDER, FFI::Platform::LITTLE_ENDIAN, FFI::Platform::BIG_ENDIAN #2098

Closed
wants to merge 2 commits into from

3 participants

@DanielVartanov

Original ffi gem provide these constant, some gems rely on them.

@jc00ke jc00ke commented on the diff
kernel/platform/ffi.rb
@@ -257,4 +257,8 @@ def self.mac?
def self.unix?
! windows?
end
+
+ LITTLE_ENDIAN = :little
+ BIG_ENDIAN = :big
+ BYTE_ORDER = Rubinius::ENDIAN == :little ? LITTLE_ENDIAN : BIG_ENDIAN
@jc00ke Owner
jc00ke added a note

This is kinda cryptic. I'd prefer to see the following.

if Rubinius::ENDIAN == :little # should this just be LITTLE_ENDIAN since it was just defined?
  BYTE_ORDER = LITTLE_ENDIAN
else
  BYTE_ORDER = BIG_ENDIAN
end

Well, if ternary operator looks cryptic, there is no problem to change it, I'll update.

should this just be LITTLE_ENDIAN since it was just defined?

It's complicated. The API off ffi assumes only that BYTE_ORDER constant will be equal to LITTLE_ENDIAN constant [or another one], without specifying a value of those constants.

In ffi, LITTLE_ENDIAN is an integer value (look here) and can be changed. I had to assign some value and I chose :little, it could be anything actually.

In Rubinius, Rubinius::ENDIAN constant is assigned here. Therefore, Rubinius::ENDIAN has nothing to do with FFI::Platform::LITTLE_ENDIAN. I'd call it dangerous if we would rely on equality of these constants just because the same symbol is assigned to both of them in two completely different parts of codebase.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jc00ke jc00ke commented on the diff
spec/ruby/optional/ffi/platform_spec.rb
@@ -125,3 +125,17 @@
end
end
end
+
+describe "FFI::Platform::BYTE_ORDER" do
+ little_endian do
+ it "should be equal to LITTLE_ENDIAN" do
@jc00ke Owner
jc00ke added a note

We don't use the word should in descriptions.

it "equals LITTLE_ENDIAN" do
   # ...
end

Yes, right, I made the same mistake twice. Thank you for noticing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@brixen
Owner

Please hold off on more FFI PRs. I'm going to work on getting the ffi gem to run on Rubinius.

@brixen
Owner

We now bundle a 1.2.0 version of the FFI gem, however, there is a bug in RVM that omits the pre-installed gems. So if using RVM, use the recently released 1.3.0 FFI gem which builds correctly on Rubinius.

@brixen brixen closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 0 deletions.
  1. +4 −0 kernel/platform/ffi.rb
  2. +14 −0 spec/ruby/optional/ffi/platform_spec.rb
View
4 kernel/platform/ffi.rb
@@ -257,4 +257,8 @@ def self.mac?
def self.unix?
! windows?
end
+
+ LITTLE_ENDIAN = :little
+ BIG_ENDIAN = :big
+ BYTE_ORDER = Rubinius::ENDIAN == :little ? LITTLE_ENDIAN : BIG_ENDIAN
@jc00ke Owner
jc00ke added a note

This is kinda cryptic. I'd prefer to see the following.

if Rubinius::ENDIAN == :little # should this just be LITTLE_ENDIAN since it was just defined?
  BYTE_ORDER = LITTLE_ENDIAN
else
  BYTE_ORDER = BIG_ENDIAN
end

Well, if ternary operator looks cryptic, there is no problem to change it, I'll update.

should this just be LITTLE_ENDIAN since it was just defined?

It's complicated. The API off ffi assumes only that BYTE_ORDER constant will be equal to LITTLE_ENDIAN constant [or another one], without specifying a value of those constants.

In ffi, LITTLE_ENDIAN is an integer value (look here) and can be changed. I had to assign some value and I chose :little, it could be anything actually.

In Rubinius, Rubinius::ENDIAN constant is assigned here. Therefore, Rubinius::ENDIAN has nothing to do with FFI::Platform::LITTLE_ENDIAN. I'd call it dangerous if we would rely on equality of these constants just because the same symbol is assigned to both of them in two completely different parts of codebase.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
View
14 spec/ruby/optional/ffi/platform_spec.rb
@@ -125,3 +125,17 @@
end
end
end
+
+describe "FFI::Platform::BYTE_ORDER" do
+ little_endian do
+ it "should be equal to LITTLE_ENDIAN" do
@jc00ke Owner
jc00ke added a note

We don't use the word should in descriptions.

it "equals LITTLE_ENDIAN" do
   # ...
end

Yes, right, I made the same mistake twice. Thank you for noticing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ FFI::Platform::BYTE_ORDER.should == FFI::Platform::LITTLE_ENDIAN
+ end
+ end
+
+ big_endian do
+ it "should be equal to BIG_ENDIAN" do
+ FFI::Platform::BYTE_ORDER.should == FFI::Platform::BIG_ENDIAN
+ end
+ end
+end
Something went wrong with that request. Please try again.