Class

org.scalajs.nscplugin.GenJSCode

JSCodePhase

Related Doc: package GenJSCode

Permalink

class JSCodePhase extends StdPhase with JSExportsPhase

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. JSCodePhase
  2. JSExportsPhase
  3. StdPhase
  4. GlobalPhase
  5. Phase
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new JSCodePhase(prev: Phase)

    Permalink

Type Members

  1. abstract class Exported extends AnyRef

    Permalink
    Definition Classes
    JSExportsPhase
  2. type Id = Int

    Permalink
    Definition Classes
    Phase
  3. final class JSParamInfo extends AnyRef

    Permalink

    Info about a Scala method param when called as JS method.

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 MaybeAsInstanceOf

    Permalink
  5. object WrapArray

    Permalink
  6. def apply(cunit: G.CompilationUnit): Unit

    Permalink

    Generates the Scala.js IR for a compilation unit This method iterates over all the class and interface definitions found in the compilation unit and emits their IR (.sjsir).

    Generates the Scala.js IR for a compilation unit This method iterates over all the class and interface definitions found in the compilation unit and emits their IR (.sjsir).

    Some classes are never actually emitted:

    • Classes representing primitive types
    • The scala.Array class
    • Implementation classes for JS traits

    Some classes representing anonymous functions are not actually emitted. Instead, a temporary representation of their apply method is built and recorded, so that it can be inlined as a JavaScript anonymous function in the method that instantiates it.

    Other ClassDefs are emitted according to their nature: * Non-native JS class -> genNonNativeJSClass() * Other JS type (<: js.Any) -> genJSClassData() * Interface -> genInterface() * Normal class -> genClass()

    Definition Classes
    JSCodePhase → GlobalPhase
  7. final def applyPhase(unit: G.CompilationUnit): Unit

    Permalink
    Definition Classes
    GlobalPhase
  8. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  9. def cancelled(unit: G.CompilationUnit): Boolean

    Permalink
    Definition Classes
    GlobalPhase
  10. def checkable: Boolean

    Permalink
    Definition Classes
    Phase
  11. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  12. val currentClassSym: ScopedVar[G.Symbol]

    Permalink
  13. def description: String

    Permalink
    Definition Classes
    JSCodePhase → Phase
  14. def ensureBoxed(expr: Tree, tpeEnteringPosterasure: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Ensures that the value of the given tree is boxed.

    Ensures that the value of the given tree is boxed.

    expr

    Tree to be boxed if needed.

    tpeEnteringPosterasure

    The type of expr as it was entering the posterasure phase.

  15. def ensureResultBoxed(expr: Tree, methodSym: G.Symbol)(implicit pos: G.Position): Tree

    Permalink

    Ensures that the value of the given tree is boxed when used as a method result value.

    Ensures that the value of the given tree is boxed when used as a method result value.

    expr

    Tree to be boxed if needed.

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

    Permalink
    Definition Classes
    AnyRef
  17. def equals(other: Any): Boolean

    Permalink
    Definition Classes
    Phase → AnyRef → Any
  18. def erasedTypes: Boolean

    Permalink
    Definition Classes
    GlobalPhase → Phase
  19. def exprToStat(tree: Tree): Tree

    Permalink

    Turn a JavaScript expression of type Unit into a statement

  20. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  21. def flagMask: Long

    Permalink
    Definition Classes
    Phase
  22. def flatClasses: Boolean

    Permalink
    Definition Classes
    GlobalPhase → Phase
  23. val fmask: Long

    Permalink
    Definition Classes
    Phase
  24. def fromAny(expr: Tree, tpeEnteringPosterasure: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Extracts a value typed as Any to the given type after posterasure.

    Extracts a value typed as Any to the given type after posterasure.

    expr

    Tree to be extracted.

    tpeEnteringPosterasure

    The type of expr as it was entering the posterasure phase.

  25. def genAnonJSClassNew(sym: G.Symbol, jsSuperClassValue: Tree, args: List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Generate an instance of an anonymous (non-lambda) JS class inline

    Generate an instance of an anonymous (non-lambda) JS class inline

    sym

    Class to generate the instance of

    jsSuperClassValue

    JS class value of the super class

    args

    Arguments to the Scala constructor, which map to JS class captures

    pos

    Position of the original New tree

  26. def genApply(tree: G.Apply, isStat: Boolean): Tree

    Permalink

    Gen JS code for an Apply node (method call)

    Gen JS code for an Apply node (method call)

    There's a whole bunch of varieties of Apply nodes: regular method calls, super calls, constructor calls, isInstanceOf/asInstanceOf, primitives, JS calls, etc. They are further dispatched in here.

  27. def genApplyJSClassMethod(receiver: Tree, method: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink
  28. def genApplyMethod(receiver: Tree, method: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for a call to a Scala method.

  29. def genApplyMethodMaybeStatically(receiver: Tree, method: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink
  30. def genApplyMethodStatically(receiver: Tree, method: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink
  31. def genApplyStatic(method: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink
  32. def genArrayValue(tree: G.Tree, elems: List[G.Tree]): Tree

    Permalink

    Gen JS code for an array literal, in the context of tree (its tpe and pos) but with the elements elems.

  33. def genArrayValue(tree: G.ArrayValue): Tree

    Permalink

    Gen JS code for an array literal.

  34. def genAsInstanceOf(value: Tree, to: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for an asInstanceOf cast (for reference types only)

  35. def genCallDefaultGetter(sym: G.Symbol, paramIndex: Int, paramPos: G.Position, static: Boolean, captures: List[Tree])(previousArgsValues: (Int) ⇒ List[Tree])(implicit pos: G.Position): Tree

    Permalink
    Definition Classes
    JSExportsPhase
  36. def genClass(cd: G.ClassDef): ClassDef

    Permalink

    Gen the IR ClassDef for a class definition (maybe a module class).

  37. def genClassFields(cd: G.ClassDef): List[AnyFieldDef]

    Permalink

    Gen definitions for the fields of a class.

    Gen definitions for the fields of a class. The fields are initialized with the zero of their types.

  38. def genEqEqPrimitive(ltpe: G.Type, rtpe: G.Type, lsrc: Tree, rsrc: Tree)(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for a call to Any.==

  39. def genExposedFieldIRType(f: G.Symbol): Type

    Permalink
  40. def genExpr(name: JSName)(implicit pos: G.Position): Tree

    Permalink
  41. def genExpr(tree: G.Tree): Tree

    Permalink

    Gen JS code for a tree in expression position (in the IR).

  42. def genExprOrGlobalScope(tree: G.Tree): MaybeGlobalScope

    Permalink

    Gen JS code for a tree in expression position (in the IR) or the global scope.

  43. def genInterface(cd: G.ClassDef): ClassDef

    Permalink

    Gen the IR ClassDef for an interface definition.

  44. def genIsInstanceOf(value: Tree, to: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for an isInstanceOf test (for reference types only)

  45. def genJSArrayToVarArgs(arrayRef: Tree)(implicit pos: G.Position): Tree

    Permalink

    Wraps a js.Array to use as varargs.

  46. def genJSClassCapturesAndConstructor(constructorTrees: List[G.DefDef])(implicit pos: G.Position): (List[ParamDef], JSMethodDef)

    Permalink
  47. def genJSClassData(cd: G.ClassDef): ClassDef

    Permalink

    Gen the IR ClassDef for a JS class or trait.

  48. def genJSClassDispatchers(classSym: G.Symbol, dispatchMethodsNames: List[JSName]): List[MemberDef]

    Permalink
    Definition Classes
    JSExportsPhase
  49. def genJSFunction(cd: G.ClassDef, captures: List[Tree]): Tree

    Permalink

    Gen JS code for a JS function class.

    Gen JS code for a JS function class.

    This is called when emitting a ClassDef that represents an anonymous class extending js.FunctionN. These are generated by the SAM synthesizer when the target type is a js.FunctionN. Since JS functions are not classes, we deconstruct the ClassDef, then reconstruct it to be a genuine Closure.

    Compared to tryGenAnonFunctionClass(), this function must always succeed, because we really cannot afford keeping them as anonymous classes. The good news is that it can do so, because the body of SAM lambdas is hoisted in the enclosing class. Hence, the apply() method is just a forwarder to calling that hoisted method.

    From a class looking like this:

    final class <anon>(outer, capture1, ..., captureM) extends js.FunctionN[...] { def apply(param1, ..., paramN) = { outer.lambdaImpl(param1, ..., paramN, capture1, ..., captureM) } } new <anon>(o, c1, ..., cM)

    we generate a function:

    lambda<o, c1, ..., cM>[notype]( outer, capture1, ..., captureM, param1, ..., paramN) { outer.lambdaImpl(param1, ..., paramN, capture1, ..., captureM) }

  50. def genJSNativeMemberDef(tree: G.DefDef): JSNativeMemberDef

    Permalink

    Generates the JSNativeMemberDef of a JS native method.

  51. def genLabelDef(tree: G.LabelDef, isStat: Boolean): Tree

    Permalink

    Gen JS code for LabelDef.

    Gen JS code for LabelDef.

    If a LabelDef reaches this method, then the only valid jumps are from within it, which means it basically represents a loop. Other kinds of LabelDefs, notably those for matches, are caught upstream and transformed in ad hoc ways.

    The general transformation for

    labelName(...labelParams) {
      rhs
    }: T

    is the following:

    block[T]: {
      while (true) {
        labelName[void]: {
          return@block transformedRhs
        }
      }
    }

    where all jumps to the label inside the rhs of the form

    labelName(...args)

    are transformed into

    ...labelParams = ...args;
    return@labelName (void 0)

    This is always correct, so it can handle arbitrary labels and jumps such as those produced by loops, tail-recursive calls and even some compiler plugins (see for example #1148). However, the result is unnecessarily ugly for simple while and do while loops, so we have some post-processing to simplify those.

  52. def genLoadModule(sym0: G.Symbol)(implicit pos: G.Position): Tree

    Permalink

    Generate loading of a module value.

    Generate loading of a module value.

    Can be given either the module symbol or its module class symbol.

    If the module we load refers to the global scope (i.e., it is annotated with @JSGlobalScope), report a compile error specifying that a global scope object should only be used as the qualifier of a .-selection.

  53. def genLoadModuleOrGlobalScope(sym0: G.Symbol)(implicit pos: G.Position): MaybeGlobalScope

    Permalink

    Generate loading of a module value or the global scope.

    Generate loading of a module value or the global scope.

    Can be given either the module symbol of its module class symbol.

    Unlike genLoadModule, this method does not fail if the module we load refers to the global scope.

  54. def genMatch(tree: G.Tree, isStat: Boolean): Tree

    Permalink

    Gen JS code for a Match, i.e., a switch-able pattern match.

    Gen JS code for a Match, i.e., a switch-able pattern match.

    In most cases, this straightforwardly translates to a Match in the IR, which will eventually become a switch in JavaScript.

    However, sometimes there is a guard in here, despite the fact that matches cannot have guards (in the JVM nor in the IR). The JVM backend emits a jump to the default clause when a guard is not fulfilled. We cannot do that, since we do not have arbitrary jumps. We therefore use a funny encoding with two nested Labeled blocks. For example,

    x match {
      case 1 if y > 0 => a
      case 2          => b
      case _          => c
    }

    arrives at the back-end as

    x match {
      case 1 =>
        if (y > 0)
          a
        else
          default()
      case 2 =>
        b
      case _ =>
        default() {
          c
        }
    }

    which we then translate into the following IR:

    matchResult[I]: {
      default[V]: {
        x match {
          case 1 =>
            return(matchResult) if (y > 0)
              a
            else
              return(default) (void 0)
          case 2 =>
            return(matchResult) b
          case _ =>
            ()
        }
      }
      c
    }
  55. def genMemberExportOrDispatcher(jsName: JSName, isProp: Boolean, alts: List[G.Symbol], static: Boolean): MemberDef

    Permalink
    Definition Classes
    JSExportsPhase
  56. def genMemberExports(classSym: G.Symbol): List[MemberDef]

    Permalink

    Generates exported methods and properties for a class.

    Generates exported methods and properties for a class.

    classSym

    symbol of the class we export for

    Definition Classes
    JSExportsPhase
  57. def genMethod(dd: G.DefDef): Option[MethodDef]

    Permalink

    Maybe gen JS code for a method definition.

    Maybe gen JS code for a method definition.

    Some methods are not emitted at all:

    • Primitives, since they are never actually called (with exceptions)
    • Abstract methods in non-native JS classes
    • Default accessor of a native JS constructor
    • Constructors of hijacked classes
    • Methods with the
    @JavaDefaultMethod

    annotation mixed in classes.

    @JavaDefaultMethod }}}

  58. def genMethodDef(namespace: MemberNamespace, methodName: MethodIdent, originalName: OriginalName, jsParams: List[ParamDef], resultIRType: Type, tree: G.Tree, optimizerHints: OptimizerHints): MethodDef

    Permalink

    Generates the MethodDef of a (non-constructor) method

    Generates the MethodDef of a (non-constructor) method

    Most normal methods are emitted straightforwardly. If the result type is Unit, then the body is emitted as a statement. Otherwise, it is emitted as an expression.

    The additional complexity of this method handles the transformation of a peculiarity of recursive tail calls: the local ValDef that replaces this.

  59. def genMethodWithCurrentLocalNameScope(dd: G.DefDef): MethodDef

    Permalink

    Gen JS code for a method definition in a class or in an impl class.

    Gen JS code for a method definition in a class or in an impl class. On the JS side, method names are mangled to encode the full signature of the Scala method, as described in JSEncoding, to support overloading.

    Constructors are emitted by generating their body as a statement.

    Interface methods with the

    @JavaDefaultMethod

    annotation produce default methods forwarding to the trait impl class method.

    Other (normal) methods are emitted with genMethodDef().

    @JavaDefaultMethod }}} default methods forwarding to the trait impl class method.

    Other (normal) methods are emitted with genMethodDef().

  60. def genNew(className: ClassName, ctor: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for a call to a Scala class constructor.

  61. def genNew(clazz: G.Symbol, ctor: G.Symbol, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for a call to a Scala class constructor.

  62. def genNewArray(arrayTypeRef: ArrayTypeRef, arguments: List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for creating a new Array: new Array[T](length) For multidimensional arrays (dimensions > 1), the arguments can specify up to dimensions lengths for the first dimensions of the array.

  63. def genNonNativeJSClass(cd: G.ClassDef): ClassDef

    Permalink

    Gen the IR ClassDef for a non-native JS class.

  64. def genOptimizedMatchEndLabeled(label: LabelIdent, tpe: Type, translatedCases: List[Tree], returnCount: Int)(implicit pos: G.Position): Tree

    Permalink

    Gen JS code for a Labeled block from a pattern match'es match-end, while trying to optimize it away as an If chain.

    Gen JS code for a Labeled block from a pattern match'es match-end, while trying to optimize it away as an If chain.

    It is important to do so at compile-time because, when successful, the resulting IR can be much better optimized by the optimizer.

    The optimizer also does something similar, but *after* it has processed the body of the Labeled block, at which point it has already lost any information about stack-allocated values.

    !!! There is quite of bit of code duplication with OptimizerCore.tryOptimizePatternMatch.

  65. def genOverloadDispatch(jsName: JSName, alts: List[Exported], tpe: Type)(implicit pos: G.Position): (List[ParamDef], Option[ParamDef], Tree)

    Permalink
    Definition Classes
    JSExportsPhase
  66. def genParamDef(sym: G.Symbol): ParamDef

    Permalink
  67. def genScalaArg(methodSym: G.Symbol, paramIndex: Int, formalArgsRegistry: GenJSCode.FormalArgsRegistry, param: JSParamInfo, static: Boolean, captures: List[Tree])(previousArgsValues: (Int) ⇒ List[Tree])(implicit pos: G.Position): Tree

    Permalink

    Generates a Scala argument from dispatched JavaScript arguments (unboxing and default parameter handling).

    Generates a Scala argument from dispatched JavaScript arguments (unboxing and default parameter handling).

    Definition Classes
    JSExportsPhase
  68. def genStat(tree: G.Tree): Tree

    Permalink

    Gen JS code for a tree in statement position (in the IR).

  69. def genStatOrExpr(tree: G.Tree, isStat: Boolean): Tree

    Permalink

    Gen JS code for a tree in statement or expression position (in the IR).

    Gen JS code for a tree in statement or expression position (in the IR).

    This is the main transformation method. Each node of the Scala AST is transformed into an equivalent portion of the JS AST.

  70. def genStaticExports(classSym: G.Symbol): List[MemberDef]

    Permalink
    Definition Classes
    JSExportsPhase
  71. def genStaticForwardersForClassOrInterface(existingMembers: List[MemberDef], sym: G.Symbol)(implicit pos: G.Position): List[MemberDef]

    Permalink

    Gen the static forwarders to the members of a class or interface for methods of its companion object.

    Gen the static forwarders to the members of a class or interface for methods of its companion object.

    This is only done if there exists a companion object and it is not a JS type.

    Precondition: isCandidateForForwarders(sym) is true

  72. def genStaticForwardersFromModuleClass(existingMembers: List[MemberDef], moduleClass: G.Symbol)(implicit pos: G.Position): List[MemberDef]

    Permalink

    Gen the static forwarders for the methods of a module class.

    Gen the static forwarders for the methods of a module class.

    Precondition: isCandidateForForwarders(moduleClass) is true

  73. def genTopLevelExports(classSym: G.Symbol): List[TopLevelExportDef]

    Permalink
    Definition Classes
    JSExportsPhase
  74. def genTry(tree: G.Try, isStat: Boolean): Tree

    Permalink

    Gen JS code for a try..catch or try..finally block

    Gen JS code for a try..catch or try..finally block

    try..finally blocks are compiled straightforwardly to try..finally blocks of JS.

    try..catch blocks are a bit more subtle, as JS does not have type-based selection of exceptions to catch. We thus encode explicitly the type tests, like in:

    try { ... } catch (e) { if (e.isInstanceOf[IOException]) { ... } else if (e.isInstanceOf[Exception]) { ... } else { throw e; // default, re-throw } }

  75. def genVarRef(sym: G.Symbol)(implicit pos: G.Position): VarRef

    Permalink
  76. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  77. def hasNext: Boolean

    Permalink
    Definition Classes
    Phase
  78. def hashCode(): Int

    Permalink
    Definition Classes
    Phase → AnyRef → Any
  79. val id: Id

    Permalink
    Definition Classes
    Phase
  80. def isAbstractMethod(dd: G.DefDef): Boolean

    Permalink
  81. def isCandidateForForwarders(sym: G.Symbol): Boolean

    Permalink

    Is the given Scala class, interface or module class a candidate for static forwarders?

  82. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  83. def iterator: Iterator[Phase]

    Permalink
    Definition Classes
    Phase
  84. def jsParamInfos(sym: G.Symbol): List[JSParamInfo]

    Permalink
  85. def keepsTypeParams: Boolean

    Permalink
    Definition Classes
    Phase
  86. def makePrimitiveBox(expr: Tree, tpe: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Gen a boxing operation (tpe is the primitive type)

  87. def makePrimitiveUnbox(expr: Tree, tpe: G.Type)(implicit pos: G.Position): Tree

    Permalink

    Gen an unboxing operation (tpe is the primitive type)

  88. def name: String

    Permalink
    Definition Classes
    JSCodePhase → StdPhase → Phase
  89. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  90. def newFlags: Long

    Permalink
    Definition Classes
    StdPhase → Phase
  91. def next: Phase

    Permalink
    Definition Classes
    Phase
  92. def nextFlags: Long

    Permalink
    Definition Classes
    StdPhase → Phase
  93. final def notify(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  95. val prev: Phase

    Permalink
    Definition Classes
    Phase
  96. def refChecked: Boolean

    Permalink
    Definition Classes
    GlobalPhase → Phase
  97. def run(): Unit

    Permalink
    Definition Classes
    JSCodePhase → GlobalPhase → Phase
  98. def specialized: Boolean

    Permalink
    Definition Classes
    GlobalPhase → Phase
  99. final def synchronized[T0](arg0: ⇒ T0): T0

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

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  104. final def withCurrentUnit(unit: G.CompilationUnit)(task: ⇒ Unit): Unit

    Permalink
    Definition Classes
    GlobalPhase

Inherited from GenJSCode.JSExportsPhase

Inherited from GenJSCode.StdPhase

Inherited from G.GlobalPhase

Inherited from Phase

Inherited from AnyRef

Inherited from Any

Ungrouped