1

So I noticed, while answering this question, that the one who asked the question appears to be a javascript developer. And as the code I wrote in haskell is easy enough, I thought I give haste a try and try to compile it to javascript.

So, I downloaded the Windows binary package of haste (why does the .msi require a reboot?!!?), added it to my path, issued haste-cabal update and haste-cabal install split and after a bit of reading the output of hastec --help, I issued:

PS E:\h\stackoverflow> hastec -o hexagon.js --pretty-print hexagon.hs
as my best guess on how to get the output I am looking for.

Opposite to my expectation, haste output was this:

hastec.exe: user error (shell expression failed in readModule: Data.Binary.Get.runGet at position 8: not enough bytes)

So, my question: What do I have to do to get a java script source file?

Community
  • 1
  • 1
BitTickler
  • 10,905
  • 5
  • 32
  • 53
  • While waiting for help, I ported my hexagon.hs to hexagon.fsx, npm install fable, tweaked my f# code till fable could handle it and produced a not so terrrible looking hexagon.js. Not tested if it works yet, though :) – BitTickler Oct 26 '16 at 10:59

1 Answers1

1

Is it possible that you have an old version of Haste lying around, or have intermediate files (.jsmod, for instance) from a different version of the compiler in your source directory? This sounds like the (quite unhelpful) error message Haste produces when it runs into a corrupted intermediate file.

Check that the version of the binary you're calling is what you expect (hastec --version). Then, try getting rid of all intermediate files in the directory as well as any files in %USERPROFILE%\AppData\Roaming\haste, reinstalling split, and recompiling with the -fforce-recomp flag. You should also add a main function, so that Haste has an entry point to your program from which to start linking. If all you want to do is to make some Haskell function available to external JavaScript, you can use the export foreign function interface:

{-# LANGUAGE OverloadedStrings #-}
module Main where
import Haste.Foreign
import Hexagon

main = export "picture" Hexagon.picture

You will probably also want to compile your program with the --onexec flag, to make sure that main runs and exports picture immediately when loaded, and not on page load which is the default:

> hastec -o hexagon.js --pretty-print --onexec hexagon.hs

After doing this, any code included after hexagon.js will be able to call e.g. Haste.picture(5); in order to produce a picture of size 5.

(Re: MSI installer requiring a reboot, this is required since it adds the Haste binaries to your %PATH%, which does not take effect immediately. I assume that a re-login would be enough to make it take effect, however.)

valderman
  • 8,365
  • 4
  • 22
  • 29
  • I had indeed some strange .fsmod files in that directory. Regarding main ... I just want to create a javascript file I can then use from an html page. Is that a non-use-case? – BitTickler Oct 26 '16 at 12:34
  • ``hastec --version`` yields: ``0.5.5.1`` btw. – BitTickler Oct 26 '16 at 12:40
  • ``hastec --dont-link -o hexagon_haste.js --pretty-print hexagon.hs`` now runs without error and produces a ``hexagon.jsmod`` but no ``hexagon_haste.js`` file. – BitTickler Oct 26 '16 at 12:50
  • To call Haskell functions from JavaScript, you need to use the [foreign function interface](http://haste-lang.org/docs/haddock/0.5.5/Haste-Foreign.html). See edited answer for details. – valderman Oct 26 '16 at 13:30
  • Thanks a lot. Finally I got the hang of how it all comes together. Using your main.hs, then removing all previous .jsmod and .js files and using your command line after recreating the main.jsmod, it all works as expected. – BitTickler Oct 26 '16 at 13:50