forked from rzk-lang/rzk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
39 lines (30 loc) · 1.27 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified GHCJS.Foreign.Callback as GHCJS
import GHCJS.Marshal (fromJSVal, toJSVal)
import GHCJS.Prim (JSVal)
import Data.JSString(JSString, pack)
import JavaScript.Object
import JavaScript.Object.Internal (Object (..), create)
import qualified Rzk.Main as Rzk
import System.IO
main :: IO ()
main = do
putStr "Haskell logic core starting"
hFlush stdout
-- https://discourse.haskell.org/t/compile-library-with-ghcjs/4727
callback <- GHCJS.syncCallback1 GHCJS.ThrowWouldBlock $ \jsval -> do
let o = Object jsval
rawInput <- getProp "input" o
input <- maybe (Left "Could not turn JSRef to a String") Right <$> fromJSVal rawInput
case Rzk.typecheckString =<< input of
Left err -> setStringProp "status" "error" o >> setStringProp "result" (pack err) o
Right ok -> setStringProp "status" "ok" o >> setStringProp "result" (pack ok) o
set_rzk_typecheck_callback callback
putStr "Haskell logic core callbacks initialized"
setStringProp :: JSString -> JSString -> Object -> IO ()
setStringProp name valueRaw object = do
value <- toJSVal valueRaw
setProp name value object
foreign import javascript unsafe "rzkTypecheck_ = $1"
set_rzk_typecheck_callback :: GHCJS.Callback (JSVal -> IO ()) -> IO ()