Object

org.scalajs.core.tools.javascript

JSDesugaring

Related Doc: package javascript

Permalink

object JSDesugaring

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.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. JSDesugaring
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Type Members

  1. class DesugarException extends Exception

    Permalink
  2. final class Env extends AnyRef

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. object Env

    Permalink
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. def desugarJavaScript(tree: Tree, semantics: Semantics, outputMode: OutputMode, env: Env): Tree

    Permalink

    Desugars a statement of the IR into a given mode of JavaScript under a given environment.

  8. def desugarJavaScript(tree: Tree, semantics: Semantics, outputMode: OutputMode): Tree

    Permalink

    Desugars a statement of the IR into a given mode of JavaScript.

  9. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  10. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  11. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  19. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Deprecated Value Members

  1. def desugarJavaScript(tree: Tree, semantics: Semantics, env: Env): Tree

    Permalink

    Desugars a statement of the IR into ES5 JavaScript under a given environment.

    Desugars a statement of the IR into ES5 JavaScript under a given environment.

    Annotations
    @deprecated
    Deprecated

    (Since version 0.6.2) Use an overload with an explicit OutputMode.

  2. def desugarJavaScript(tree: Tree, semantics: Semantics): Tree

    Permalink

    Desugars a statement of the IR into ES5 JavaScript.

    Desugars a statement of the IR into ES5 JavaScript.

    Annotations
    @deprecated
    Deprecated

    (Since version 0.6.2) Use an overload with an explicit OutputMode.

Inherited from AnyRef

Inherited from Any

Ungrouped