scala.scalajs.ir

JSDesugaring

object JSDesugaring

Desugaring of the IR (herein called Extended-JS) to regular ES5 JavaScript

Extended-JS is a non-existent language that is a superset of JavaScript with Scala-esque constructs. Most notably, most constructs can be used in expression position. Extended-JS also features ES6-like classes.

GenJSCode emits Extended-JS because it is *much* easier not to deal with the expression position issue in there. But of course, in the end, we need to output genuine JavaScript code.

JSDesugaring desugars a statement of Extended-JS into genuine ES5 JavaScript code.

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.

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 JSDesugar extends Transformer

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. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. def desugarJavaScript(tree: Tree): Tree

    Desugar a statement of Extended-JS into genuine ES5 JavaScript

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

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

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

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

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

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

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

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

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

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

    Definition Classes
    AnyRef
  17. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped