Quid2 / flat

Principled and efficient binary serialization
BSD 3-Clause "New" or "Revised" License
60 stars 17 forks source link

Encoding does not seem to be portable between GHC and GHCJS #10

Closed tysonzero closed 6 years ago

tysonzero commented 6 years ago

The following code:

print . fmap ord . unpack $ flat (Right (20, 30) :: Either Int (Double, Double))

Gives [160,26,0,0,0,0,0,0,32,31,0,0,0,0,0,0,1] in GHC.

But [32,26,0,0,128,0,0,0,32,31,0,0,0,0,0,0,1] in GHCJS.

Specifically ghc 8.0.2 and ghcjs 0.2.1 (ghc 8.0.2).

It seems like a lot of instances are exactly the same, but sum types (both Generic-derived and Either) seem to have different first bytes. Which then causes TooMuchSpace errors on the ghc/server end.

tittoassini commented 6 years ago

I am travelling for a few days but I will have a look at this as soon as possible.

I have used flat with GHCJS but I never run the full test suite, obviously this was a mistake :-(

tittoassini commented 6 years ago

What is happening is that the 64 bit shiftL under ghcjs is behaving like a 32 bit rollL.

The result is that the bit that corresponds to 'Right' gets moved from the first byte '160' to the fifth byte '128'.

I will look further into it tomorrow.

tittoassini commented 6 years ago

Or possibly a byte begin extended to 32 rather than 64 bits.

Anyway, it should not be hard to fix.

tittoassini commented 6 years ago

The version of flat on github should be working now, it passes all tests.

I will make a hackage release in a few days time, in the meantime, you can refer to the github version as:

- location:
     git: https://github.com/Quid2/flat
     commit: 4795b519e2bd58127044d54b69dc371018607366
  extra-dep: true 

Let me know if it works for you.

I have tested it with this ghcjs:

resolver: lts-7.19
compiler: ghcjs-0.2.1.9007019_ghc-8.0.1
compiler-check: match-exact
setup-info:
  ghcjs:
    source:
      ghcjs-0.2.1.9007019_ghc-8.0.1:
           url: http://ghcjs.tolysz.org/ghc-8.0-2017-02-05-lts-7.19-9007019.tar.gz
           sha1: d2cfc25f9cda32a25a87d9af68891b2186ee52f9

Which version do you use?

tysonzero commented 6 years ago

I upgraded to GHCJS 8.4 and the existing version started working. That looks like a similar version of GHCJS to the version I was previously using, so I can check that out later. Thanks!

tittoassini commented 6 years ago

This sounds like bad news, the fixed version might not work with this latest compiler.

Can you give me the stack.yaml stanza for this new ghcjs compiler you are using?

tysonzero commented 6 years ago

I do not use stack as I find nix to be better for GHCJS development. Here is the github.json of the versions I have been using:

older:

{
  "owner": "ghcjs",
  "repo": "ghcjs",
  "rev": "eb8670c4b03b4a0ff5dd7ac3f524b410b37051ea",
  "sha256": "13s8wz9gk82cpwskc7ic3fvcpy572g87ny4icmj1apfzrrcpdp1v"
}

newer:

{
  "url": "https://github.com/ghcjs/ghcjs",
  "rev": "d20da90a4819faad1c6309a06363b34edac0374c",
  "sha256": "0jmxgfm1zwg6xscjcaycfam7zss8ik4ql4ii5lpryh4h6cdhvkbr",
  "fetchSubmodules": true
}
tittoassini commented 6 years ago

I have just released flat-0.3.4.

I have tested it with a few different ghcjs versions and it seems ok.