blob: 311c2634f4d51a1938b5450b3e1931561be0fba0 (
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
25
26
27
28
29
30
31
32
33
|
import GHC.JS.Prim
import GHC.JS.Foreign.Callback
foreign import javascript "((f) => { globalF = f; })"
setF :: Callback (JSVal -> IO ()) -> IO ()
foreign import javascript "((x) => { globalF(x); })"
callF :: JSVal -> IO ()
foreign import javascript "((x,y) => { return x + y })"
js_plus :: JSVal -> JSVal -> IO JSVal
foreign import javascript "((g) => { globalG = g; })"
setG :: Callback (JSVal -> JSVal -> IO JSVal) -> IO ()
foreign import javascript "((x,y) => { return globalG(x,y); })"
callG :: JSVal -> JSVal -> IO JSVal
main :: IO ()
main = do
-- Set functions globally on the JavaScript side, to be accessed in regular JavaScript code
f <- syncCallback1 ThrowWouldBlock (\x -> if isNull x then putStrLn "isNull" else putStrLn "isNotNull")
g <- syncCallback2' js_plus
setF f
setG g
-- Do other things before using the globally-set functions
putStrLn "test"
-- Use the globally-set functions
callF jsNull
callF $ toJSString ""
print . fromJSInt =<< callG (toJSInt 1) (toJSInt 2)
|