A Standard ML to JavaScript Compiler

By Martin Elsman, 2008-08-16


Introduction

SMLtoJs (pronounced "SML toys") is a compiler from Standard ML to JavaScript, which allows programmers to enjoy the power of Standard ML static typing, higher-order functions, pattern matching, and modules for programming client-side web applications.

SMLtoJs compiles all of Standard ML, including most of the Standard ML Basis Library. It also has support for calling JavaScript functions and for executing plain JavaScript.

Features

  • All of Standard ML. SMLtoJs has support for all of Standard ML, including modules, pattern matching, higher-order functions, generative exceptions, etc.
  • Standard ML Basis Library support. SMLtoJs has support for most of the Standard ML basis library, including the following structures: Array2 ArraySlice Array Bool Byte Char CharArray CharArraySlice CharVector CharVectorSlice Date General Int Int31 Int32 IntInf LargeWord ListPair List Math Option OS.Path Pack32Big Pack32Little Random Real StringCvt String Substring Text Time Timer Vector VectorSlice Word Word31 Word32 Word8 Word8Array Word8ArraySlice Word8Vector Word8VectorSlice.
  • JavaScript integration. SMLtoJs has support for calling JavaScript functions and for executing plain JavaScript. Moreover, Standard ML functions may be exported and called by JavaScript code.
  • Simple DOM access. Simple DOM access and support for installing Standard ML functions as DOM event handlers and timer call back functions.
  • Optimization. All Standard ML module language constructs, including functors, functor applications, and signature constraints, are eliminated by SMLtoJs at compile time. Moreover, SMLtoJs performs a series of compile time optimizations, including function inlining and specialization of higher-order recursive functions, such as map and foldl. Optimizations can be controlled using compile-time flags. As a result, SMLtoJs generates fairly efficient JavaScript code, although there are rooms for improvements; see below.
  • Reactive Web Programming. SMLtoJs has library support for Reactive Web Programming. See the rwp_ex1 example for an introduction to what you can do with this library. The simple library API captures the basic notions of behaviors and event streams as well as the concepts of behavior transformers and event stream transformers.

Online Demonstration

To see SMLtoJs in action, click on the examples in the table to the right; there are also links to the Standard ML source code. To learn more about the details of the SMLtoJs implementation, see the paper presented at PLASTIC'2011.

Getting the Sources

SMLtoJs compiles on Debian Linux systems and on Mac OS X with MLton or MLKit installed. The SMLtoJS sources are available through an MLKit sourceforge svn checkout:

 $ svn co https://svn.code.sf.net/p/mlkit/code/tags/smltojs-4.3.7 smltojs
This command copies the sources to the directory smltojs.

You may also checkout the sources from the trunk, as follows:

 $ svn co https://svn.code.sf.net/p/mlkit/code/trunk/kit smltojs

Building SMLtoJs

To compile SMLtoJs from the sources (MLKit svn checkout; see above), simply type

  $ cd smltojs
  $ ./autobuild
  $ ./configure
  $ make smltojs
  $ make smltojs_basislibs
If compilation succeeds, an executable file bin/smltojs should now be available.

See also the file README_SMLTOJS for detailed instructions.

How it Works

The SMLtoJs executable bin/smltojs takes as argument an sml-file (or an mlb-file referencing the sml-files and other mlb-files of the project) and produces an html file called run.html provided there are no type errors! The resulting html-file mentions the generated JavaScript files and a file prims.js, which contains a set of primitive JavaScript functions used by the generated code.

Hint: Adding the flag "-o name" as command-line argument to smltojs results in the file name.html being generated instead of run.html.

Testing that it Works

To compile and test the test programs, cd to the js/test directory and run "make clean all":

 $ cd js/test
 $ make clean all
You can now start Chromium or Firefox on the generated html-files; the file all.html includes links to all the test files:
 $ firefox all.html
The examples temp.html, counter.html, and life.html are the most interesting examples at the moment (more will come).

Issues

There is a known issue with a bug in the following test (in some cases, the implementation pretty prints reals slightly different than suggested by the spec):

  real
  ----
   test13c: WRONG

Copyright and License

SMLtoJs is distributed under the GPL license; see the files in smltojs/doc/license for copyright notices and detailed license information. Library files in the js/ directory are distributed under an MIT-style license; see the file js/MIT_LICENSE.

News
2013-09-17: Version 4.3.7 of SMLtoJs is released.
2011-04-16: Use SMLtoJsOnline for compiling and running Standard ML programs in your browser (improved 2013).
2010-09-25: Martin gave a talk at the 2010 ML Workshop on using SMLtoJs for Hosting a Standard ML Compiler in a Web Browser.
2008-08-16: SMLtoJs version 4.3.5 is available.
2007-11-27: Martin gave a talk in the DIKU TOPPS group (slides).
2007-09-17: Martin gave a talk on the PLS Monday lunch meeting (slides).
2007-09-05: SMLtoJs version 4.3.4 is available.
2007-08-31: SMLtoJs version 4.3.3 is available.
2007-08-08: SMLtoJs version 4.3.2 is available.
 
SMLtoJs Examples
program sources
Ex: rwp_ex1 src
Ex: rwp_ex2 src
Ex: rwp_ex3 src
Ex: counter src
Ex: temp src
Ex: life src
Ex: cal src
Test: array2 src
Test: int31 src
Test: int src
Test: vectorslice src
Test: word8arrayslice src
Test: word8vector src
Test: arrayslice src
Test: general src
Test: int32_2 src
Test: math src
Test: stringcvt src
Test: testdyn1 src
Test: vector src
Test: word8array src
Test: array src
Test: int_2 src
Test: int32 src
Test: listpair src
Test: string src
Test: testmatc src
Test: word src
Test: word8 src
Test: word31 src
Test: word32 src
Test: bytechar src
Test: int31_2 src
Test: intinf src
Test: list src
Test: real src
Test: substring src
Test: word8vectorslice src
Test: time src
Test: date src
Test: unixpath src
Test: exec src

mael@elsman.com