summaryrefslogtreecommitdiff
path: root/ir/src/main/scala/scala/scalajs/ir/InfoSerializers.scala
blob: dfb520ff4f7bf758fb8a01b27922d9f0a54549b6 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js IR                **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2014, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.ir

import java.io._

import Infos._

object InfoSerializers {

  /** Scala.js IR File Magic Number
   *
   *    CA FE : first part of magic number of Java class files
   *    4A 53 : "JS" in ASCII
   *
   */
  final val IRMagicNumber = 0xCAFE4A53

  def serialize(stream: OutputStream, classInfo: ClassInfo): Unit = {
    new Serializer().serialize(stream, classInfo)
  }

  def deserializeRoughInfo(stream: InputStream): RoughClassInfo = {
    deserializeVersionRoughInfo(stream)._2
  }

  def deserializeFullInfo(stream: InputStream): ClassInfo = {
    deserializeVersionFullInfo(stream)._2
  }

  def deserializeVersionRoughInfo(stream: InputStream): (String, RoughClassInfo) = {
    new Deserializer(stream).deserializeRough()
  }

  def deserializeVersionFullInfo(stream: InputStream): (String, ClassInfo) = {
    new Deserializer(stream).deserializeFull()
  }

  private final class Serializer {
    def serialize(stream: OutputStream, classInfo: ClassInfo): Unit = {
      val s = new DataOutputStream(stream)

      def writeSeq[A](seq: Seq[A])(writeElem: A => Unit): Unit = {
        s.writeInt(seq.size)
        seq.foreach(writeElem)
      }

      def writeStrings(seq: Seq[String]): Unit =
        writeSeq(seq)(s.writeUTF(_))

      // Write the Scala.js IR magic number
      s.writeInt(IRMagicNumber)

      // Write the Scala.js Version
      s.writeUTF(ScalaJSVersions.binaryEmitted)

      import classInfo._
      s.writeUTF(name)
      s.writeUTF(encodedName)
      s.writeBoolean(isExported)
      s.writeInt(ancestorCount)
      s.writeByte(ClassKind.toByte(kind))
      s.writeUTF(superClass)
      writeStrings(ancestors)
      s.writeInt(optimizerHints.bits)

      def writeMethodInfo(methodInfo: MethodInfo): Unit = {
        import methodInfo._
        s.writeUTF(encodedName)
        s.writeBoolean(isAbstract)
        s.writeBoolean(isExported)
        writeSeq(calledMethods.toSeq) {
          case (caller, callees) => s.writeUTF(caller); writeStrings(callees)
        }
        writeSeq(calledMethodsStatic.toSeq) {
          case (caller, callees) => s.writeUTF(caller); writeStrings(callees)
        }
        writeStrings(instantiatedClasses)
        writeStrings(accessedModules)
        writeStrings(accessedClassData)
        s.writeInt(optimizerHints.bits)
      }

      writeSeq(methods)(writeMethodInfo(_))

      s.flush()
    }
  }

  private final class Deserializer(stream: InputStream) {
    private[this] val input = new DataInputStream(stream)

    def readList[A](readElem: => A): List[A] =
      List.fill(input.readInt())(readElem)

    def readStrings(): List[String] =
      readList(input.readUTF())

    def deserializeRough(): (String, RoughClassInfo) = {
      val version = readHeader()

      import input._
      val name = readUTF()
      val encodedName = readUTF()
      val isExported = readBoolean()
      val ancestorCount = readInt()
      val info = RoughClassInfo(name, encodedName, isExported, ancestorCount)

      (version, info)
    }

    def deserializeFull(): (String, ClassInfo) = {
      val version = readHeader()

      import input._

      val name = readUTF()
      val encodedName = readUTF()
      val isExported = readBoolean()
      val ancestorCount = readInt()
      val kind = ClassKind.fromByte(readByte())
      val superClass = readUTF()
      val ancestors = readList(readUTF())

      val optimizerHints =
        if (version == "0.5.0" || version == "0.5.2") OptimizerHints.empty
        else new OptimizerHints(readInt())

      def readMethod(): MethodInfo = {
        val encodedName = readUTF()
        val isAbstract = readBoolean()
        val isExported = readBoolean()
        val calledMethods = readList(readUTF() -> readStrings()).toMap
        val calledMethodsStatic = readList(readUTF() -> readStrings()).toMap
        val instantiatedClasses = readStrings()
        val accessedModules = readStrings()
        val accessedClassData = readStrings()
        val optimizerHints = new OptimizerHints(readInt())
        MethodInfo(encodedName, isAbstract, isExported,
            calledMethods, calledMethodsStatic,
            instantiatedClasses, accessedModules, accessedClassData,
            optimizerHints)
      }

      val methods = readList(readMethod())

      val info = ClassInfo(name, encodedName, isExported, ancestorCount, kind,
          superClass, ancestors, optimizerHints, methods)

      (version, info)
    }

    /** Reads the Scala.js IR header and verifies the version compatibility.
     *  Returns the emitted binary version.
     */
    def readHeader(): String = {
      // Check magic number
      if (input.readInt() != IRMagicNumber)
        throw new IOException("Not a Scala.js IR file")

      // Check that we support this version of the IR
      val version = input.readUTF()
      val supported = ScalaJSVersions.binarySupported
      if (!supported.contains(version)) {
        throw new IOException(
            s"This version ($version) of Scala.js IR is not supported. " +
            s"Supported versions are: ${supported.mkString(", ")}")
      }

      version
    }
  }
}