on master help sign in
Support unit values flowing through ports
commitc8eeedf0f2f7cab265638b5272ce978ce2d63fde authorEvan Czaplicki <info@elm-lang.org> dateThu 11 Sep 2014, 13:23 (12 years ago) parentaa00bf8
2 files changed+17−10
compiler/Generate/JavaScript/Ports.hs+16−10 compiler/Type/ExtraChecks.hs+1
compiler/Generate/JavaScript/Ports.hs+16−10
@@ -52,6 +52,7 @@ inc tipe x =
5252
5353 Type name
5454 | Var.isJson name -> V.value "Native.Json" "fromJS" <| x
55+ | Var.isTuple name -> incomingTuple [] x
5556 | otherwise -> error "bad type got to incoming port generation code"
5657
5758 App f args ->
@@ -68,16 +69,7 @@ inc tipe x =
6869 array = DotRef () x (var "map") <| incoming t
6970
7071 Type name : ts
71− | Var.isTuple name -> check x JSArray tuple
72− where
73− tuple = ObjectLit () $ (prop "ctor", ctor) : values
74−
75− ctor = string ("_Tuple" ++ show (length ts))
76− values = zipWith convert [0..] ts
77−
78− convert n t = ( prop ('_':show n)
79− , inc t (BracketRef () x (IntLit () n))
80− )
72+ | Var.isTuple name -> incomingTuple ts x
8173
8274 _ -> error "bad ADT got to incoming port generation code"
8375
@@ -91,6 +83,19 @@ inc tipe x =
9183 keys = map convert fields
9284 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
9385
86+incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
87+incomingTuple types x =
88+ check x JSArray (ObjectLit () fields)
89+ where
90+ fields = (prop "ctor", ctor) : zipWith convert [0..] types
91+
92+ ctor = string ("_Tuple" ++ show (length types))
93+
94+ convert n t =
95+ ( prop ('_':show n)
96+ , inc t (BracketRef () x (IntLit () n))
97+ )
98+
9499outgoing :: CanonicalType -> Expression ()
95100outgoing tipe =
96101 case tipe of
@@ -127,6 +132,7 @@ out tipe x =
127132
128133 Type name
129134 | Var.isJson name -> V.value "Native.Json" "toJS" <| x
135+ | Var.isTuple name -> ArrayLit () []
130136 | otherwise -> error "bad type got to outgoing port generation code"
131137
132138 App f args ->
compiler/Type/ExtraChecks.hs+1
@@ -65,6 +65,7 @@ portTypes expr =
6565 ST.Type v
6666 | V.isJson v -> return ()
6767 | V.isPrimitive v -> return ()
68+ | V.isTuple v -> return ()
6869 | otherwise -> err "an unsupported type"
6970
7071 ST.App t [] -> valid t