summaryrefslogtreecommitdiff
path: root/src/JavaScript/Val.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/JavaScript/Val.hs')
-rw-r--r--src/JavaScript/Val.hs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/JavaScript/Val.hs b/src/JavaScript/Val.hs
new file mode 100644
index 0000000..e1879b4
--- /dev/null
+++ b/src/JavaScript/Val.hs
@@ -0,0 +1,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