@@ -9,6 +9,7 @@ module Data.Foreign
99 ) where
1010
1111import Data.Array
12+ import Data.Function
1213import Data.Either
1314import Data.Maybe
1415import Data.Tuple
@@ -17,36 +18,44 @@ import Global (Error(..))
1718
1819foreign import data Foreign :: *
1920
20- foreign import fromString
21- " function fromString ( str) { \
21+ foreign import fromStringImpl
22+ " function fromStringImpl(left, right, str) { \
2223 \ try { \
23- \ return Data_Either.Right (JSON.parse(str)); \
24+ \ return right (JSON.parse(str)); \
2425 \ } catch (e) { \
25- \ return Data_Either.Left (e.toString()); \
26+ \ return left (e.toString()); \
2627 \ } \
27- \}" :: String -> Either String Foreign
28-
29- foreign import readPrimType
30- " function readPrimType (typeName) { \
31- \ return function (value) { \
32- \ if (toString.call(value) == '[object ' + typeName + ']') { \
33- \ return Data_Either.Right(value);\
34- \ } \
35- \ return Data_Either.Left('Value is not a ' + typeName + ''); \
36- \ }; \
37- \}" :: forall a . String -> Foreign -> Either String a
28+ \}" :: Fn3 (String -> Either String Foreign ) (Foreign -> Either String Foreign ) String (Either String Foreign )
29+
30+ fromString :: String -> Either String Foreign
31+ fromString = runFn3 fromStringImpl Left Right
32+
33+ foreign import readPrimTypeImpl
34+ " function readPrimTypeImpl(left, right, typeName, value) { \
35+ \ if (toString.call(value) == '[object ' + typeName + ']') { \
36+ \ return right(value);\
37+ \ } \
38+ \ return left('Value is not a ' + typeName + ''); \
39+ \}" :: forall a . Fn4 (String -> Either String a ) (a -> Either String a ) String Foreign (Either String a )
40+
41+ readPrimType :: forall a . String -> Foreign -> Either String a
42+ readPrimType = runFn4 readPrimTypeImpl Left Right
3843
3944foreign import readMaybeImpl
40- " function readMaybeImpl (value) { \
41- \ return value === undefined || value === null ? Data_Maybe.Nothing : Data_Maybe.Just(value); \
42- \}" :: forall a . Foreign -> Maybe Foreign
43-
45+ " function readMaybeImpl(nothing, just, value) { \
46+ \ return value === undefined || value === null ? nothing : just(value); \
47+ \}" :: forall a . Fn3 (Maybe Foreign ) (Foreign -> Maybe Foreign ) Foreign (Maybe Foreign )
48+
49+ readMaybeImpl' :: Foreign -> Maybe Foreign
50+ readMaybeImpl' = runFn3 readMaybeImpl Nothing Just
51+
4452foreign import readPropImpl
45- " function readPropImpl (k) { \
46- \ return function (obj) { \
47- \ return Data_Either.Right(obj[k]);\
48- \ }; \
49- \}" :: forall a . String -> Foreign -> Either String Foreign
53+ " function readPropImpl(k, obj) { \
54+ \ return obj === undefined ? undefined : obj[k];\
55+ \}" :: forall a . Fn2 String Foreign Foreign
56+
57+ readPropImpl' :: String -> Foreign -> Foreign
58+ readPropImpl' = runFn2 readPropImpl
5059
5160foreign import showForeignImpl
5261 " var showForeignImpl = JSON.stringify;" :: Foreign -> String
@@ -105,13 +114,13 @@ instance readArray :: (ReadForeign a) => ReadForeign [a] where
105114 ForeignParser \_ -> arrayItem `traverse` (zip (range 0 (length xs)) xs)
106115
107116instance readMaybe :: (ReadForeign a ) => ReadForeign (Maybe a ) where
108- read = (ForeignParser $ Right <<< readMaybeImpl) >>= \x ->
117+ read = (ForeignParser $ Right <<< readMaybeImpl' ) >>= \x ->
109118 ForeignParser \_ -> case x of
110119 Just x' -> parseForeign read x' >>= return <<< Just
111120 Nothing -> return Nothing
112121
113122prop :: forall a . (ReadForeign a ) => String -> ForeignParser a
114- prop p = (ForeignParser \x -> readPropImpl p x) >>= \x ->
123+ prop p = (ForeignParser \x -> Right $ readPropImpl' p x) >>= \x ->
115124 ForeignParser \_ -> case parseForeign read x of
116125 Right result -> Right result
117126 Left err -> Left $ " Error reading property '" ++ p ++ " ':\n " ++ err
0 commit comments