summaryrefslogtreecommitdiff
path: root/tools/shared/src/main/scala/scala/scalajs/tools/classpath/IRClasspath.scala
blob: a92293b2e5b4c9a969c51dc81b35c051cf7d8ea1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js tools             **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013-2014, LAMP/EPFL   **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.tools.classpath

import scala.collection.immutable.{Seq, Traversable}

import scala.scalajs.tools.sem.Semantics
import scala.scalajs.tools.io._
import scala.scalajs.tools.logging._
import scala.scalajs.tools.optimizer.ScalaJSOptimizer
import scala.scalajs.tools.jsdep.ResolutionInfo

/** A [[CompleteClasspath]] that contains only IR as scalaJSCode */
final class IRClasspath(
    /** The JS libraries the IR code depends on */
    jsLibs: Seq[ResolvedJSDependency],
    val requiredCompliance: Traversable[ComplianceRequirement],
    /** The IR itself. Ancestor count is used for later ordering */
    val scalaJSIR: Traversable[VirtualScalaJSIRFile],
    requiresDOM: Boolean,
    version: Option[String]
) extends CompleteClasspath(jsLibs, requiresDOM, version) {

  /** Orders and optimizes the contained IR.
   *
   *  Consider using [[ScalaJSOptimizer]] for a canonical way to do so. It
   *  allows to persist the resulting file and create a source map, as well as
   *  using non-default [[Semantics]].
   */
  override lazy val scalaJSCode: VirtualJSFile = {
    import ScalaJSOptimizer._

    val outName = "temporary-fastOpt.js"

    if (scalaJSIR.nonEmpty) {
      val semantics = Semantics.compliantTo(requiredCompliance.map(_.semantics))
      val output = WritableMemVirtualJSFile(outName)
      new ScalaJSOptimizer(semantics).optimizeCP(
          Inputs(this),
          OutputConfig(output),
          NullLogger)
      output
    } else {
      // We cannot run the optimizer without IR, because it will complain about
      // java.lang.Object missing. However, an empty JS file is perfectly valid
      // for no IR at all.
      VirtualJSFile.empty(outName)
    }
  }

  /** Checks whether the given semantics are compliant with the requirements of
   *  this CompleteClasspath. Throws an exception otherwise.
   */
  final def checkCompliance(semantics: Semantics): Unit = {
    val unmet = requiredCompliance filterNot { compliance =>
      semantics.isCompliant(compliance.semantics)
    }

    if (unmet.nonEmpty)
      throw new BadComplianceException(unmet.toList)
  }
}