diff options
author | Josh Meredith <joshmeredith2008@gmail.com> | 2023-03-06 09:18:03 +0000 |
---|---|---|
committer | Josh Meredith <joshmeredith2008@gmail.com> | 2023-04-11 10:27:20 +0000 |
commit | ffb4fc49cc57c411dcd2d83f8b5e6c3fb48923b7 (patch) | |
tree | 5951ce0b43bfc9870c026bd16099b9539547ec99 /testsuite/tests/javascript/js-callback03.hs | |
parent | 3ba77b369a170ba68f4eb5c8f3ae13e03dcbb28d (diff) | |
download | haskell-wip/js-exports.tar.gz |
Base/JS: GHC.JS.Foreign.Callback module (issue 23126)wip/js-exports
* Add the Callback module for "exporting" Haskell functions
to be available to plain JavaScript code
* Fix some primitives defined in GHC.JS.Prim
* Add a JavaScript section to the user guide with instructions
on how to use the JavaScript FFI, building up to using Callbacks
to interact with the browser
* Add tests for the JavaScript FFI and Callbacks
Diffstat (limited to 'testsuite/tests/javascript/js-callback03.hs')
-rw-r--r-- | testsuite/tests/javascript/js-callback03.hs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/testsuite/tests/javascript/js-callback03.hs b/testsuite/tests/javascript/js-callback03.hs new file mode 100644 index 0000000000..311c2634f4 --- /dev/null +++ b/testsuite/tests/javascript/js-callback03.hs @@ -0,0 +1,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) |