Running the Haskell compiled to JavaScript on the JVM : <p>Java 8 has an inbuilt JavaScript engine called Nashorn so it is actually possible to run Haskell compiled to JavaScript on the JVM.</p>

<p>The following program works:</p>

<pre><code>{-# LANGUAGE JavaScriptFFI #-}

module Main where

foreign import javascript unsafe "console={log: function(s) { java.lang.System.out.print(s); }}"
  setupConsole :: IO ()

foreign import javascript unsafe "java.lang.System.exit($1)"
  sysexit :: Int -&gt; IO ()

main = do
  setupConsole
  putStrLn "Hello from Haskell!"
  sysexit 0
</code></pre>

<p>We can run it with: (<em>Side note:</em> It is possible to run this as a normal Java program.<code>jjs</code> is just a convenient way to run pure JavaScript code on the JVM)</p>

<pre><code>$ ghcjs -o Main Main.hs
[1 of 1] Compiling Main             ( Main.hs, Main.js_o )
Linking Main.jsexe (Main)

$ which jjs
~/bin/jdk/bin/jjs

$ jjs Main.jsexe/all.js
Hello from Haskell!
</code></pre>

<p>In the above code, <code>console.log</code> needs to be defined using <code>java.lang.System.print</code> as Nashorn doesn't provide the default global <code>console</code> object and Haskell's <code>putStrLn</code> otherwise doesn't seem to be printing anything.</p>

<p>The other thing is that the JVM needs to be exited with <code>sysexit</code> FFI function implemented with <code>java.lang.System.exit</code>. </p>

<p>I have 2 questions:</p>

<ol>
<li>Similar to <code>console.log</code>, what other host dependencies are assumed in ghcjs that have to be defined?</li>
<li>Is the JVM not shutting down normally because of ghcjs creating an event loop in the background or some other reason? Is there any way to avoid that and make the program exit normally?</li>
</ol>
