-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceMapBuilder.cs
More file actions
326 lines (282 loc) · 12.8 KB
/
Copy pathSourceMapBuilder.cs
File metadata and controls
326 lines (282 loc) · 12.8 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Based on the Source Map library:
* https://github.com/mozilla/source-map
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*
* Based on the Base 64 VLQ implementation in Closure Compiler:
* https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java
*
* Copyright 2011 The Closure Compiler Authors. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil.Cil;
namespace JSIL.Internal
{
public class SourceMapBuilder
{
private readonly List<Mapping> _mappings = new List<Mapping>();
public void AddInfo(int genaratedLine, int generatedColumn, IEnumerable<SequencePoint> info)
{
var point = info != null ? info.FirstOrDefault() : null;
_mappings.Add(new Mapping(
genaratedLine + 1,
generatedColumn,
point == null ? -1 : point.StartLine,
point == null ? -1 : point.StartColumn - 1,
point == null ? null : point.Document.Url,
null));
}
public bool Build(string path, string sourceName)
{
if (!_mappings.Any())
{
return false;
}
var mappings = _mappings.OrderBy(item => item.GeneratedLine).ThenBy(item => item.GeneratedColumn).ToList();
var sources = new List<string>();
var names = new List<string>();
var previousGeneratedColumn = 0;
var previousGeneratedLine = 1;
var previousOriginalColumn = 0;
var previousOriginalLine = 0;
var previousName = 0;
var previousSource = 0;
StringBuilder result = new StringBuilder();
for (int i = 0; i < mappings.Count; i++)
{
var mapping = mappings[i];
if (mapping.Source != null && !sources.Contains(mapping.Source))
{
sources.Add(mapping.Source);
}
if (mapping.Name != null && !names.Contains(mapping.Name))
{
names.Add(mapping.Name);
}
if (mapping.GeneratedLine != previousGeneratedLine)
{
previousGeneratedColumn = 0;
while (mapping.GeneratedLine != previousGeneratedLine)
{
result.Append(';');
previousGeneratedLine++;
}
}
else
{
if (i > 0)
{
if (mapping == mappings[i - 1])
{
continue;
}
result.Append(',');
}
}
result.Append(Base64Vlq.Encode(mapping.GeneratedColumn - previousGeneratedColumn));
previousGeneratedColumn = mapping.GeneratedColumn;
if (mapping.Source != null)
{
var sourceIdx = sources.IndexOf(mapping.Source);
result.Append(Base64Vlq.Encode(sourceIdx - previousSource));
previousSource = sourceIdx;
// lines are stored 0-based in SourceMap spec version 3
result.Append(Base64Vlq.Encode(mapping.OriginalLine - 1 - previousOriginalLine));
previousOriginalLine = mapping.OriginalLine - 1;
result.Append(Base64Vlq.Encode(mapping.OriginalColumn - previousOriginalColumn));
previousOriginalColumn = mapping.OriginalColumn;
if (mapping.Name != null)
{
var nameIdx = names.IndexOf(mapping.Name);
result.Append(Base64Vlq.Encode(nameIdx - previousName));
previousName = nameIdx;
}
}
//else
//{
////IK: I'm not sure if we should do it?
// previousOriginalLine = 0;
// previousOriginalColumn = 0;
//}
}
var sourceMap = new StringBuilder();
sourceMap.AppendLine("{");
sourceMap.AppendLine("\t\"version\" : 3,");
sourceMap.AppendLine(string.Format("\t\"file\" : \"{0}\",", sourceName));
sourceMap.AppendLine(string.Format("\t\"sourceRoot\" : \"{0}\",", new Uri(Path.GetFullPath(path))));
sourceMap.AppendLine(string.Format("\t\"sources\" : [{0}],", string.Join(", ", sources.Select(item => "\"" + MakeRelativePath(path, item) + "\""))));
sourceMap.AppendLine(string.Format("\t\"names\" : [{0}],", string.Join(", ", names.Select(item => "\"" + item + "\""))));
sourceMap.AppendLine(string.Format("\t\"mappings\" : \"{0}\"", result));
sourceMap.AppendLine("}");
using (var file = File.Create(GetFullMapPath(path, sourceName)))
{
using (var tw = new StreamWriter(file))
{
tw.Write(sourceMap.ToString());
tw.Flush();
}
}
return true;
}
public static Uri MakeRelativePath(string fromPath, string toPath)
{
if (string.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
if (string.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");
Uri fromUri = new Uri(Path.GetFullPath(fromPath));
Uri toUri = new Uri(Path.GetFullPath(toPath));
if (fromUri.Scheme != toUri.Scheme) { return toUri; } // path can't be made relative.
return fromUri.MakeRelativeUri(toUri);
}
private sealed class Mapping
{
public int GeneratedLine { get; private set; }
public int GeneratedColumn { get; private set; }
public int OriginalLine { get; private set; }
public int OriginalColumn { get; private set; }
public string Source { get; private set; }
public string Name { get; private set; }
public Mapping(int generatedLine, int generatedColumn, int originalLine, int originalColumn, string source, string name)
{
GeneratedLine = generatedLine;
GeneratedColumn = generatedColumn;
OriginalLine = originalLine;
OriginalColumn = originalColumn;
Source = source;
Name = name;
}
private bool Equals(Mapping other)
{
return GeneratedLine == other.GeneratedLine && GeneratedColumn == other.GeneratedColumn && OriginalLine == other.OriginalLine && OriginalColumn == other.OriginalColumn && string.Equals(Source, other.Source) && string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Mapping) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = GeneratedLine.GetHashCode();
hashCode = (hashCode*397) ^ GeneratedColumn.GetHashCode();
hashCode = (hashCode*397) ^ OriginalLine.GetHashCode();
hashCode = (hashCode*397) ^ OriginalColumn.GetHashCode();
hashCode = (hashCode*397) ^ (Source != null ? Source.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (Name != null ? Name.GetHashCode() : 0);
return hashCode;
}
}
public static bool operator ==(Mapping left, Mapping right)
{
return Equals(left, right);
}
public static bool operator !=(Mapping left, Mapping right)
{
return !Equals(left, right);
}
}
private static class Base64Vlq
{
private const string IntToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// A single base 64 digit can contain 6 bits of data. For the base 64 variable
// length quantities we use in the source map spec, the first bit is the sign,
// the next four bits are the actual value, and the 6th bit is the
// continuation bit. The continuation bit tells us whether there are more
// digits in this value following this digit.
//
// Continuation
// | Sign
// | |
// V V
// 101011
private const int VLQ_BASE_SHIFT = 5;
// binary: 100000
private const int VLQ_BASE = 1 << VLQ_BASE_SHIFT;
// binary: 011111
private const int VLQ_BASE_MASK = VLQ_BASE - 1;
// binary: 100000
private const int VLQ_CONTINUATION_BIT = VLQ_BASE;
/// <summary>
/// Converts from a two-complement value to a value where the sign bit is
/// placed in the least significant bit.For example, as decimals:
/// 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
/// 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
/// </summary>
private static uint ToVlqSigned(int aValue)
{
return (uint) (aValue < 0
? ((-aValue) << 1) + 1
: (aValue << 1) + 0);
}
public static string Encode(int aValue)
{
var encoded = "";
var vlq = ToVlqSigned(aValue);
do
{
var digit = vlq & VLQ_BASE_MASK;
vlq >>= VLQ_BASE_SHIFT;
if (vlq > 0)
{
// There are still more digits in this value, so we must make sure the
// continuation bit is marked.
digit |= VLQ_CONTINUATION_BIT;
}
encoded += EncodeDigit((int) digit);
} while (vlq > 0);
return encoded;
}
private static char EncodeDigit(int number)
{
if (number >= 0 && number < IntToCharMap.Length)
{
return IntToCharMap[number];
}
throw new ArgumentException("Must be between 0 and 63: " + number);
}
}
public void WriteSourceMapLink(Stream outputStream, string path, string sourceName)
{
var writer = new StreamWriter(outputStream);
writer.Write("//# sourceMappingURL=" + new Uri(GetFullMapPath(path, sourceName)));
writer.Flush();
}
private string GetFullMapPath(string path, string sourceName)
{
return Path.GetFullPath(Path.Combine(path, sourceName + ".map")).Replace(" ", "");
}
}
}