A Standard ML to JavaScript Compiler
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.
Getting the Sources
SMLtoJs compiles on Debian Linux systems with MLton or MLKit
installed. The SMLtoJS sources are available through an MLKit
sourceforge svn checkout:
$ svn co https://mlkit.svn.sourceforge.net/svnroot/mlkit/tags/smltojs-4.3.5/kit smltojs
This command copies the sources to the directory smltojs.
You may also checkout the sources from the trunk, as follows:
$ svn co https://mlkit.svn.sourceforge.net/svnroot/mlkit/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.
|