Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: lib/compiler/implementation/util/source_map_builder.dart

Issue 10579019: First shot at source maps generation in dart2js. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Return source map via diagnostic handler. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library('source_map_builder');
6
7 #import('dart:json');
8
9 #import('../source_file.dart');
10
11 class SourceMappingEntry {
12 SourceFile sourceFile;
13 int sourceOffset;
14 int targetOffset;
15 String sourceName;
16
17 SourceMappingEntry(this.sourceFile,
18 this.sourceOffset,
19 this.targetOffset,
20 [this.sourceName]);
21 }
22
23 class SourceMapBuilder {
24 static final int VLQ_BASE_SHIFT = 5;
25 static final int VLQ_BASE_MASK = (1 << 5) - 1;
26 static final int VLQ_CONTINUATION_BIT = 1 << 5;
27 static final int VLQ_CONTINUATION_MASK = 1 << 5;
28 static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn'
29 'opqrstuvwxyz0123456789+/';
30
31 Map<String, int> sourceURLMap;
32 List<String> sourceURLList;
33 Map<String, int> sourceNameMap;
34 List<String> sourceNameList;
35
36 int previousTargetLine;
37 int previousTargetColumn;
38 int previousSourceURLIndex;
39 int previousSourceLine;
40 int previousSourceColumn;
41 int previousSourceNameIndex;
42 bool firstEntryInLine;
43
44 SourceMapBuilder() {
45 sourceURLMap = new Map<String, int>();
46 sourceURLList = new List<String>();
47 sourceNameMap = new Map<String, int>();
48 sourceNameList = new List<String>();
49
50 previousTargetLine = 0;
51 previousTargetColumn = 0;
52 previousSourceURLIndex = 0;
53 previousSourceLine = 0;
54 previousSourceColumn = 0;
55 previousSourceNameIndex = 0;
56 firstEntryInLine = true;
57 }
58
59 String build(List<SourceMappingEntry> mappingEntries, SourceFile targetFile) {
60 StringBuffer mappingsBuffer = new StringBuffer();
61 mappingEntries.forEach((SourceMappingEntry entry) {
62 writeEntry(entry, targetFile, mappingsBuffer);
63 });
64
65 Map<String, Object> sourceMap = new Map<String, Object>();
66 sourceMap['version'] = 3;
67 sourceMap['mappings'] = mappingsBuffer.toString();
68 sourceMap['sources'] = sourceURLList;
69 sourceMap['names'] = sourceNameList;
70 return JSON.stringify(sourceMap);
ahe 2012/06/20 11:56:59 I'm really not convinced that JSON.stringify is th
podivilov 2012/06/20 14:15:15 OK, but I think it makes sense to use JSON.stringi
ahe 2012/06/20 17:08:16 How many copies are you going to make of the outpu
podivilov 2012/06/21 07:58:49 Sorry, not sure what do you mean. I believe there'
71 }
72
73 void writeEntry(SourceMappingEntry entry,
74 SourceFile targetFile,
75 StringBuffer output) {
76 int targetLine = targetFile.getLine(entry.targetOffset);
77 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset);
78 String sourceURL = entry.sourceFile.filename;
79 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset);
80 int sourceColumn = entry.sourceFile.getColumn(sourceLine,
81 entry.sourceOffset);
82 String sourceName = entry.sourceName;
83
84 if (targetLine > previousTargetLine) {
85 for (int i = previousTargetLine; i < targetLine; ++i) {
86 output.add(';');
87 }
88 previousTargetLine = targetLine;
89 previousTargetColumn = 0;
90 firstEntryInLine = true;
91 }
92
93 if (!firstEntryInLine) {
94 output.add(',');
95 }
96 firstEntryInLine = false;
97
98 encodeVLQ(output, targetColumn - previousTargetColumn);
99 previousTargetColumn = targetColumn;
100
101 if (sourceURL === null)
102 return;
103
104 int sourceURLIndex = indexOf(sourceURLList, sourceURL, sourceURLMap);
105 encodeVLQ(output, sourceURLIndex - previousSourceURLIndex);
106 previousSourceURLIndex = sourceURLIndex;
107
108 encodeVLQ(output, sourceLine - previousSourceLine);
109 previousSourceLine = sourceLine;
110 encodeVLQ(output, sourceColumn - previousSourceColumn);
111 previousSourceColumn = sourceColumn;
112
113 if (sourceName === null)
114 return;
115
116 int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap);
117 encodeVLQ(output, sourceNameIndex - previousSourceNameIndex);
118 previousSourceNameIndex = sourceNameIndex;
119 }
120
121 int indexOf(List<String> list, String value, Map<String, int> map) {
122 return map.putIfAbsent(value, () {
123 int index = list.length;
124 map[value] = index;
125 list.add(value);
126 return index;
127 });
128 }
129
130 static void encodeVLQ(StringBuffer output, int value) {
131 int signBit = 0;
132 if (value < 0) {
133 signBit = 1;
134 value = -value;
135 }
136 value = (value << 1) | signBit;
137 do {
138 int digit = value & VLQ_BASE_MASK;
139 value >>= VLQ_BASE_SHIFT;
140 if (value > 0) {
141 digit |= VLQ_CONTINUATION_BIT;
142 }
143 output.add(BASE64_DIGITS[digit]);
144 } while (value > 0);
145 }
146 }
OLDNEW
« lib/compiler/implementation/leg.dart ('K') | « lib/compiler/implementation/leg.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698