summaryrefslogtreecommitdiff
path: root/testsuite/tests/javascript/js-callback02.hs
diff options
context:
space:
mode:
authorJosh Meredith <joshmeredith2008@gmail.com>2023-03-06 09:18:03 +0000
committerJosh Meredith <joshmeredith2008@gmail.com>2023-04-11 10:27:20 +0000
commitffb4fc49cc57c411dcd2d83f8b5e6c3fb48923b7 (patch)
tree5951ce0b43bfc9870c026bd16099b9539547ec99 /testsuite/tests/javascript/js-callback02.hs
parent3ba77b369a170ba68f4eb5c8f3ae13e03dcbb28d (diff)
downloadhaskell-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-callback02.hs')
-rw-r--r--testsuite/tests/javascript/js-callback02.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/testsuite/tests/javascript/js-callback02.hs b/testsuite/tests/javascript/js-callback02.hs
new file mode 100644
index 0000000000..3fc8896f54
--- /dev/null
+++ b/testsuite/tests/javascript/js-callback02.hs
@@ -0,0 +1,42 @@
+import GHC.JS.Prim
+import GHC.JS.Foreign.Callback
+
+foreign import javascript "(() => { return 1; })"
+ plus_one0 :: IO JSVal
+
+foreign import javascript "((x) => { return x + 1; })"
+ plus_one1 :: JSVal -> IO JSVal
+
+foreign import javascript "((x,y) => { return x + y + 1; })"
+ plus_one2 :: JSVal -> JSVal -> IO JSVal
+
+foreign import javascript "((x,y,z) => { return x + y + z + 1; })"
+ plus_one3 :: JSVal -> JSVal -> JSVal -> IO JSVal
+
+foreign import javascript "((f) => { return f(); })"
+ js_apply0 :: Callback (IO JSVal) -> IO JSVal
+
+foreign import javascript "((f,x) => { return f(x); })"
+ js_apply1 :: Callback (JSVal -> IO JSVal) -> JSVal -> IO JSVal
+
+foreign import javascript "((f,x,y) => { return f(x,y); })"
+ js_apply2 :: Callback (JSVal -> JSVal -> IO JSVal) -> JSVal -> JSVal -> IO JSVal
+
+foreign import javascript "((f,x,y,z) => { return f(x,y,z); })"
+ js_apply3 :: Callback (JSVal -> JSVal -> JSVal -> IO JSVal) -> JSVal -> JSVal -> JSVal -> IO JSVal
+
+logJSInt :: JSVal -> IO ()
+logJSInt = print . fromJSInt
+
+main :: IO ()
+main = do
+ plusOne0 <- syncCallback' plus_one0
+ plusOne1 <- syncCallback1' plus_one1
+ plusOne2 <- syncCallback2' plus_one2
+ plusOne3 <- syncCallback3' plus_one3
+
+ logJSInt =<< js_apply0 plusOne0
+ logJSInt =<< js_apply1 plusOne1 (toJSInt 2)
+ logJSInt =<< js_apply2 plusOne2 (toJSInt 2) (toJSInt 3)
+ logJSInt =<< js_apply3 plusOne3 (toJSInt 2) (toJSInt 3) (toJSInt 4)
+