Scala.js

What has it become, and how is it used?

Scala.js logo

Sébastien Doeraene @sjrdoeraene

ScalaDays 2014, June 18

LAMP, lamp.epfl.ch
École polytechnique fédérale de Lausanne
 

EPFL logo             Scala logo

What is Scala.js?

Big picture

What does it look like?

Previously on Scala.js

A bit of history

  • June 2013, first prototype
    • 16 MB of JavaScript for Hello World
    • The "famous" Reversi demo
  • August 2013, introduced Google Closure Compiler
    • 900 KB
    • in several tens of seconds to a few minutes
  • December 2013, version 0.1
    • Finally a test suite, thanks to @jonas
    • 400 KB

Thanks to @lihaoyi for these data

A bit of history

  • February 2014, version 0.3
    • 300 KB
    • Scala test suite of Scala 2.11.0-M7 passes
  • March 2014, version 0.4.0
    • 200 KB in a few seconds!
    • 1.5 MB in ~1 second for iterative dev
  • June 2014, version 0.5.0
    • 170 KB
    • 1.2 MB in less than 1 second
    • And many other cool things!

Recap

  • sbt plugin (build tool)
    with incremental compilation in sbt
  • IDE support (Eclipse, IDEA, etc.)
    with auto-completion of JavaScript APIs
  • Dependency management à la Maven
  • Debugging with source maps
  • Cross-compiling {JVM,JS} x {2.10,2.11}

So what's new in Scala.js 0.5?

Language changes

Correct integer semantics

For example, Int = signed 32-bit integer

  • JavaScript (and Scala.js <= 0.4.4)
    2,000,000,000 + 2,000,000,000 = 4,000,000,000
  • Scala, Java (and Scala.js >= 0.5.0)
    2,000,000,000 + 2,000,000,000 = -294,967,296

(Division by 0 is still undefined behavior.)


val x = 1000000000
val y = 3 * x             // -1294967296
val z = y - x             //  2000000000
      

var x = 1000000000;
var y = Math.imul(3, x);  // -1294967296
var z = (y - x) | 0;      //  2000000000
      

Improved interoperability with JavaScript

Primitive Scala types really are primitive JavaScript types

Scala typesJavaScript types
Float, Doublenumber
Byte, Short, Intnumber (restricted range)
Stringstring
Nullnull
Unitundefined
_ <: js.Anyitself
anything elseopaque, except @JSExport'ed members

Improvements to the generated code

No boxes

  • Primitive types, excluding Char, are not boxed anymore

Better optimizer

  • Fast-optimizer (fastOptJS)
    • Started in 0.4.x, improved in 0.5.0
    • Produces smaller code
    • Produces it quickly (sub-second incremental re-optimization)
  • Full-optimizer (fullOptJS)
Compilation pipeline
JavaScript pipeline

Better tooling

through sbt plugin

JavaScript interpreters

  • run and test* use Rhino
  • fastOptStage::run and fastOptStage::test use Node.js
    • With requiresDOM := true, use PhantomJS
    • Recommended for iterative test cycle
      ~fastOptStage::test
  • Also fullOptStage::

* and testOnly and testQuick

Dependency management

Backward binary compatibility

  • The 0.5.x series are backward compatible
  • Artifacts published with _sjs0.5_2.11 suffix
  • Use %%% to depend on cross-cross-published libraries

libraryDependencies +=
  "org.scala-lang.modules.scalajs" %%%
    "scalajs-dom" % "0.6"
      

Dependencies on JavaScript libraries

  • Declare dependencies on JavaScript libraries through WebJars

ScalaJSKeys.jsDependencies +=
  "org.webjars" % "jquery" % "1.10.2" / "jquery.js"
      
  • Included when running with run or test
  • Packaged in one file with
    skip in ScalaJSKeys.packageJSDependencies := false

Improved documentation

How is Scala.js used?

Scala.jsFiddle

by @lihaoyi

uTest

Double-cross {JVM,JS} x {2.10,2.11} unit testing library

by @lihaoyi

Scala.js Actors (aka Akka/JS)

Very much in a proof-of-concept state

Shapeless and Parboiled2

No changes to the source code (only build files)

Scala.js port by @alexander-myltsev

scala-js-react

Scala-esque statically typed interface to Facebook's React

by @kanterov

scalajs-binding

Pure Scala/Scala.js library for data binding

by @antonkulaga

Papa Carlo

Scala parsing library using an incremental parsing approach

Web demo with UI in JavaScript talking to the Scala.js library

by @Eliah-Lakhin

And other cool stuff ...

How stable is it?

Can I use it?

Official answer: it is still experimental, still an 0.x version

Scala.js test suite

  • 405 unit tests
  • Run for every pull request
    • With Rhino, Node.js and PhantomJS
    • With Scala 2.10.{2,3,4} and 2.11.{0,1}
    • With -Xexperimental and without
    • In fastOptStage and fullOptStage
      (and packageStage)
  • Also typechecks our Intermediate Representation

Scala test suite (partest)

  • 822 run tests (51 %) and ~2000 pos/neg tests (99 %)
  • Run on a nightly basis
    • With Rhino and Node.js
    • With Scala 2.11.{0,1}
    • In fastOptStage and fullOptStage
  • Also typechecks our Intermediate Representation

What's missing?

  • Mostly parts of the Java standard library
    Only a small fraction has been ported
  • Official support and artifacts for important Scala libraries
  • Static types for more JavaScript libraries

You can help with all of these!

What's coming?

  • An even better, incremental, optimizer
  • Compiler in the browser (Scala.js compiling itself) (?)
  • Non-prototype port of Akka Actors
Scala.js logo

scala-js.org