I have a small cabal package whose functions I’d like to call from JavaScript code in the browser. All the GHCJS examples I’ve seen load a main.js
in the browser. Is it instead possible to call individual Haskell functions from JavaScript? Would one use main.js
to register the Haskell functions in the DOM and then call them from JavaScript code afterwards or is there a better way?
Perhaps you can wrap your Haskell functions in a CGI program and execute that via javascript? If your functions have output, you can write it as json. I did it like this with golang in the past.
Thanks, but running on the client (browser) is a specific requirement in this case.
Maybe have a look at how the UI of https://incredible.pm (written in plain JS) invokes the functions exposed by the HTML core, in particular this file:
It’s main
sets writes callbacks to the Haskell functions you want to expose to global JS variables, and then the JS code can just call them.
This is the JS code that calls these callbacks and uses a hack to return values:
This is rather old code, so maybe there are better ways to do it now, but it worked well for me
This sort of thing is more or less what I was after. Thanks!