forked from scalameta/scalafmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceMapWriter.scala
310 lines (261 loc) · 9.12 KB
/
SourceMapWriter.scala
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* __ *\
** ________ ___ / / ___ __ ____ Scala.js tools **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2014, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package org.scalajs.core.tools.javascript
import java.io.Writer
import java.net.URI
import java.{util => ju}
import scala.collection.mutable.{ ListBuffer, HashMap, Stack, StringBuilder }
import org.scalajs.core.ir
import ir.Position
import ir.Position._
import ir.Utils
private object SourceMapWriter {
private val Base64Map =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789+/"
// Some constants for writeBase64VLQ
// Each base-64 digit covers 6 bits, but 1 is used for the continuation
private final val VLQBaseShift = 5
private final val VLQBase = 1 << VLQBaseShift
private final val VLQBaseMask = VLQBase - 1
private final val VLQContinuationBit = VLQBase
private def printJSONString(s: String, out: Writer) = {
out.write('\"')
Utils.printEscapeJS(s, out)
out.write('\"')
}
private final class NodePosStack {
private var topIndex: Int = -1
private var posStack: Array[Position] = new Array(128)
private var nameStack: Array[String] = new Array(128)
def pop(): Unit =
topIndex -= 1
def topPos: Position =
posStack(topIndex)
def topName: String =
nameStack(topIndex)
def push(pos: Position, originalName: String): Unit = {
val newTopIdx = topIndex + 1
topIndex = newTopIdx
if (newTopIdx >= posStack.length)
growStack()
posStack(newTopIdx) = pos
nameStack(newTopIdx) = originalName
}
private def growStack(): Unit = {
val newSize = 2 * posStack.length
posStack = ju.Arrays.copyOf(posStack, newSize)
nameStack = ju.Arrays.copyOf(nameStack, newSize)
}
}
}
class SourceMapWriter(
val out: Writer,
val generatedFile: String,
val relativizeBaseURI: Option[URI] = None) {
import SourceMapWriter._
private val sources = new ListBuffer[String]
private val _srcToIndex = new HashMap[SourceFile, Int]
private val names = new ListBuffer[String]
private val _nameToIndex = new HashMap[String, Int]
// Strings are nullable in this stack
private val nodePosStack = new SourceMapWriter.NodePosStack
nodePosStack.push(NoPosition, null)
private var lineCountInGenerated = 0
private var lastColumnInGenerated = 0
private var firstSegmentOfLine = true
private var lastSource: SourceFile = null
private var lastSourceIndex = 0
private var lastLine: Int = 0
private var lastColumn: Int = 0
private var lastNameIndex: Int = 0
private var pendingColumnInGenerated: Int = -1
private var pendingPos: Position = NoPosition
// pendingName string is nullable
private var pendingName: String = null
writeHeader()
private def sourceToIndex(source: SourceFile): Int = {
if (_srcToIndex.contains(source)) {
_srcToIndex(source)
} else {
val index = sources.size
_srcToIndex.put(source, index)
sources += sourceToURI(source)
index
}
}
/** Relatively hacky way to get a Web-friendly URI to the source file */
private def sourceToURI(source: SourceFile): String = {
val uri = source
val relURI = relativizeBaseURI.fold(uri)(Utils.relativize(_, uri))
Utils.fixFileURI(relURI).toASCIIString
}
private def nameToIndex(name: String): Int = {
if (_nameToIndex.contains(name)) {
_nameToIndex(name)
} else {
val index = names.size
_nameToIndex.put(name, index)
names += name
index
}
}
private def writeHeader(): Unit = {
out.write("{\n\"version\": 3,\n\"file\": ")
printJSONString(generatedFile, out)
out.write(",\n\"mappings\": \"")
}
def nextLine(): Unit = {
writePendingSegment()
out.write(';')
lineCountInGenerated += 1
lastColumnInGenerated = 0
firstSegmentOfLine = true
pendingColumnInGenerated = -1
pendingPos = nodePosStack.topPos
pendingName = nodePosStack.topName
}
def startNode(column: Int, originalPos: Position): Unit = {
nodePosStack.push(originalPos, null)
startSegment(column, originalPos, null)
}
def startNode(column: Int, originalPos: Position,
optOriginalName: Option[String]): Unit = {
val originalName =
if (optOriginalName.isDefined) optOriginalName.get
else null
nodePosStack.push(originalPos, originalName)
startSegment(column, originalPos, originalName)
}
def endNode(column: Int): Unit = {
nodePosStack.pop()
startSegment(column, nodePosStack.topPos, nodePosStack.topName)
}
private def startSegment(startColumn: Int, originalPos: Position,
originalName: String): Unit = {
// There is no point in outputting a segment with the same information
if ((originalPos == pendingPos) && (originalName == pendingName))
return
// Write pending segment if it covers a non-empty range
if (startColumn != pendingColumnInGenerated)
writePendingSegment()
// New pending
pendingColumnInGenerated = startColumn
pendingPos = originalPos
pendingName = originalName
}
private def writePendingSegment() {
if (pendingColumnInGenerated < 0)
return
// Segments of a line are separated by ','
if (firstSegmentOfLine) firstSegmentOfLine = false
else out.write(',')
// Generated column field
writeBase64VLQ(pendingColumnInGenerated-lastColumnInGenerated)
lastColumnInGenerated = pendingColumnInGenerated
// If the position is NoPosition, stop here
val pendingPos1 = pendingPos
if (pendingPos1.isEmpty)
return
// Extract relevant properties of pendingPos
val source = pendingPos1.source
val line = pendingPos1.line
val column = pendingPos1.column
// Source index field
if (source eq lastSource) { // highly likely
writeBase64VLQ0()
} else {
val sourceIndex = sourceToIndex(source)
writeBase64VLQ(sourceIndex-lastSourceIndex)
lastSource = source
lastSourceIndex = sourceIndex
}
// Line field
writeBase64VLQ(line - lastLine)
lastLine = line
// Column field
writeBase64VLQ(column - lastColumn)
lastColumn = column
// Name field
if (pendingName != null) {
val nameIndex = nameToIndex(pendingName)
writeBase64VLQ(nameIndex-lastNameIndex)
lastNameIndex = nameIndex
}
}
def complete(): Unit = {
writePendingSegment()
var restSources = sources.result()
out.write("\",\n\"sources\": [")
while (restSources.nonEmpty) {
printJSONString(restSources.head, out)
restSources = restSources.tail
if (restSources.nonEmpty)
out.write(", ")
}
var restNames = names.result()
out.write("],\n\"names\": [")
while (restNames.nonEmpty) {
printJSONString(restNames.head, out)
restNames = restNames.tail
if (restNames.nonEmpty)
out.write(", ")
}
out.write("],\n\"lineCount\": ")
out.write(lineCountInGenerated.toString)
out.write("\n}\n")
}
/** Write the Base 64 VLQ of an integer to the mappings
* Inspired by the implementation in Closure Compiler:
* http://code.google.com/p/closure-compiler/source/browse/src/com/google/debugging/sourcemap/Base64VLQ.java
*/
private def writeBase64VLQ(value0: Int): Unit = {
/* The sign is encoded in the least significant bit, while the
* absolute value is shifted one bit to the left.
* So in theory the "definition" of `value` is:
* val value =
* if (value0 < 0) ((-value0) << 1) | 1
* else value0 << 1
* The following code is a branchless version of that spec.
* It is valid because:
* - if value0 < 0:
* signExtended == value0 >> 31 == 0xffffffff
* value0 ^ signExtended == ~value0
* (value0 ^ signExtended) - signExtended == ~value0 - (-1) == -value0
* signExtended & 1 == 1
* So we get ((-value0) << 1) | 1 as required
* - if n >= 0:
* signExtended == value0 >> 31 == 0
* value0 ^ signExtended == value0
* (value0 ^ signExtended) - signExtended == value0 - 0 == value0
* signExtended & 1 == 0
* So we get (value0 << 1) | 0 == value0 << 1 as required
*/
val signExtended = value0 >> 31
val value = (((value0 ^ signExtended) - signExtended) << 1) | (signExtended & 1)
// Write as many base-64 digits as necessary to encode value
if (value < 26) {
return out.write('A' + value)
} else {
def writeBase64VLQSlowPath(value0: Int): Unit = {
var value = value0
do {
var digit = value & VLQBaseMask
value = value >>> VLQBaseShift
if (value != 0)
digit |= VLQContinuationBit
out.write(Base64Map.charAt(digit))
} while (value != 0)
}
writeBase64VLQSlowPath(value)
}
}
private def writeBase64VLQ0(): Unit =
out.write('A')
}