2012/09/13 Leave a comment
The hello world example Hello.hsused runs in a browser, popping up an alert:
module Hello where import Language.UHC.JS.Prelude import Language.UHC.JS.Assorted main = alert "Hi"
> git clone git://github.com/UU-ComputerScience/uhc-js.git uhcjs # read only access > uhc --import-path=uhcjs/uhc-js/src -tjs Hello.hs
This will create Hello.js and Hello.html; Hello.html loads both Hello.js and library modules, omitting most scripttags for brevity:
Opening Hello.htmlin a browser then pops up an alert box.
The problem with the resulting Hello.html is that it loads too much code; running a word count reveals that almost 2MB will be loaded!
This might be ok for locally running the html file, but now for network based access.
Luckily the -O optimization flag for UHCallows to specify in which compiler stage linking will take place:
> uhc --import-path=uhcjs/uhc-js/src -tjs -O,2 Hello.hs
With the -O flag both the amount of optimization may be specified (e.g. classical -O2) as well as the scope of it, the 2 behind the comma indicating that optimizations should be done on the whole program on the Core level (instead of just per module, being the default). Currently not many optimizations are in place in UHC but this mechanism links all imported modules on the Core level, only pulling in required code, thus automatically minimizing its size. The size of Hello.js now is almost 60KB, of which the major part is the runtime system. No other modules need to be loaded, as shown by the corresponding Hello.html:
In addition to main also the foreign exports declarations of all linked modules are used as a root.