org.scalajs.core.tools.javascript

JSDesugaring

object JSDesugaring

Desugaring of the IR to regular ES5 JavaScript.

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., 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.


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
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. class DesugarException extends Exception

  2. final class Env extends AnyRef

Value Members

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

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

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

    Definition Classes
    AnyRef → Any
  4. object Env

  5. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  6. def clone(): AnyRef

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

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

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

    Desugars a statement of the IR into ES5 JavaScript.

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

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

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

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

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

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

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

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

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

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

    Definition Classes
    AnyRef
  19. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped