summaryrefslogtreecommitdiff
path: root/src/JavaScript/Val.hs
blob: e1879b46d120b6af7d19edca613f2919702a2a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module JavaScript.Val (
    module GHC.Wasm.Prim,
    ToJSVal(..), FromJSVal(..),
    nullToNothing,
) where

import GHC.Wasm.Prim


class ToJSVal a where
    toJSVal :: a -> JSVal

class FromJSVal a where
    fromJSValUnchecked :: JSVal -> a

instance ToJSVal JSVal where toJSVal = id
instance FromJSVal JSVal where fromJSValUnchecked = id


nullToNothing :: FromJSVal a => JSVal -> Maybe a
nullToNothing val | isNull val = Nothing
                  | otherwise  = Just $ fromJSValUnchecked val
foreign import javascript unsafe "$1 === null"
    isNull :: JSVal -> Bool