8000 `Data.JSString.Int.decimal` crashes on large `Word64`. · Issue #570 · ghcjs/ghcjs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Data.JSString.Int.decimal crashes on large Word64. #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fisx opened this issue Mar 19, 2017 · 7 comments
Open

Data.JSString.Int.decimal crashes on large Word64. #570

fisx opened this issue Mar 19, 2017 · 7 comments

Comments

@fisx
Copy link
Contributor
fisx commented Mar 19, 2017

Build the following bit of code and run it in node:

import Data.Word
import GHC.Fingerprint
import Data.JSString
import Data.JSString.Int

w :: Word64
w = 16555890932631501216

d :: JSString
d = decimal w

main :: IO ()
main = do
  print w
  print d

Output

16555890932631501216
uncaught exception in Haskell main thread: TypeError: Cannot read property 'toString' of null
TypeError: Cannot read property 'toString' of null
    at h$ghcjszmbasezm0zi2zi0zi0zmBK9rIv0oPpkC2QVjbqIDmkZCDataziJSStringziIntzijszudecInteger_e 
[...]

I have seen this happen on resolver-7.13, resolver-7.1, resolver-7.19, and (I think) on e6ad1f2 31cebf9 but I have also seen it not to happen on 7.13.

Is it possible that I have to do more than stack clean in my project between changes of compiler version?

Thanks!

@fisx
Copy link
Contributor Author
fisx commented Mar 20, 2017

I'm a bit less confused now: it can be reproduced on the tip of branch ghc-8.0. If you change w to 1, there is no crash. So I'm pretty confident this is a ghcjs bug.

@fisx
Copy link
Contributor Author
fisx commented Mar 20, 2017

I've written a decimal clone that works. It is pretty awkward, but it may be good enough for the test suite, and it will keep me going until this is fixed.

import Prelude hiding (reverse, take, dropWhile)
import Data.JSString
import Data.JSString.Int
import Data.Monoid
import Data.Word
import GHC.Fingerprint

main :: IO ()
main = do
  test [16555890932631501216, 6555890932631501216]
  print (decimal (16555890932631501216 :: Word64))  -- crashes

test :: [Word64] -> IO ()
test [] = pure ()
test (w:ws) = do
  print w
  print (decimal_workaround_570 w)
  test ws

-- | See https://github.com/ghcjs/ghcjs/issues/570 for details.
decimal_workaround_570 :: Word64 -> JSString
decimal_workaround_570 w = dropleadingzeros . mconcat $ showpadded <$> chunks
  where
    n = 10^5

    chunks :: [Integer]
    chunks =
      [ (fromIntegral w `div` n^3) `mod` n
      , (fromIntegral w `div` n^2) `mod` n
      , (fromIntegral w `div` n)   `mod` n
      , fromIntegral w             `mod` n
      ]

    showpadded :: Integer -> JSString
    showpadded i = reverse . take 5 . reverse $ pack "00000" <> decimal i

    dropleadingzeros :: JSString -> JSString
    dropleadingzeros = dropWhile (== '0')

@fisx
Copy link
Contributor Author
fisx commented Mar 20, 2017

@fisx
Copy link
Contributor Author
fisx commented Aug 8, 2017

@luite
Copy link
Member
luite commented Sep 19, 2017

I can still see it with the latest version. Looks like a bug in either one of the Data.JSString conversion functions or the JavaScript bindings/backend for the integer-tmp package (GHC 8 got a new implementation)

@luite
Copy link
Member
luite commented Sep 19, 2017

found the problem, fix coming

@luite
Copy link
Member
luite commented Sep 20, 2017

fix in ghc-8.0 branch of ghcjs-boot:

ghcjs/ghcjs-boot@d033128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0