JavaScript output mode.
Defines methods to emit Scala.js classes to JavaScript code.
Desugaring of the IR to JavaScript.
Desugaring of the IR to JavaScript.
The general shape and compliance to standards is chosen with an OutputMode.
The major difference between the IR and JS is that most constructs can be used in expression position. The main work of the desugaring is to unnest complex constructs in expression position so that they become statements.
The general idea is two-folded:
1) Unnest complex constructs in "argument position":
When a complex construct is used in a non-rhs expression position
(argument to a function, operand, condition of an if, etc.), that we
call "argument position", declare a variable before the statement,
assign the complex construct to it and then use that variable in the
argument position instead.
2) Push LHS's inside complex RHS's:
When an rhs is a complex construct, push the lhs inside the complex
construct. Are considered lhs:
* Assign, i.e., x =
* VarDef, i.e., val x =
or var x =
* Return, i.e., return
* (EmptyTree is also used as a trick for code reuse)
In fact, think that, in this context, LHS means: what to do with the
result of evaluating the RHS.
When VarDefs are emitted as Lets (i.e., in ES 6 mode), they cannot be pushed in all complex constructs, since that would alter their scope. In those cases, they are first declared without an initial value, then an Assign is pushed instead.
Typical example, consider the method call:
obj.meth({ var x = foo(42); x*x });
According to rule 1), the block that is passed as a parameter to obj.meth is first extracted in a synthetic var:
var x$1 = { var x = foo(42); x*x } obj.meth(x$1);
Then, according to rule 2), the lhs var x$1 =
is pushed inside the block:
{ var x = foo(42); var x$1 = x*x; } obj.meth(x$1);
Because bare blocks are non-significant in JS, this is equivalent to
var x = foo(42); var x$1 = x*x; obj.meth(x$1);
JSDesugaring does all this in a single pass, but it helps to think that: * Rule 1) is implemented by unnest(), and used most notably in * transformStat() for statement-only constructs * pushLhsInto() for statement-or-expression constructs * Rule 2) is implemented by pushLhsInto() * Emitting the class structure is delegated to ScalaJSClassEmitter.
There are a few other things that JSDesugaring takes care of: * Transform Scala expressions into their JS equivalent, taking the Scala.js class encoding into account. * And tiny details.
Defines methods to emit Scala.js classes to JavaScript code. The results are completely desugared.