valderman / haste-compiler

A GHC-based Haskell to JavaScript compiler
http://haste-lang.org
BSD 3-Clause "New" or "Revised" License
1.45k stars 109 forks source link

output *almost* works in node.js #403

Closed LaylBongers closed 7 years ago

LaylBongers commented 7 years ago

I'm trying to use Haste to compile Haskell for the node.js-based MMORTS programming game Screeps. Code produced by haste runs pretty much out of the box, with one exception. To make it run properly, I have to add the following to the end of the file:

hasteMain();

This is because main is triggered using window.onload, which is never called in node.js. Other than that it seems to work fine. Is there a more elegant solution that could be done for this?

valderman commented 7 years ago

You can compile your program with the --onexec flag. This launches appends a call to hasteMain to the program output.

I've never heard of Screeps, but if your code involves any kind of file manipulation or other OS calls, readFile and the other usual suspects from base won't work. The reasoning is that if you want to run your code on some platform where manipulating files, creating threads, etc. makes sense, it'd very likely be both easier and faster to just use vanilla GHC. Seems like games such as this might be an exception to that rule. Of course, you can wrap the native node.js facilities for this functionality using the FFI.

LaylBongers commented 7 years ago

How would I add the --onexec flag when building my project with cabal? Screeps doesn't have files anyways, so that's no problem.

LaylBongers commented 7 years ago

For other people who come past this, add this to your executable bit in the cabal file:

ghc-options: "--onexec"