From: Bryan Duxbury Date: Tue, 24 Mar 2009 00:34:16 +0000 (+0000) Subject: THRIFT-385. hs: 64-bit integer and double types incorrectly serialized on 32-bit... X-Git-Tag: 0.2.0~239 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=f3c83cf88e2a1978d68b1e74dbd7d7b04569dfd5;p=common%2Fthrift.git THRIFT-385. hs: 64-bit integer and double types incorrectly serialized on 32-bit platforms Use 64-bit types where appropriate. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@757619 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc index 0ea5ddcc..580340da 100644 --- a/compiler/cpp/src/generate/t_hs_generator.cc +++ b/compiler/cpp/src/generate/t_hs_generator.cc @@ -1453,7 +1453,7 @@ string t_hs_generator::render_hs_type(t_type* type) { case t_base_type::TYPE_I32: return "Int"; case t_base_type::TYPE_I64: - return "Int"; + return "Int64"; case t_base_type::TYPE_DOUBLE: return "Double"; } diff --git a/lib/hs/src/TBinaryProtocol.hs b/lib/hs/src/TBinaryProtocol.hs index 2903d014..ff0df162 100644 --- a/lib/hs/src/TBinaryProtocol.hs +++ b/lib/hs/src/TBinaryProtocol.hs @@ -12,9 +12,12 @@ module TBinaryProtocol (TBinaryProtocol(..)) where version_mask = 0xffff0000 version_1 = 0x80010000; - getByte i b= 255 .&. (shiftR i (8*b)) + getByte :: Bits a => a -> Int -> a + getByte i b = 255 .&. (shiftR i (8*b)) + + getBytes :: (Bits a, Integral a) => a -> Int -> String getBytes i 0 = [] - getBytes i n = (toEnum (getByte i (n-1)) :: Char):(getBytes i (n-1)) + getBytes i n = (toEnum $ fromIntegral $ getByte i (n-1)):(getBytes i (n-1)) floatBits :: Double -> Word64 floatBits (D# d#) = W64# (unsafeCoerce# d#) @@ -40,7 +43,7 @@ module TBinaryProtocol (TBinaryProtocol(..)) where writeI16 (TBinaryProtocol tr) b = twrite tr (getBytes b 2) writeI32 (TBinaryProtocol tr) b = twrite tr (getBytes b 4) writeI64 (TBinaryProtocol tr) b = twrite tr (getBytes b 8) - writeDouble (TBinaryProtocol tr) b = writeI64 (TBinaryProtocol tr) (fromIntegral (floatBits b) :: Int) + writeDouble (TBinaryProtocol tr) b = writeI64 (TBinaryProtocol tr) (fromIntegral (floatBits b) :: Int64) writeString (TBinaryProtocol tr) s = do twrite tr (getBytes (length s) 4) twrite tr s writeBinary = writeString @@ -70,7 +73,7 @@ module TBinaryProtocol (TBinaryProtocol(..)) where readI32 (TBinaryProtocol tr) = do b <- treadAll tr 4 return $ (fromIntegral (fromIntegral (compBytes b) :: Int32) :: Int) readI64 (TBinaryProtocol tr) = do b <- treadAll tr 8 - return $ (fromIntegral (fromIntegral (compBytes64 b) :: Int64) :: Int) + return $ (fromIntegral (compBytes64 b) :: Int64) readDouble (TBinaryProtocol tr) = do b <- readI64 (TBinaryProtocol tr) return $ floatOfBits (fromIntegral b :: Word64) readBool (TBinaryProtocol tr) = do b <- readByte (TBinaryProtocol tr) diff --git a/lib/hs/src/Thrift.hs b/lib/hs/src/Thrift.hs index bb9f0bcd..74810a36 100644 --- a/lib/hs/src/Thrift.hs +++ b/lib/hs/src/Thrift.hs @@ -137,7 +137,7 @@ module Thrift (TransportExn(..),TransportExn_Type(..),TTransport(..), T_type(..) writeByte :: TTransport t => a t -> Int -> IO () writeI16 :: TTransport t => a t -> Int -> IO () writeI32 :: TTransport t => a t -> Int -> IO () - writeI64 :: TTransport t => a t -> Int -> IO () + writeI64 :: TTransport t => a t -> Int64 -> IO () writeDouble :: TTransport t => a t -> Double -> IO () writeString :: TTransport t => a t -> [Char] -> IO () writeBinary :: TTransport t => a t -> [Char] -> IO () @@ -157,7 +157,7 @@ module Thrift (TransportExn(..),TransportExn_Type(..),TTransport(..), T_type(..) readByte :: TTransport t => a t -> IO Int readI16 :: TTransport t => a t -> IO Int readI32 :: TTransport t => a t -> IO Int - readI64 :: TTransport t => a t -> IO Int + readI64 :: TTransport t => a t -> IO Int64 readDouble :: TTransport t => a t -> IO Double readString :: TTransport t => a t -> IO [Char] readBinary :: TTransport t => a t -> IO [Char]