Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
reflex-frp
GitHub Repository: reflex-frp/reflex-platform
Path: blob/develop/haskell-overlays/text-jsstring/hashable.patch
1 views
1
diff --git a/hashable.cabal b/hashable.cabal
2
index 4197060..1d3cd53 100644
3
--- a/hashable.cabal
4
+++ b/hashable.cabal
5
@@ -84,6 +84,9 @@ library
6
, ghc-prim
7
, text >=0.12 && <1.3
8
9
+ if impl(ghcjs)
10
+ Build-depends: ghcjs-base
11
+
12
if impl(ghc >=9)
13
build-depends: ghc-bignum ==1.0.* || ==1.2.*
14
15
diff --git a/src/Data/Hashable/Class.hs b/src/Data/Hashable/Class.hs
16
index c7dd8c5..036f450 100644
17
--- a/src/Data/Hashable/Class.hs
18
+++ b/src/Data/Hashable/Class.hs
19
@@ -5,6 +5,10 @@
20
21
{-# LANGUAGE Trustworthy #-}
22
23
+#ifdef __GHCJS__
24
+{-# LANGUAGE JavaScriptFFI, UnboxedTuples, GHCForeignImportPrim #-}
25
+#endif
26
+
27
#if __GLASGOW_HASKELL__ >= 801
28
{-# LANGUAGE PolyKinds #-} -- For TypeRep instances
29
#endif
30
@@ -72,7 +76,9 @@ import Data.Int (Int8, Int16, Int32, Int64)
31
import Data.List (foldl')
32
import Data.Ratio (Ratio, denominator, numerator)
33
import qualified Data.Text as T
34
+#ifndef __GHCJS__
35
import qualified Data.Text.Array as TA
36
+#endif
37
import qualified Data.Text.Internal as T
38
import qualified Data.Text.Lazy as TL
39
import Data.Version (Version(..))
40
@@ -92,6 +98,9 @@ import qualified Data.Map as Map
41
import qualified Data.Sequence as Seq
42
import qualified Data.Set as Set
43
import qualified Data.Tree as Tree
44
+#ifdef __GHCJS__
45
+import Data.JSString (JSString)
46
+#endif
47
48
-- As we use qualified F.Foldable, we don't get warnings with newer base
49
import qualified Data.Foldable as F
50
@@ -152,7 +161,11 @@ import GHC.Exts (Int (..), sizeofByteArray#)
51
# define MIN_VERSION_integer_gmp_1_0_0
52
# endif
53
54
+#ifndef __GHCJS__
55
import GHC.Exts (Int(..))
56
+#else
57
+import GHC.Exts (Int(..), Int#)
58
+#endif
59
import GHC.Integer.GMP.Internals (Integer(..))
60
# if defined(MIN_VERSION_integer_gmp_1_0_0)
61
import GHC.Exts (sizeofByteArray#)
62
@@ -676,17 +689,31 @@ instance Hashable BSI.ShortByteString where
63
#endif
64
65
instance Hashable T.Text where
66
+#ifndef __GHCJS__
67
hashWithSalt salt (T.Text arr off len) =
68
hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1)
69
(hashWithSalt salt len)
70
+#else
71
+ hashWithSalt salt (T.Text txt) =
72
+ hashByteArrayWithSalt ba (0 `shiftL` 1) (I# len `shiftL` 1)
73
+ (hashWithSalt salt (I# len))
74
+ where (# ba, len #) = js_textFromJSString txt
75
+#endif
76
77
instance Hashable TL.Text where
78
hashWithSalt salt = finalise . TL.foldlChunks step (SP salt 0)
79
where
80
finalise (SP s l) = hashWithSalt s l
81
+#ifndef __GHCJS__
82
step (SP s l) (T.Text arr off len) = SP
83
(hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1) s)
84
(l + len)
85
+#else
86
+ step (SP s l) (T.Text txt) = SP
87
+ (hashByteArrayWithSalt ba (0 `shiftL` 1) (I# len `shiftL` 1) s)
88
+ (l + I# len)
89
+ where (# ba, len #) = js_textFromJSString txt
90
+#endif
91
92
-- | Compute the hash of a ThreadId.
93
hashThreadId :: ThreadId -> Int
94
@@ -880,6 +907,12 @@ instance Hashable1 Option where liftHashWithSalt h salt (Option a) = liftHashWit
95
#endif
96
#endif
97
98
+#ifdef __GHCJS__
99
+foreign import javascript unsafe
100
+ "h$textFromString"
101
+ js_textFromJSString :: JSString -> (# ByteArray#, Int# #)
102
+#endif
103
+
104
-- instances for @Data.Functor.{Product,Sum,Compose}@, present
105
-- in base-4.9 and onward.
106
#if MIN_VERSION_base(4,9,0)
107
108